Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-ties-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/storage": patch
---

Return URIs with gateway URLs
35 changes: 23 additions & 12 deletions packages/storage/src/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,33 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
.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(
Expand Down
27 changes: 27 additions & 0 deletions packages/storage/test/ipfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down