From cbbdc98744fd6542051d10b5309e0de00371ea3c Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Fri, 20 Dec 2019 01:39:59 +0900 Subject: [PATCH] =?UTF-8?q?/files/=20=E4=B8=8B=E3=81=AE=E3=83=98=E3=83=83?= =?UTF-8?q?=E3=83=80=E8=A8=AD=E5=AE=9A=E3=82=BF=E3=82=A4=E3=83=9F=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3=20(#5650)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/file/index.ts | 9 ++------- src/server/file/send-drive-file.ts | 17 +++++++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/server/file/index.ts b/src/server/file/index.ts index e6df321d4..bb328ee68 100644 --- a/src/server/file/index.ts +++ b/src/server/file/index.ts @@ -12,19 +12,14 @@ import sendDriveFile from './send-drive-file'; const app = new Koa(); app.use(cors()); -app.use(async (ctx, next) => { - // Cache 365days - ctx.set('Cache-Control', 'max-age=31536000, immutable'); - await next(); -}); - // Init router const router = new Router(); router.get('/app-default.jpg', ctx => { const file = fs.createReadStream(`${__dirname}/assets/dummy.png`); - ctx.set('Content-Type', 'image/jpeg'); ctx.body = file; + ctx.set('Content-Type', 'image/jpeg'); + ctx.set('Cache-Control', 'max-age=31536000, immutable'); }); router.get('/:key', sendDriveFile); diff --git a/src/server/file/send-drive-file.ts b/src/server/file/send-drive-file.ts index e7d0ab291..a05477488 100644 --- a/src/server/file/send-drive-file.ts +++ b/src/server/file/send-drive-file.ts @@ -11,6 +11,7 @@ const assets = `${__dirname}/../../server/file/assets/`; const commonReadableHandlerGenerator = (ctx: Koa.Context) => (e: Error): void => { serverLogger.error(e); ctx.status = 500; + ctx.set('Cache-Control', 'max-age=300'); }; export default async function(ctx: Koa.Context) { @@ -25,12 +26,14 @@ export default async function(ctx: Koa.Context) { if (file == null) { ctx.status = 404; + ctx.set('Cache-Control', 'max-age=86400'); await send(ctx as any, '/dummy.png', { root: assets }); return; } if (!file.storedInternal) { ctx.status = 204; + ctx.set('Cache-Control', 'max-age=86400'); return; } @@ -38,19 +41,21 @@ export default async function(ctx: Koa.Context) { const isWebpublic = file.webpublicAccessKey === key; if (isThumbnail) { + ctx.body = InternalStorage.read(key); ctx.set('Content-Type', 'image/jpeg'); + ctx.set('Cache-Control', 'max-age=31536000, immutable'); ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-thumb', extname: '.jpeg' })}`)); - ctx.body = InternalStorage.read(key); } else if (isWebpublic) { - ctx.set('Content-Type', file.type === 'image/apng' ? 'image/png' : file.type); - ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-web' })}`)); ctx.body = InternalStorage.read(key); + ctx.set('Content-Type', file.type === 'image/apng' ? 'image/png' : file.type); + ctx.set('Cache-Control', 'max-age=31536000, immutable'); + ctx.set('Content-Disposition', contentDisposition('inline', `${rename(file.name, { suffix: '-web' })}`)); } else { - ctx.set('Content-Disposition', contentDisposition('inline', `${file.name}`)); - const readable = InternalStorage.read(file.accessKey!); readable.on('error', commonReadableHandlerGenerator(ctx)); - ctx.set('Content-Type', file.type); ctx.body = readable; + ctx.set('Content-Type', file.type); + ctx.set('Cache-Control', 'max-age=31536000, immutable'); + ctx.set('Content-Disposition', contentDisposition('inline', `${file.name}`)); } }