feat: add cache header for file

This commit is contained in:
Innei
2022-05-24 13:28:31 +08:00
parent ae5436bb92
commit 0a26299083
3 changed files with 9 additions and 22 deletions

View File

@@ -37,7 +37,7 @@ export class FileController {
@Auth()
async getTypes(@Query() query: PagerDto, @Param() params: FileUploadDto) {
const { type = 'file' } = params
const { page, size } = query
// const { page, size } = query
const dir = await this.service.getDir(type)
return Promise.all(
dir.map(async (name) => {
@@ -58,6 +58,11 @@ export class FileController {
const buffer = await this.service.getFile(type, name)
if (mimetype) {
reply.type(mimetype)
reply.header('cache-control', 'public, max-age=31536000')
reply.header(
'expires',
new Date(Date.now() + 31536000 * 1000).toUTCString(),
)
}
reply.send(buffer)

View File

@@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify'
import { createReadStream, existsSync, statSync } from 'fs'
import fs from 'fs/promises'
import { isNull } from 'lodash'
import { lookup } from 'mime-types'
import PKG from 'package.json'
import { extname, join } from 'path'
@@ -212,7 +213,8 @@ export class PageProxyController {
})
}
const stream = createReadStream(path)
const minetype = this.service.getMineTypeByExt(extname(path))
const minetype = lookup(extname(path))
reply.header('cache-control', 'public, max-age=31536000')
reply.header(
'expires',

View File

@@ -120,26 +120,6 @@ export class PageProxyService {
return dom.document.toString()
}
getMineTypeByExt(ext: string) {
return {
'.css': 'text/css',
'.js': 'application/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml',
'.woff': 'application/font-woff',
'.woff2': 'application/font-woff2',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.html': 'text/html',
'.xml': 'text/xml',
'.txt': 'text/plain',
'.json': 'application/json',
}[ext]
}
async getUrlFromConfig() {
const config = await this.configs.waitForConfigReady()
const url = config.url