@@ -6,10 +6,11 @@ import {
66} from "@nestjs/common" ;
77import { JwtService , JwtSignOptions } from "@nestjs/jwt" ;
88import { Share , User } from "@prisma/client" ;
9- import * as archiver from "archiver" ;
109import * as argon from "argon2" ;
1110import * as fs from "fs" ;
1211import * as moment from "moment" ;
12+ import * as path from "path" ;
13+ import { Worker } from "worker_threads" ;
1314import { ClamScanService } from "src/clamscan/clamscan.service" ;
1415import { ConfigService } from "src/config/config.service" ;
1516import { EmailService } from "src/email/email.service" ;
@@ -32,7 +33,7 @@ export class ShareService {
3233 private jwtService : JwtService ,
3334 private reverseShareService : ReverseShareService ,
3435 private clamScanService : ClamScanService ,
35- ) { }
36+ ) { }
3637
3738 async create ( share : CreateShareDTO , user ?: User , reverseShareToken ?: string ) {
3839 if ( ! ( await this . isShareIdAvailable ( share . id ) ) . isAvailable )
@@ -62,7 +63,7 @@ export class ShareService {
6263 maxExpiration . value !== 0 &&
6364 ( expiresNever ||
6465 parsedExpiration >
65- moment ( ) . add ( maxExpiration . value , maxExpiration . unit ) . toDate ( ) )
66+ moment ( ) . add ( maxExpiration . value , maxExpiration . unit ) . toDate ( ) )
6667 ) {
6768 throw new BadRequestException (
6869 "Expiration date exceeds maximum expiration date" ,
@@ -109,22 +110,36 @@ export class ShareService {
109110 async createZip ( shareId : string ) {
110111 if ( this . config . get ( "s3.enabled" ) ) return ;
111112
112- const path = `${ SHARE_DIRECTORY } /${ shareId } ` ;
113-
114113 const files = await this . prisma . file . findMany ( { where : { shareId } } ) ;
115- const archive = archiver ( "zip" , {
116- zlib : { level : this . config . get ( "share.zipCompressionLevel" ) } ,
117- } ) ;
118- const writeStream = fs . createWriteStream ( `${ path } /archive.zip` ) ;
119114
120- for ( const file of files ) {
121- archive . append ( fs . createReadStream ( `${ path } /${ file . id } ` ) , {
122- name : file . name ,
115+ return new Promise ( ( resolve , reject ) => {
116+ const isTs = __filename . endsWith ( ".ts" ) ;
117+ const workerFileName = isTs ? "zip.worker.ts" : "zip.worker.js" ;
118+ const workerPath = path . join ( __dirname , workerFileName ) ;
119+
120+ const worker = new Worker ( workerPath , {
121+ workerData : {
122+ shareId,
123+ files,
124+ shareDirectory : SHARE_DIRECTORY ,
125+ compressionLevel : this . config . get ( "share.zipCompressionLevel" ) ,
126+ } ,
127+ execArgv : isTs ? [ "-r" , "ts-node/register" ] : undefined ,
123128 } ) ;
124- }
125129
126- archive . pipe ( writeStream ) ;
127- await archive . finalize ( ) ;
130+ worker . on ( "message" , ( message ) => {
131+ if ( typeof message === "object" && message . error ) {
132+ reject ( new Error ( message . error ) ) ;
133+ } else {
134+ resolve ( message ) ;
135+ }
136+ } ) ;
137+ worker . on ( "error" , reject ) ;
138+ worker . on ( "exit" , ( code ) => {
139+ if ( code !== 0 )
140+ reject ( new Error ( `Worker stopped with exit code ${ code } ` ) ) ;
141+ } ) ;
142+ } ) ;
128143 }
129144
130145 async complete ( id : string , reverseShareToken ?: string ) {
@@ -165,7 +180,7 @@ export class ShareService {
165180
166181 const notifyReverseShareCreator = share . reverseShare
167182 ? this . config . get ( "smtp.enabled" ) &&
168- share . reverseShare . sendEmailNotification
183+ share . reverseShare . sendEmailNotification
169184 : undefined ;
170185
171186 if ( notifyReverseShareCreator ) {
@@ -323,7 +338,7 @@ export class ShareService {
323338 // Handle security updates
324339 if ( updateData . security ) {
325340 hasSecurityUpdates = true ;
326-
341+
327342 if ( updateData . security . password ) {
328343 updateSecurity . password = await argon . hash ( updateData . security . password ) ;
329344 }
@@ -341,11 +356,11 @@ export class ShareService {
341356 ...( hasSecurityUpdates && {
342357 security : existingShare . security
343358 ? {
344- update : updateSecurity ,
345- }
359+ update : updateSecurity ,
360+ }
346361 : {
347- create : updateSecurity ,
348- } ,
362+ create : updateSecurity ,
363+ } ,
349364 } ) ,
350365 } ,
351366 } ) ;
0 commit comments