Skip to content

Commit

Permalink
Auto-Drive: add api namespace
Browse files Browse the repository at this point in the history
Add namespace to api calls
  • Loading branch information
clostao authored Dec 17, 2024
2 parents 8755366 + 5027380 commit d6c267c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
15 changes: 11 additions & 4 deletions packages/auto-drive/src/api/calls/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export * from './download'
export * from './read'
export * from './upload'
export * from './write'
import * as downloadCalls from './download'
import * as readCalls from './read'
import * as uploadCalls from './upload'
import * as writeCalls from './write'

export const apiCalls = {
...downloadCalls,
...readCalls,
...uploadCalls,
...writeCalls,
}
33 changes: 12 additions & 21 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import mime from 'mime-types'
import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async'
import { progressToPercentage } from '../utils/misc'
import { PromisedObservable } from '../utils/observable'

import {
completeUpload,
createFileUpload,
createFileUploadWithinFolderUpload,
createFolderUpload,
downloadObject,
getObjectMetadata,
uploadFileChunk,
} from './calls/index'
import { apiCalls } from './calls/index'
import { AutoDriveApi } from './connection'
import { GenericFile, GenericFileWithinFolder } from './models/file'
import { constructFromInput, constructZipBlobFromTreeAndPaths } from './models/folderTree'
Expand All @@ -35,7 +26,7 @@ const uploadFileChunks = (
let index = 0
let uploadBytes = 0
for await (const chunk of asyncByChunk(asyncIterable, uploadChunkSize)) {
await uploadFileChunk(api, { uploadId: fileUploadId, chunk, index })
await apiCalls.uploadFileChunk(api, { uploadId: fileUploadId, chunk, index })
uploadBytes += chunk.length
subscriber.next({ uploadBytes })
index++
Expand Down Expand Up @@ -99,7 +90,7 @@ export const uploadFileFromInput = (
}
: undefined,
}
const fileUpload = await createFileUpload(api, {
const fileUpload = await apiCalls.createFileUpload(api, {
mimeType: mime.lookup(file.name) || undefined,
filename: file.name,
uploadOptions,
Expand All @@ -109,7 +100,7 @@ export const uploadFileFromInput = (
subscriber.next({ type: 'file', progress: progressToPercentage(e.uploadBytes, file.size) }),
)

const result = await completeUpload(api, { uploadId: fileUpload.id })
const result = await apiCalls.completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.complete()
Expand Down Expand Up @@ -172,7 +163,7 @@ export const uploadFile = (
}
: undefined,
}
const fileUpload = await createFileUpload(api, {
const fileUpload = await apiCalls.createFileUpload(api, {
mimeType: mime.lookup(file.name) || undefined,
filename: file.name,
uploadOptions,
Expand All @@ -182,7 +173,7 @@ export const uploadFile = (
subscriber.next({ type: 'file', progress: progressToPercentage(e.uploadBytes, file.size) }),
)

const result = await completeUpload(api, { uploadId: fileUpload.id })
const result = await apiCalls.completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.complete()
Expand Down Expand Up @@ -239,7 +230,7 @@ export const uploadFolderFromInput = async (

return new PromisedObservable<UploadFolderStatus>(async (subscriber) => {
// Otherwise, we upload the files as a folder w/o compression or encryption
const folderUpload = await createFolderUpload(api, {
const folderUpload = await apiCalls.createFolderUpload(api, {
fileTree,
})

Expand Down Expand Up @@ -267,7 +258,7 @@ export const uploadFolderFromInput = async (
currentBytesUploaded += file.size
}

const result = await completeUpload(api, { uploadId: folderUpload.id })
const result = await apiCalls.completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.complete()
Expand All @@ -290,7 +281,7 @@ export const uploadFileWithinFolderUpload = (
uploadChunkSize?: number,
): PromisedObservable<UploadChunksStatus> => {
return new PromisedObservable<UploadChunksStatus>(async (subscriber) => {
const fileUpload = await createFileUploadWithinFolderUpload(api, {
const fileUpload = await apiCalls.createFileUploadWithinFolderUpload(api, {
uploadId,
name: file.name,
mimeType: file.mimeType,
Expand All @@ -302,7 +293,7 @@ export const uploadFileWithinFolderUpload = (
subscriber.next({ uploadBytes: e.uploadBytes }),
)

await completeUpload(api, { uploadId: fileUpload.id })
await apiCalls.completeUpload(api, { uploadId: fileUpload.id })

subscriber.complete()
})
Expand All @@ -324,9 +315,9 @@ export const downloadFile = async (
'@autonomys/auto-dag-data'
)

const metadata = await getObjectMetadata(api, { cid })
const metadata = await apiCalls.getObjectMetadata(api, { cid })

let iterable = asyncFromStream(await downloadObject(api, { cid }))
let iterable = asyncFromStream(await apiCalls.downloadObject(api, { cid }))
if (metadata.uploadOptions?.encryption) {
if (!password) {
throw new Error('Password is required to decrypt the file')
Expand Down
6 changes: 3 additions & 3 deletions packages/auto-drive/src/fs/wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import mime from 'mime-types'
import { AutoDriveApi } from '../api/connection.js'
import { completeUpload, createFolderUpload } from '../api/index.js'
import { apiCalls } from '../api/index.js'
import { GenericFileWithinFolder } from '../api/models/file.js'
import { constructFromFileSystemEntries } from '../api/models/folderTree.js'
import { UploadFileStatus, UploadFolderStatus } from '../api/models/uploads.js'
Expand Down Expand Up @@ -100,7 +100,7 @@ export const uploadFolderFromFolderPath = async (

return new PromisedObservable<UploadFolderStatus>(async (subscriber) => {
const { CompressionAlgorithm } = await import('@autonomys/auto-dag-data')
const folderUpload = await createFolderUpload(api, {
const folderUpload = await apiCalls.createFolderUpload(api, {
fileTree,
uploadOptions: {
compression: {
Expand Down Expand Up @@ -131,7 +131,7 @@ export const uploadFolderFromFolderPath = async (
progress += file.size
}

const result = await completeUpload(api, { uploadId: folderUpload.id })
const result = await apiCalls.completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.complete()
Expand Down

0 comments on commit d6c267c

Please sign in to comment.