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/moody-ties-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/storage": patch
---

Replace schemes after files are uploaded
43 changes: 20 additions & 23 deletions packages/storage/src/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,35 +199,32 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
options?: T,
): Promise<Json[]> {
let cleaned = data;
// TODO: Gateway URLs should probably be top-level since both uploader and downloader need them
if (this.gatewayUrls) {
// Replace any gateway URLs with their hashes
cleaned = replaceObjectGatewayUrlsWithSchemes(
cleaned,
this.gatewayUrls,
) as Json[];

if (options?.uploadWithGatewayUrl || this.uploader.uploadWithGatewayUrl) {
// If flag is set, replace all schemes with their preferred gateway URL
// Ex: used for Solana, where services don't resolve schemes for you, so URLs must be useable by default
cleaned = replaceObjectSchemesWithGatewayUrls(
cleaned,
this.gatewayUrls,
) as Json[];
}
}
// Replace any gateway URLs with their hashes
cleaned = replaceObjectGatewayUrlsWithSchemes(
cleaned,
this.gatewayUrls,
) as Json[];

// Recurse through data and extract files to upload
const files = extractObjectFiles(cleaned);

if (!files.length) {
return cleaned;
if (files.length) {
// Upload all files that came from the object
const uris = await this.uploader.uploadBatch(files);

// Recurse through data and replace files with hashes
cleaned = replaceObjectFilesWithUris(cleaned, uris) as Json[];
}

// Upload all files that came from the object
const uris = await this.uploader.uploadBatch(files);
if (options?.uploadWithGatewayUrl || this.uploader.uploadWithGatewayUrl) {
// If flag is set, replace all schemes with their preferred gateway URL
// Ex: used for Solana, where services don't resolve schemes for you, so URLs must be useable by default
cleaned = replaceObjectSchemesWithGatewayUrls(
cleaned,
this.gatewayUrls,
) as Json[];
}

// Recurse through data and replace files with hashes
return replaceObjectFilesWithUris(cleaned, uris) as Json[];
return cleaned;
}
}
Binary file added packages/storage/test/images/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/storage/test/images/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/storage/test/images/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/storage/test/images/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/storage/test/images/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/storage/test/images/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 22 additions & 26 deletions packages/storage/test/ipfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,31 +199,6 @@ describe("IPFS", async () => {
expect(json.description).to.equal("Uploading alone without a directory...");
});

it("Should upload without directory if specified on class", async () => {
const solanaStorage = new ThirdwebStorage({
uploader: new IpfsUploader({ uploadWithGatewayUrl: true }),
});

const uri = await solanaStorage.upload(
{
name: "Upload Without Directory",
description: "Uploading alone without a directory...",
},
{
uploadWithoutDirectory: true,
},
);

expect(uri).to.equal(
"ipfs://QmdnBEP9UFcRfbuAyXFefNccNbuKWTscHrpWZatvqz9VcV",
);

const json = await storage.downloadJSON(uri);

expect(json.name).to.equal("Upload Without Directory");
expect(json.description).to.equal("Uploading alone without a directory...");
});

it("Should throw an error on upload without directory with multiple uploads", async () => {
try {
await storage.uploadBatch(
Expand Down Expand Up @@ -267,7 +242,28 @@ describe("IPFS", async () => {
);
});

it("Should upload files with gateway URLs if specified", async () => {
it("Should upload files with gateway URLs if specified on class", async () => {
const uploader = new IpfsUploader({ uploadWithGatewayUrl: true });
const singleStorage = new ThirdwebStorage({ uploader });

const uri = await singleStorage.upload({
// Gateway URLs should first be converted back to ipfs:// and then all ipfs:// should convert to first gateway URL
image: readFileSync("test/images/0.jpg"),
animation_url: "ipfs://QmbaNzUcv7KPgdwq9u2qegcptktpUK6CdRZF72eSjSa6iJ/0",
});

const res = await singleStorage.download(uri);
const json = await res.json();

expect(json.image).to.equal(
`${DEFAULT_GATEWAY_URLS["ipfs://"][0]}QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0`,
);
expect(json.animation_url).to.equal(
`${DEFAULT_GATEWAY_URLS["ipfs://"][0]}QmbaNzUcv7KPgdwq9u2qegcptktpUK6CdRZF72eSjSa6iJ/0`,
);
});

it("Should upload files with gateway URLs if specified on function", async () => {
const uri = await storage.upload(
{
// Gateway URLs should first be converted back to ipfs:// and then all ipfs:// should convert to first gateway URL
Expand Down