From 5f59800229b0df8d790118a512349fa1fc875160 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 22 Nov 2020 12:52:01 +0100 Subject: [PATCH] Make express download fallback max size configurable --- config/default.ts | 5 +++-- config/production.ts | 3 ++- src/controllers/LinkController.ts | 10 +++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/config/default.ts b/config/default.ts index 0fca77e..7b327c0 100644 --- a/config/default.ts +++ b/config/default.ts @@ -20,7 +20,8 @@ export default Object.assign(require("wms-core/config/default").default, { }, newlyGeneratedSlugSize: 3, default_file_ttl: 30, // 30 seconds - max_upload_size: 1, // MB + max_upload_size: 16, // MB + max_hotlink_size: 1, // MB approval_mode: false, mail: { host: "127.0.0.1", @@ -38,4 +39,4 @@ export default Object.assign(require("wms-core/config/default").default, { ], default_url_domain_for_files: 0, default_url_domain_for_urls: 1, -}); \ No newline at end of file +}); diff --git a/config/production.ts b/config/production.ts index 756f6cd..5ed73c8 100644 --- a/config/production.ts +++ b/config/production.ts @@ -15,6 +15,7 @@ export default Object.assign(require("wms-core/config/production").default, { newlyGeneratedSlugSize: 5, default_file_ttl: 30 * 24 * 3600, // 30 days max_upload_size: 8192, // MB + max_hotlink_size: 128, // MB approval_mode: true, mail: { secure: true, @@ -26,4 +27,4 @@ export default Object.assign(require("wms-core/config/production").default, { ], default_url_domain_for_files: 0, default_url_domain_for_urls: 1, -}); \ No newline at end of file +}); diff --git a/src/controllers/LinkController.ts b/src/controllers/LinkController.ts index 47779ec..2e70526 100644 --- a/src/controllers/LinkController.ts +++ b/src/controllers/LinkController.ts @@ -11,7 +11,6 @@ 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 { @@ -38,18 +37,19 @@ export default class LinkController extends Controller { return next(); } + const fileName = file.real_name!; + 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) { + // If file is bigger than max hotlink size, fallback to express download + if (stats.size > config.get('max_hotlink_size') * 1024 * 1024) { Logger.info(`Fallback to express download for file of size ${stats.size}`); - return res.download(file.storage_path!); + return res.download(file.storage_path!, fileName); } // File type - const fileName = file.real_name!; const parts = fileName.split('.'); res.type(parts[parts.length - 1]);