From 07847e06c594634583c5092541c56f7e83a46218 Mon Sep 17 00:00:00 2001 From: mcdamo Date: Mon, 3 May 2021 22:49:41 +1000 Subject: [PATCH] Convert require to import packages --- package-lock.json | 9 +++++++++ package.json | 1 + src/backend/middlewares/GalleryMWs.ts | 16 ++++++---------- src/backend/middlewares/RenderingMWs.ts | 7 ------- src/backend/routes/GalleryRouter.ts | 3 +-- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index eb207077..ff8c83b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3260,6 +3260,15 @@ "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==", "dev": true }, + "@types/archiver": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@types/archiver/-/archiver-5.1.0.tgz", + "integrity": "sha512-baFOhanb/hxmcOd1Uey2TfFg43kTSmM6py1Eo7Rjbv/ivcl7PXLhY0QgXGf50Hx/eskGCFqPfhs/7IZLb15C5g==", + "dev": true, + "requires": { + "@types/glob": "*" + } + }, "@types/bcrypt": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-3.0.1.tgz", diff --git a/package.json b/package.json index 9f3234c8..058b499b 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "@angular/platform-browser-dynamic": "11.2.9", "@angular/router": "11.2.9", "@ngx-loading-bar/core": "5.1.1", + "@types/archiver": "^5.1.0", "@types/bcrypt": "3.0.1", "@types/bcryptjs": "2.4.2", "@types/chai": "4.2.16", diff --git a/src/backend/middlewares/GalleryMWs.ts b/src/backend/middlewares/GalleryMWs.ts index a2b6ed48..903f0b24 100644 --- a/src/backend/middlewares/GalleryMWs.ts +++ b/src/backend/middlewares/GalleryMWs.ts @@ -1,5 +1,6 @@ import * as path from 'path'; import {promises as fsp} from 'fs'; +import * as archiver from 'archiver'; import {NextFunction, Request, Response} from 'express'; import {ErrorCodes, ErrorDTO} from '../../common/entities/Error'; import {DirectoryDTO, DirectoryDTOUtils} from '../../common/entities/DirectoryDTO'; @@ -75,8 +76,6 @@ export class GalleryMWs { res.set('Content-Type', 'application/zip'); res.set('Content-Disposition', 'attachment; filename=Gallery.zip'); - const fs = require('fs'); - const archiver = require('archiver'); const archive = archiver('zip'); res.on('close', function() { @@ -89,21 +88,18 @@ export class GalleryMWs { archive.pipe(res); - // append photos in selected directory + // append photos in absoluteDirectoryName + // using case-insensitive glob of extensions for (const ext of SupportedFormats.WithDots.Photos) { archive.glob(`*${ext}`, {cwd:absoluteDirectoryName, nocase:true}); } - // append videos in selected directory + // append videos in absoluteDirectoryName + // using case-insensitive glob of extensions for (const ext of SupportedFormats.WithDots.Videos) { archive.glob(`*${ext}`, {cwd:absoluteDirectoryName, nocase:true}); } - await archive.finalize(function(err: any) { - if (err) { - throw err; - } - req.resultPipe = true; - }); + await archive.finalize(); return next(); } catch (err) { diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index 7e3b5f06..9f2636be 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -71,13 +71,6 @@ export class RenderingMWs { return res.sendFile(req.resultPipe, {maxAge: 31536000, dotfiles: 'allow'}); } - public static renderZipStream(req: Request, res: Response, next: NextFunction): any { - if (!req.resultPipe) { - return next(); - } - return res; - } - public static renderOK(req: Request, res: Response, next: NextFunction): void { const message = new Message(null, 'ok'); res.json(message); diff --git a/src/backend/routes/GalleryRouter.ts b/src/backend/routes/GalleryRouter.ts index e219c7f8..8aa35545 100644 --- a/src/backend/routes/GalleryRouter.ts +++ b/src/backend/routes/GalleryRouter.ts @@ -53,8 +53,7 @@ export class GalleryRouter { AuthenticationMWs.authorisePath('directory', true), // specific part - GalleryMWs.zipDirectory, - RenderingMWs.renderZipStream + GalleryMWs.zipDirectory ); }