From 07fa213978d1fb0389f93b3413883323ffc2b604 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 22 Nov 2020 12:26:09 +0100 Subject: [PATCH 1/2] Fallback to express download if file size is bigger than max buffer size --- src/controllers/LinkController.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/controllers/LinkController.ts b/src/controllers/LinkController.ts index 4479209..47779ec 100644 --- a/src/controllers/LinkController.ts +++ b/src/controllers/LinkController.ts @@ -1,6 +1,6 @@ import Controller from "wms-core/Controller"; import {NextFunction, Request, Response} from "express"; -import {BadRequestError, ForbiddenHttpError, NotFoundHttpError, ServerError} from "wms-core/HttpError"; +import {BadRequestError, NotFoundHttpError, ServerError} from "wms-core/HttpError"; import config from "config"; import {REQUIRE_REQUEST_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent"; import URLRedirect from "../models/URLRedirect"; @@ -10,6 +10,9 @@ import generateSlug from "../SlugGenerator"; import FileController, {FILE_UPLOAD_FORM_MIDDLEWARE} from "./FileController"; import * as fs from "fs"; import {encodeRFC5987ValueChars} from "../Utils"; +import {promisify} from "util"; +import * as buffer from "buffer"; +import Logger from "wms-core/Logger"; export default class LinkController extends Controller { routes(): void { @@ -35,16 +38,24 @@ export default class LinkController extends Controller { return next(); } - // File type - const fileName = file.real_name!; - const parts = fileName.split('.'); - res.type(parts[parts.length - 1]); - - // File name - res.header('Content-Disposition', `inline; filename*=UTF-8''${encodeRFC5987ValueChars(fileName)}`); - switch (file.storage_type) { case 'local': + const stats = await promisify(fs.stat)(file.storage_path!); + + // If file is bigger than max supported one shot reading, fallback to express download + if (stats.size > buffer.constants.MAX_LENGTH) { + Logger.info(`Fallback to express download for file of size ${stats.size}`); + return res.download(file.storage_path!); + } + + // File type + const fileName = file.real_name!; + const parts = fileName.split('.'); + res.type(parts[parts.length - 1]); + + // File name + res.header('Content-Disposition', `inline; filename*=UTF-8''${encodeRFC5987ValueChars(fileName)}`); + fs.readFile(file.storage_path!, (err, data) => { if (err) return next(err); res.send(data); @@ -94,4 +105,4 @@ export default class LinkController extends Controller { } next(); } -} \ No newline at end of file +} From b5daa7c3b3feeaca44f823bd60136ad77cb9864c Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 22 Nov 2020 12:39:59 +0100 Subject: [PATCH 2/2] Version 0.5.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da0ec11..f128ac4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ily.li", - "version": "0.5.2", + "version": "0.5.3", "description": "Self-hosted file pusher", "repository": "https://gitlab.com/ArisuOngaku/ily.li", "author": "Alice Gaudon ",