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>
46 lines
2.1 KiB
JavaScript
46 lines
2.1 KiB
JavaScript
// Bildtyp anhand der Magic Bytes bestimmen (ADR 0004 §2). Der Header des Clients zählt nicht.
|
|
// Liefert 'png' | 'jpeg' | 'gif' | 'webp' oder null. Zu kurze oder abgeschnittene Dateien ergeben null.
|
|
|
|
const startsWith = (buf, bytes, offset = 0)=>buf.length >= offset + bytes.length && bytes.every((b, i)=>buf[offset + i] === b);
|
|
const ascii = (buf, str, offset = 0)=>buf.length >= offset + str.length && buf.toString('latin1', offset, offset + str.length) === str;
|
|
|
|
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
|
|
const isPng = (buf)=>{
|
|
if(!startsWith(buf, PNG_SIGNATURE) || !ascii(buf, 'IHDR', 12)) return false;
|
|
// Schluss mit IEND-Chunk (12 Byte: Länge 0, "IEND", CRC), sonst abgeschnitten
|
|
return buf.length >= 45 && ascii(buf, 'IEND', buf.length - 8);
|
|
};
|
|
|
|
// EOI-Marker (FF D9) in den letzten 32 Byte: manche Kameras hängen Füllbytes an
|
|
const isJpeg = (buf)=>{
|
|
if(!startsWith(buf, [0xff, 0xd8, 0xff]) || buf.length < 4) return false;
|
|
const tail = buf.subarray(Math.max(0, buf.length - 32));
|
|
return tail.includes(Buffer.from([0xff, 0xd9]));
|
|
};
|
|
|
|
const isGif = (buf)=>(ascii(buf, 'GIF87a') || ascii(buf, 'GIF89a')) && buf.length >= 14 && buf[buf.length - 1] === 0x3b;
|
|
|
|
const isWebp = (buf)=>{
|
|
if(!ascii(buf, 'RIFF') || !ascii(buf, 'WEBP', 8) || buf.length < 20) return false;
|
|
if(!['VP8 ', 'VP8L', 'VP8X'].some((f)=>ascii(buf, f, 12))) return false;
|
|
// RIFF-Länge muss zur Dateigröße passen (ohne Padding-Byte), sonst abgeschnitten
|
|
const declared = buf.readUInt32LE(4) + 8;
|
|
return buf.length === declared || buf.length === declared + 1;
|
|
};
|
|
|
|
export const sniffImageType = (buf)=>{
|
|
if(!Buffer.isBuffer(buf) || buf.length === 0) return null;
|
|
if(isPng(buf)) return 'png';
|
|
if(isJpeg(buf)) return 'jpeg';
|
|
if(isGif(buf)) return 'gif';
|
|
if(isWebp(buf)) return 'webp';
|
|
return null;
|
|
};
|
|
|
|
// Content-Type-Header auf unseren Typnamen abbilden (ohne Parameter, jpg/pjpeg-Aliasse)
|
|
export const typeFromMime = (mime)=>{
|
|
const m = String(mime ?? '').split(';')[0].trim().toLowerCase();
|
|
return { 'image/png': 'png', 'image/jpeg': 'jpeg', 'image/jpg': 'jpeg', 'image/pjpeg': 'jpeg', 'image/gif': 'gif', 'image/webp': 'webp' }[m] ?? null;
|
|
};
|