ファイルサイズのハードリミット (#7760)

* maxFileSize

* CHANGELOG
This commit is contained in:
MeiMei 2021-09-04 20:33:14 +09:00 committed by GitHub
parent da20675ada
commit e21ff916b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 1 deletions

View file

@ -39,6 +39,8 @@ export type Source = {
allowedPrivateNetworks?: string[];
maxFileSize?: number;
accesslog?: string;
clusterLimit?: number;

View file

@ -18,6 +18,7 @@ export async function downloadUrl(url: string, path: string) {
const timeout = 30 * 1000;
const operationTimeout = 60 * 1000;
const maxSize = config.maxFileSize || 262144000;
const req = got.stream(url, {
headers: {
@ -44,6 +45,20 @@ export async function downloadUrl(url: string, path: string) {
req.destroy();
}
}
const contentLength = res.headers['content-length'];
if (contentLength != null) {
const size = Number(contentLength);
if (size > maxSize) {
logger.warn(`maxSize exceeded (${size} > ${maxSize}) on response`);
req.destroy();
}
}
}).on('downloadProgress', (progress: Got.Progress) => {
if (progress.transferred > maxSize) {
logger.warn(`maxSize exceeded (${progress.transferred} > ${maxSize}) on downloadProgress`);
req.destroy();
}
}).on('error', (e: any) => {
if (e.name === 'HTTPError') {
const statusCode = e.response?.statusCode;

View file

@ -16,6 +16,7 @@ import discord from './service/discord';
import github from './service/github';
import twitter from './service/twitter';
import { Instances, AccessTokens, Users } from '@/models/index';
import config from '@/config';
// Init app
const app = new Koa();
@ -37,7 +38,11 @@ app.use(bodyParser({
// Init multer instance
const upload = multer({
storage: multer.diskStorage({})
storage: multer.diskStorage({}),
limits: {
fileSize: config.maxFileSize || 262144000,
files: 1,
}
});
// Init router