Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return file hash from uploaded object #978

Merged
merged 14 commits into from
Oct 6, 2024
5 changes: 5 additions & 0 deletions .changeset/nice-crabs-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"uploadthing": minor
---

feat: return object hash in onUploadComplete
2 changes: 1 addition & 1 deletion examples/minimal-appdir/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
2 changes: 1 addition & 1 deletion examples/minimal-appdir/src/server/uploadthing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const uploadRouter = {
})
.middleware(({ req, files }) => {
// Check some condition based on the incoming requrest
console.log("Request", req);
req;
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
//^?
// if (!req.headers.get("x-some-header")) {
// throw new Error("x-some-header is required");
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
1 change: 1 addition & 0 deletions packages/uploadthing/src/internal/shared-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class UploadedFileData extends FileUploadDataWithCustomId.extend<Uploaded
key: S.String,
url: S.String,
appUrl: S.String,
hash: S.String,
markflorkowski marked this conversation as resolved.
Show resolved Hide resolved
}) {}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/uploadthing/src/internal/upload.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ export const uploadFile = <
Micro.map(
unsafeCoerce<
unknown,
{ url: string; appUrl: string; serverData: TServerOutput }
{
url: string;
appUrl: string;
serverData: TServerOutput;
hash: string;
}
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
>,
),
Micro.map((uploadResponse) => ({
Expand All @@ -108,6 +113,7 @@ export const uploadFile = <
appUrl: uploadResponse.appUrl,
customId: presigned.customId,
type: file.type,
hash: uploadResponse.hash,
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
})),
);

Expand Down
4 changes: 3 additions & 1 deletion packages/uploadthing/src/internal/upload.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export const uploadWithoutProgress = (
),
HttpClientResponse.json,

Effect.andThen(unsafeCoerce<unknown, { url: string; appUrl: string }>),
Effect.andThen(
unsafeCoerce<unknown, { url: string; appUrl: string; hash: string }>,
),
);

yield* Effect.logDebug(`File ${file.name} uploaded successfully`).pipe(
Expand Down
3 changes: 2 additions & 1 deletion packages/uploadthing/src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const uploadFile = (
Effect.gen(function* () {
const { file, presigned } = input;

const { url, appUrl } = yield* uploadWithoutProgress(file, presigned);
const { url, appUrl, hash } = yield* uploadWithoutProgress(file, presigned);

return {
key: presigned.key,
Expand All @@ -189,5 +189,6 @@ const uploadFile = (
size: file.size,
type: file.type,
customId: file.customId ?? null,
hash,
};
}).pipe(Effect.withLogSpan("uploadFile"));
4 changes: 4 additions & 0 deletions packages/uploadthing/test/__test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from "crypto";
import * as S from "@effect/schema/Schema";
import type { StrictRequest } from "msw";
import { http, HttpResponse } from "msw";
Expand Down Expand Up @@ -119,6 +120,9 @@ export const it = itBase.extend({
url: `${UTFS_IO_URL}/f/${params.key}`,
appUrl: `${UTFS_IO_URL}/a/${appId}/${params.key}`,
serverData: null,
hash: createHash("md5")
.update(new Uint8Array(await request.arrayBuffer()))
.digest("hex"),
});
},
),
Expand Down
3 changes: 3 additions & 0 deletions packages/uploadthing/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe("uploadFiles", () => {
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern()),
hash: expect.any(String),
},
]);

Expand Down Expand Up @@ -177,6 +178,7 @@ describe("uploadFiles", () => {
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern()),
hash: expect.any(String),
},
]);

Expand Down Expand Up @@ -211,6 +213,7 @@ describe("uploadFiles", () => {
key: expect.stringMatching(/.+/),
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern()),
hash: expect.any(String),
},
]);

Expand Down
4 changes: 4 additions & 0 deletions packages/uploadthing/test/request-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ describe(".onUploadComplete()", () => {
size: 48,
type: "image/png",
customId: null,
hash: "some-md5-hash",
}),
});
const signature = await Effect.runPromise(
Expand Down Expand Up @@ -366,6 +367,7 @@ describe(".onUploadComplete()", () => {
type: "image/png",
url: "https://utfs.io/f/some-random-key.png",
appUrl: `https://utfs.io/a/${testToken.decoded.appId}/some-random-key.png`,
hash: "some-md5-hash",
},
metadata: {},
});
Expand All @@ -383,6 +385,7 @@ describe(".onUploadComplete()", () => {
size: 48,
type: "image/png",
customId: null,
hash: "some-md5-hash",
}),
});

Expand Down Expand Up @@ -415,6 +418,7 @@ describe(".onUploadComplete()", () => {
size: 48,
type: "image/png",
customId: null,
hash: "some-md5-hash",
}),
});
const signature = await Effect.runPromise(
Expand Down
10 changes: 10 additions & 0 deletions packages/uploadthing/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
appUrl: `${UTFS_IO_URL}/a/${testToken.decoded.appId}/${key}`,
customId: null,
type: "text/plain",
hash: expect.any(String),
},
error: null,
});
Expand Down Expand Up @@ -136,6 +137,7 @@
appUrl: `${UTFS_IO_URL}/a/${testToken.decoded.appId}/${key}`,
customId: null,
type: "text/plain",
hash: expect.any(String),
},
error: null,
});
Expand Down Expand Up @@ -235,6 +237,7 @@
type: "text/plain",
url: `${UTFS_IO_URL}/f/${key1}`,
appUrl: `${UTFS_IO_URL}/a/${testToken.decoded.appId}/${key1}`,
hash: expect.any(String),
},
error: null,
},
Expand All @@ -257,6 +260,7 @@
type: "text/plain",
url: `${UTFS_IO_URL}/f/${key2}`,
appUrl: `${UTFS_IO_URL}/a/${testToken.decoded.appId}/${key2}`,
hash: expect.any(String),
},
error: null,
},
Expand Down Expand Up @@ -463,7 +467,7 @@
const file = new File(["foo"], "foo.txt", { type: "text/plain" });
const result = await utapi.uploadFiles(file);
const key = result.data!.key;
expect(result).toEqual({

Check failure on line 470 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should upload a file

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { Object (data, error) } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": null, - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": 1727124078084, "name": "foo.txt", "size": 3, "type": "text/plain", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:470:22
data: {
customId: null,
key: expect.stringMatching(/.+/),
Expand All @@ -473,6 +477,7 @@
type: "text/plain",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand All @@ -498,7 +503,7 @@
acl: "private",
});
const key = result.data!.key;
expect(result).toEqual({

Check failure on line 506 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should upload a private file

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { Object (data, error) } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": null, - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": 1727124080003, "name": "foo.txt", "size": 3, "type": "text/plain", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:506:22
data: {
customId: null,
key: expect.stringMatching(/.+/),
Expand All @@ -508,6 +513,7 @@
type: "text/plain",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand All @@ -528,7 +534,7 @@
"https://uploadthing.com/favicon.ico",
);
const key = result.data!.key;
expect(result).toEqual({

Check failure on line 537 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should upload a file from a url

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { Object (data, error) } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": null, - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": Any<Number>, "name": "favicon.ico", "size": Any<Number>, "type": "image/vnd.microsoft.icon", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:537:22
data: {
customId: null,
key: expect.stringMatching(/.+/),
Expand All @@ -538,6 +544,7 @@
type: "image/vnd.microsoft.icon",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand All @@ -552,7 +559,7 @@
const file = new UTFile(["foo"], "bar.txt");
const result = await utapi.uploadFiles(file);
const fileKey = result.data!.key;
expect(result).toEqual({

Check failure on line 562 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should rename a file with fileKey

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { Object (data, error) } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": null, - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": Any<Number>, "name": "bar.txt", "size": 3, "type": "text/plain", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:562:22
data: {
customId: null,
key: expect.stringMatching(/.+/),
Expand All @@ -563,6 +570,7 @@
type: "text/plain",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand Down Expand Up @@ -595,7 +603,7 @@
const file = new UTFile(["foo"], "bar.txt", { customId });
const result = await utapi.uploadFiles(file);
const key = result.data!.key;
expect(result).toEqual({

Check failure on line 606 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should rename a file with customId

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { data: { …(9) }, error: null } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": "76dffcfb-8599-47e8-9e32-f8c56dceac13", - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": 1727124084506, "name": "bar.txt", "size": 3, "type": "text/plain", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:606:22
data: {
customId: customId,
key: expect.stringMatching(/.+/),
Expand All @@ -605,6 +613,7 @@
type: "text/plain",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand Down Expand Up @@ -635,7 +644,7 @@
const file = new File(["foo"], "foo.txt", { type: "text/plain" });
const result = await utapi.uploadFiles(file);
const { key, url } = result.data!;
expect(result).toEqual({

Check failure on line 647 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should update ACL

AssertionError: expected { data: { …(9) }, error: null } to deeply equal { Object (data, error) } - Expected + Received Object { "data": Object { "appUrl": StringMatching /^https:\/\/utfs.io\/a\/fr0hfwpst1\/.+$/, "customId": null, - "hash": Any<String>, + "hash": undefined, "key": StringMatching /.+/, "lastModified": 1727124085984, "name": "foo.txt", "size": 3, "type": "text/plain", "url": StringMatching /^https:\/\/utfs.io\/f\/.+$/, }, "error": null, } ❯ test/sdk.test.ts:647:22
data: {
customId: null,
key: expect.stringMatching(/.+/),
Expand All @@ -645,6 +654,7 @@
type: "text/plain",
url: expect.stringMatching(fileUrlPattern),
appUrl: expect.stringMatching(appUrlPattern(appId)),
hash: expect.any(String),
},
error: null,
});
Expand Down Expand Up @@ -680,7 +690,7 @@

rawIt("should have correct usage info", async () => {
const usageInfo = await utapi.getUsageInfo();
expect(usageInfo).toEqual({

Check failure on line 693 in packages/uploadthing/test/sdk.test.ts

View workflow job for this annotation

GitHub Actions / build

test/sdk.test.ts > smoke test with live api > should have correct usage info

AssertionError: expected GetUsageInfoResponse{ …(4) } to deeply equal { totalBytes: -3, …(3) } - Expected + Received - Object { - "appTotalBytes": -3, - "filesUploaded": -1, + GetUsageInfoResponse { + "appTotalBytes": 15418, + "filesUploaded": 5, "limitBytes": 2147483648, - "totalBytes": -3, + "totalBytes": 15418, } ❯ test/sdk.test.ts:693:25
totalBytes: localInfo.totalBytes,
appTotalBytes: localInfo.totalBytes,
filesUploaded: localInfo.filesUploaded,
Expand Down
Loading