inlaycms 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +469 -2
- package/assets/admin/admin.css +2 -0
- package/assets/admin/admin.js +1 -0
- package/assets/admin/editor.js +125 -0
- package/dist/adapters/admin/activity.d.ts +17 -0
- package/dist/adapters/admin/activity.js +59 -0
- package/dist/adapters/admin/assets.d.ts +14 -0
- package/dist/adapters/admin/assets.js +64 -0
- package/dist/adapters/admin/attention.d.ts +15 -0
- package/dist/adapters/admin/attention.js +34 -0
- package/dist/adapters/admin/context.d.ts +40 -0
- package/dist/adapters/admin/context.js +142 -0
- package/dist/adapters/admin/form/walk.d.ts +61 -0
- package/dist/adapters/admin/form/walk.js +565 -0
- package/dist/adapters/admin/pages.d.ts +38 -0
- package/dist/adapters/admin/pages.js +66 -0
- package/dist/adapters/admin/publish.d.ts +12 -0
- package/dist/adapters/admin/publish.js +195 -0
- package/dist/adapters/admin/routes.d.ts +47 -0
- package/dist/adapters/admin/routes.js +255 -0
- package/dist/adapters/admin/screens/backups.d.ts +11 -0
- package/dist/adapters/admin/screens/backups.js +223 -0
- package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
- package/dist/adapters/admin/screens/dashboard.js +160 -0
- package/dist/adapters/admin/screens/editor.d.ts +67 -0
- package/dist/adapters/admin/screens/editor.js +416 -0
- package/dist/adapters/admin/screens/history.d.ts +4 -0
- package/dist/adapters/admin/screens/history.js +105 -0
- package/dist/adapters/admin/screens/lists.d.ts +4 -0
- package/dist/adapters/admin/screens/lists.js +297 -0
- package/dist/adapters/admin/screens/media.d.ts +20 -0
- package/dist/adapters/admin/screens/media.js +186 -0
- package/dist/adapters/admin/screens/pickers.d.ts +29 -0
- package/dist/adapters/admin/screens/pickers.js +134 -0
- package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
- package/dist/adapters/admin/screens/preview-pane.js +74 -0
- package/dist/adapters/admin/screens/redirects.d.ts +4 -0
- package/dist/adapters/admin/screens/redirects.js +137 -0
- package/dist/adapters/admin/screens/releases.d.ts +7 -0
- package/dist/adapters/admin/screens/releases.js +287 -0
- package/dist/adapters/admin/screens/search.d.ts +22 -0
- package/dist/adapters/admin/screens/search.js +113 -0
- package/dist/adapters/admin/screens/security.d.ts +4 -0
- package/dist/adapters/admin/screens/security.js +85 -0
- package/dist/adapters/admin/screens/users.d.ts +4 -0
- package/dist/adapters/admin/screens/users.js +330 -0
- package/dist/adapters/admin/theme.d.ts +1 -0
- package/dist/adapters/admin/theme.js +1 -0
- package/dist/adapters/admin/translations.d.ts +16 -0
- package/dist/adapters/admin/translations.js +23 -0
- package/dist/adapters/admin/upload.d.ts +8 -0
- package/dist/adapters/admin/upload.js +116 -0
- package/dist/adapters/admin/views/data-table.d.ts +82 -0
- package/dist/adapters/admin/views/data-table.js +117 -0
- package/dist/adapters/admin/views/icons.d.ts +48 -0
- package/dist/adapters/admin/views/icons.js +47 -0
- package/dist/adapters/admin/views/layout.d.ts +51 -0
- package/dist/adapters/admin/views/layout.js +145 -0
- package/dist/adapters/admin/views/sheet.d.ts +15 -0
- package/dist/adapters/admin/views/sheet.js +6 -0
- package/dist/adapters/admin/views/status.d.ts +32 -0
- package/dist/adapters/admin/views/status.js +74 -0
- package/dist/adapters/auth/handler.d.ts +10 -0
- package/dist/adapters/auth/handler.js +63 -0
- package/dist/adapters/auth/request.d.ts +8 -0
- package/dist/adapters/auth/request.js +14 -0
- package/dist/adapters/cli/bin.d.ts +2 -0
- package/dist/adapters/cli/bin.js +42 -0
- package/dist/adapters/cli/main.d.ts +7 -0
- package/dist/adapters/cli/main.js +982 -0
- package/dist/adapters/cli/remote.d.ts +52 -0
- package/dist/adapters/cli/remote.js +161 -0
- package/dist/adapters/mcp/agent-refs.d.ts +9 -0
- package/dist/adapters/mcp/agent-refs.js +25 -0
- package/dist/adapters/mcp/documents.d.ts +15 -0
- package/dist/adapters/mcp/documents.js +26 -0
- package/dist/adapters/mcp/fetch-url.d.ts +19 -0
- package/dist/adapters/mcp/fetch-url.js +244 -0
- package/dist/adapters/mcp/request-state.d.ts +15 -0
- package/dist/adapters/mcp/request-state.js +38 -0
- package/dist/adapters/mcp/results.d.ts +20 -0
- package/dist/adapters/mcp/results.js +61 -0
- package/dist/adapters/mcp/route.d.ts +34 -0
- package/dist/adapters/mcp/route.js +52 -0
- package/dist/adapters/mcp/server.d.ts +44 -0
- package/dist/adapters/mcp/server.js +49 -0
- package/dist/adapters/mcp/tools/history.d.ts +5 -0
- package/dist/adapters/mcp/tools/history.js +84 -0
- package/dist/adapters/mcp/tools/media.d.ts +5 -0
- package/dist/adapters/mcp/tools/media.js +102 -0
- package/dist/adapters/mcp/tools/publish.d.ts +5 -0
- package/dist/adapters/mcp/tools/publish.js +157 -0
- package/dist/adapters/mcp/tools/read.d.ts +5 -0
- package/dist/adapters/mcp/tools/read.js +279 -0
- package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
- package/dist/adapters/mcp/tools/redirects.js +74 -0
- package/dist/adapters/mcp/tools/write.d.ts +5 -0
- package/dist/adapters/mcp/tools/write.js +304 -0
- package/dist/adapters/site/backup-schedule.d.ts +8 -0
- package/dist/adapters/site/backup-schedule.js +31 -0
- package/dist/adapters/site/cache.d.ts +29 -0
- package/dist/adapters/site/cache.js +63 -0
- package/dist/adapters/site/context.d.ts +29 -0
- package/dist/adapters/site/context.js +278 -0
- package/dist/adapters/site/head.d.ts +23 -0
- package/dist/adapters/site/head.js +44 -0
- package/dist/adapters/site/limiter.d.ts +17 -0
- package/dist/adapters/site/limiter.js +39 -0
- package/dist/adapters/site/lock.d.ts +30 -0
- package/dist/adapters/site/lock.js +143 -0
- package/dist/adapters/site/media-routes.d.ts +4 -0
- package/dist/adapters/site/media-routes.js +55 -0
- package/dist/adapters/site/plugin.d.ts +122 -0
- package/dist/adapters/site/plugin.js +436 -0
- package/dist/adapters/site/preview.d.ts +36 -0
- package/dist/adapters/site/preview.js +82 -0
- package/dist/adapters/site/public.d.ts +6 -0
- package/dist/adapters/site/public.js +87 -0
- package/dist/adapters/site/render.d.ts +45 -0
- package/dist/adapters/site/render.js +191 -0
- package/dist/adapters/site/routes.d.ts +17 -0
- package/dist/adapters/site/routes.js +125 -0
- package/dist/adapters/site/sitemap.d.ts +10 -0
- package/dist/adapters/site/sitemap.js +10 -0
- package/dist/adapters/web.d.ts +22 -0
- package/dist/adapters/web.js +75 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +58 -0
- package/dist/core/agent/format.d.ts +62 -0
- package/dist/core/agent/format.js +211 -0
- package/dist/core/agent/schema.d.ts +6 -0
- package/dist/core/agent/schema.js +86 -0
- package/dist/core/archive.d.ts +12 -0
- package/dist/core/archive.js +72 -0
- package/dist/core/auth/auth.d.ts +4911 -0
- package/dist/core/auth/auth.js +61 -0
- package/dist/core/auth/roles.d.ts +10 -0
- package/dist/core/auth/roles.js +38 -0
- package/dist/core/auth/service.d.ts +118 -0
- package/dist/core/auth/service.js +424 -0
- package/dist/core/auth/upload-token.d.ts +8 -0
- package/dist/core/auth/upload-token.js +27 -0
- package/dist/core/backup/backup.d.ts +28 -0
- package/dist/core/backup/backup.js +126 -0
- package/dist/core/backup/restore.d.ts +41 -0
- package/dist/core/backup/restore.js +264 -0
- package/dist/core/backup/schedule.d.ts +12 -0
- package/dist/core/backup/schedule.js +29 -0
- package/dist/core/boot.d.ts +63 -0
- package/dist/core/boot.js +141 -0
- package/dist/core/check.d.ts +32 -0
- package/dist/core/check.js +111 -0
- package/dist/core/content/activity.d.ts +8 -0
- package/dist/core/content/activity.js +101 -0
- package/dist/core/content/delete.d.ts +5 -0
- package/dist/core/content/delete.js +100 -0
- package/dist/core/content/diff.d.ts +6 -0
- package/dist/core/content/diff.js +129 -0
- package/dist/core/content/dry-run.d.ts +24 -0
- package/dist/core/content/dry-run.js +69 -0
- package/dist/core/content/gaps.d.ts +25 -0
- package/dist/core/content/gaps.js +50 -0
- package/dist/core/content/hidden.d.ts +33 -0
- package/dist/core/content/hidden.js +103 -0
- package/dist/core/content/history.d.ts +15 -0
- package/dist/core/content/history.js +62 -0
- package/dist/core/content/patch.d.ts +40 -0
- package/dist/core/content/patch.js +287 -0
- package/dist/core/content/path.d.ts +6 -0
- package/dist/core/content/path.js +38 -0
- package/dist/core/content/publish.d.ts +26 -0
- package/dist/core/content/publish.js +211 -0
- package/dist/core/content/read.d.ts +99 -0
- package/dist/core/content/read.js +141 -0
- package/dist/core/content/reader.d.ts +37 -0
- package/dist/core/content/reader.js +195 -0
- package/dist/core/content/refs.d.ts +44 -0
- package/dist/core/content/refs.js +161 -0
- package/dist/core/content/release-migrations.d.ts +12 -0
- package/dist/core/content/release-migrations.js +154 -0
- package/dist/core/content/save.d.ts +25 -0
- package/dist/core/content/save.js +87 -0
- package/dist/core/content/schema.d.ts +22 -0
- package/dist/core/content/schema.js +127 -0
- package/dist/core/content/search.d.ts +8 -0
- package/dist/core/content/search.js +89 -0
- package/dist/core/content/shipped-input.d.ts +39 -0
- package/dist/core/content/shipped-input.js +221 -0
- package/dist/core/content/shipped.d.ts +112 -0
- package/dist/core/content/shipped.js +437 -0
- package/dist/core/content/slug.d.ts +23 -0
- package/dist/core/content/slug.js +182 -0
- package/dist/core/content/stats.d.ts +11 -0
- package/dist/core/content/stats.js +30 -0
- package/dist/core/content/translate.d.ts +3 -0
- package/dist/core/content/translate.js +68 -0
- package/dist/core/content/usages.d.ts +13 -0
- package/dist/core/content/usages.js +70 -0
- package/dist/core/content/validate.d.ts +21 -0
- package/dist/core/content/validate.js +124 -0
- package/dist/core/content/write.d.ts +10 -0
- package/dist/core/content/write.js +93 -0
- package/dist/core/core.d.ts +227 -0
- package/dist/core/core.js +251 -0
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +12 -0
- package/dist/core/db/migrate.d.ts +9 -0
- package/dist/core/db/migrate.js +42 -0
- package/dist/core/db/migrations/0001_content.d.ts +2 -0
- package/dist/core/db/migrations/0001_content.js +69 -0
- package/dist/core/db/migrations/0002_media.d.ts +2 -0
- package/dist/core/db/migrations/0002_media.js +26 -0
- package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
- package/dist/core/db/migrations/0003_redirects.js +19 -0
- package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
- package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
- package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
- package/dist/core/db/migrations/0005_site_lock.js +10 -0
- package/dist/core/db/migrations/0006_releases.d.ts +3 -0
- package/dist/core/db/migrations/0006_releases.js +34 -0
- package/dist/core/db/schema.d.ts +155 -0
- package/dist/core/db/schema.js +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.js +15 -0
- package/dist/core/export/export.d.ts +17 -0
- package/dist/core/export/export.js +185 -0
- package/dist/core/export/format.d.ts +241 -0
- package/dist/core/export/format.js +106 -0
- package/dist/core/export/import.d.ts +54 -0
- package/dist/core/export/import.js +180 -0
- package/dist/core/export/read.d.ts +16 -0
- package/dist/core/export/read.js +234 -0
- package/dist/core/export/replace.d.ts +17 -0
- package/dist/core/export/replace.js +36 -0
- package/dist/core/export/report.d.ts +55 -0
- package/dist/core/export/report.js +50 -0
- package/dist/core/export/schema.d.ts +8 -0
- package/dist/core/export/schema.js +47 -0
- package/dist/core/export/stage.d.ts +25 -0
- package/dist/core/export/stage.js +192 -0
- package/dist/core/fields/builtins.d.ts +70 -0
- package/dist/core/fields/builtins.js +335 -0
- package/dist/core/fields/define.d.ts +86 -0
- package/dist/core/fields/define.js +34 -0
- package/dist/core/fields/walk.d.ts +49 -0
- package/dist/core/fields/walk.js +270 -0
- package/dist/core/fields/widgets.d.ts +27 -0
- package/dist/core/fields/widgets.js +330 -0
- package/dist/core/fields/zod-internals.d.ts +29 -0
- package/dist/core/fields/zod-internals.js +28 -0
- package/dist/core/media/detect.d.ts +5 -0
- package/dist/core/media/detect.js +66 -0
- package/dist/core/media/labels.d.ts +6 -0
- package/dist/core/media/labels.js +17 -0
- package/dist/core/media/library.d.ts +45 -0
- package/dist/core/media/library.js +239 -0
- package/dist/core/media/store.d.ts +11 -0
- package/dist/core/media/store.js +49 -0
- package/dist/core/media/variants.d.ts +10 -0
- package/dist/core/media/variants.js +59 -0
- package/dist/core/messages/en.d.ts +1278 -0
- package/dist/core/messages/en.js +1287 -0
- package/dist/core/messages/index.d.ts +7 -0
- package/dist/core/messages/index.js +14 -0
- package/dist/core/preview.d.ts +16 -0
- package/dist/core/preview.js +42 -0
- package/dist/core/redirects/collapse.d.ts +68 -0
- package/dist/core/redirects/collapse.js +153 -0
- package/dist/core/redirects/csv.d.ts +15 -0
- package/dist/core/redirects/csv.js +68 -0
- package/dist/core/redirects/redirects.d.ts +58 -0
- package/dist/core/redirects/redirects.js +545 -0
- package/dist/core/registry/content.d.ts +26 -0
- package/dist/core/registry/content.js +9 -0
- package/dist/core/registry/define.d.ts +145 -0
- package/dist/core/registry/define.js +41 -0
- package/dist/core/registry/migration.d.ts +44 -0
- package/dist/core/registry/migration.js +4 -0
- package/dist/core/registry/registry.d.ts +18 -0
- package/dist/core/registry/registry.js +255 -0
- package/dist/core/registry/render.d.ts +98 -0
- package/dist/core/registry/render.js +1 -0
- package/dist/core/registry/route.d.ts +42 -0
- package/dist/core/registry/route.js +3 -0
- package/dist/core/release/bundle.d.ts +21 -0
- package/dist/core/release/bundle.js +252 -0
- package/dist/core/release/keys.d.ts +11 -0
- package/dist/core/release/keys.js +60 -0
- package/dist/core/release/manifest.d.ts +103 -0
- package/dist/core/release/manifest.js +138 -0
- package/dist/core/release/pack.d.ts +20 -0
- package/dist/core/release/pack.js +97 -0
- package/dist/core/release/pointer.d.ts +19 -0
- package/dist/core/release/pointer.js +53 -0
- package/dist/core/release/read.d.ts +59 -0
- package/dist/core/release/read.js +256 -0
- package/dist/core/report.d.ts +31 -0
- package/dist/core/report.js +37 -0
- package/dist/core/richtext/html.d.ts +15 -0
- package/dist/core/richtext/html.js +81 -0
- package/dist/core/richtext/markdown.d.ts +19 -0
- package/dist/core/richtext/markdown.js +951 -0
- package/dist/core/richtext/schema.d.ts +98 -0
- package/dist/core/richtext/schema.js +275 -0
- package/dist/core/site/live.d.ts +56 -0
- package/dist/core/site/live.js +145 -0
- package/dist/core/site/paths.d.ts +15 -0
- package/dist/core/site/paths.js +64 -0
- package/dist/core/site/resolve.d.ts +9 -0
- package/dist/core/site/resolve.js +202 -0
- package/dist/core/site/sitemap.d.ts +11 -0
- package/dist/core/site/sitemap.js +24 -0
- package/dist/core/site-lock/site-lock.d.ts +21 -0
- package/dist/core/site-lock/site-lock.js +65 -0
- package/dist/core/site-lock/token.d.ts +7 -0
- package/dist/core/site-lock/token.js +24 -0
- package/dist/core/theme.d.ts +22 -0
- package/dist/core/theme.js +133 -0
- package/dist/core/types.d.ts +253 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.d.ts +1 -0
- package/dist/core/version.js +5 -0
- package/dist/host/activate.d.ts +53 -0
- package/dist/host/activate.js +162 -0
- package/dist/host/app.d.ts +46 -0
- package/dist/host/app.js +129 -0
- package/dist/host/bin.d.ts +5 -0
- package/dist/host/bin.js +8 -0
- package/dist/host/config.d.ts +36 -0
- package/dist/host/config.js +152 -0
- package/dist/host/dev.d.ts +10 -0
- package/dist/host/dev.js +293 -0
- package/dist/host/diff.d.ts +78 -0
- package/dist/host/diff.js +346 -0
- package/dist/host/hooks.d.ts +41 -0
- package/dist/host/hooks.js +49 -0
- package/dist/host/ipc.d.ts +123 -0
- package/dist/host/ipc.js +61 -0
- package/dist/host/preview-proxy.d.ts +24 -0
- package/dist/host/preview-proxy.js +154 -0
- package/dist/host/preview-worker.d.ts +42 -0
- package/dist/host/preview-worker.js +242 -0
- package/dist/host/primary.d.ts +38 -0
- package/dist/host/primary.js +357 -0
- package/dist/host/push.d.ts +125 -0
- package/dist/host/push.js +420 -0
- package/dist/host/serve.d.ts +16 -0
- package/dist/host/serve.js +150 -0
- package/dist/host/store.d.ts +48 -0
- package/dist/host/store.js +109 -0
- package/dist/host/worker.d.ts +67 -0
- package/dist/host/worker.js +310 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +32 -0
- package/dist/jsx/html.d.ts +16 -0
- package/dist/jsx/html.js +65 -0
- package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
- package/dist/jsx/jsx-dev-runtime.js +5 -0
- package/dist/jsx/jsx-runtime.d.ts +17 -0
- package/dist/jsx/jsx-runtime.js +41 -0
- package/dist/site.d.ts +17 -0
- package/dist/site.js +14 -0
- package/package.json +111 -3
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Schema types that wrap one inner schema. A pipe wraps its `in` side. */
|
|
2
|
+
export const WRAPPERS = new Set([
|
|
3
|
+
'optional',
|
|
4
|
+
'default',
|
|
5
|
+
'nullable',
|
|
6
|
+
'prefault',
|
|
7
|
+
'readonly',
|
|
8
|
+
'catch',
|
|
9
|
+
'pipe',
|
|
10
|
+
]);
|
|
11
|
+
export const defOf = (s) => s._zod.def;
|
|
12
|
+
/** The schema `s` wraps, or `undefined` if it isn't a wrapper. */
|
|
13
|
+
export function innerOf(s) {
|
|
14
|
+
const def = defOf(s);
|
|
15
|
+
if (!WRAPPERS.has(def.type))
|
|
16
|
+
return undefined;
|
|
17
|
+
return def.type === 'pipe' ? def.in : def.innerType;
|
|
18
|
+
}
|
|
19
|
+
/** `s` with every wrapper removed. */
|
|
20
|
+
export function unwrap(s) {
|
|
21
|
+
let current = s;
|
|
22
|
+
for (let inner = innerOf(current); inner; inner = innerOf(current))
|
|
23
|
+
current = inner;
|
|
24
|
+
return current;
|
|
25
|
+
}
|
|
26
|
+
export function isRecord(v) {
|
|
27
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
28
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const DEFAULT_UPLOAD_TYPES: readonly string[];
|
|
2
|
+
export declare const DEFAULT_MAX_BYTES: number;
|
|
3
|
+
export declare function detectType(bytes: Uint8Array, filename: string): string;
|
|
4
|
+
export declare function isRasterImage(mime: string): boolean;
|
|
5
|
+
export declare function typeLabel(mime: string): string;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export const DEFAULT_UPLOAD_TYPES = [
|
|
2
|
+
'image/jpeg',
|
|
3
|
+
'image/png',
|
|
4
|
+
'image/gif',
|
|
5
|
+
'image/webp',
|
|
6
|
+
'image/avif',
|
|
7
|
+
'application/pdf',
|
|
8
|
+
];
|
|
9
|
+
export const DEFAULT_MAX_BYTES = 20 * 1024 * 1024;
|
|
10
|
+
const RASTER = new Set(['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif']);
|
|
11
|
+
const BY_EXTENSION = {
|
|
12
|
+
txt: 'text/plain',
|
|
13
|
+
csv: 'text/csv',
|
|
14
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
15
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
16
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
17
|
+
zip: 'application/zip',
|
|
18
|
+
mp3: 'audio/mpeg',
|
|
19
|
+
mp4: 'video/mp4',
|
|
20
|
+
};
|
|
21
|
+
const LABELS = {
|
|
22
|
+
'image/jpeg': 'JPEG',
|
|
23
|
+
'image/png': 'PNG',
|
|
24
|
+
'image/gif': 'GIF',
|
|
25
|
+
'image/webp': 'WebP',
|
|
26
|
+
'image/avif': 'AVIF',
|
|
27
|
+
'application/pdf': 'PDF',
|
|
28
|
+
};
|
|
29
|
+
function startsWith(bytes, sig, offset = 0) {
|
|
30
|
+
return sig.every((b, i) => bytes[offset + i] === b);
|
|
31
|
+
}
|
|
32
|
+
function ascii(bytes, start, end) {
|
|
33
|
+
return String.fromCharCode(...bytes.subarray(start, end));
|
|
34
|
+
}
|
|
35
|
+
export function detectType(bytes, filename) {
|
|
36
|
+
if (startsWith(bytes, [0xff, 0xd8, 0xff]))
|
|
37
|
+
return 'image/jpeg';
|
|
38
|
+
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
|
|
39
|
+
return 'image/png';
|
|
40
|
+
const head6 = ascii(bytes, 0, 6);
|
|
41
|
+
if (head6 === 'GIF87a' || head6 === 'GIF89a')
|
|
42
|
+
return 'image/gif';
|
|
43
|
+
if (ascii(bytes, 0, 4) === 'RIFF' && ascii(bytes, 8, 12) === 'WEBP')
|
|
44
|
+
return 'image/webp';
|
|
45
|
+
if (ascii(bytes, 4, 8) === 'ftyp') {
|
|
46
|
+
const brand = ascii(bytes, 8, 12);
|
|
47
|
+
if (brand === 'avif' || brand === 'avis')
|
|
48
|
+
return 'image/avif';
|
|
49
|
+
}
|
|
50
|
+
if (ascii(bytes, 0, 5) === '%PDF-')
|
|
51
|
+
return 'application/pdf';
|
|
52
|
+
const text = new TextDecoder('utf-8').decode(bytes.subarray(0, 2048)).replace(/^/, '').trimStart().toLowerCase();
|
|
53
|
+
if (text.startsWith('<svg') || (text.startsWith('<?xml') && text.includes('<svg')))
|
|
54
|
+
return 'image/svg+xml';
|
|
55
|
+
if (text.startsWith('<!doctype html') || text.startsWith('<html'))
|
|
56
|
+
return 'text/html';
|
|
57
|
+
const dot = filename.lastIndexOf('.');
|
|
58
|
+
const ext = dot >= 0 ? filename.slice(dot + 1).toLowerCase() : '';
|
|
59
|
+
return BY_EXTENSION[ext] ?? 'application/octet-stream';
|
|
60
|
+
}
|
|
61
|
+
export function isRasterImage(mime) {
|
|
62
|
+
return RASTER.has(mime);
|
|
63
|
+
}
|
|
64
|
+
export function typeLabel(mime) {
|
|
65
|
+
return LABELS[mime] ?? mime;
|
|
66
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { t } from '../messages/index.js';
|
|
2
|
+
const SIZE_UNITS = [
|
|
3
|
+
{ bytes: 1024 * 1024, id: 'size.mb' },
|
|
4
|
+
{ bytes: 1024, id: 'size.kb' },
|
|
5
|
+
];
|
|
6
|
+
/** A byte count in plain words: MB from 1 MB up, KB from 1 KB, bytes below that. */
|
|
7
|
+
export function formatSize(bytes) {
|
|
8
|
+
for (const unit of SIZE_UNITS) {
|
|
9
|
+
if (bytes >= unit.bytes)
|
|
10
|
+
return t(unit.id, { n: Math.round((bytes / unit.bytes) * 10) / 10 });
|
|
11
|
+
}
|
|
12
|
+
return t('size.bytes', { n: bytes });
|
|
13
|
+
}
|
|
14
|
+
const missingDate = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' });
|
|
15
|
+
export function missingMediaLabel(entry) {
|
|
16
|
+
return t('media.missing', { filename: entry.filename, date: missingDate.format(new Date(entry.deletedAt)) });
|
|
17
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { CoreDeps, Ctx, MediaInfo, MediaStatus } from '../types.js';
|
|
2
|
+
import type { MediaStore } from './store.js';
|
|
3
|
+
export { missingMediaLabel } from './labels.js';
|
|
4
|
+
export declare function sanitizeFilename(name: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Runs `fn` once earlier calls for the same file in the same media folder have
|
|
7
|
+
* finished, so adding a file can't interleave with deleting it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function withMediaLock<T>(store: MediaStore, id: string, fn: () => Promise<T>): Promise<T>;
|
|
10
|
+
export declare function inspectUpload(deps: CoreDeps, bytes: Uint8Array, filename: string): Promise<{
|
|
11
|
+
id: string;
|
|
12
|
+
filename: string;
|
|
13
|
+
mime: string;
|
|
14
|
+
size: number;
|
|
15
|
+
width: number | null;
|
|
16
|
+
height: number | null;
|
|
17
|
+
}>;
|
|
18
|
+
export declare function addMedia(deps: CoreDeps, input: {
|
|
19
|
+
data: Uint8Array;
|
|
20
|
+
filename: string;
|
|
21
|
+
}, ctx: Ctx): Promise<{
|
|
22
|
+
media: MediaInfo;
|
|
23
|
+
reused: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
export declare function getMedia(deps: CoreDeps, id: string): Promise<MediaInfo | null>;
|
|
26
|
+
export declare function listMedia(deps: CoreDeps, opts?: {
|
|
27
|
+
kind?: 'image' | 'file';
|
|
28
|
+
search?: string;
|
|
29
|
+
limit?: number;
|
|
30
|
+
offset?: number;
|
|
31
|
+
}): Promise<MediaInfo[]>;
|
|
32
|
+
export declare function setMediaAlt(deps: CoreDeps, id: string, alt: string | null, ctx: Ctx): Promise<MediaInfo>;
|
|
33
|
+
export declare function lookupMedia(deps: CoreDeps, ids: string[]): Promise<Map<string, MediaStatus>>;
|
|
34
|
+
export declare function mediaFile(deps: CoreDeps, id: string): Promise<{
|
|
35
|
+
path: string;
|
|
36
|
+
mime: string;
|
|
37
|
+
filename: string;
|
|
38
|
+
size: number;
|
|
39
|
+
}>;
|
|
40
|
+
export declare function deleteMedia(deps: CoreDeps, id: string, ctx: Ctx): Promise<void>;
|
|
41
|
+
export declare function mediaStats(deps: CoreDeps): Promise<{
|
|
42
|
+
count: number;
|
|
43
|
+
totalBytes: number;
|
|
44
|
+
imagesWithoutAlt: number;
|
|
45
|
+
}>;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
import { sql } from 'kysely';
|
|
4
|
+
import sharp from 'sharp';
|
|
5
|
+
import { assertCan } from '../auth/roles.js';
|
|
6
|
+
import { InlayError } from '../errors.js';
|
|
7
|
+
import { findUsages } from '../content/usages.js';
|
|
8
|
+
import { detectType, isRasterImage, typeLabel } from './detect.js';
|
|
9
|
+
import { formatSize } from './labels.js';
|
|
10
|
+
import { withTransaction } from '../content/dry-run.js';
|
|
11
|
+
export { missingMediaLabel } from './labels.js';
|
|
12
|
+
export function sanitizeFilename(name) {
|
|
13
|
+
const base = basename(name.replace(/\\/g, '/'));
|
|
14
|
+
// eslint-disable-next-line no-control-regex
|
|
15
|
+
const clean = base.replace(/[\u0000-\u001f\u007f]/g, '').trim().slice(0, 255).trim();
|
|
16
|
+
return clean || 'file';
|
|
17
|
+
}
|
|
18
|
+
function toInfo(row) {
|
|
19
|
+
return {
|
|
20
|
+
id: row.id,
|
|
21
|
+
filename: row.filename,
|
|
22
|
+
mime: row.mime,
|
|
23
|
+
size: row.size,
|
|
24
|
+
width: row.width,
|
|
25
|
+
height: row.height,
|
|
26
|
+
defaultAlt: row.default_alt,
|
|
27
|
+
createdBy: row.created_by_name,
|
|
28
|
+
createdAt: row.created_at,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const mediaLocks = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* Runs `fn` once earlier calls for the same file in the same media folder have
|
|
34
|
+
* finished, so adding a file can't interleave with deleting it.
|
|
35
|
+
*/
|
|
36
|
+
export async function withMediaLock(store, id, fn) {
|
|
37
|
+
const key = `${store.dir}\0${id}`;
|
|
38
|
+
const run = (mediaLocks.get(key) ?? Promise.resolve()).then(fn);
|
|
39
|
+
const done = run.then(() => undefined, () => undefined);
|
|
40
|
+
mediaLocks.set(key, done);
|
|
41
|
+
try {
|
|
42
|
+
return await run;
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
if (mediaLocks.get(key) === done)
|
|
46
|
+
mediaLocks.delete(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function requireStore(deps) {
|
|
50
|
+
if (!deps.media)
|
|
51
|
+
throw new InlayError('invalid_operation', 'error.mediaNotConfigured');
|
|
52
|
+
return deps.media;
|
|
53
|
+
}
|
|
54
|
+
export async function inspectUpload(deps, bytes, filename) {
|
|
55
|
+
const name = sanitizeFilename(filename);
|
|
56
|
+
if (bytes.length === 0)
|
|
57
|
+
throw new InlayError('validation', 'error.mediaEmpty', { filename: name });
|
|
58
|
+
if (bytes.length > deps.uploads.maxBytes) {
|
|
59
|
+
throw new InlayError('validation', 'error.mediaTooLarge', { filename: name, max: formatSize(deps.uploads.maxBytes) });
|
|
60
|
+
}
|
|
61
|
+
const mime = detectType(bytes, name);
|
|
62
|
+
if (!deps.uploads.types.includes(mime)) {
|
|
63
|
+
throw new InlayError('validation', 'error.mediaTypeNotAllowed', {
|
|
64
|
+
filename: name,
|
|
65
|
+
types: deps.uploads.types.map(typeLabel).join(', '),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
let width = null;
|
|
69
|
+
let height = null;
|
|
70
|
+
if (isRasterImage(mime)) {
|
|
71
|
+
try {
|
|
72
|
+
const meta = await sharp(bytes).metadata();
|
|
73
|
+
if (!meta.width || !meta.height)
|
|
74
|
+
throw new Error('no size');
|
|
75
|
+
// Force a full decode so truncated files are caught.
|
|
76
|
+
await sharp(bytes).rotate().toBuffer();
|
|
77
|
+
const swap = (meta.orientation ?? 1) >= 5;
|
|
78
|
+
width = swap ? meta.height : meta.width;
|
|
79
|
+
height = swap ? meta.width : meta.height;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
throw new InlayError('validation', 'error.mediaUnreadable', { filename: name });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
id: createHash('sha256').update(bytes).digest('hex'),
|
|
87
|
+
filename: name,
|
|
88
|
+
mime,
|
|
89
|
+
size: bytes.length,
|
|
90
|
+
width,
|
|
91
|
+
height,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export async function addMedia(deps, input, ctx) {
|
|
95
|
+
assertCan(ctx, 'media.write');
|
|
96
|
+
const store = requireStore(deps);
|
|
97
|
+
const info = await inspectUpload(deps, input.data, input.filename);
|
|
98
|
+
return withMediaLock(store, info.id, () => storeMedia(deps, store, info, input.data, ctx));
|
|
99
|
+
}
|
|
100
|
+
async function storeMedia(deps, store, info, data, ctx) {
|
|
101
|
+
const existing = await deps.db.selectFrom('media').selectAll().where('id', '=', info.id).executeTakeFirst();
|
|
102
|
+
if (existing) {
|
|
103
|
+
if (!(await store.has(info.id)))
|
|
104
|
+
await store.put(info.id, data);
|
|
105
|
+
return { media: toInfo(existing), reused: true };
|
|
106
|
+
}
|
|
107
|
+
await store.put(info.id, data);
|
|
108
|
+
await deps.db
|
|
109
|
+
.insertInto('media')
|
|
110
|
+
.values({
|
|
111
|
+
id: info.id,
|
|
112
|
+
filename: info.filename,
|
|
113
|
+
mime: info.mime,
|
|
114
|
+
size: info.size,
|
|
115
|
+
width: info.width,
|
|
116
|
+
height: info.height,
|
|
117
|
+
default_alt: null,
|
|
118
|
+
created_by_id: ctx.actor.id,
|
|
119
|
+
created_by_name: ctx.actor.name,
|
|
120
|
+
created_at: deps.now().toISOString(),
|
|
121
|
+
})
|
|
122
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
123
|
+
.execute();
|
|
124
|
+
const row = await deps.db.selectFrom('media').selectAll().where('id', '=', info.id).executeTakeFirstOrThrow();
|
|
125
|
+
return { media: toInfo(row), reused: false };
|
|
126
|
+
}
|
|
127
|
+
export async function getMedia(deps, id) {
|
|
128
|
+
const row = await deps.db.selectFrom('media').selectAll().where('id', '=', id).executeTakeFirst();
|
|
129
|
+
return row ? toInfo(row) : null;
|
|
130
|
+
}
|
|
131
|
+
export async function listMedia(deps, opts = {}) {
|
|
132
|
+
let q = deps.db.selectFrom('media').selectAll();
|
|
133
|
+
if (opts.kind === 'image')
|
|
134
|
+
q = q.where('mime', 'like', 'image/%');
|
|
135
|
+
if (opts.kind === 'file')
|
|
136
|
+
q = q.where('mime', 'not like', 'image/%');
|
|
137
|
+
if (opts.search) {
|
|
138
|
+
const escaped = opts.search.toLowerCase().replace(/[\\%_]/g, (c) => `\\${c}`);
|
|
139
|
+
q = q.where(sql `lower(filename) like ${`%${escaped}%`} escape '\\'`);
|
|
140
|
+
}
|
|
141
|
+
const rows = await q
|
|
142
|
+
.orderBy('created_at', 'desc')
|
|
143
|
+
.orderBy('id')
|
|
144
|
+
.limit(opts.limit ?? 50)
|
|
145
|
+
.offset(opts.offset ?? 0)
|
|
146
|
+
.execute();
|
|
147
|
+
return rows.map(toInfo);
|
|
148
|
+
}
|
|
149
|
+
export async function setMediaAlt(deps, id, alt, ctx) {
|
|
150
|
+
assertCan(ctx, 'media.write');
|
|
151
|
+
const value = alt === null ? null : alt.trim() || null;
|
|
152
|
+
const res = await deps.db.updateTable('media').set({ default_alt: value }).where('id', '=', id).executeTakeFirst();
|
|
153
|
+
if (Number(res.numUpdatedRows) === 0)
|
|
154
|
+
throw new InlayError('not_found', 'error.mediaNotFound');
|
|
155
|
+
const media = await getMedia(deps, id);
|
|
156
|
+
if (!media)
|
|
157
|
+
throw new InlayError('not_found', 'error.mediaNotFound');
|
|
158
|
+
return media;
|
|
159
|
+
}
|
|
160
|
+
export async function lookupMedia(deps, ids) {
|
|
161
|
+
const out = new Map();
|
|
162
|
+
const unique = [...new Set(ids)];
|
|
163
|
+
if (unique.length === 0)
|
|
164
|
+
return out;
|
|
165
|
+
const present = await deps.db.selectFrom('media').selectAll().where('id', 'in', unique).execute();
|
|
166
|
+
for (const row of present)
|
|
167
|
+
out.set(row.id, { status: 'present', media: toInfo(row) });
|
|
168
|
+
const missing = unique.filter((id) => !out.has(id));
|
|
169
|
+
if (missing.length > 0) {
|
|
170
|
+
const dels = await deps.db
|
|
171
|
+
.selectFrom('media_deletions')
|
|
172
|
+
.selectAll()
|
|
173
|
+
.where('media_id', 'in', missing)
|
|
174
|
+
.orderBy('id', 'desc')
|
|
175
|
+
.execute();
|
|
176
|
+
for (const d of dels) {
|
|
177
|
+
if (!out.has(d.media_id))
|
|
178
|
+
out.set(d.media_id, { status: 'deleted', filename: d.filename, deletedAt: d.deleted_at });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const id of unique)
|
|
182
|
+
if (!out.has(id))
|
|
183
|
+
out.set(id, { status: 'unknown' });
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
export async function mediaFile(deps, id) {
|
|
187
|
+
const store = requireStore(deps);
|
|
188
|
+
const media = await getMedia(deps, id);
|
|
189
|
+
if (!media || !(await store.has(id)))
|
|
190
|
+
throw new InlayError('not_found', 'error.mediaNotFound');
|
|
191
|
+
return { path: store.path(id), mime: media.mime, filename: media.filename, size: media.size };
|
|
192
|
+
}
|
|
193
|
+
export async function deleteMedia(deps, id, ctx) {
|
|
194
|
+
assertCan(ctx, 'media.write');
|
|
195
|
+
const store = deps.media;
|
|
196
|
+
if (store)
|
|
197
|
+
await withMediaLock(store, id, () => removeMedia(deps, store, id, ctx));
|
|
198
|
+
else
|
|
199
|
+
await removeMedia(deps, null, id, ctx);
|
|
200
|
+
}
|
|
201
|
+
async function removeMedia(deps, store, id, ctx) {
|
|
202
|
+
await withTransaction(deps, async ({ db: trx }) => {
|
|
203
|
+
const row = await trx.selectFrom('media').selectAll().where('id', '=', id).executeTakeFirst();
|
|
204
|
+
if (!row)
|
|
205
|
+
throw new InlayError('not_found', 'error.mediaNotFound');
|
|
206
|
+
const usages = await findUsages({ ...deps, db: trx }, { kind: 'media', id });
|
|
207
|
+
if (usages.length > 0) {
|
|
208
|
+
throw new InlayError('in_use', 'error.mediaInUse', { filename: row.filename, count: usages.length }, { usages });
|
|
209
|
+
}
|
|
210
|
+
await trx.deleteFrom('media').where('id', '=', id).execute();
|
|
211
|
+
await trx
|
|
212
|
+
.insertInto('media_deletions')
|
|
213
|
+
.values({
|
|
214
|
+
media_id: id,
|
|
215
|
+
filename: row.filename,
|
|
216
|
+
deleted_at: deps.now().toISOString(),
|
|
217
|
+
deleted_by_id: ctx.actor.id,
|
|
218
|
+
deleted_by_name: ctx.actor.name,
|
|
219
|
+
})
|
|
220
|
+
.execute();
|
|
221
|
+
});
|
|
222
|
+
if (store)
|
|
223
|
+
await store.remove(id);
|
|
224
|
+
}
|
|
225
|
+
export async function mediaStats(deps) {
|
|
226
|
+
const row = await deps.db
|
|
227
|
+
.selectFrom('media')
|
|
228
|
+
.select([
|
|
229
|
+
sql `count(*)`.as('count'),
|
|
230
|
+
sql `coalesce(sum(size), 0)`.as('total_bytes'),
|
|
231
|
+
sql `coalesce(sum(case when mime like 'image/%' and default_alt is null then 1 else 0 end), 0)`.as('without_alt'),
|
|
232
|
+
])
|
|
233
|
+
.executeTakeFirst();
|
|
234
|
+
return {
|
|
235
|
+
count: Number(row?.count ?? 0),
|
|
236
|
+
totalBytes: Number(row?.total_bytes ?? 0),
|
|
237
|
+
imagesWithoutAlt: Number(row?.without_alt ?? 0),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface MediaStore {
|
|
2
|
+
dir: string;
|
|
3
|
+
path(id: string): string;
|
|
4
|
+
variantPath(id: string, width: number, format: string): string;
|
|
5
|
+
has(id: string): Promise<boolean>;
|
|
6
|
+
hasVariant(id: string, width: number, format: string): Promise<boolean>;
|
|
7
|
+
put(id: string, bytes: Uint8Array): Promise<void>;
|
|
8
|
+
putVariant(id: string, width: number, format: string, bytes: Uint8Array): Promise<void>;
|
|
9
|
+
remove(id: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare function fileMediaStore(dir: string): MediaStore;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { access, mkdir, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
async function exists(path) {
|
|
5
|
+
try {
|
|
6
|
+
await access(path);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function writeAtomic(dir, target, bytes) {
|
|
14
|
+
await mkdir(dirname(target), { recursive: true });
|
|
15
|
+
const tmp = join(dir, `.tmp-${randomUUID()}`);
|
|
16
|
+
try {
|
|
17
|
+
await writeFile(tmp, bytes);
|
|
18
|
+
await rename(tmp, target);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
await rm(tmp, { force: true });
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function fileMediaStore(dir) {
|
|
26
|
+
const path = (id) => join(dir, id);
|
|
27
|
+
const variantPath = (id, width, format) => join(dir, 'cache', id, `${width}.${format}`);
|
|
28
|
+
return {
|
|
29
|
+
dir,
|
|
30
|
+
path,
|
|
31
|
+
variantPath,
|
|
32
|
+
has: (id) => exists(path(id)),
|
|
33
|
+
hasVariant: (id, width, format) => exists(variantPath(id, width, format)),
|
|
34
|
+
async put(id, bytes) {
|
|
35
|
+
if (await exists(path(id)))
|
|
36
|
+
return;
|
|
37
|
+
await mkdir(dir, { recursive: true });
|
|
38
|
+
await writeAtomic(dir, path(id), bytes);
|
|
39
|
+
},
|
|
40
|
+
async putVariant(id, width, format, bytes) {
|
|
41
|
+
await mkdir(dir, { recursive: true });
|
|
42
|
+
await writeAtomic(dir, variantPath(id, width, format), bytes);
|
|
43
|
+
},
|
|
44
|
+
async remove(id) {
|
|
45
|
+
await rm(path(id), { force: true });
|
|
46
|
+
await rm(join(dir, 'cache', id), { recursive: true, force: true });
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CoreDeps } from '../types.js';
|
|
2
|
+
export declare const IMAGE_WIDTHS: readonly [320, 640, 960, 1280, 1920, 2560];
|
|
3
|
+
export type VariantFormat = 'webp' | 'avif' | 'jpeg' | 'png';
|
|
4
|
+
export declare function mediaVariant(deps: CoreDeps, id: string, opts: {
|
|
5
|
+
width: number;
|
|
6
|
+
format: VariantFormat;
|
|
7
|
+
}): Promise<{
|
|
8
|
+
path: string;
|
|
9
|
+
mime: string;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
import { InlayError } from '../errors.js';
|
|
3
|
+
import { isRasterImage } from './detect.js';
|
|
4
|
+
import { getMedia, withMediaLock } from './library.js';
|
|
5
|
+
export const IMAGE_WIDTHS = [320, 640, 960, 1280, 1920, 2560];
|
|
6
|
+
const MIME = {
|
|
7
|
+
webp: 'image/webp',
|
|
8
|
+
avif: 'image/avif',
|
|
9
|
+
jpeg: 'image/jpeg',
|
|
10
|
+
png: 'image/png',
|
|
11
|
+
};
|
|
12
|
+
const inFlight = new WeakMap();
|
|
13
|
+
function notFound() {
|
|
14
|
+
return new InlayError('not_found', 'error.mediaNotFound');
|
|
15
|
+
}
|
|
16
|
+
export async function mediaVariant(deps, id, opts) {
|
|
17
|
+
const store = deps.media;
|
|
18
|
+
if (!store)
|
|
19
|
+
throw new InlayError('invalid_operation', 'error.mediaNotConfigured');
|
|
20
|
+
const { width, format } = opts;
|
|
21
|
+
if (!IMAGE_WIDTHS.includes(width) || !Object.hasOwn(MIME, format))
|
|
22
|
+
throw notFound();
|
|
23
|
+
const media = await getMedia(deps, id);
|
|
24
|
+
if (!media || !isRasterImage(media.mime) || !(await store.has(id)))
|
|
25
|
+
throw notFound();
|
|
26
|
+
const path = store.variantPath(id, width, format);
|
|
27
|
+
const mime = MIME[format];
|
|
28
|
+
if (await store.hasVariant(id, width, format))
|
|
29
|
+
return { path, mime };
|
|
30
|
+
let pending = inFlight.get(store);
|
|
31
|
+
if (!pending)
|
|
32
|
+
inFlight.set(store, (pending = new Map()));
|
|
33
|
+
const key = `${id}/${width}.${format}`;
|
|
34
|
+
let job = pending.get(key);
|
|
35
|
+
if (!job) {
|
|
36
|
+
const jobs = pending;
|
|
37
|
+
// Under the media lock, so a variant can't be written after its image was deleted.
|
|
38
|
+
job = withMediaLock(store, id, async () => {
|
|
39
|
+
if (!(await getMedia(deps, id)) || !(await store.has(id)))
|
|
40
|
+
throw notFound();
|
|
41
|
+
let bytes;
|
|
42
|
+
try {
|
|
43
|
+
bytes = await sharp(store.path(id))
|
|
44
|
+
.rotate()
|
|
45
|
+
.resize({ width, withoutEnlargement: true })
|
|
46
|
+
.toFormat(format)
|
|
47
|
+
.toBuffer();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new InlayError('invalid_operation', 'error.mediaUnreadable', { filename: media.filename });
|
|
51
|
+
}
|
|
52
|
+
await store.putVariant(id, width, format, bytes);
|
|
53
|
+
return path;
|
|
54
|
+
}).finally(() => jobs.delete(key));
|
|
55
|
+
pending.set(key, job);
|
|
56
|
+
}
|
|
57
|
+
await job;
|
|
58
|
+
return { path, mime };
|
|
59
|
+
}
|