Upload-Endpunkt POST /api/git/brews/:editId/assets (Magic Bytes, konfigurierbare Limits HB_ASSET_*, Dedupe, Commit ins Repo, Snapshot in brew_assets) und Auslieferung /git-assets/:shareId/:sha256.:ext gemäß ADR 0004. Postgres-Migration 0003, Mongo-Repo, Editor-Button, Drag-and-drop und Einfügen. Closes #10 Co-authored-by: Dev (Paperclip) <dev-bot@noreply.git.morlana.online>
51 lines
2.3 KiB
JavaScript
51 lines
2.3 KiB
JavaScript
import express from 'express';
|
|
import asyncHandler from 'express-async-handler';
|
|
import { typeForExt, mimeFor } from './paths.js';
|
|
|
|
// GET /git-assets/:shareId/:sha256.:ext (ADR 0004 §4). Kein Login, keine Cookies: wer die shareId kennt,
|
|
// sieht den Brew samt Bildern. Muss VOR der Content-Negotiation-Middleware hängen (Accept: image/* → 406).
|
|
const FILE = /^([0-9a-f]{64})\.([a-z0-9]{1,5})$/;
|
|
|
|
export const createAssetsDeliveryRouter = ({ getStorage, logger = console })=>{
|
|
const router = express.Router();
|
|
|
|
const notFound = (res)=>res.status(404).set('Cache-Control', 'no-store').type('text/plain').send('Nicht gefunden');
|
|
|
|
router.get('/git-assets/:shareId/:file', asyncHandler(async (req, res)=>{
|
|
const m = FILE.exec(req.params.file);
|
|
const type = m && typeForExt(m[2]);
|
|
if(!type || !/^[A-Za-z0-9_-]{1,64}$/.test(req.params.shareId)) return notFound(res);
|
|
const sha256 = m[1];
|
|
|
|
let brew; let asset;
|
|
try {
|
|
brew = await getStorage().brews.findByShareId(req.params.shareId);
|
|
if(!brew) return notFound(res);
|
|
asset = await getStorage().brewAssets.findByBrewAndHash(brew.id, sha256, { withData: true });
|
|
} catch (err) {
|
|
logger.error?.('[assets] Auslieferung:', err?.message);
|
|
return res.status(503).set('Cache-Control', 'no-store').type('text/plain').send('Datenbank nicht erreichbar');
|
|
}
|
|
if(!asset || asset.contentType !== mimeFor(type)) return notFound(res);
|
|
|
|
// Gesperrte Brews: wie /share (Upstream-Fehler 51)
|
|
if(brew.lock) return res.status(403).set('Cache-Control', 'no-store').json({ HBErrorCode: '51', message: brew.lock.shareMessage ?? 'Dieser Brew ist gesperrt.' });
|
|
|
|
const etag = `"${asset.sha256}"`;
|
|
res.set({
|
|
'Content-Type' : asset.contentType,
|
|
'Cache-Control' : 'public, max-age=31536000, immutable',
|
|
'ETag' : etag,
|
|
'X-Content-Type-Options' : 'nosniff',
|
|
'Content-Security-Policy' : 'default-src \'none\'; sandbox',
|
|
'Cross-Origin-Resource-Policy' : 'same-site'
|
|
});
|
|
if(req.get('if-none-match')?.split(',').map((s)=>s.trim().replace(/^W\//, '')).some((t)=>t === etag || t === '*')) return res.status(304).end();
|
|
res.set('Content-Length', String(asset.data.length));
|
|
if(req.method === 'HEAD') return res.end();
|
|
res.end(asset.data);
|
|
}));
|
|
|
|
return router;
|
|
};
|