This commit is contained in:
seb
2026-10-09 21:43:17 +02:00
parent ac10840fb6
commit d4e2b05cb0
6 changed files with 132 additions and 2 deletions

View File

@@ -209,4 +209,89 @@ export function register(app, basePath = '') {
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /smallthumb/{fullHash}:
* get:
* tags: [Image]
* summary: Small thumb image by hash
* description: |
* Returns the small thumb AVIF (40px max edge) for `pic.hash` (fullHash — not thumbhash).
* Auth via article category access. Browser-cacheable (`private`, `immutable`, `ETag`).
* security:
* - cookieAuth: []
* x-markdown: smallthumb/get.md
* parameters:
* - in: path
* name: fullHash
* required: true
* schema:
* type: string
* pattern: '^[a-zA-Z0-9]{8}$'
* description: 8-char alphanumeric hash of the full image bytes
* responses:
* 200:
* description: AVIF small thumb bytes
* headers:
* Cache-Control:
* schema:
* type: string
* example: private, max-age=31536000, immutable
* ETag:
* schema:
* type: string
* content:
* image/avif:
* schema:
* type: string
* format: binary
* 304:
* description: Not modified (If-None-Match matched ETag)
* 400:
* description: Invalid hash
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Image not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
const smallThumbPaths = [`${basePath}/smallthumb/:fullHash`];
if (basePath) smallThumbPaths.push('/smallthumb/:fullHash');
app.get(smallThumbPaths, async (req, res) => {
const parsed = parseFullHash(req.params.fullHash);
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
}
try {
const image = await db.getSmallThumbByHash(req.user.id, parsed.fullHash);
sendImmutableAvif(req, res, {
buffer: image.buffer,
contentType: image.contentType,
fullHash: parsed.fullHash,
});
} catch (error) {
const status = picErrorStatus(error);
if (status === 500) console.error('Get smallthumb by hash failed:', error);
res.status(status).json({ error: error.message });
}
});
}

View File

@@ -9,3 +9,8 @@ export async function getThumbByHash(googleAuthId, fullHash) {
const store = await getStore();
return store.pic.getThumbByHash(googleAuthId, fullHash);
}
export async function getSmallThumbByHash(googleAuthId, fullHash) {
const store = await getStore();
return store.pic.getSmallThumbByHash(googleAuthId, fullHash);
}