Medienbibliothek: signierte Direkt-Uploads, echte Typprüfung, EXIF/GPS entfernt, Alt-Text, Fokuspunkt, srcset
Installieren
genpm add @core/mediaWas du bekommst
- Quellcode in src/lib/media/, 13 Dateien. (32,5 kB)
- KI-Regeln in src/lib/media/AGENTS.md, dazu Regeldateien für die IDE.
- Umgebungsvariablen in .env.example ergänzt: MEDIA_TRANSFORM.
- Löst @core/contracts, @core/db, @core/storage für dich auf.
README
Dieses Paket hat keine README.
Genau das liest deine KI, wenn sie in src/lib/media arbeitet. Sonst wird ihrem Kontext nichts hinzugefügt.
@core/media — rules for AI agents
Purpose
Media library on top of @core/storage: the browser uploads directly with a signed URL to a private staging key
(uploads/pending/<id>.<ext>), then confirmUpload checks the real file type by its bytes (declared type must match),
removes EXIF/GPS/XMP from JPEG, PNG and WebP without re-encoding, writes the validated bytes to the final public/
key, deletes the staging copy and stores dimensions. Malformed images (chunk lengths past the end) are rejected. Alt text and focal point per file, responsive URLs (Cloudflare Images or Next.js
image optimizer) and ready <img> attributes. SVG and HTML are never accepted. Table: media.
Map
index.ts— public API:createUpload,confirmUpload,getMedia,updateMedia,deleteMedia,imageUrl,imgAttrs,mediaAdminResource.remote.ts—importRemoteImage(url, { allowedHosts })for server-side imports (https, host allowlist, size limit).library.ts— upload flow, allowed types andstagingKey(media).sniff.ts,strip.ts— byte checks.urls.ts— URLs and<img>attributes.adapters/hono.ts—mediaRoutes({ authorize }).adapters/next.ts—createUploadRoute,confirmUploadRoute.
Integration
- Configure @core/storage first (bucket CORS must allow
PUTfrom the site). Recommended: a bucket lifecycle rule that deletesuploads/pending/objects after 1 day (abandoned or replayed uploads). OptionalMEDIA_TRANSFORM:cloudflare(Images transformations enabled on the zone),vercel(Next.js image optimizer; add the storage host toimages.remotePatterns) ornone. - Migrations as in
src/lib/db/AGENTS.md. - Mount the upload routes with an
authorize(req)that returns{ userId }only for users withmedia:create(@core/rbac). - Upload from the browser:
POST /api/media/uploadswith{ filename, mime, size }→PUTthe file toupload.urlwithupload.headers→POST /api/media/uploads/<id>/confirm. Store the mediaidin your content. - Render:
const m = await getMedia(id); <img {...imgAttrs(m, { sizes: '(min-width: 768px) 50vw, 100vw' })} />. - Add
mediaAdminResourcetosrc/genpm/admin.ts. - Verify: upload a photo with GPS data; after confirm, the stored file has no EXIF and the row has width/height.
Conventions
- Reference media by
idin content; resolve URLs at render time (driver or CDN can change). - Every non-decorative image needs
alt; the admin can filtermissingAlt=true. - Hero/LCP images:
imgAttrs(m, { priority: true }); everything else stays lazy.
Don't
- Don't accept files without
confirmUpload; pending or rejected media must never be shown. - Don't presign uploads to the final
public/key: a signed PUT can be replayed until it expires and would swap a validated file for an unchecked one (servedimmutable). Upload tostagingKey(); onlyconfirmUploadwritespublic/. - Don't allow SVG uploads or serve user files from the site's own origin without the storage route's sandbox headers.
- Don't hotlink third-party images; copy them into storage first.
# @core/media — rules for AI agents
## Purpose
Media library on top of @core/storage: the browser uploads directly with a signed URL to a private staging key
(`uploads/pending/<id>.<ext>`), then `confirmUpload` checks the real file type by its bytes (declared type must match),
removes EXIF/GPS/XMP from JPEG, PNG and WebP without re-encoding, writes the validated bytes to the final `public/`
key, deletes the staging copy and stores dimensions. Malformed images (chunk lengths past the end) are rejected. Alt text and focal point per file, responsive URLs (Cloudflare Images or Next.js
image optimizer) and ready `<img>` attributes. SVG and HTML are never accepted. Table: `media`.
## Map
- `index.ts` — public API: `createUpload`, `confirmUpload`, `getMedia`, `updateMedia`, `deleteMedia`, `imageUrl`, `imgAttrs`, `mediaAdminResource`.
- `remote.ts` — `importRemoteImage(url, { allowedHosts })` for server-side imports (https, host allowlist, size limit).
- `library.ts` — upload flow, allowed types and `stagingKey(media)`. `sniff.ts`, `strip.ts` — byte checks. `urls.ts` — URLs and `<img>` attributes.
- `adapters/hono.ts` — `mediaRoutes({ authorize })`. `adapters/next.ts` — `createUploadRoute`, `confirmUploadRoute`.
## Integration
1. Configure @core/storage first (bucket CORS must allow `PUT` from the site). Recommended: a bucket lifecycle rule
that deletes `uploads/pending/` objects after 1 day (abandoned or replayed uploads). Optional `MEDIA_TRANSFORM`:
`cloudflare` (Images transformations enabled on the zone), `vercel` (Next.js image optimizer; add the storage host to `images.remotePatterns`) or `none`.
2. Migrations as in `src/lib/db/AGENTS.md`.
3. Mount the upload routes with an `authorize(req)` that returns `{ userId }` only for users with `media:create` (@core/rbac).
4. Upload from the browser: `POST /api/media/uploads` with `{ filename, mime, size }` → `PUT` the file to `upload.url`
with `upload.headers` → `POST /api/media/uploads/<id>/confirm`. Store the media `id` in your content.
5. Render: `const m = await getMedia(id); <img {...imgAttrs(m, { sizes: '(min-width: 768px) 50vw, 100vw' })} />`.
6. Add `mediaAdminResource` to `src/genpm/admin.ts`.
7. Verify: upload a photo with GPS data; after confirm, the stored file has no EXIF and the row has width/height.
## Conventions
- Reference media by `id` in content; resolve URLs at render time (driver or CDN can change).
- Every non-decorative image needs `alt`; the admin can filter `missingAlt=true`.
- Hero/LCP images: `imgAttrs(m, { priority: true })`; everything else stays lazy.
## Don't
- Don't accept files without `confirmUpload`; pending or rejected media must never be shown.
- Don't presign uploads to the final `public/` key: a signed PUT can be replayed until it expires and would swap a
validated file for an unchecked one (served `immutable`). Upload to `stagingKey()`; only `confirmUpload` writes `public/`.
- Don't allow SVG uploads or serve user files from the site's own origin without the storage route's sandbox headers.
- Don't hotlink third-party images; copy them into storage first.
Der genaue Baum, der nach .genpmignore eingebunden wird. Gepinnt an
// URLs de imagen responsivas según el servicio de transformación (MEDIA_TRANSFORM) y atributos <img> listos.
import { getStorage } from '../storage/index.ts';
import type { Media } from './schema.ts';
export type Transform = 'none' | 'cloudflare' | 'vercel';
export type ImageOptions = { width?: number; height?: number; fit?: 'cover' | 'contain' | 'scale-down'; quality?: number };
const transform = (): Transform => {
const t = process.env.MEDIA_TRANSFORM ?? 'none';
if (t !== 'none' && t !== 'cloudflare' && t !== 'vercel') throw new Error(`unknown MEDIA_TRANSFORM: ${t}`);
return t;
};
export const mediaUrl = (m: Pick<Media, 'key'>) => getStorage().publicUrl(m.key);
/** URL de la imagen redimensionada. Sin servicio de transformación (`none`) devuelve la original. */
export function imageUrl(m: Pick<Media, 'key' | 'mime'>, opts: ImageOptions = {}): string {
const src = mediaUrl(m);
const t = transform();
if (t === 'none' || !m.mime.startsWith('image/') || m.mime === 'image/gif') return src;
const q = Math.min(Math.max(Math.round(opts.quality ?? 75), 1), 100);
if (t === 'vercel') return `/_next/image?url=${encodeURIComponent(src)}&w=${opts.width ?? 1200}&q=${q}`;
const params = [
opts.width && `width=${Math.round(opts.width)}`,
opts.height && `height=${Math.round(opts.height)}`,
opts.fit && `fit=${opts.fit}`,
`quality=${q}`,
'format=auto',
].filter(Boolean);
return `/cdn-cgi/image/${params.join(',')}/${src}`;
}
export const DEFAULT_WIDTHS = [320, 640, 960, 1280, 1920];
export type ImgAttrs = {
src: string;
srcSet?: string;
sizes?: string;
width?: number;
height?: number;
alt: string;
loading: 'lazy' | 'eager';
decoding: 'async';
style: { objectPosition: string };
};
/** Atributos para `<img>` (o `<Image unoptimized>`) con srcset, dimensiones, alt y punto focal. */
export function imgAttrs(
m: Pick<Media, 'key' | 'mime' | 'width' | 'height' | 'alt' | 'focalX' | 'focalY'>,
opts: { sizes?: string; widths?: number[]; priority?: boolean; alt?: string } = {},
): ImgAttrs {
const t = transform();
const widths = (opts.widths ?? DEFAULT_WIDTHS).filter((w) => !m.width || w <= m.width);
const responsive = t !== 'none' && m.mime.startsWith('image/') && m.mime !== 'image/gif' && widths.length > 0;
return {
src: imageUrl(m, { width: widths.at(-1) ?? m.width ?? undefined }),
...(responsive && { srcSet: widths.map((w) => `${imageUrl(m, { width: w })} ${w}w`).join(', '), sizes: opts.sizes ?? '100vw' }),
...(m.width && { width: m.width }),
...(m.height && { height: m.height }),
alt: opts.alt ?? m.alt,
loading: opts.priority ? 'eager' : 'lazy',
decoding: 'async',
style: { objectPosition: `${Math.round(m.focalX * 100)}% ${Math.round(m.focalY * 100)}%` },
};
}
Dieses Paket deklariert keine MCP-Server.
| Version | Commit | Veröffentlicht | Prüfung |
|---|---|---|---|
| 1.1.0 | 868490c | vor 3 Stunden | Prüfung bestanden |
- npm
- zod ^4.0.0
- vorgeschlagen
- GenPM schlägt den npm-Befehl vor und führt ihn nur aus, wenn du zustimmst.
- Verwendet von (3)
- @core/blocks ^1.0.0@core/kit-cms ^1.0.0@core/suppliers ^1.0.0
- Prüfung
- Prüfung bestanden · 0 Befunde
- Commit
- v1.1.0 → 868490cea569361668f1903822e20c1becc618d4 · nach dem Abruf verifiziert
- Skripte
- Keine. GenPM führt niemals Paketcode aus.
- Lizenz
- MIT
- Qualität
- 100/100
- Anerkannte Lizenzerfüllt
- AGENTS.md erklärt den Zweckerfüllt
- AGENTS.md enthält Integrationsschritteerfüllt
- AGENTS.md nennt Konventionen oder Verboteerfüllt
- Enthält Testserfüllt
- Sicherheitsscan bestandenerfüllt
- In den letzten 6 Monaten veröffentlichterfüllt
- Verifizierter Herausgebererfüllt
- Zusammenfassung und Schlagwörtererfüllt
- Meldung
- Stimmt etwas nicht?