Skip to content

Commit

Permalink
Add upload object as json method
Browse files Browse the repository at this point in the history
Add upload object as json
  • Loading branch information
clostao authored Dec 24, 2024
2 parents 87d725f + 7580913 commit c1f6bcd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 42 additions & 2 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mime from 'mime-types'
import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async'
import { asyncByChunk, asyncFromStream, bufferToIterable, fileToIterable } from '../utils/async'
import { progressToPercentage } from '../utils/misc'
import { apiCalls } from './calls/index'
import { AutoDriveApi } from './connection'
Expand Down Expand Up @@ -125,7 +125,7 @@ export const uploadFileFromInput = (
* @param {string} [options.password] - The password for encryption (optional).
* @param {boolean} [options.compression=true] - Whether to compress the file (optional).
* @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
* @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
* @returns {Promise<string>} - The CID of the uploaded file.
* @throws {Error} - Throws an error if the upload fails at any stage.
*/
export const uploadFile = async (
Expand Down Expand Up @@ -182,6 +182,46 @@ export const uploadFile = async (
return result.cid
}

/**
* Uploads an object as a JSON file to the server.
*
* This function serializes the provided object to a JSON string,
* and then uploads the JSON string as a file to the server.
*
* @param {AutoDriveApi} api - The API instance used to send requests.
* @param {File | GenericFile} file - The file to be uploaded, which can be a File or a GenericFile.
* @param {UploadFileOptions} options - Options for the upload process.
* @param {string} [options.password] - The password for encryption (optional).
* @param {boolean} [options.compression=true] - Whether to compress the file (optional).
* @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
* @returns {Promise<string>} - The CID of the uploaded file.
* @throws {Error} - Throws an error if the upload fails at any stage.
*/
export const uploadObjectAsJSON = async (
api: AutoDriveApi,
object: unknown,
name?: string | undefined,
options: UploadFileOptions = {},
uploadChunkSize?: number,
): Promise<string> => {
try {
const json = Buffer.from(JSON.stringify(object))
return uploadFile(
api,
{
read: () => bufferToIterable(json),
name: name || 'object.json',
mimeType: 'application/json',
size: json.length,
},
options,
uploadChunkSize,
)
} catch (e) {
throw new Error('Failed to serialize object to JSON')
}
}

/**
* Uploads an entire folder to the server.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/auto-drive/src/utils/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ export const fileToIterable = async function* (file: File | Blob): AsyncIterable
yield Buffer.from(await file.slice(i, i + chunkSize).arrayBuffer())
}
}

export const bufferToIterable = async function* (buffer: Buffer): AsyncIterable<Buffer> {
yield buffer
}

0 comments on commit c1f6bcd

Please sign in to comment.