2019-01-06 00:50:05 +00:00
|
|
|
import path from 'path';
|
|
|
|
import etag from 'etag';
|
2018-07-31 17:07:27 +00:00
|
|
|
|
2019-07-10 00:21:25 +00:00
|
|
|
import getContentTypeHeader from '../utils/getContentTypeHeader.js';
|
2018-07-31 17:07:27 +00:00
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export default function serveStaticFile(req, res) {
|
2018-12-17 17:38:05 +00:00
|
|
|
const tags = ['file'];
|
2018-07-31 17:07:27 +00:00
|
|
|
|
|
|
|
const ext = path.extname(req.entry.name).substr(1);
|
|
|
|
if (ext) {
|
|
|
|
tags.push(`${ext}-file`);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
.set({
|
2018-12-17 17:38:05 +00:00
|
|
|
'Content-Length': req.entry.size,
|
|
|
|
'Content-Type': getContentTypeHeader(req.entry.contentType),
|
2019-01-07 05:10:18 +00:00
|
|
|
'Cache-Control': 'public, max-age=31536000', // 1 year
|
2018-12-17 17:38:05 +00:00
|
|
|
'Last-Modified': req.entry.lastModified,
|
2018-07-31 17:07:27 +00:00
|
|
|
ETag: etag(req.entry.content),
|
2019-01-02 02:03:54 +00:00
|
|
|
'Cache-Tag': tags.join(', ')
|
2018-07-31 17:07:27 +00:00
|
|
|
})
|
|
|
|
.send(req.entry.content);
|
|
|
|
}
|