Make express download fallback max size configurable

This commit is contained in:
Alice Gaudon 2020-11-22 12:52:01 +01:00
parent b5daa7c3b3
commit 5f59800229
3 changed files with 10 additions and 8 deletions

View File

@ -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,
});
});

View File

@ -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,
});
});

View File

@ -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<number>('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]);