sandcms 0.6.2 → 0.7.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/README.md +14 -10
- package/dist/admin/Account.svelte +286 -83
- package/dist/admin/Admin.svelte +14 -1
- package/dist/admin/ApiKeys.svelte +146 -79
- package/dist/admin/AssetAttachment.svelte +10 -12
- package/dist/admin/AssetAttachment.svelte.d.ts +0 -1
- package/dist/admin/BlueprintFields.svelte +20 -5
- package/dist/admin/BlueprintFields.svelte.d.ts +2 -1
- package/dist/admin/Collection.svelte +111 -54
- package/dist/admin/Dashboard.svelte +3 -37
- package/dist/admin/DateField.svelte +75 -0
- package/dist/admin/DateField.svelte.d.ts +11 -0
- package/dist/admin/Editor.svelte +70 -65
- package/dist/admin/Fields.svelte +24 -23
- package/dist/admin/Fields.svelte.d.ts +0 -1
- package/dist/admin/FocusPicker.svelte +1 -2
- package/dist/admin/Gallery.svelte +2 -7
- package/dist/admin/Gallery.svelte.d.ts +0 -1
- package/dist/admin/GlobalSearch.svelte +54 -26
- package/dist/admin/HoverHint.svelte +37 -0
- package/dist/admin/HoverHint.svelte.d.ts +8 -0
- package/dist/admin/Inbox.svelte +6 -11
- package/dist/admin/LinkField.svelte +128 -66
- package/dist/admin/LinkField.svelte.d.ts +0 -1
- package/dist/admin/Login.svelte +0 -1
- package/dist/admin/MediaLibrary.svelte +70 -56
- package/dist/admin/MediaPicker.svelte +62 -55
- package/dist/admin/MediaPicker.svelte.d.ts +0 -1
- package/dist/admin/NotificationSettings.svelte +35 -27
- package/dist/admin/RichText.svelte +2 -2
- package/dist/admin/RowMenu.svelte +28 -0
- package/dist/admin/RowMenu.svelte.d.ts +10 -0
- package/dist/admin/SearchList.svelte +51 -14
- package/dist/admin/Shell.svelte +64 -29
- package/dist/admin/ThemePicker.svelte +72 -0
- package/dist/admin/ThemePicker.svelte.d.ts +18 -0
- package/dist/admin/UploadButton.svelte +2 -1
- package/dist/admin/UploadProgress.svelte +24 -0
- package/dist/admin/UploadProgress.svelte.d.ts +7 -0
- package/dist/admin/Users.svelte +106 -50
- package/dist/admin/client.d.ts +5 -670
- package/dist/admin/client.js +20 -1
- package/dist/admin/components/ui/alert/alert.svelte.d.ts +3 -13
- package/dist/admin/components/ui/attachment/attachment-media.svelte.d.ts +3 -13
- package/dist/admin/components/ui/attachment/attachment.svelte.d.ts +6 -26
- package/dist/admin/components/ui/badge/badge.svelte.d.ts +7 -25
- package/dist/admin/components/ui/button/button.svelte.d.ts +15 -53
- package/dist/admin/components/ui/calendar/calendar-caption.svelte +76 -0
- package/dist/admin/components/ui/calendar/calendar-caption.svelte.d.ts +19 -0
- package/dist/admin/components/ui/calendar/calendar-cell.svelte +19 -0
- package/dist/admin/components/ui/calendar/calendar-cell.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-day.svelte +33 -0
- package/dist/admin/components/ui/calendar/calendar-day.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-grid-body.svelte +12 -0
- package/dist/admin/components/ui/calendar/calendar-grid-body.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-grid-head.svelte +12 -0
- package/dist/admin/components/ui/calendar/calendar-grid-head.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-grid-row.svelte +12 -0
- package/dist/admin/components/ui/calendar/calendar-grid-row.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-grid.svelte +16 -0
- package/dist/admin/components/ui/calendar/calendar-grid.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-head-cell.svelte +19 -0
- package/dist/admin/components/ui/calendar/calendar-head-cell.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-header.svelte +19 -0
- package/dist/admin/components/ui/calendar/calendar-header.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-heading.svelte +16 -0
- package/dist/admin/components/ui/calendar/calendar-heading.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-month-select.svelte +48 -0
- package/dist/admin/components/ui/calendar/calendar-month-select.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar-month.svelte +15 -0
- package/dist/admin/components/ui/calendar/calendar-month.svelte.d.ts +5 -0
- package/dist/admin/components/ui/calendar/calendar-months.svelte +19 -0
- package/dist/admin/components/ui/calendar/calendar-months.svelte.d.ts +5 -0
- package/dist/admin/components/ui/calendar/calendar-nav.svelte +19 -0
- package/dist/admin/components/ui/calendar/calendar-nav.svelte.d.ts +5 -0
- package/dist/admin/components/ui/calendar/calendar-next-button.svelte +37 -0
- package/dist/admin/components/ui/calendar/calendar-next-button.svelte.d.ts +8 -0
- package/dist/admin/components/ui/calendar/calendar-prev-button.svelte +37 -0
- package/dist/admin/components/ui/calendar/calendar-prev-button.svelte.d.ts +8 -0
- package/dist/admin/components/ui/calendar/calendar-year-select.svelte +47 -0
- package/dist/admin/components/ui/calendar/calendar-year-select.svelte.d.ts +4 -0
- package/dist/admin/components/ui/calendar/calendar.svelte +115 -0
- package/dist/admin/components/ui/calendar/calendar.svelte.d.ts +21 -0
- package/dist/admin/components/ui/calendar/index.d.ts +19 -0
- package/dist/admin/components/ui/calendar/index.js +21 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-checkbox-group.svelte +16 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-checkbox-group.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-checkbox-item.svelte +46 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-checkbox-item.svelte.d.ts +9 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-content.svelte +31 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-content.svelte.d.ts +10 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-group-heading.svelte +22 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-group-heading.svelte.d.ts +8 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-group.svelte +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-group.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-item.svelte +27 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-item.svelte.d.ts +8 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-label.svelte +24 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-label.svelte.d.ts +8 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-portal.svelte +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-portal.svelte.d.ts +3 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-radio-group.svelte +16 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-radio-group.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-radio-item.svelte +36 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-radio-item.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-separator.svelte +17 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-separator.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-shortcut.svelte +23 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-shortcut.svelte.d.ts +5 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte +31 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte.d.ts +10 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub-trigger.svelte +29 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub-trigger.svelte.d.ts +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub.svelte +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-sub.svelte.d.ts +3 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-trigger.svelte +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu-trigger.svelte.d.ts +4 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu.svelte +7 -0
- package/dist/admin/components/ui/dropdown-menu/dropdown-menu.svelte.d.ts +3 -0
- package/dist/admin/components/ui/dropdown-menu/index.d.ts +18 -0
- package/dist/admin/components/ui/dropdown-menu/index.js +18 -0
- package/dist/admin/components/ui/input-otp/index.d.ts +5 -0
- package/dist/admin/components/ui/input-otp/index.js +5 -0
- package/dist/admin/components/ui/input-otp/input-otp-group.svelte +23 -0
- package/dist/admin/components/ui/input-otp/input-otp-group.svelte.d.ts +5 -0
- package/dist/admin/components/ui/input-otp/input-otp-separator.svelte +27 -0
- package/dist/admin/components/ui/input-otp/input-otp-separator.svelte.d.ts +5 -0
- package/dist/admin/components/ui/input-otp/input-otp-slot.svelte +29 -0
- package/dist/admin/components/ui/input-otp/input-otp-slot.svelte.d.ts +4 -0
- package/dist/admin/components/ui/input-otp/input-otp.svelte +24 -0
- package/dist/admin/components/ui/input-otp/input-otp.svelte.d.ts +4 -0
- package/dist/admin/components/ui/popover/index.d.ts +9 -0
- package/dist/admin/components/ui/popover/index.js +11 -0
- package/dist/admin/components/ui/popover/popover-close.svelte +7 -0
- package/dist/admin/components/ui/popover/popover-close.svelte.d.ts +4 -0
- package/dist/admin/components/ui/popover/popover-content.svelte +31 -0
- package/dist/admin/components/ui/popover/popover-content.svelte.d.ts +10 -0
- package/dist/admin/components/ui/popover/popover-description.svelte +20 -0
- package/dist/admin/components/ui/popover/popover-description.svelte.d.ts +5 -0
- package/dist/admin/components/ui/popover/popover-header.svelte +20 -0
- package/dist/admin/components/ui/popover/popover-header.svelte.d.ts +5 -0
- package/dist/admin/components/ui/popover/popover-portal.svelte +7 -0
- package/dist/admin/components/ui/popover/popover-portal.svelte.d.ts +3 -0
- package/dist/admin/components/ui/popover/popover-title.svelte +20 -0
- package/dist/admin/components/ui/popover/popover-title.svelte.d.ts +5 -0
- package/dist/admin/components/ui/popover/popover-trigger.svelte +17 -0
- package/dist/admin/components/ui/popover/popover-trigger.svelte.d.ts +4 -0
- package/dist/admin/components/ui/popover/popover.svelte +7 -0
- package/dist/admin/components/ui/popover/popover.svelte.d.ts +3 -0
- package/dist/admin/components/ui/switch/switch.svelte +2 -2
- package/dist/admin/components/ui/toggle/toggle.svelte.d.ts +6 -26
- package/dist/admin/components/ui/tooltip/index.d.ts +6 -0
- package/dist/admin/components/ui/tooltip/index.js +8 -0
- package/dist/admin/components/ui/tooltip/tooltip-content.svelte +48 -0
- package/dist/admin/components/ui/tooltip/tooltip-content.svelte.d.ts +11 -0
- package/dist/admin/components/ui/tooltip/tooltip-portal.svelte +7 -0
- package/dist/admin/components/ui/tooltip/tooltip-portal.svelte.d.ts +4 -0
- package/dist/admin/components/ui/tooltip/tooltip-provider.svelte +7 -0
- package/dist/admin/components/ui/tooltip/tooltip-provider.svelte.d.ts +4 -0
- package/dist/admin/components/ui/tooltip/tooltip-trigger.svelte +7 -0
- package/dist/admin/components/ui/tooltip/tooltip-trigger.svelte.d.ts +25 -0
- package/dist/admin/components/ui/tooltip/tooltip.svelte +7 -0
- package/dist/admin/components/ui/tooltip/tooltip.svelte.d.ts +25 -0
- package/dist/admin/generated/admin.css +1298 -234
- package/dist/admin/slug.d.ts +1 -0
- package/dist/admin/slug.js +6 -0
- package/dist/admin/theme-context.d.ts +7 -0
- package/dist/admin/theme-context.js +3 -0
- package/dist/admin/theme.d.ts +7 -0
- package/dist/admin/theme.js +14 -0
- package/dist/admin/types.d.ts +2 -1
- package/dist/admin/uploads.d.ts +8 -1
- package/dist/admin/uploads.js +94 -16
- package/dist/blueprints/files.js +1 -0
- package/dist/blueprints/load.js +7 -2
- package/dist/blueprints/types.d.ts +4 -1
- package/dist/cli/doctor.d.ts +3 -1
- package/dist/cli/doctor.js +85 -30
- package/dist/cli/files.d.ts +2 -6
- package/dist/cli/files.js +11 -2
- package/dist/cli/index.js +99 -31
- package/dist/cli/scaffold.js +8 -8
- package/dist/cli/setup.js +16 -26
- package/dist/cli/upgrade.js +21 -2
- package/dist/env.d.ts +32 -0
- package/dist/env.js +53 -3
- package/dist/server/admin-load.d.ts +13 -5
- package/dist/server/admin-load.js +8 -3
- package/dist/server/api-access.d.ts +4 -0
- package/dist/server/api-access.js +15 -2
- package/dist/server/api-pagination.d.ts +3 -0
- package/dist/server/api.js +11 -3
- package/dist/server/auth.d.ts +11 -9
- package/dist/server/auth.js +23 -0
- package/dist/server/cms.d.ts +65 -18
- package/dist/server/cms.js +62 -6
- package/dist/server/db/migration-data.js +4 -0
- package/dist/server/db/migrations/0009_local_media.sql +9 -0
- package/dist/server/db/migrations/meta/0009_snapshot.json +1763 -0
- package/dist/server/db/migrations/meta/_journal.json +7 -0
- package/dist/server/db/schema.d.ts +119 -2
- package/dist/server/db/schema.js +15 -3
- package/dist/server/hooks.js +155 -81
- package/dist/server/media/contracts.d.ts +115 -0
- package/dist/server/media/contracts.js +12 -0
- package/dist/server/media/deletions.d.ts +2 -1
- package/dist/server/media/deletions.js +3 -3
- package/dist/server/media/delivery.js +7 -11
- package/dist/server/media/import-r2.d.ts +56 -0
- package/dist/server/media/import-r2.js +342 -0
- package/dist/server/media/processor-child.d.ts +2 -0
- package/dist/server/media/processor-child.js +236 -0
- package/dist/server/media/processor.d.ts +51 -0
- package/dist/server/media/processor.js +135 -0
- package/dist/server/media/render-math.d.ts +24 -0
- package/dist/server/media/render-math.js +50 -0
- package/dist/server/media/runner.d.ts +30 -1
- package/dist/server/media/runner.js +418 -165
- package/dist/server/media/service.d.ts +56 -10
- package/dist/server/media/service.js +309 -98
- package/dist/server/media/storage.d.ts +5 -0
- package/dist/server/media/storage.js +279 -0
- package/dist/server/media/svg.d.ts +5 -0
- package/dist/server/media/svg.js +142 -0
- package/dist/server/media/upload-ticket.d.ts +12 -0
- package/dist/server/media/upload-ticket.js +35 -0
- package/dist/server/media/verify.d.ts +36 -0
- package/dist/server/media/verify.js +109 -0
- package/dist/server/platform/http.d.ts +27 -0
- package/dist/server/platform/http.js +77 -0
- package/dist/server/platform/index.d.ts +52 -0
- package/dist/server/platform/index.js +98 -0
- package/dist/server/platform/snapshots.d.ts +18 -0
- package/dist/server/platform/snapshots.js +110 -0
- package/dist/server/platform/volume.d.ts +20 -0
- package/dist/server/platform/volume.js +60 -0
- package/dist/worker/svg.d.ts +1 -5
- package/dist/worker/svg.js +2 -142
- package/docs/acceptance.md +6 -4
- package/docs/api.md +42 -4
- package/docs/cloudflare.md +48 -142
- package/docs/content.md +2 -2
- package/docs/deployment.md +159 -57
- package/docs/forms.md +1 -1
- package/docs/media.md +85 -21
- package/docs/package.md +21 -18
- package/docs/plans/0.7-self-hosted.md +264 -0
- package/docs/releases.md +27 -1
- package/docs/upgrades.md +44 -6
- package/package.json +38 -37
- package/templates/Dockerfile +28 -0
- package/templates/integration.ts +18 -2
- package/templates/railpack.json +1 -1
- package/templates/sandcms/blueprints/files/image.yaml +1 -1
- package/templates/sandcms/config.yaml +0 -1
- package/dist/cli/cloudflare.d.ts +0 -8
- package/dist/cli/cloudflare.js +0 -135
- package/templates/sandcms/worker/index.ts +0 -1
- package/templates/sandcms/worker/local.ts +0 -1
- package/templates/sandcms/worker/wrangler.jsonc +0 -6
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { variantKeyPattern } from './storage.js';
|
|
1
2
|
// Both routes are intercepted by cmsHandle, so integrations need no extra route files.
|
|
2
3
|
export async function mediaDelivery(cms, event, preview) {
|
|
4
|
+
// Service responses are built locally (Bun.file bodies with Content-Length), so headers are adjusted in place.
|
|
3
5
|
const finish = (response) => {
|
|
4
|
-
const headers =
|
|
6
|
+
const { headers } = response;
|
|
5
7
|
// Only authorized public representations (including revalidation) may be cached.
|
|
6
8
|
const cacheable = !preview && (response.status === 200 || response.status === 304);
|
|
7
9
|
headers.set('Cache-Control', cacheable ? 'public, max-age=86400, s-maxage=2592000' : 'private, no-store');
|
|
@@ -14,14 +16,9 @@ export async function mediaDelivery(cms, event, preview) {
|
|
|
14
16
|
headers.set('X-Robots-Tag', 'noindex, nofollow');
|
|
15
17
|
headers.delete('ETag');
|
|
16
18
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
headers
|
|
20
|
-
headers.delete('Transfer-Encoding');
|
|
21
|
-
return new Response(event.request.method === 'HEAD' ? null : response.body, {
|
|
22
|
-
status: response.status,
|
|
23
|
-
headers
|
|
24
|
-
});
|
|
19
|
+
if (event.request.method !== 'HEAD' || response.body === null)
|
|
20
|
+
return response;
|
|
21
|
+
return new Response(null, { status: response.status, headers });
|
|
25
22
|
};
|
|
26
23
|
const method = event.request.method;
|
|
27
24
|
if (method !== 'GET' && method !== 'HEAD')
|
|
@@ -35,8 +32,7 @@ export async function mediaDelivery(cms, event, preview) {
|
|
|
35
32
|
}
|
|
36
33
|
const prefix = preview ? '/api/cms/media/' : '/_sandcms/media/';
|
|
37
34
|
const key = event.url.pathname.slice(prefix.length);
|
|
38
|
-
if (event.url.search ||
|
|
39
|
-
!/^([a-f0-9-]{36})\/([a-f0-9]{16})\/([a-z][a-z0-9_-]*)-(\d+)\.(webp|avif|jpeg|svg)$/.test(key))
|
|
35
|
+
if (event.url.search || !variantKeyPattern.test(key))
|
|
40
36
|
return finish(new Response('Not found', { status: 404 }));
|
|
41
37
|
return finish(await cms.media.deliver(key, method, preview, preview ? null : event.request.headers.get('if-none-match')));
|
|
42
38
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Store } from '../db/store.js';
|
|
2
|
+
import { type MediaProcessor, type MediaStorage } from './contracts.js';
|
|
3
|
+
export interface MediaTarget {
|
|
4
|
+
store: Store;
|
|
5
|
+
media: {
|
|
6
|
+
storage: MediaStorage;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export interface ImportOptions {
|
|
10
|
+
workerUrl: string;
|
|
11
|
+
workerToken: string;
|
|
12
|
+
log?: (line: string) => void;
|
|
13
|
+
/** Assets imported at once. */
|
|
14
|
+
concurrency?: number;
|
|
15
|
+
/** Attempts per request for network errors and 5xx/429 responses. */
|
|
16
|
+
attempts?: number;
|
|
17
|
+
/** Base backoff in ms, doubled per attempt. */
|
|
18
|
+
retryDelay?: number;
|
|
19
|
+
/** How long to wait for a running processor to release its lease. */
|
|
20
|
+
leaseWait?: number;
|
|
21
|
+
/** Measures originals without stored dimensions; defaults to the sandboxed sharp child. */
|
|
22
|
+
processor?: MediaProcessor;
|
|
23
|
+
}
|
|
24
|
+
export interface ImportFailure {
|
|
25
|
+
id: string;
|
|
26
|
+
key?: string;
|
|
27
|
+
error: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ImportSummary {
|
|
30
|
+
assets: number;
|
|
31
|
+
originals: {
|
|
32
|
+
imported: number;
|
|
33
|
+
present: number;
|
|
34
|
+
};
|
|
35
|
+
variants: {
|
|
36
|
+
imported: number;
|
|
37
|
+
present: number;
|
|
38
|
+
};
|
|
39
|
+
dimensions: number;
|
|
40
|
+
skipped: {
|
|
41
|
+
id: string;
|
|
42
|
+
reason: string;
|
|
43
|
+
}[];
|
|
44
|
+
failures: ImportFailure[];
|
|
45
|
+
verified: {
|
|
46
|
+
originals: number;
|
|
47
|
+
variants: number;
|
|
48
|
+
};
|
|
49
|
+
ok: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** Waits for and holds a named lease, renewing it until `release()`. `check()` throws once it is lost. */
|
|
52
|
+
export declare function holdLease(store: Store, name: string, owner: string, wait: number, log?: (line: string) => void): Promise<{
|
|
53
|
+
check(): void;
|
|
54
|
+
release(): void;
|
|
55
|
+
}>;
|
|
56
|
+
export declare function importFromR2(cms: MediaTarget, options: ImportOptions): Promise<ImportSummary>;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// One-time import of R2 media into local storage (docs/plans/0.7-self-hosted.md §4.6).
|
|
2
|
+
// Talks to the deprecated protocol-6 Worker; keeps asset IDs, variant keys and preset versions.
|
|
3
|
+
import { copyFile } from 'node:fs/promises';
|
|
4
|
+
import { basename, join } from 'node:path';
|
|
5
|
+
import { and, eq, notExists } from 'drizzle-orm';
|
|
6
|
+
import { jobs, media, mediaDeletions, uploads, uploadSessions } from '../db/schema.js';
|
|
7
|
+
import { sourceURL } from '../../worker/source.js';
|
|
8
|
+
import { PROCESSOR_LEASE } from './contracts.js';
|
|
9
|
+
import { liveLegacyUpload, liveUploadSession, releaseLease, takeLease } from './runner.js';
|
|
10
|
+
/** Not worth retrying: missing objects, rejected credentials, inconsistent data. */
|
|
11
|
+
class Permanent extends Error {
|
|
12
|
+
}
|
|
13
|
+
class Missing extends Permanent {
|
|
14
|
+
}
|
|
15
|
+
const leaseTtl = 60_000;
|
|
16
|
+
/** Waits for and holds a named lease, renewing it until `release()`. `check()` throws once it is lost. */
|
|
17
|
+
export async function holdLease(store, name, owner, wait, log = console.log) {
|
|
18
|
+
const deadline = Date.now() + wait;
|
|
19
|
+
for (let i = 0; !takeLease(store, name, owner, leaseTtl); i++) {
|
|
20
|
+
if (Date.now() > deadline)
|
|
21
|
+
throw new Error('The media processor is busy. Stop the website or try again later.');
|
|
22
|
+
if (!i)
|
|
23
|
+
log('Waiting for the media processor to become idle…');
|
|
24
|
+
await Bun.sleep(1000);
|
|
25
|
+
}
|
|
26
|
+
let lost = false;
|
|
27
|
+
const timer = setInterval(() => {
|
|
28
|
+
if (!takeLease(store, name, owner, leaseTtl))
|
|
29
|
+
lost = true;
|
|
30
|
+
}, leaseTtl / 4);
|
|
31
|
+
return {
|
|
32
|
+
check() {
|
|
33
|
+
if (lost)
|
|
34
|
+
throw new Error('Another process took the media processor lease');
|
|
35
|
+
},
|
|
36
|
+
release() {
|
|
37
|
+
clearInterval(timer);
|
|
38
|
+
releaseLease(store, name, owner);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export async function importFromR2(cms, options) {
|
|
43
|
+
const { db } = cms.store, storage = cms.media.storage;
|
|
44
|
+
const log = options.log ?? console.log, attempts = options.attempts ?? 5, retryDelay = options.retryDelay ?? 1000;
|
|
45
|
+
if (!options.workerUrl)
|
|
46
|
+
throw new Error('Set SANDCMS_WORKER_URL or --worker-url to the old Worker');
|
|
47
|
+
if (!options.workerToken || options.workerToken.length < 32)
|
|
48
|
+
throw new Error('Set SANDCMS_WORKER_TOKEN to the old Worker token (32+ characters)');
|
|
49
|
+
const base = new URL(options.workerUrl).toString().replace(/\/$/, '');
|
|
50
|
+
const origin = new URL(options.workerUrl).origin;
|
|
51
|
+
const auth = {
|
|
52
|
+
authorization: `Bearer ${options.workerToken}`,
|
|
53
|
+
'x-sandcms-protocol': '6'
|
|
54
|
+
};
|
|
55
|
+
// Expired or permanently failed rows stay behind for their error message; only live ones block.
|
|
56
|
+
if (db
|
|
57
|
+
.select({ id: uploadSessions.mediaId })
|
|
58
|
+
.from(uploadSessions)
|
|
59
|
+
.where(liveUploadSession())
|
|
60
|
+
.limit(1)
|
|
61
|
+
.get())
|
|
62
|
+
throw new Error('Uploads are in progress (upload_sessions). Let them finish or expire, then run the import again.');
|
|
63
|
+
if (db.select({ id: uploads.mediaId }).from(uploads).where(liveLegacyUpload()).limit(1).get())
|
|
64
|
+
throw new Error('Legacy uploads are still queued (uploads). Finish them with the 0.6 release before importing.');
|
|
65
|
+
async function retry(label, run) {
|
|
66
|
+
for (let i = 1;; i++) {
|
|
67
|
+
try {
|
|
68
|
+
return await run();
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
if (e instanceof Permanent || i >= attempts)
|
|
72
|
+
throw e;
|
|
73
|
+
log(` ${label}: ${message(e)}; retrying`);
|
|
74
|
+
await Bun.sleep(retryDelay * 2 ** (i - 1));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Streams a Worker response to `dest`; returns the byte count after checking Content-Length.
|
|
79
|
+
async function download(url, headers, dest, timeout) {
|
|
80
|
+
let response;
|
|
81
|
+
try {
|
|
82
|
+
response = await fetch(await url(), {
|
|
83
|
+
headers,
|
|
84
|
+
redirect: 'error',
|
|
85
|
+
signal: AbortSignal.timeout(timeout)
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
// Fetch errors can include signed source URLs; keep them out of logs.
|
|
90
|
+
throw new Error(e?.name === 'TimeoutError' ? 'Worker request timed out' : 'Worker request failed');
|
|
91
|
+
}
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
await response.body?.cancel();
|
|
94
|
+
const error = `Worker returned ${response.status}`;
|
|
95
|
+
if (response.status === 404)
|
|
96
|
+
throw new Missing(error);
|
|
97
|
+
if (response.status >= 500 || response.status === 429 || response.status === 408)
|
|
98
|
+
throw new Error(error);
|
|
99
|
+
throw new Permanent(error);
|
|
100
|
+
}
|
|
101
|
+
const expected = response.headers.get('content-length');
|
|
102
|
+
let bytes;
|
|
103
|
+
try {
|
|
104
|
+
bytes = await Bun.write(dest, response);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
throw new Error('Worker response was interrupted');
|
|
108
|
+
}
|
|
109
|
+
if (expected !== null && Number(expected) !== bytes)
|
|
110
|
+
throw new Error(`Received ${bytes} of ${expected} bytes`);
|
|
111
|
+
return bytes;
|
|
112
|
+
}
|
|
113
|
+
const fetchVariant = (key, dest) => retry(key, () => download(async () => `${base}/internal/media/${key}`, auth, dest, 120_000));
|
|
114
|
+
// Sign per attempt: source URLs expire after five minutes.
|
|
115
|
+
const fetchOriginal = (id, dest) => retry(`original ${id}`, () => download(() => sourceURL(origin, id, options.workerToken), {}, dest, 600_000));
|
|
116
|
+
await retry('health', async () => {
|
|
117
|
+
let response;
|
|
118
|
+
try {
|
|
119
|
+
response = await fetch(`${base}/internal/health`, {
|
|
120
|
+
headers: auth,
|
|
121
|
+
signal: AbortSignal.timeout(30_000)
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
throw new Error(`Cannot reach the Worker at ${base}`);
|
|
126
|
+
}
|
|
127
|
+
if (response.status === 404)
|
|
128
|
+
throw new Permanent('The Worker rejected SANDCMS_WORKER_TOKEN, or the URL is not a SandCMS Worker');
|
|
129
|
+
if (!response.ok)
|
|
130
|
+
throw new Error(`Worker health check returned ${response.status}`);
|
|
131
|
+
const { protocol } = (await response.json());
|
|
132
|
+
if (!protocol || protocol < 6)
|
|
133
|
+
throw new Permanent(`The Worker speaks protocol ${protocol}; deploy the 0.6.2+ Worker first`);
|
|
134
|
+
});
|
|
135
|
+
const lease = await holdLease(cms.store, PROCESSOR_LEASE, `import-r2:${process.pid}:${crypto.randomUUID()}`, options.leaseWait ?? 120_000, log);
|
|
136
|
+
let processor = options.processor;
|
|
137
|
+
const ownProcessor = !processor;
|
|
138
|
+
// Same loader allowlist and format check as uploads; only the header is trusted for dimensions.
|
|
139
|
+
async function measure(path, mime) {
|
|
140
|
+
processor ??= (await import('./processor.js')).createProcessor();
|
|
141
|
+
return processor.inspect(path, mime, 1_000_000_000).catch(() => null);
|
|
142
|
+
}
|
|
143
|
+
const summary = {
|
|
144
|
+
assets: 0,
|
|
145
|
+
originals: { imported: 0, present: 0 },
|
|
146
|
+
variants: { imported: 0, present: 0 },
|
|
147
|
+
dimensions: 0,
|
|
148
|
+
skipped: [],
|
|
149
|
+
failures: [],
|
|
150
|
+
verified: { originals: 0, variants: 0 },
|
|
151
|
+
ok: false
|
|
152
|
+
};
|
|
153
|
+
// Content-Length seen during this run, for the verification pass.
|
|
154
|
+
const sizes = new Map();
|
|
155
|
+
const get = (id) => db.select().from(media).where(eq(media.id, id)).get();
|
|
156
|
+
const readyJobs = (id) => db
|
|
157
|
+
.select()
|
|
158
|
+
.from(jobs)
|
|
159
|
+
.where(and(eq(jobs.mediaId, id), eq(jobs.status, 'ready')))
|
|
160
|
+
.all();
|
|
161
|
+
// Every ready result, not only the active set: older versions may still be referenced by caches.
|
|
162
|
+
function variantKeys(asset) {
|
|
163
|
+
const keys = new Set(asset.variants.map((v) => v.key));
|
|
164
|
+
for (const job of readyJobs(asset.id))
|
|
165
|
+
if (job.result)
|
|
166
|
+
keys.add(job.result.key);
|
|
167
|
+
return [...keys];
|
|
168
|
+
}
|
|
169
|
+
function etagged(asset, key) {
|
|
170
|
+
const current = get(asset.id);
|
|
171
|
+
return (!!current?.variants.every((v) => v.key !== key || v.etag) &&
|
|
172
|
+
readyJobs(asset.id).every((j) => j.result?.key !== key || j.result.etag));
|
|
173
|
+
}
|
|
174
|
+
function recordEtag(id, key, etag) {
|
|
175
|
+
db.transaction(() => {
|
|
176
|
+
for (const job of readyJobs(id))
|
|
177
|
+
if (job.result?.key === key)
|
|
178
|
+
db.update(jobs)
|
|
179
|
+
.set({ result: { ...job.result, etag } })
|
|
180
|
+
.where(eq(jobs.id, job.id))
|
|
181
|
+
.run();
|
|
182
|
+
const row = get(id);
|
|
183
|
+
if (row?.variants.some((v) => v.key === key))
|
|
184
|
+
db.update(media)
|
|
185
|
+
.set({ variants: row.variants.map((v) => (v.key === key ? { ...v, etag } : v)) })
|
|
186
|
+
.where(eq(media.id, id))
|
|
187
|
+
.run();
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
const assets = () => db
|
|
191
|
+
.select()
|
|
192
|
+
.from(media)
|
|
193
|
+
.where(notExists(db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, media.id))))
|
|
194
|
+
.all()
|
|
195
|
+
.filter((a) => a.status !== 'deleting');
|
|
196
|
+
async function importAsset(asset) {
|
|
197
|
+
const svg = asset.mime === 'image/svg+xml';
|
|
198
|
+
const keys = variantKeys(asset);
|
|
199
|
+
const svgKey = svg
|
|
200
|
+
? (keys.find((k) => asset.activeVersion &&
|
|
201
|
+
k.startsWith(`${asset.id}/${asset.activeVersion}/`) &&
|
|
202
|
+
k.endsWith('.svg')) ?? keys.find((k) => k.endsWith('.svg')))
|
|
203
|
+
: undefined;
|
|
204
|
+
const fail = (error, key) => summary.failures.push({ id: asset.id, ...(key ? { key } : {}), error: message(error) });
|
|
205
|
+
const work = await storage.workDir(asset.id);
|
|
206
|
+
let imported = 0;
|
|
207
|
+
try {
|
|
208
|
+
try {
|
|
209
|
+
const existing = storage.originalPath(asset.id);
|
|
210
|
+
if (existing) {
|
|
211
|
+
const size = Bun.file(existing).size;
|
|
212
|
+
if (!svg && size !== asset.bytes)
|
|
213
|
+
throw new Permanent(`Local original is ${size} bytes; the database expects ${asset.bytes}`);
|
|
214
|
+
summary.originals.present++;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const staged = join(work, 'import-original');
|
|
218
|
+
if (svg) {
|
|
219
|
+
if (!svgKey)
|
|
220
|
+
throw new Missing('No sanitized SVG variant to import; upload this file again');
|
|
221
|
+
sizes.set(svgKey, await fetchVariant(svgKey, staged));
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
const bytes = await fetchOriginal(asset.id, staged);
|
|
225
|
+
if (bytes !== asset.bytes)
|
|
226
|
+
throw new Permanent(`R2 original is ${bytes} bytes; the database expects ${asset.bytes}`);
|
|
227
|
+
}
|
|
228
|
+
await storage.promoteOriginal(asset.id, staged);
|
|
229
|
+
summary.originals.imported++;
|
|
230
|
+
imported++;
|
|
231
|
+
}
|
|
232
|
+
const path = storage.originalPath(asset.id);
|
|
233
|
+
if (!svg && path && (asset.width === null || asset.height === null)) {
|
|
234
|
+
const { width, height } = (await measure(path, asset.mime)) ?? {};
|
|
235
|
+
if (width && height) {
|
|
236
|
+
db.update(media).set({ width, height }).where(eq(media.id, asset.id)).run();
|
|
237
|
+
summary.dimensions++;
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
log(` ${asset.id}: could not read the original's dimensions`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
if (e instanceof Missing && !keys.length) {
|
|
245
|
+
summary.skipped.push({ id: asset.id, reason: 'No original in R2 and no ready variants' });
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
fail(e);
|
|
249
|
+
}
|
|
250
|
+
for (const key of keys) {
|
|
251
|
+
lease.check();
|
|
252
|
+
try {
|
|
253
|
+
const existing = storage.variantPath(key);
|
|
254
|
+
if (existing && etagged(asset, key)) {
|
|
255
|
+
summary.variants.present++;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const tmp = join(work, 'import-' + basename(key));
|
|
259
|
+
const original = storage.originalPath(asset.id);
|
|
260
|
+
// Re-commit a file written before a crash so its ETag is recorded; no download needed.
|
|
261
|
+
if (existing)
|
|
262
|
+
await copyFile(existing, tmp);
|
|
263
|
+
else if (key === svgKey && original)
|
|
264
|
+
await copyFile(original, tmp);
|
|
265
|
+
else
|
|
266
|
+
sizes.set(key, await fetchVariant(key, tmp));
|
|
267
|
+
const { etag } = await storage.writeVariant(key, tmp);
|
|
268
|
+
recordEtag(asset.id, key, etag);
|
|
269
|
+
if (existing)
|
|
270
|
+
summary.variants.present++;
|
|
271
|
+
else {
|
|
272
|
+
summary.variants.imported++;
|
|
273
|
+
imported++;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
fail(e, key);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
await storage.removeWork(asset.id);
|
|
283
|
+
const current = get(asset.id);
|
|
284
|
+
// Deleted while importing: the deletion runner may already have cleaned storage.
|
|
285
|
+
if (!current || current.status === 'deleting')
|
|
286
|
+
await storage.removeAsset(asset.id);
|
|
287
|
+
}
|
|
288
|
+
log(`${asset.filename} (${asset.id}): ${imported ? `${imported} files imported` : 'up to date'}`);
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
const list = assets();
|
|
292
|
+
summary.assets = list.length;
|
|
293
|
+
log(`Importing ${list.length} assets from ${base}`);
|
|
294
|
+
let next = 0;
|
|
295
|
+
await Promise.all(Array.from({ length: Math.min(options.concurrency ?? 4, list.length) }, async () => {
|
|
296
|
+
while (next < list.length)
|
|
297
|
+
await importAsset(list[next++]);
|
|
298
|
+
}));
|
|
299
|
+
// Verification: every original and every ready variant exists, with the expected size.
|
|
300
|
+
const skipped = new Set(summary.skipped.map((s) => s.id));
|
|
301
|
+
const failed = new Set(summary.failures.map((f) => `${f.id}/${f.key ?? ''}`));
|
|
302
|
+
for (const asset of assets()) {
|
|
303
|
+
if (skipped.has(asset.id))
|
|
304
|
+
continue;
|
|
305
|
+
const svg = asset.mime === 'image/svg+xml';
|
|
306
|
+
const path = storage.originalPath(asset.id);
|
|
307
|
+
if (path && (svg || Bun.file(path).size === asset.bytes))
|
|
308
|
+
summary.verified.originals++;
|
|
309
|
+
else if (!failed.has(`${asset.id}/`))
|
|
310
|
+
summary.failures.push({
|
|
311
|
+
id: asset.id,
|
|
312
|
+
error: path
|
|
313
|
+
? 'Original size does not match the database'
|
|
314
|
+
: 'Original missing after import'
|
|
315
|
+
});
|
|
316
|
+
for (const key of variantKeys(asset)) {
|
|
317
|
+
const file = storage.variantPath(key);
|
|
318
|
+
let error;
|
|
319
|
+
if (!file)
|
|
320
|
+
error = 'Variant missing after import';
|
|
321
|
+
else if (sizes.has(key) && Bun.file(file).size !== sizes.get(key))
|
|
322
|
+
error = 'Variant size does not match the Worker';
|
|
323
|
+
else if (!etagged(asset, key))
|
|
324
|
+
error = 'Variant ETag not recorded';
|
|
325
|
+
if (!error)
|
|
326
|
+
summary.verified.variants++;
|
|
327
|
+
else if (!failed.has(`${asset.id}/${key}`))
|
|
328
|
+
summary.failures.push({ id: asset.id, key, error });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
lease.release();
|
|
334
|
+
if (ownProcessor)
|
|
335
|
+
processor?.close();
|
|
336
|
+
}
|
|
337
|
+
summary.ok = summary.failures.length === 0;
|
|
338
|
+
return summary;
|
|
339
|
+
}
|
|
340
|
+
function message(e) {
|
|
341
|
+
return e instanceof Error ? e.message : String(e);
|
|
342
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// Media processor child process (spawned by processor.ts over Bun IPC). One request per process.
|
|
2
|
+
import { appendFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { availableParallelism, getPriority, setPriority } from 'node:os';
|
|
4
|
+
import { rm } from 'node:fs/promises';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import sharp, {} from 'sharp';
|
|
7
|
+
import { masterSize, renderPlan } from './render-math.js';
|
|
8
|
+
/** libvips threads per decode/encode; more lets an idle server finish a batch sooner. */
|
|
9
|
+
export function mediaThreads(value = process.env.SANDCMS_MEDIA_THREADS) {
|
|
10
|
+
const threads = Number(value);
|
|
11
|
+
return Number.isSafeInteger(threads) && threads > 0
|
|
12
|
+
? Math.min(threads, availableParallelism())
|
|
13
|
+
: 1;
|
|
14
|
+
}
|
|
15
|
+
sharp.cache(false);
|
|
16
|
+
sharp.concurrency(mediaThreads());
|
|
17
|
+
// Allowlist loaders so an SVG/PDF/etc. declared as a raster type is never rasterized.
|
|
18
|
+
sharp.block({ operation: ['VipsForeignLoad'] });
|
|
19
|
+
sharp.unblock({
|
|
20
|
+
operation: [
|
|
21
|
+
'VipsForeignLoadJpeg',
|
|
22
|
+
'VipsForeignLoadPng',
|
|
23
|
+
'VipsForeignLoadWebp',
|
|
24
|
+
'VipsForeignLoadHeif',
|
|
25
|
+
'VipsForeignLoadNsgif',
|
|
26
|
+
'VipsForeignLoadTiff'
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
try {
|
|
30
|
+
// Under memory pressure the kernel should kill this child, not the web server.
|
|
31
|
+
writeFileSync('/proc/self/oom_score_adj', '1000');
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Not Linux, or not permitted.
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
// Background work: the site's own requests always win the CPU over image processing.
|
|
38
|
+
setPriority(10);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Not permitted on this platform.
|
|
42
|
+
}
|
|
43
|
+
if (process.env.SANDCMS_PROCESSOR_TRACE)
|
|
44
|
+
appendFileSync(process.env.SANDCMS_PROCESSOR_TRACE, `start threads=${sharp.concurrency()} nice=${getPriority()}\n`);
|
|
45
|
+
const formats = {
|
|
46
|
+
'image/jpeg': (m) => m.format === 'jpeg',
|
|
47
|
+
'image/png': (m) => m.format === 'png',
|
|
48
|
+
'image/webp': (m) => m.format === 'webp',
|
|
49
|
+
'image/avif': (m) => m.format === 'heif' && m.compression === 'av1',
|
|
50
|
+
'image/gif': (m) => m.format === 'gif',
|
|
51
|
+
'image/tiff': (m) => m.format === 'tiff'
|
|
52
|
+
};
|
|
53
|
+
class Rejection extends Error {
|
|
54
|
+
status;
|
|
55
|
+
constructor(status, message) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.status = status;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const open = (path, maxPixels) => sharp(path, { failOn: 'error', limitInputPixels: maxPixels });
|
|
61
|
+
function classify(error) {
|
|
62
|
+
if (error instanceof Rejection)
|
|
63
|
+
return error;
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
if (/unsupported image format/i.test(message))
|
|
66
|
+
return new Rejection(415, 'Unsupported image format');
|
|
67
|
+
if (/pixel limit/i.test(message))
|
|
68
|
+
return new Rejection(413, 'Image has too many pixels');
|
|
69
|
+
return new Rejection(422, `Image could not be decoded: ${message.split('\n')[0]}`);
|
|
70
|
+
}
|
|
71
|
+
async function probe(path, mime, maxPixels) {
|
|
72
|
+
let meta;
|
|
73
|
+
try {
|
|
74
|
+
meta = await open(path, maxPixels).metadata();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw classify(error);
|
|
78
|
+
}
|
|
79
|
+
if (!formats[mime]?.(meta))
|
|
80
|
+
throw new Rejection(415, `File is not a valid ${mime} image`);
|
|
81
|
+
const { width = 0, height = 0 } = meta.autoOrient ?? meta;
|
|
82
|
+
if (width < 1 || height < 1)
|
|
83
|
+
throw new Rejection(422, 'Image has no dimensions');
|
|
84
|
+
if (width * height > maxPixels)
|
|
85
|
+
throw new Rejection(413, 'Image has too many pixels');
|
|
86
|
+
return { width, height, exif: meta.exif };
|
|
87
|
+
}
|
|
88
|
+
/** Validate the whole stream cheaply (shrink-on-load where supported) so truncated files are rejected up front. */
|
|
89
|
+
async function verifyDecode(path, maxPixels) {
|
|
90
|
+
try {
|
|
91
|
+
await open(path, maxPixels)
|
|
92
|
+
.resize(64, 64, { fit: 'inside', withoutEnlargement: true })
|
|
93
|
+
.raw()
|
|
94
|
+
.toBuffer();
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
throw classify(error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Minimal TIFF/EXIF IFD0 reader: only Artist (0x013b) and Copyright (0x8298) survive into variants.
|
|
101
|
+
function credits(exif) {
|
|
102
|
+
if (!exif || exif.length < 14)
|
|
103
|
+
return undefined;
|
|
104
|
+
const base = exif.subarray(0, 6).toString('latin1') === 'Exif\0\0' ? 6 : 0;
|
|
105
|
+
const le = exif.toString('latin1', base, base + 2) === 'II';
|
|
106
|
+
const u16 = (o) => (le ? exif.readUInt16LE(o) : exif.readUInt16BE(o));
|
|
107
|
+
const u32 = (o) => (le ? exif.readUInt32LE(o) : exif.readUInt32BE(o));
|
|
108
|
+
const out = {};
|
|
109
|
+
try {
|
|
110
|
+
const ifd = base + u32(base + 4);
|
|
111
|
+
const count = u16(ifd);
|
|
112
|
+
for (let i = 0; i < count; i++) {
|
|
113
|
+
const entry = ifd + 2 + i * 12;
|
|
114
|
+
const tag = u16(entry);
|
|
115
|
+
const name = tag === 0x013b ? 'Artist' : tag === 0x8298 ? 'Copyright' : undefined;
|
|
116
|
+
if (!name || u16(entry + 2) !== 2)
|
|
117
|
+
continue;
|
|
118
|
+
const length = u32(entry + 4);
|
|
119
|
+
const start = length <= 4 ? entry + 8 : base + u32(entry + 8);
|
|
120
|
+
const value = exif
|
|
121
|
+
.toString('latin1', start, start + length)
|
|
122
|
+
.replace(/\0[\s\S]*$/, '')
|
|
123
|
+
.trim();
|
|
124
|
+
if (value)
|
|
125
|
+
out[name] = value;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return Object.keys(out).length ? out : undefined;
|
|
132
|
+
}
|
|
133
|
+
const send = (message) => process.send(message);
|
|
134
|
+
let resume;
|
|
135
|
+
const proceed = () => new Promise((resolve) => (resume = resolve));
|
|
136
|
+
async function report(id, value) {
|
|
137
|
+
send({ type: 'report', id, report: value });
|
|
138
|
+
await proceed();
|
|
139
|
+
}
|
|
140
|
+
async function encode(master, source, spec, exif, path) {
|
|
141
|
+
if (!['webp', 'avif', 'jpeg'].includes(spec.format))
|
|
142
|
+
throw new Error(`Raster input cannot become ${spec.format}`);
|
|
143
|
+
const plan = renderPlan(source.width, source.height, spec);
|
|
144
|
+
let image = sharp(master.path);
|
|
145
|
+
if (plan.resize.width !== master.width || plan.resize.height !== master.height)
|
|
146
|
+
image = image.resize(plan.resize.width, plan.resize.height, { fit: 'fill' });
|
|
147
|
+
if (plan.crop)
|
|
148
|
+
image = image.extract(plan.crop);
|
|
149
|
+
if (exif)
|
|
150
|
+
image = image.withExif({ IFD0: exif });
|
|
151
|
+
if (spec.format === 'jpeg')
|
|
152
|
+
image = image.flatten({ background: '#ffffff' }).jpeg({ quality: spec.quality, mozjpeg: true });
|
|
153
|
+
else if (spec.format === 'webp')
|
|
154
|
+
image = image.webp({ quality: spec.quality });
|
|
155
|
+
else
|
|
156
|
+
image = image.avif({ quality: spec.quality });
|
|
157
|
+
return image.toFile(path);
|
|
158
|
+
}
|
|
159
|
+
async function render(id, input) {
|
|
160
|
+
const { specs } = input;
|
|
161
|
+
let master;
|
|
162
|
+
let source;
|
|
163
|
+
let exif;
|
|
164
|
+
let failure;
|
|
165
|
+
// Uncompressed 8-bit sRGB TIFF: lossless, cheap to reread, and readable through the allowed loaders.
|
|
166
|
+
const masterPath = join(input.workDir, `master-${process.pid}.tif`);
|
|
167
|
+
try {
|
|
168
|
+
const probed = await probe(input.sourcePath, input.mime, input.maxPixels);
|
|
169
|
+
source = { width: probed.width, height: probed.height };
|
|
170
|
+
exif = credits(probed.exif);
|
|
171
|
+
const size = masterSize(source.width, source.height, specs);
|
|
172
|
+
if (process.env.SANDCMS_PROCESSOR_TRACE)
|
|
173
|
+
appendFileSync(process.env.SANDCMS_PROCESSOR_TRACE, `decode ${input.sourcePath}\n`);
|
|
174
|
+
let image = open(input.sourcePath, input.maxPixels).autoOrient();
|
|
175
|
+
if (size.width !== source.width || size.height !== source.height)
|
|
176
|
+
image = image.resize(size.width, size.height, { fit: 'fill' });
|
|
177
|
+
const info = await image.toColourspace('srgb').tiff({ compression: 'none' }).toFile(masterPath);
|
|
178
|
+
master = { path: masterPath, width: info.width, height: info.height };
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
failure = classify(error).message;
|
|
182
|
+
}
|
|
183
|
+
for (let index = 0; index < specs.length; index++) {
|
|
184
|
+
const spec = specs[index];
|
|
185
|
+
const name = `${spec.preset}-${spec.size}.${spec.format}`;
|
|
186
|
+
if (!master || !source) {
|
|
187
|
+
await report(id, { index, error: failure ?? 'Image could not be decoded' });
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const tmpPath = join(input.workDir, `${name}.tmp`);
|
|
191
|
+
try {
|
|
192
|
+
const info = await encode(master, source, spec, exif, tmpPath);
|
|
193
|
+
await report(id, {
|
|
194
|
+
index,
|
|
195
|
+
tmpPath,
|
|
196
|
+
variant: {
|
|
197
|
+
key: `${input.id}/${input.version}/${name}`,
|
|
198
|
+
preset: spec.preset,
|
|
199
|
+
size: spec.size,
|
|
200
|
+
width: info.width,
|
|
201
|
+
height: info.height,
|
|
202
|
+
format: spec.format
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
await rm(tmpPath, { force: true });
|
|
208
|
+
await report(id, {
|
|
209
|
+
index,
|
|
210
|
+
error: error instanceof Error ? error.message.split('\n')[0] : String(error)
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
await rm(masterPath, { force: true });
|
|
215
|
+
}
|
|
216
|
+
async function handle(message) {
|
|
217
|
+
if (message.type === 'continue')
|
|
218
|
+
return resume?.();
|
|
219
|
+
try {
|
|
220
|
+
if (message.type === 'inspect') {
|
|
221
|
+
const { width, height } = await probe(message.path, message.mime, message.maxPixels);
|
|
222
|
+
await verifyDecode(message.path, message.maxPixels);
|
|
223
|
+
send({ type: 'result', id: message.id, value: { width, height } });
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
await render(message.id, message.input);
|
|
227
|
+
send({ type: 'result', id: message.id, value: null });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
const rejection = classify(error);
|
|
232
|
+
send({ type: 'error', id: message.id, status: rejection.status, message: rejection.message });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
process.on('message', (message) => void handle(message));
|
|
236
|
+
send({ type: 'ready' });
|