@@ -24,44 +24,9 @@ import { createMedia } from "../media/queries";
2424import getTags from "../media/utils/get-tags" ;
2525import { getTusUpload , markTusUploadComplete } from "./queries" ;
2626import * as presignedUrlService from "../presigning/service" ;
27-
28- const generateAndUploadThumbnail = async ( {
29- workingDirectory,
30- key,
31- mimetype,
32- originalFilePath,
33- tags,
34- } : {
35- workingDirectory : string ;
36- key : string ;
37- mimetype : string ;
38- originalFilePath : string ;
39- tags : string ;
40- } ) : Promise < boolean > => {
41- const thumbPath = `${ workingDirectory } /thumb.webp` ;
42-
43- let isThumbGenerated = false ;
44- if ( imagePatternForThumbnailGeneration . test ( mimetype ) ) {
45- await thumbnail . forImage ( originalFilePath , thumbPath ) ;
46- isThumbGenerated = true ;
47- }
48- if ( videoPattern . test ( mimetype ) ) {
49- await thumbnail . forVideo ( originalFilePath , thumbPath ) ;
50- isThumbGenerated = true ;
51- }
52-
53- if ( isThumbGenerated ) {
54- await putObject ( {
55- Key : key ,
56- Body : createReadStream ( thumbPath ) ,
57- ContentType : "image/webp" ,
58- ACL : USE_CLOUDFRONT ? "private" : "public-read" ,
59- Tagging : tags ,
60- } ) ;
61- }
62-
63- return isThumbGenerated ;
64- } ;
27+ import { getUser } from "../user/queries" ;
28+ import { hasEnoughStorage } from "../media/storage-middleware" ;
29+ import { NOT_ENOUGH_STORAGE } from "../config/strings" ;
6530
6631export default async function finalizeUpload ( uploadId : string ) {
6732 logger . info ( { uploadId } , "Finalizing tus upload" ) ;
@@ -79,6 +44,11 @@ export default async function finalizeUpload(uploadId: string) {
7944 const { userId, apikey, metadata, uploadLength, tempFilePath, signature } =
8045 tusUpload ;
8146
47+ const user = await getUser ( userId ) ;
48+ if ( ! ( await hasEnoughStorage ( uploadLength , user ! ) ) ) {
49+ throw new Error ( NOT_ENOUGH_STORAGE ) ;
50+ }
51+
8252 // Read the completed file from tus data store
8353 const tusFilePath = path . join (
8454 `${ tempFileDirForUploads } /tus-uploads` ,
@@ -95,41 +65,43 @@ export default async function finalizeUpload(uploadId: string) {
9565 const webpOutputQuality = mediaSettings ?. webpOutputQuality || 0 ;
9666
9767 // Generate unique media ID
98- const fileName = generateFileName ( metadata . filename ) ;
68+ const fileName = generateFileName ( metadata . fileName ) ;
9969 const temporaryFolderForWork = `${ tempFileDirForUploads } /${ fileName . name } ` ;
10070 if ( ! foldersExist ( [ temporaryFolderForWork ] ) ) {
10171 createFolders ( [ temporaryFolderForWork ] ) ;
10272 }
10373
104- let fileExtension = path . extname ( metadata . filename ) . replace ( "." , "" ) ;
105- let mimeType = metadata . mimetype ;
74+ let fileExtension = path . extname ( metadata . fileName ) . replace ( "." , "" ) ;
75+ let mimeType = metadata . mimeType ;
10676 if ( useWebP && imagePattern . test ( mimeType ) ) {
10777 fileExtension = "webp" ;
10878 mimeType = "image/webp" ;
10979 }
11080
11181 const mainFilePath = `${ temporaryFolderForWork } /main.${ fileExtension } ` ;
11282
113- // Copy file from tus store to working directory
83+ console . log ( mainFilePath , tusFilePath ) ;
84+
85+ //Copy file from tus store to working directory
11486 const tusFileContent = readFileSync ( tusFilePath ) ;
11587 require ( "fs" ) . writeFileSync ( mainFilePath , tusFileContent ) ;
11688
11789 // Apply WebP conversion if needed
118- if ( useWebP && imagePattern . test ( metadata . mimetype ) ) {
90+ if ( useWebP && imagePattern . test ( metadata . mimeType ) ) {
11991 await imageUtils . convertToWebp ( mainFilePath , webpOutputQuality ) ;
12092 }
12193
12294 const uploadParams : UploadParams = {
12395 Key : generateKey ( {
12496 mediaId : fileName . name ,
125- access : metadata . access === "public" ? "public" : "private" ,
97+ access : metadata . accessControl === "public" ? "public" : "private" ,
12698 filename : `main.${ fileExtension } ` ,
12799 } ) ,
128100 Body : createReadStream ( mainFilePath ) ,
129101 ContentType : mimeType ,
130102 ACL : USE_CLOUDFRONT
131103 ? "private"
132- : metadata . access === "public"
104+ : metadata . accessControl === "public"
133105 ? "public-read"
134106 : "private" ,
135107 } ;
@@ -142,7 +114,7 @@ export default async function finalizeUpload(uploadId: string) {
142114 try {
143115 isThumbGenerated = await generateAndUploadThumbnail ( {
144116 workingDirectory : temporaryFolderForWork ,
145- mimetype : metadata . mimetype ,
117+ mimetype : metadata . mimeType ,
146118 originalFilePath : mainFilePath ,
147119 key : generateKey ( {
148120 mediaId : fileName . name ,
@@ -162,12 +134,13 @@ export default async function finalizeUpload(uploadId: string) {
162134 mediaId : fileName . name ,
163135 userId : new mongoose . Types . ObjectId ( userId ) ,
164136 apikey,
165- originalFileName : metadata . filename ,
137+ originalFileName : metadata . fileName ,
166138 mimeType,
167139 size : uploadLength ,
168140 thumbnailGenerated : isThumbGenerated ,
169141 caption : metadata . caption ,
170- accessControl : metadata . access === "public" ? "public-read" : "private" ,
142+ accessControl :
143+ metadata . accessControl === "public" ? "public-read" : "private" ,
171144 group : metadata . group ,
172145 } ;
173146 const media = await createMedia ( mediaObject ) ;
@@ -201,3 +174,41 @@ export default async function finalizeUpload(uploadId: string) {
201174
202175 return media . mediaId ;
203176}
177+
178+ const generateAndUploadThumbnail = async ( {
179+ workingDirectory,
180+ key,
181+ mimetype,
182+ originalFilePath,
183+ tags,
184+ } : {
185+ workingDirectory : string ;
186+ key : string ;
187+ mimetype : string ;
188+ originalFilePath : string ;
189+ tags : string ;
190+ } ) : Promise < boolean > => {
191+ const thumbPath = `${ workingDirectory } /thumb.webp` ;
192+
193+ let isThumbGenerated = false ;
194+ if ( imagePatternForThumbnailGeneration . test ( mimetype ) ) {
195+ await thumbnail . forImage ( originalFilePath , thumbPath ) ;
196+ isThumbGenerated = true ;
197+ }
198+ if ( videoPattern . test ( mimetype ) ) {
199+ await thumbnail . forVideo ( originalFilePath , thumbPath ) ;
200+ isThumbGenerated = true ;
201+ }
202+
203+ if ( isThumbGenerated ) {
204+ await putObject ( {
205+ Key : key ,
206+ Body : createReadStream ( thumbPath ) ,
207+ ContentType : "image/webp" ,
208+ ACL : USE_CLOUDFRONT ? "private" : "public-read" ,
209+ Tagging : tags ,
210+ } ) ;
211+ }
212+
213+ return isThumbGenerated ;
214+ } ;
0 commit comments