-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfinalize.ts
More file actions
210 lines (185 loc) · 6.36 KB
/
finalize.ts
File metadata and controls
210 lines (185 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {
createReadStream,
existsSync,
copyFileSync,
promises as fsPromises,
} from "fs";
import path from "path";
import thumbnail from "@medialit/thumbnail";
import mongoose from "mongoose";
import {
tempFileDirForUploads,
imagePattern,
imagePatternForThumbnailGeneration,
videoPattern,
USE_CLOUDFRONT,
} from "../config/constants";
import imageUtils from "@medialit/images";
import {
foldersExist,
createFolders,
} from "../media/utils/manage-files-on-disk";
import type { MediaWithUserId } from "../media/model";
import { putObject, UploadParams } from "../services/s3";
import logger from "../services/log";
import generateKey from "../media/utils/generate-key";
import { getMediaSettings } from "../media-settings/queries";
import generateFileName from "../media/utils/generate-file-name";
import { createMedia } from "../media/queries";
import getTags from "../media/utils/get-tags";
import { getTusUpload, markTusUploadComplete } from "./queries";
import * as presignedUrlService from "../signature/service";
import { getUser } from "../user/queries";
import { hasEnoughStorage } from "../media/storage-middleware";
import { NOT_ENOUGH_STORAGE } from "../config/strings";
import { removeTusFiles } from "./utils";
export default async function finalizeUpload(uploadId: string) {
const tusUpload = await getTusUpload(uploadId);
if (!tusUpload) {
throw new Error(`Tus upload not found: ${uploadId}`);
}
if (tusUpload.isComplete) {
logger.info({ uploadId }, "Upload already finalized");
return;
}
const { userId, apikey, metadata, uploadLength, tempFilePath, signature } =
tusUpload;
const user = await getUser(userId);
if (!(await hasEnoughStorage(uploadLength, user!))) {
throw new Error(NOT_ENOUGH_STORAGE);
}
// Read the completed file from tus data store
const tusFilePath = path.join(
`${tempFileDirForUploads}/tus-uploads`,
tempFilePath,
);
if (!existsSync(tusFilePath)) {
logger.error({ uploadId, tusFilePath }, "Tus file not found");
throw new Error(`Tus file not found: ${tusFilePath}`);
}
const mediaSettings = await getMediaSettings(userId, apikey);
const useWebP = mediaSettings?.useWebP || false;
const webpOutputQuality = mediaSettings?.webpOutputQuality || 0;
// Generate unique media ID
const fileName = generateFileName(metadata.fileName);
const temporaryFolderForWork = `${tempFileDirForUploads}/${fileName.name}`;
if (!foldersExist([temporaryFolderForWork])) {
createFolders([temporaryFolderForWork]);
}
let fileExtension = path.extname(metadata.fileName).replace(".", "");
let mimeType = metadata.mimeType;
if (useWebP && imagePattern.test(mimeType)) {
fileExtension = "webp";
mimeType = "image/webp";
}
const mainFilePath = `${temporaryFolderForWork}/main.${fileExtension}`;
copyFileSync(tusFilePath, mainFilePath);
// Apply WebP conversion if needed
if (useWebP && imagePattern.test(metadata.mimeType)) {
await imageUtils.convertToWebp(mainFilePath, webpOutputQuality);
}
const uploadParams: UploadParams = {
Key: generateKey({
mediaId: fileName.name,
access: metadata.accessControl === "public" ? "public" : "private",
filename: `main.${fileExtension}`,
}),
Body: createReadStream(mainFilePath),
ContentType: mimeType,
ACL: USE_CLOUDFRONT
? "private"
: metadata.accessControl === "public"
? "public-read"
: "private",
};
const tags = getTags(userId, metadata.group);
uploadParams.Tagging = tags;
await putObject(uploadParams);
let isThumbGenerated = false;
try {
isThumbGenerated = await generateAndUploadThumbnail({
workingDirectory: temporaryFolderForWork,
mimetype: metadata.mimeType,
originalFilePath: mainFilePath,
key: generateKey({
mediaId: fileName.name,
access: "public",
filename: "thumb.webp",
}),
tags,
});
} catch (err: any) {
logger.error({ err }, err.message);
}
await fsPromises.rm(temporaryFolderForWork, { recursive: true });
const mediaObject: MediaWithUserId = {
fileName: `main.${fileExtension}`,
mediaId: fileName.name,
userId: new mongoose.Types.ObjectId(userId),
apikey,
originalFileName: metadata.fileName,
mimeType,
size: uploadLength,
thumbnailGenerated: isThumbGenerated,
caption: metadata.caption,
accessControl:
metadata.accessControl === "public" ? "public-read" : "private",
group: metadata.group,
};
const media = await createMedia(mediaObject);
// Mark upload as complete
await markTusUploadComplete(uploadId);
// Cleanup presigned URL if used
if (signature) {
presignedUrlService.cleanup(userId, signature).catch((err: any) => {
logger.error(
{ err },
`Error in cleaning up expired links for ${userId}`,
);
});
}
// Cleanup tus file
try {
if (existsSync(tusFilePath)) {
removeTusFiles(tempFilePath);
}
} catch (err) {
logger.error({ err }, "Error cleaning up tus file");
}
return media.mediaId;
}
const generateAndUploadThumbnail = async ({
workingDirectory,
key,
mimetype,
originalFilePath,
tags,
}: {
workingDirectory: string;
key: string;
mimetype: string;
originalFilePath: string;
tags: string;
}): Promise<boolean> => {
const thumbPath = `${workingDirectory}/thumb.webp`;
let isGenerated = false;
if (imagePatternForThumbnailGeneration.test(mimetype)) {
await thumbnail.forImage(originalFilePath, thumbPath);
isGenerated = true;
}
if (videoPattern.test(mimetype)) {
await thumbnail.forVideo(originalFilePath, thumbPath);
isGenerated = true;
}
if (isGenerated) {
await putObject({
Key: key,
Body: createReadStream(thumbPath),
ContentType: "image/webp",
ACL: USE_CLOUDFRONT ? "private" : "public-read",
Tagging: tags,
});
await fsPromises.rm(thumbPath);
}
return isGenerated;
};