diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index 557ccfef6..3414a982c 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -1,5 +1,6 @@ import http from 'http'; -import { resolve } from 'path'; +import { tmpdir } from 'os'; +import { join, resolve } from 'path'; import cac from 'cac'; import fs from 'fs-extra'; import Koa from 'koa'; @@ -484,14 +485,22 @@ export async function apply(pluginContext: Context) { ${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`); }); } + const uploadDir = join(tmpdir(), 'hydro', 'upload', process.env.NODE_APP_INSTANCE || '0'); + fs.ensureDirSync(uploadDir); + logger.debug('Using upload dir: %s', uploadDir); app.use(Body({ multipart: true, jsonLimit: '8mb', formLimit: '8mb', formidable: { + uploadDir, maxFileSize: parseMemoryMB(system.get('server.upload') || '256m') * 1024 * 1024, + keepExtensions: true, }, })); + pluginContext.on('app/exit', () => { + fs.emptyDirSync(uploadDir); + }); const layers = [baseLayer, rendererLayer(router, logger), responseLayer(logger), userLayer]; app.use(async (ctx, next) => await next().catch(console.error)).use(domainLayer); app.use(router.routes()).use(router.allowedMethods()); diff --git a/packages/hydrooj/src/ui.ts b/packages/hydrooj/src/ui.ts index 3a29c5225..9372c9002 100644 --- a/packages/hydrooj/src/ui.ts +++ b/packages/hydrooj/src/ui.ts @@ -2,8 +2,14 @@ import { Logger } from './logger'; import * as bus from './service/bus'; +let terminating = false; async function terminate() { + if (terminating) process.exit(1); let hasError = false; + terminating = true; + setTimeout(() => { + new Logger('exit').info('Cleaning up temporary files... (Press Ctrl-C again to force exit)'); + }, 1000); try { await bus.parallel('app/exit'); } catch (e) { @@ -12,6 +18,7 @@ async function terminate() { process.exit(hasError ? 1 : 0); } process.on('SIGINT', terminate); +process.on('SIGTERM', terminate); const shell = new Logger('shell'); async function executeCommand(input: string) {