fix(worker-hall): 优化抢单大厅两级分类联动体验并支持分类图片浏览器长效缓存
This commit is contained in:
@@ -1,19 +1,49 @@
|
||||
import { Router } from 'express'
|
||||
|
||||
import { getStoredFileObject } from '../services/file-storage/file-storage-service.js'
|
||||
import {
|
||||
getStoredFileObject,
|
||||
isPublicObjectKey,
|
||||
} from '../services/file-storage/file-storage-service.js'
|
||||
import { buildNotFoundPayload, sendRouteError } from '../utils/http.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
router.get('/object', async (req, res) => {
|
||||
try {
|
||||
const rawKey = String(req.query.key || '').trim()
|
||||
const isPublic = isPublicObjectKey(rawKey)
|
||||
const object = await getStoredFileObject(req.query.key, req.query.variant, {
|
||||
expires: req.query.expires,
|
||||
signature: req.query.signature,
|
||||
})
|
||||
|
||||
if (object.etag) {
|
||||
res.setHeader('ETag', object.etag)
|
||||
if (req.headers['if-none-match'] === object.etag) {
|
||||
res.status(304).end()
|
||||
return
|
||||
}
|
||||
}
|
||||
if (object.lastModified) {
|
||||
res.setHeader('Last-Modified', object.lastModified.toUTCString())
|
||||
if (req.headers['if-modified-since']) {
|
||||
const ifModifiedSince = new Date(req.headers['if-modified-since'])
|
||||
if (!isNaN(ifModifiedSince.getTime()) && object.lastModified <= ifModifiedSince) {
|
||||
res.status(304).end()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', object.contentType || 'application/octet-stream')
|
||||
res.setHeader('Cache-Control', 'private, max-age=300')
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff')
|
||||
|
||||
if (isPublic) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=2592000, stale-while-revalidate=604800')
|
||||
} else {
|
||||
res.setHeader('Cache-Control', 'private, max-age=300')
|
||||
}
|
||||
|
||||
object.reader.on('error', (error) => {
|
||||
if (!res.headersSent) {
|
||||
sendRouteError(res, error, '读取文件失败', '[files/object]')
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from 'express'
|
||||
|
||||
import {
|
||||
getWorkerHallCategories,
|
||||
getWorkerHallLeaderboard,
|
||||
grabWorkerHallOrder,
|
||||
joinWorkerSharingOrder,
|
||||
@@ -11,6 +12,16 @@ import { createRouteHandler } from '../../utils/http.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
router.get(
|
||||
'/hall/categories',
|
||||
requireActiveWorker,
|
||||
createRouteHandler((req) => getWorkerHallCategories(getRequiredWorkerSession(req)), {
|
||||
successMessage: 'ok',
|
||||
errorMessage: '读取抢单大厅分类失败',
|
||||
scope: '[worker/hall/categories]',
|
||||
}),
|
||||
)
|
||||
|
||||
router.get(
|
||||
'/hall/orders',
|
||||
requireActiveWorker,
|
||||
|
||||
Reference in New Issue
Block a user