@@ -9,6 +9,7 @@ import { DriveFile, TYPE } from "../entities/drive-file";
9
9
import { FileVersion , TYPE as FileVersionType } from "../entities/file-version" ;
10
10
import { CompanyExecutionContext , DriveItemDetails , RootType , TrashType } from "../types" ;
11
11
import {
12
+ addDriveItemToArchive ,
12
13
calculateItemSize ,
13
14
checkAccess ,
14
15
getDefaultDriveItem ,
@@ -17,6 +18,8 @@ import {
17
18
updateItemSize ,
18
19
} from "../utils" ;
19
20
21
+ import archiver from "archiver" ;
22
+
20
23
export class DocumentsService {
21
24
version : "1" ;
22
25
repository : Repository < DriveFile > ;
@@ -74,7 +77,7 @@ export class DocumentsService {
74
77
75
78
//Check access to entity
76
79
try {
77
- const hasAccess = checkAccess ( id , entity , "read" , this . repository , context ) ;
80
+ const hasAccess = await checkAccess ( id , entity , "read" , this . repository , context ) ;
78
81
if ( ! hasAccess ) {
79
82
this . logger . error ( "user does not have access drive item " , id ) ;
80
83
throw Error ( "user does not have access to this item" ) ;
@@ -161,7 +164,13 @@ export class DocumentsService {
161
164
const driveItem = getDefaultDriveItem ( content , context ) ;
162
165
const driveItemVersion = getDefaultDriveItemVersion ( version , context ) ;
163
166
164
- const hasAccess = checkAccess ( driveItem . parent_id , null , "write" , this . repository , context ) ;
167
+ const hasAccess = await checkAccess (
168
+ driveItem . parent_id ,
169
+ null ,
170
+ "write" ,
171
+ this . repository ,
172
+ context ,
173
+ ) ;
165
174
if ( ! hasAccess ) {
166
175
this . logger . error ( "user does not have access drive item parent" , driveItem . parent_id ) ;
167
176
throw Error ( "user does not have access to this item parent" ) ;
@@ -174,6 +183,7 @@ export class DocumentsService {
174
183
driveItem . extension = file . metadata . name . split ( "." ) . pop ( ) ;
175
184
driveItemVersion . filename = driveItemVersion . filename || file . metadata . name ;
176
185
driveItemVersion . file_size = file . upload_data . size ;
186
+ driveItemVersion . file_id = file . id ;
177
187
}
178
188
179
189
await this . fileVersionRepository . save ( driveItemVersion ) ;
@@ -209,7 +219,7 @@ export class DocumentsService {
209
219
}
210
220
211
221
try {
212
- const hasAccess = checkAccess ( id , null , "write" , this . repository , context ) ;
222
+ const hasAccess = await checkAccess ( id , null , "write" , this . repository , context ) ;
213
223
214
224
if ( ! hasAccess ) {
215
225
this . logger . error ( "user does not have access drive item " , id ) ;
@@ -296,7 +306,7 @@ export class DocumentsService {
296
306
}
297
307
298
308
try {
299
- if ( ! checkAccess ( item . id , item , "write" , this . repository , context ) ) {
309
+ if ( ! ( await checkAccess ( item . id , item , "write" , this . repository , context ) ) ) {
300
310
this . logger . error ( "user does not have access drive item " , id ) ;
301
311
throw Error ( "user does not have access to this item" ) ;
302
312
}
@@ -372,7 +382,7 @@ export class DocumentsService {
372
382
}
373
383
374
384
try {
375
- const hasAccess = checkAccess ( id , null , "write" , this . repository , context ) ;
385
+ const hasAccess = await checkAccess ( id , null , "write" , this . repository , context ) ;
376
386
if ( ! hasAccess ) {
377
387
this . logger . error ( "user does not have access drive item " , id ) ;
378
388
throw Error ( "user does not have access to this item" ) ;
@@ -408,4 +418,43 @@ export class DocumentsService {
408
418
throw new CrudException ( "Failed to create Drive item version" , 500 ) ;
409
419
}
410
420
} ;
421
+
422
+ /**
423
+ * Creates a zip archive containing the drive items.
424
+ *
425
+ * @param {string[] } ids - the drive item list
426
+ * @param {CompanyExecutionContext } context - the execution context
427
+ * @returns {Promise<archiver.Archiver> } the created archive.
428
+ */
429
+ createZip = async (
430
+ ids : string [ ] = [ ] ,
431
+ context : CompanyExecutionContext ,
432
+ ) : Promise < archiver . Archiver > => {
433
+ if ( ! context ) {
434
+ this . logger . error ( "invalid execution context" ) ;
435
+ return null ;
436
+ }
437
+
438
+ const archive = archiver ( "zip" , {
439
+ zlib : { level : 9 } ,
440
+ } ) ;
441
+
442
+ await Promise . all (
443
+ ids . map ( async id => {
444
+ if ( ! ( await checkAccess ( id , null , "read" , this . repository , context ) ) ) {
445
+ this . logger . warn ( `not enough permissions to download ${ id } , skipping` ) ;
446
+ return ;
447
+ }
448
+
449
+ try {
450
+ await addDriveItemToArchive ( id , null , archive , this . repository , context ) ;
451
+ } catch ( error ) {
452
+ this . logger . warn ( "failed to add item to archive" , error ) ;
453
+ throw new Error ( "Failed to add item to archive" ) ;
454
+ }
455
+ } ) ,
456
+ ) ;
457
+
458
+ return archive ;
459
+ } ;
411
460
}
0 commit comments