feat(nbstore): add upload function to blob frontend (#11247)

This commit is contained in:
EYHN
2025-03-27 12:11:30 +00:00
parent f2cdf67c2a
commit 7091111f85
5 changed files with 116 additions and 38 deletions
+23 -4
View File
@@ -23,7 +23,9 @@ export class BlobFrontend {
}
await lock[Symbol.asyncDispose]();
await this.sync.downloadBlob(blobId);
await this.sync.downloadBlob(blobId).catch(() => {
// ignore the error as it has already been recorded in the sync status
});
return await this.storage.get(blobId);
}
@@ -40,14 +42,31 @@ export class BlobFrontend {
// We don't wait for the upload to complete,
// as the upload process runs asynchronously in the background
this.sync.uploadBlob(blob).catch(err => {
// never reach here
console.error(err);
this.sync.uploadBlob(blob, true /* force upload */).catch(() => {
// ignore the error as it has already been recorded in the sync status
});
return;
}
/**
* Uploads a blob to the peer. Do nothing if the blob has already been uploaded.
*
* @returns Always resolves to true when successful
*
* @throws This method will throw an error if the blob is not found locally, if the upload is aborted, or if it fails due to storage limitations.
*/
async upload(blobIdOrRecord: string | BlobRecord): Promise<boolean> {
const blob =
typeof blobIdOrRecord === 'string'
? await this.storage.get(blobIdOrRecord)
: blobIdOrRecord;
if (!blob) {
throw new Error(`Blob ${blobIdOrRecord} not found`);
}
return this.sync.uploadBlob(blob, false);
}
fullDownload(peerId?: string, signal?: AbortSignal) {
return this.sync.fullDownload(peerId, signal);
}