Skip to content

Commit 0145339

Browse files
committed
merge in main
2 parents 654b277 + f4074dd commit 0145339

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

.changeset/moody-ties-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/storage": patch
3+
---
4+
5+
Replace schemes after files are uploaded

packages/storage/src/core/storage.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,35 +195,32 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions> {
195195
options?: T,
196196
): Promise<unknown[]> {
197197
let cleaned = data;
198-
// TODO: Gateway URLs should probably be top-level since both uploader and downloader need them
199-
if (this.gatewayUrls) {
200-
// Replace any gateway URLs with their hashes
201-
cleaned = replaceObjectGatewayUrlsWithSchemes(
202-
cleaned,
203-
this.gatewayUrls,
204-
) as unknown[];
205-
206-
if (options?.uploadWithGatewayUrl || this.uploader.uploadWithGatewayUrl) {
207-
// If flag is set, replace all schemes with their preferred gateway URL
208-
// Ex: used for Solana, where services don't resolve schemes for you, so URLs must be useable by default
209-
cleaned = replaceObjectSchemesWithGatewayUrls(
210-
cleaned,
211-
this.gatewayUrls,
212-
) as unknown[];
213-
}
214-
}
198+
// Replace any gateway URLs with their hashes
199+
cleaned = replaceObjectGatewayUrlsWithSchemes(
200+
cleaned,
201+
this.gatewayUrls,
202+
) as unknown[];
215203

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

219-
if (!files.length) {
220-
return cleaned;
207+
if (files.length) {
208+
// Upload all files that came from the object
209+
const uris = await this.uploader.uploadBatch(files);
210+
211+
// Recurse through data and replace files with hashes
212+
cleaned = replaceObjectFilesWithUris(cleaned, uris) as unknown[];
221213
}
222214

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

226-
// Recurse through data and replace files with hashes
227-
return replaceObjectFilesWithUris(cleaned, uris) as unknown[];
224+
return cleaned;
228225
}
229226
}
-104 KB
Binary file not shown.

packages/storage/test/ipfs.test.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,6 @@ describe("IPFS", async () => {
199199
expect(json.description).to.equal("Uploading alone without a directory...");
200200
});
201201

202-
it("Should upload without directory if specified on class", async () => {
203-
const solanaStorage = new ThirdwebStorage({
204-
uploader: new IpfsUploader({ uploadWithGatewayUrl: true }),
205-
});
206-
207-
const uri = await solanaStorage.upload(
208-
{
209-
name: "Upload Without Directory",
210-
description: "Uploading alone without a directory...",
211-
},
212-
{
213-
uploadWithoutDirectory: true,
214-
},
215-
);
216-
217-
expect(uri).to.equal(
218-
"ipfs://QmdnBEP9UFcRfbuAyXFefNccNbuKWTscHrpWZatvqz9VcV",
219-
);
220-
221-
const json = await storage.downloadJSON(uri);
222-
223-
expect(json.name).to.equal("Upload Without Directory");
224-
expect(json.description).to.equal("Uploading alone without a directory...");
225-
});
226-
227202
it("Should throw an error on upload without directory with multiple uploads", async () => {
228203
try {
229204
await storage.uploadBatch(
@@ -267,7 +242,28 @@ describe("IPFS", async () => {
267242
);
268243
});
269244

270-
it("Should upload files with gateway URLs if specified", async () => {
245+
it("Should upload files with gateway URLs if specified on class", async () => {
246+
const uploader = new IpfsUploader({ uploadWithGatewayUrl: true });
247+
const singleStorage = new ThirdwebStorage({ uploader });
248+
249+
const uri = await singleStorage.upload({
250+
// Gateway URLs should first be converted back to ipfs:// and then all ipfs:// should convert to first gateway URL
251+
image: readFileSync("test/files/0.jpg"),
252+
animation_url: "ipfs://QmbaNzUcv7KPgdwq9u2qegcptktpUK6CdRZF72eSjSa6iJ/0",
253+
});
254+
255+
const res = await singleStorage.download(uri);
256+
const json = await res.json();
257+
258+
expect(json.image).to.equal(
259+
`${DEFAULT_GATEWAY_URLS["ipfs://"][0]}QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0`,
260+
);
261+
expect(json.animation_url).to.equal(
262+
`${DEFAULT_GATEWAY_URLS["ipfs://"][0]}QmbaNzUcv7KPgdwq9u2qegcptktpUK6CdRZF72eSjSa6iJ/0`,
263+
);
264+
});
265+
266+
it("Should upload files with gateway URLs if specified on function", async () => {
271267
const uri = await storage.upload(
272268
{
273269
// Gateway URLs should first be converted back to ipfs:// and then all ipfs:// should convert to first gateway URL

0 commit comments

Comments
 (0)