Skip to content

Commit 10e0f9c

Browse files
author
Rajat Saxena
committed
sending media object in header on tus upload finished hook
1 parent 2bcce28 commit 10e0f9c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

apps/api/src/tus/finalize.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ import { hasEnoughStorage } from "../media/storage-middleware";
3434
import { NOT_ENOUGH_STORAGE } from "../config/strings";
3535
import { removeTusFiles } from "./utils";
3636

37-
export default async function finalizeUpload(uploadId: string) {
37+
export default async function finalizeUpload(
38+
uploadId: string,
39+
): Promise<string> {
3840
const tusUpload = await getTusUpload(uploadId);
3941
if (!tusUpload) {
4042
throw new Error(`Tus upload not found: ${uploadId}`);
4143
}
4244

4345
if (tusUpload.isComplete) {
4446
logger.info({ uploadId }, "Upload already finalized");
45-
return;
47+
return "";
4648
}
4749

4850
const { userId, apikey, metadata, uploadLength, tempFilePath, signature } =

apps/api/src/tus/tus-server.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getUser } from "../user/queries";
1616
import { hasEnoughStorage } from "../media/storage-middleware";
1717
import { createTusUpload, updateTusUploadOffset } from "./queries";
1818
import getMaxFileUploadSize from "../media/utils/get-max-file-upload-size";
19+
import mediaService from "../media/service";
1920

2021
const store = new FileStore({
2122
directory: `${tempFileDirForUploads}/tus-uploads`,
@@ -25,6 +26,7 @@ export const server = new Server({
2526
path: "/media/create/resumable",
2627
datastore: store,
2728
respectForwardedHeaders: true,
29+
exposedHeaders: ["media"],
2830
onIncomingRequest: async (req: any) => {
2931
try {
3032
const response = await getUserAndAPIKey(req);
@@ -80,15 +82,24 @@ export const server = new Server({
8082
onUploadFinish: async (req: any, upload: any) => {
8183
try {
8284
console.time("finalize");
83-
await finalizeUpload(upload.id);
85+
const mediaId = await finalizeUpload(upload.id);
8486
console.timeEnd("finalize");
85-
return {};
87+
const media = await mediaService.getMediaDetails({
88+
userId: req.user._id,
89+
apikey: req.apikey,
90+
mediaId,
91+
});
92+
return {
93+
headers: {
94+
media: JSON.stringify(media),
95+
},
96+
};
8697
} catch (err: any) {
8798
logger.error(
8899
{ err, uploadId: upload.id },
89100
"Error finalizing tus upload",
90101
);
91-
return {
102+
throw {
92103
status_code: 403,
93104
body: err.message,
94105
};

0 commit comments

Comments
 (0)