Skip to content

Commit

Permalink
Merge pull request #163 from autonomys/fix/decryption
Browse files Browse the repository at this point in the history
Update auto-drive wrapper functions
  • Loading branch information
clostao authored Nov 11, 2024
2 parents cb7993a + f95a698 commit dd6579c
Show file tree
Hide file tree
Showing 13 changed files with 460 additions and 117 deletions.
4 changes: 3 additions & 1 deletion packages/auto-dag-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
},
"dependencies": {
"@ipld/dag-pb": "^4.1.2",
"blake3": "1.1.0",
"@webbuf/blake3": "^3.0.26",
"@webbuf/fixedbuf": "^3.0.26",
"@webbuf/webbuf": "^3.0.26",
"fflate": "^0.8.2",
"multiformats": "^13.2.2",
"protobufjs": "^7.4.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/auto-dag-data/src/cid/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { hash } from 'blake3'
import { blake3Hash } from '@webbuf/blake3'
import { WebBuf } from '@webbuf/webbuf'
import * as base32 from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
Expand All @@ -8,7 +9,9 @@ import { encodeNode, PBNode } from '../ipld/utils.js'
export const BLAKE3_CODE = 0x1f

export const cidOfNode = (node: PBNode) => {
return cidFromBlakeHash(hash(encodeNode(node)))
const encodedNode = WebBuf.from(encodeNode(node))
const hash = Buffer.from(blake3Hash(encodedNode).buf)
return cidFromBlakeHash(hash)
}

export const cidToString = (cid: CID) => {
Expand Down
1 change: 1 addition & 0 deletions packages/auto-drive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@autonomys/auto-dag-data": "^1.0.4",
"jszip": "^3.10.1",
"mime-types": "^2.1.35",
"zod": "^3.23.8"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-drive/src/api/calls/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const createFolderUpload = async (
api: AutoDriveApi,
{
fileTree,
uploadOptions,
}: ArgsWithoutPagination<{ fileTree: FolderTree; uploadOptions: FileUploadOptions }>,
uploadOptions = {},
}: ArgsWithoutPagination<{ fileTree: FolderTree; uploadOptions?: FileUploadOptions }>,
): Promise<FolderUpload> => {
const response = await api.sendRequest(
`/uploads/folder`,
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-drive/src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const createAutoDriveApi = ({
apiKey,
url = 'https://demo.auto-drive.autonomys.xyz',
}: {
provider: AuthProvider
provider?: AuthProvider
apiKey: string
url?: string
}): AutoDriveApi => {
Expand Down
7 changes: 7 additions & 0 deletions packages/auto-drive/src/api/models/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface GenericFile {
read(): AsyncIterable<Buffer>
name: string
mimeType?: string
size: number
path: string
}
51 changes: 51 additions & 0 deletions packages/auto-drive/src/api/models/folderTree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import fs from 'fs'
import JSZip from 'jszip'
import { z } from 'zod'

export type FolderTreeFolder = {
name: string
type: 'folder'
Expand Down Expand Up @@ -75,3 +78,51 @@ export const constructFromFileSystemEntries = (entries: string[]): FolderTree =>

return root.children.length === 1 ? root.children[0] : root
}

export const constructFromInput = (input: File[]): FolderTree => {
return constructFromFileSystemEntries(
Array.from(input).map((file) => {
if (!file.webkitRelativePath) {
throw new Error('webkitRelativePath is not supported')
}
return file.webkitRelativePath
}),
)
}

const addFilesToZip = (
folder: JSZip,
folderNode: FolderTreeFolder,
files: Record<string, File | string>,
) => {
folderNode.children.forEach((child) => {
if (child.type === 'file') {
const file = files[child.id]
if (typeof file === 'string') {
folder.file(child.name, fs.createReadStream(file))
} else {
folder.file(child.name, file)
}
} else if (child.type === 'folder') {
const subFolder = folder.folder(child.name)
if (!subFolder) {
throw new Error('Failed to create folder in zip')
}
addFilesToZip(subFolder, child as FolderTreeFolder, files)
}
})
}

export const constructZipBlobFromTreeAndPaths = async (
tree: FolderTree,
files: Record<string, File | string>,
) => {
if (tree.type === 'file') {
throw new Error('Cannot construct zip from file')
}

const zip = new JSZip()
addFilesToZip(zip, tree as FolderTreeFolder, files)

return zip.generateAsync({ type: 'blob' })
}
18 changes: 17 additions & 1 deletion packages/auto-drive/src/api/models/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileUploadOptions } from '@autonomys/auto-dag-data'
import { CID, FileUploadOptions } from '@autonomys/auto-dag-data'
import { z } from 'zod'
import { FolderTreeFolderSchema } from './folderTree.js'

Expand Down Expand Up @@ -51,3 +51,19 @@ export type FolderUpload = z.infer<typeof folderUploadSchema>
export type CompleteUploadResponse = {
cid: string
}

export type UploadFileStatus = {
type: 'file'
progress: number
cid?: CID
}

export type UploadFolderStatus = {
type: 'folder'
progress: number
cid?: CID
}

export type UploadChunksStatus = {
uploadBytes: number
}
Loading

0 comments on commit dd6579c

Please sign in to comment.