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}`)); } }