diff --git a/.changeset/cuddly-ties-complain.md b/.changeset/cuddly-ties-complain.md new file mode 100644 index 00000000000..6b24d3ba398 --- /dev/null +++ b/.changeset/cuddly-ties-complain.md @@ -0,0 +1,5 @@ +--- +"@thirdweb-dev/storage": patch +--- + +Return URIs with gateway URLs diff --git a/packages/storage/src/core/storage.ts b/packages/storage/src/core/storage.ts index ccd84a423e2..a5150a40a92 100644 --- a/packages/storage/src/core/storage.ts +++ b/packages/storage/src/core/storage.ts @@ -172,22 +172,33 @@ export class ThirdwebStorage { .map((item) => isFileOrBuffer(item) || typeof item === "string") .every((item) => !!item); + let uris: string[] = []; + // If data is an array of files, pass it through to upload directly if (isFileArray) { - return this.uploader.uploadBatch(data as FileOrBufferOrString[], options); + uris = await this.uploader.uploadBatch( + data as FileOrBufferOrString[], + options, + ); + } else { + // Otherwise it is an array of JSON objects, so we have to prepare it first + const metadata = ( + await this.uploadAndReplaceFilesWithHashes(data, options) + ).map((item) => { + if (typeof item === "string") { + return item; + } + return JSON.stringify(item); + }); + + uris = await this.uploader.uploadBatch(metadata, options); } - // Otherwise it is an array of JSON objects, so we have to prepare it first - const metadata = ( - await this.uploadAndReplaceFilesWithHashes(data, options) - ).map((item) => { - if (typeof item === "string") { - return item; - } - return JSON.stringify(item); - }); - - return this.uploader.uploadBatch(metadata, options); + if (options?.uploadWithGatewayUrl || this.uploader.uploadWithGatewayUrl) { + return uris.map((uri) => this.resolveScheme(uri)); + } else { + return uris; + } } private async uploadAndReplaceFilesWithHashes( diff --git a/packages/storage/test/ipfs.test.ts b/packages/storage/test/ipfs.test.ts index ba1e4afd7e2..fb96f19063c 100644 --- a/packages/storage/test/ipfs.test.ts +++ b/packages/storage/test/ipfs.test.ts @@ -287,6 +287,33 @@ describe("IPFS", async () => { ); }); + it("Should return URIs with gateway URLs if specified on function", async () => { + const uri = await storage.upload( + { + name: "String", + image: readFileSync("test/files/0.jpg"), + }, + { + uploadWithGatewayUrl: true, + }, + ); + + expect(uri.startsWith(`${DEFAULT_GATEWAY_URLS["ipfs://"][0]}`)).to.equal( + true, + ); + }); + + it("Should return URIs with gateway URLs if specified on class", async () => { + const uploader = new IpfsUploader({ uploadWithGatewayUrl: true }); + const storageSingleton = new ThirdwebStorage({ uploader }); + + const uri = await storageSingleton.upload(readFileSync("test/files/0.jpg")); + + expect(uri).to.equal( + `${DEFAULT_GATEWAY_URLS["ipfs://"][0]}QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0`, + ); + }); + it("Should throw an error when trying to upload different files with the same name", async () => { try { await storage.uploadBatch([