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,24 @@
|
|
|
1
|
+
import { type MediaProcessor, type MediaStorage, type UploadTicketResponse } from './contracts.js';
|
|
1
2
|
import type { Store } from '../db/store.js';
|
|
2
3
|
import type { Content } from '../content.js';
|
|
3
4
|
import type { Field, Payload, SiteDefinition } from '../../blueprints/types.js';
|
|
4
|
-
export
|
|
5
|
+
export interface MediaOptions {
|
|
6
|
+
secret: string;
|
|
7
|
+
/** Public origin; chunk URLs are absolute. */
|
|
8
|
+
origin: string;
|
|
9
|
+
mediaDir: string;
|
|
10
|
+
processor?: MediaProcessor;
|
|
11
|
+
storage?: MediaStorage;
|
|
12
|
+
/** @deprecated 0.7.0: only read by `sandcms media import-r2`. */
|
|
13
|
+
workerUrl?: string;
|
|
14
|
+
/** Internal timings in ms, shortened by tests. */
|
|
15
|
+
timing?: {
|
|
16
|
+
heartbeat?: number;
|
|
17
|
+
chunkIdle?: number;
|
|
18
|
+
chunkDeadline?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare function createMedia(store: Store, definition: SiteDefinition, content: Content, options: MediaOptions): {
|
|
5
22
|
get: (id: string) => {
|
|
6
23
|
blueprint: string;
|
|
7
24
|
metadata: Payload;
|
|
@@ -16,6 +33,9 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
16
33
|
error: string | null;
|
|
17
34
|
createdAt: number;
|
|
18
35
|
searchText: string | null;
|
|
36
|
+
width: number | null;
|
|
37
|
+
height: number | null;
|
|
38
|
+
uploadAttempt: string | null;
|
|
19
39
|
} | undefined;
|
|
20
40
|
upload: (file: File, blueprintId?: string) => Promise<{
|
|
21
41
|
blueprint: string;
|
|
@@ -31,18 +51,18 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
31
51
|
error: string | null;
|
|
32
52
|
createdAt: number;
|
|
33
53
|
searchText: string | null;
|
|
54
|
+
width: number | null;
|
|
55
|
+
height: number | null;
|
|
56
|
+
uploadAttempt: string | null;
|
|
34
57
|
}>;
|
|
35
58
|
initiateUpload: (input: {
|
|
36
59
|
name: string;
|
|
37
60
|
type: string;
|
|
38
61
|
size: number;
|
|
39
|
-
}, blueprintId
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
44
|
-
}>;
|
|
45
|
-
completeUpload: (id: string) => {
|
|
62
|
+
}, blueprintId: string | undefined, principal: {
|
|
63
|
+
userId: string;
|
|
64
|
+
}) => Promise<UploadTicketResponse>;
|
|
65
|
+
completeUpload: (id: string) => Promise<{
|
|
46
66
|
blueprint: string;
|
|
47
67
|
metadata: Payload;
|
|
48
68
|
id: string;
|
|
@@ -56,9 +76,20 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
56
76
|
error: string | null;
|
|
57
77
|
createdAt: number;
|
|
58
78
|
searchText: string | null;
|
|
59
|
-
|
|
79
|
+
width: number | null;
|
|
80
|
+
height: number | null;
|
|
81
|
+
uploadAttempt: string | null;
|
|
82
|
+
}>;
|
|
60
83
|
tick: () => Promise<boolean>;
|
|
61
84
|
drain: () => Promise<void>;
|
|
85
|
+
receiveChunk: (id: string, index: number, body: ReadableStream<Uint8Array>, ticketToken: string | null, userId: string) => Promise<{
|
|
86
|
+
received: number[];
|
|
87
|
+
}>;
|
|
88
|
+
uploadStatus: (id: string, ticketToken: string | null, userId: string) => Promise<{
|
|
89
|
+
received: number[];
|
|
90
|
+
chunkSize: number;
|
|
91
|
+
complete: boolean;
|
|
92
|
+
}>;
|
|
62
93
|
retry: (id: string) => void;
|
|
63
94
|
version: string;
|
|
64
95
|
versionForAsset: (asset: NonNullable<ReturnType<(id: string) => {
|
|
@@ -75,7 +106,12 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
75
106
|
error: string | null;
|
|
76
107
|
createdAt: number;
|
|
77
108
|
searchText: string | null;
|
|
109
|
+
width: number | null;
|
|
110
|
+
height: number | null;
|
|
111
|
+
uploadAttempt: string | null;
|
|
78
112
|
} | undefined>>) => string;
|
|
113
|
+
storage: MediaStorage;
|
|
114
|
+
/** @deprecated 0.7.0: media no longer uses the Worker; kept for `sandcms media import-r2`. */
|
|
79
115
|
workerUrl: string;
|
|
80
116
|
list: () => {
|
|
81
117
|
blueprint: string;
|
|
@@ -91,10 +127,15 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
91
127
|
error: string | null;
|
|
92
128
|
createdAt: number;
|
|
93
129
|
searchText: string | null;
|
|
130
|
+
width: number | null;
|
|
131
|
+
height: number | null;
|
|
132
|
+
uploadAttempt: string | null;
|
|
94
133
|
}[];
|
|
95
134
|
updateMetadata: (id: string, input: unknown) => void;
|
|
96
135
|
health(): Promise<{
|
|
97
|
-
|
|
136
|
+
bytes: number;
|
|
137
|
+
free: number;
|
|
138
|
+
storage: "local";
|
|
98
139
|
}>;
|
|
99
140
|
rebuild: () => void;
|
|
100
141
|
remove(id: string): Promise<void>;
|
|
@@ -111,6 +152,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
111
152
|
width: number;
|
|
112
153
|
height: number;
|
|
113
154
|
format: string;
|
|
155
|
+
etag?: string;
|
|
114
156
|
}[];
|
|
115
157
|
photographer: {
|
|
116
158
|
id: string;
|
|
@@ -130,6 +172,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
130
172
|
width: number;
|
|
131
173
|
height: number;
|
|
132
174
|
format: string;
|
|
175
|
+
etag?: string;
|
|
133
176
|
}[];
|
|
134
177
|
photographer: {
|
|
135
178
|
id: string;
|
|
@@ -153,6 +196,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
153
196
|
width: number;
|
|
154
197
|
height: number;
|
|
155
198
|
format: string;
|
|
199
|
+
etag?: string;
|
|
156
200
|
}[];
|
|
157
201
|
photographer: {
|
|
158
202
|
id: string;
|
|
@@ -176,6 +220,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
176
220
|
width: number;
|
|
177
221
|
height: number;
|
|
178
222
|
format: string;
|
|
223
|
+
etag?: string;
|
|
179
224
|
}[];
|
|
180
225
|
photographer: {
|
|
181
226
|
id: string;
|
|
@@ -183,6 +228,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
183
228
|
} | null;
|
|
184
229
|
};
|
|
185
230
|
}[];
|
|
231
|
+
/** Background work: one render loop (one decode at a time) plus deletions, expiry and upload validation. */
|
|
186
232
|
start: () => () => void;
|
|
187
233
|
};
|
|
188
234
|
export type MediaService = ReturnType<typeof createMedia>;
|
|
@@ -1,22 +1,77 @@
|
|
|
1
|
+
import { closeSync, createReadStream, fstatSync, openSync } from 'node:fs';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
1
3
|
import { isLinkValue } from '../../link.js';
|
|
2
4
|
import { effectiveMetadata, fileFocus, getFileBlueprint, publicMetadata } from '../../blueprints/files.js';
|
|
3
5
|
import { validatePayload, walkValues } from '../../blueprints/validate.js';
|
|
4
6
|
import { mergeDraft } from '../../blueprints/merge.js';
|
|
5
|
-
import { createRunner } from './runner.js';
|
|
7
|
+
import { createRunner, liveUploadSession, processorQueue, variantKey } from './runner.js';
|
|
6
8
|
import { createDeletionRunner } from './deletions.js';
|
|
9
|
+
import { createLocalStorage } from './storage.js';
|
|
10
|
+
import { MediaRequestError, UPLOAD_CHUNK_SIZE, UPLOAD_TICKET_HEADER } from './contracts.js';
|
|
11
|
+
import { signUploadTicket, verifyUploadTicket } from './upload-ticket.js';
|
|
7
12
|
import { assetPresetVersion, presetVersion } from './presets.js';
|
|
8
|
-
import { and, eq, desc, isNotNull } from 'drizzle-orm';
|
|
13
|
+
import { and, eq, desc, inArray, isNotNull, sql } from 'drizzle-orm';
|
|
9
14
|
import { media, mediaDeletions, jobs, uploads, uploadSessions, references, documents } from '../db/schema.js';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const mimeTypes = [
|
|
16
|
+
'image/jpeg',
|
|
17
|
+
'image/png',
|
|
18
|
+
'image/webp',
|
|
19
|
+
'image/avif',
|
|
20
|
+
'image/gif',
|
|
21
|
+
'image/tiff',
|
|
22
|
+
'image/svg+xml'
|
|
23
|
+
];
|
|
24
|
+
const contentTypes = {
|
|
25
|
+
webp: 'image/webp',
|
|
26
|
+
avif: 'image/avif',
|
|
27
|
+
jpeg: 'image/jpeg',
|
|
28
|
+
svg: 'image/svg+xml'
|
|
29
|
+
};
|
|
30
|
+
const sessionTimeout = 15 * 60_000,
|
|
31
|
+
// A ticket outlives the idle timeout so a slow but active upload can finish.
|
|
32
|
+
ticketLifetime = 24 * 60 * 60_000, chunkLease = 120_000, diskMargin = 5_000_000_000;
|
|
33
|
+
/**
|
|
34
|
+
* Pass a request body through, failing with 408 when no bytes arrive for `idle` ms or the whole body takes
|
|
35
|
+
* longer than `deadline` ms, so a half-open connection cannot hold an upload lease forever.
|
|
36
|
+
*/
|
|
37
|
+
function guardStream(body, idle, deadline) {
|
|
38
|
+
const reader = body.getReader(), end = Date.now() + deadline;
|
|
39
|
+
return new ReadableStream({
|
|
40
|
+
async pull(controller) {
|
|
41
|
+
let timer;
|
|
42
|
+
const wait = Math.min(idle, end - Date.now());
|
|
43
|
+
const stalled = new Promise((_, reject) => {
|
|
44
|
+
timer = setTimeout(() => reject(new MediaRequestError(408, 'Chunk upload stalled. Send this chunk again.')), Math.max(0, wait));
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
const { done, value } = await Promise.race([reader.read(), stalled]);
|
|
48
|
+
if (done)
|
|
49
|
+
controller.close();
|
|
50
|
+
else
|
|
51
|
+
controller.enqueue(value);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
void reader.cancel(error).catch(() => { });
|
|
55
|
+
controller.error(error);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
cancel: (reason) => reader.cancel(reason)
|
|
62
|
+
});
|
|
16
63
|
}
|
|
17
|
-
export function createMedia(store, definition, content,
|
|
64
|
+
export function createMedia(store, definition, content, options) {
|
|
18
65
|
const { db } = store, settings = definition.config.media;
|
|
19
66
|
const version = presetVersion(definition);
|
|
67
|
+
const storage = options.storage ?? createLocalStorage(options.mediaDir);
|
|
68
|
+
const origin = new URL(options.origin).origin;
|
|
69
|
+
let loaded;
|
|
70
|
+
// sharp is loaded only when an image is processed, so tests and CLI commands can inject a fake.
|
|
71
|
+
const loadProcessor = () => (loaded ??= options.processor
|
|
72
|
+
? Promise.resolve(options.processor)
|
|
73
|
+
: import('./processor.js').then((m) => m.createProcessor()));
|
|
74
|
+
const processor = processorQueue(loadProcessor);
|
|
20
75
|
const get = (id) => db.select().from(media).where(eq(media.id, id)).get();
|
|
21
76
|
// Only published snapshots grant access; draft edits never change public visibility.
|
|
22
77
|
const isPublic = (id) => !!db
|
|
@@ -25,18 +80,6 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
25
80
|
.innerJoin(documents, eq(documents.id, references.documentId))
|
|
26
81
|
.where(and(eq(references.targetId, id), eq(references.kind, 'media'), eq(references.snapshot, 'published'), isNotNull(documents.published)))
|
|
27
82
|
.get();
|
|
28
|
-
async function worker(path, init = {}) {
|
|
29
|
-
if (!token)
|
|
30
|
-
throw new Error('Configure SANDCMS_WORKER_TOKEN before uploading media');
|
|
31
|
-
const response = await fetch(workerUrl.replace(/\/$/, '') + '/internal/' + path, {
|
|
32
|
-
...init,
|
|
33
|
-
headers: { ...init.headers, authorization: `Bearer ${token}`, 'x-sandcms-protocol': '6' },
|
|
34
|
-
signal: init.signal ?? AbortSignal.timeout(60_000)
|
|
35
|
-
});
|
|
36
|
-
if (!response.ok && !(path.startsWith('media/') && response.status === 304))
|
|
37
|
-
throw new WorkerResponseError(response.status, (await response.text().catch(() => '')).slice(0, 300));
|
|
38
|
-
return response;
|
|
39
|
-
}
|
|
40
83
|
const assetVersion = (asset) => assetPresetVersion(definition, asset);
|
|
41
84
|
function enqueue(id, mime) {
|
|
42
85
|
const asset = get(id);
|
|
@@ -88,7 +131,7 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
88
131
|
.where(eq(media.id, id))
|
|
89
132
|
.run();
|
|
90
133
|
}
|
|
91
|
-
async function
|
|
134
|
+
async function createUpload(input, blueprintId) {
|
|
92
135
|
const blueprint = getFileBlueprint(definition, blueprintId);
|
|
93
136
|
if (!blueprint.accept.includes(input.type))
|
|
94
137
|
throw new Error(`Choose a file accepted by ${blueprint.label}`);
|
|
@@ -96,23 +139,23 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
96
139
|
throw new Error(`Choose an image under ${settings.uploadLimit / 1_000_000} MB`);
|
|
97
140
|
if (input.type === 'image/svg+xml' && input.size > 1_000_000)
|
|
98
141
|
throw new Error('Choose an SVG under 1 MB');
|
|
99
|
-
if (!
|
|
100
|
-
'image/jpeg',
|
|
101
|
-
'image/png',
|
|
102
|
-
'image/webp',
|
|
103
|
-
'image/avif',
|
|
104
|
-
'image/gif',
|
|
105
|
-
'image/svg+xml'
|
|
106
|
-
].includes(input.type))
|
|
142
|
+
if (!mimeTypes.includes(input.type))
|
|
107
143
|
throw new Error('Choose a raster image or SVG');
|
|
108
144
|
if (typeof input.name !== 'string' || !input.name.trim())
|
|
109
145
|
throw new Error('Choose a file');
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
146
|
+
// Reserve room for the chunks, the assembled file and its variants, plus a fixed margin.
|
|
147
|
+
const reserved = db
|
|
148
|
+
.select({ bytes: sql `coalesce(sum(${media.bytes}), 0)` })
|
|
149
|
+
.from(uploadSessions)
|
|
150
|
+
.innerJoin(media, eq(media.id, uploadSessions.mediaId))
|
|
151
|
+
.where(liveUploadSession())
|
|
152
|
+
.get()?.bytes ?? 0;
|
|
153
|
+
const usage = await storage.usage();
|
|
154
|
+
if (usage.free < 3 * input.size + reserved + diskMargin)
|
|
155
|
+
throw new MediaRequestError(507, 'Not enough disk space for this upload');
|
|
156
|
+
if (settings.maxStorage && usage.bytes + reserved + input.size > settings.maxStorage)
|
|
157
|
+
throw new MediaRequestError(413, 'Media storage quota exceeded');
|
|
158
|
+
const id = crypto.randomUUID(), attempt = crypto.randomUUID(), expiresAt = Date.now() + sessionTimeout;
|
|
116
159
|
db.transaction(() => {
|
|
117
160
|
db.insert(media)
|
|
118
161
|
.values({
|
|
@@ -126,11 +169,112 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
126
169
|
createdAt: Date.now()
|
|
127
170
|
})
|
|
128
171
|
.run();
|
|
129
|
-
db.insert(uploadSessions).values({ mediaId: id, attempt, expiresAt
|
|
172
|
+
db.insert(uploadSessions).values({ mediaId: id, attempt, expiresAt }).run();
|
|
173
|
+
});
|
|
174
|
+
return { id, attempt, expiresAt };
|
|
175
|
+
}
|
|
176
|
+
async function initiateUpload(input, blueprintId, principal) {
|
|
177
|
+
const { id, attempt, expiresAt } = await createUpload(input, blueprintId);
|
|
178
|
+
const ticket = signUploadTicket(options.secret, {
|
|
179
|
+
id,
|
|
180
|
+
attempt,
|
|
181
|
+
mime: input.type,
|
|
182
|
+
bytes: input.size,
|
|
183
|
+
sub: principal.userId,
|
|
184
|
+
exp: Date.now() + ticketLifetime
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
id,
|
|
188
|
+
url: `${origin}/api/cms/media/upload/${id}`,
|
|
189
|
+
chunkSize: UPLOAD_CHUNK_SIZE,
|
|
190
|
+
headers: { [UPLOAD_TICKET_HEADER]: ticket },
|
|
191
|
+
expiresAt
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function authorize(id, token, userId) {
|
|
195
|
+
const ticket = verifyUploadTicket(options.secret, token);
|
|
196
|
+
if (!ticket || ticket.id !== id || ticket.sub !== userId)
|
|
197
|
+
throw new MediaRequestError(403, 'Upload ticket is invalid or expired');
|
|
198
|
+
return ticket;
|
|
199
|
+
}
|
|
200
|
+
// A chunk holds the session lease while it streams and slides the idle expiry forward.
|
|
201
|
+
const streaming = new Map();
|
|
202
|
+
const holdSession = (id, attempt, release = false) => {
|
|
203
|
+
const now = Date.now();
|
|
204
|
+
db.update(uploadSessions)
|
|
205
|
+
.set({
|
|
206
|
+
status: 'receiving',
|
|
207
|
+
leaseUntil: release ? 0 : now + chunkLease,
|
|
208
|
+
expiresAt: sql `max(${uploadSessions.expiresAt}, ${now + sessionTimeout})`
|
|
209
|
+
})
|
|
210
|
+
.where(and(eq(uploadSessions.mediaId, id), eq(uploadSessions.attempt, attempt), inArray(uploadSessions.status, ['uploading', 'receiving'])))
|
|
211
|
+
.run();
|
|
212
|
+
};
|
|
213
|
+
function openSession(id, attempt) {
|
|
214
|
+
const asset = get(id);
|
|
215
|
+
const session = db.select().from(uploadSessions).where(eq(uploadSessions.mediaId, id)).get();
|
|
216
|
+
if (!asset || !session || session.attempt !== attempt)
|
|
217
|
+
throw new MediaRequestError(404, 'Upload not found. Upload this file again.');
|
|
218
|
+
if (asset.status === 'deleting' ||
|
|
219
|
+
db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, id)).get())
|
|
220
|
+
throw new MediaRequestError(409, 'This image is being deleted');
|
|
221
|
+
if (session.status === 'expired')
|
|
222
|
+
throw new MediaRequestError(410, 'Upload expired. Upload this file again.');
|
|
223
|
+
return { asset, session };
|
|
224
|
+
}
|
|
225
|
+
async function writeChunk(id, attempt, bytes, index, body) {
|
|
226
|
+
const count = Math.ceil(bytes / UPLOAD_CHUNK_SIZE);
|
|
227
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= count)
|
|
228
|
+
throw new MediaRequestError(400, 'Invalid chunk index');
|
|
229
|
+
const expected = index === count - 1 ? bytes - index * UPLOAD_CHUNK_SIZE : UPLOAD_CHUNK_SIZE;
|
|
230
|
+
db.transaction(() => {
|
|
231
|
+
const { session } = openSession(id, attempt);
|
|
232
|
+
const now = Date.now();
|
|
233
|
+
if (session.status !== 'uploading' && session.status !== 'receiving')
|
|
234
|
+
throw new MediaRequestError(409, 'This upload is already complete');
|
|
235
|
+
if (session.expiresAt <= now && session.leaseUntil <= now)
|
|
236
|
+
throw new MediaRequestError(410, 'Upload expired. Upload this file again.');
|
|
237
|
+
holdSession(id, attempt);
|
|
130
238
|
});
|
|
131
|
-
|
|
239
|
+
streaming.set(id, (streaming.get(id) ?? 0) + 1);
|
|
240
|
+
const keepAlive = setInterval(() => holdSession(id, attempt), chunkLease / 4);
|
|
241
|
+
try {
|
|
242
|
+
await storage.writeChunk(id, attempt, index, guardStream(body, options.timing?.chunkIdle ?? 60_000, options.timing?.chunkDeadline ?? 15 * 60_000), expected, true);
|
|
243
|
+
}
|
|
244
|
+
finally {
|
|
245
|
+
clearInterval(keepAlive);
|
|
246
|
+
const left = streaming.get(id) - 1;
|
|
247
|
+
if (left)
|
|
248
|
+
streaming.set(id, left);
|
|
249
|
+
else
|
|
250
|
+
streaming.delete(id);
|
|
251
|
+
// Nothing is in flight once the last chunk lands; the sliding expiry covers idle time.
|
|
252
|
+
holdSession(id, attempt, !left);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async function receiveChunk(id, index, body, ticketToken, userId) {
|
|
256
|
+
const ticket = authorize(id, ticketToken, userId);
|
|
257
|
+
await writeChunk(id, ticket.attempt, ticket.bytes, index, body);
|
|
258
|
+
return { received: await storage.listChunks(id, ticket.attempt) };
|
|
259
|
+
}
|
|
260
|
+
/** Stored chunks for resume; `complete` once the chunks were assembled (e.g. the completion response was lost). */
|
|
261
|
+
async function uploadStatus(id, ticketToken, userId) {
|
|
262
|
+
const ticket = authorize(id, ticketToken, userId);
|
|
263
|
+
const asset = get(id), session = db.select().from(uploadSessions).where(eq(uploadSessions.mediaId, id)).get();
|
|
264
|
+
if (asset &&
|
|
265
|
+
asset.status !== 'deleting' &&
|
|
266
|
+
(session
|
|
267
|
+
? session.attempt === ticket.attempt && session.status === 'pending'
|
|
268
|
+
: asset.uploadAttempt === ticket.attempt))
|
|
269
|
+
return { received: [], chunkSize: UPLOAD_CHUNK_SIZE, complete: true };
|
|
270
|
+
openSession(id, ticket.attempt);
|
|
271
|
+
return {
|
|
272
|
+
received: await storage.listChunks(id, ticket.attempt),
|
|
273
|
+
chunkSize: UPLOAD_CHUNK_SIZE,
|
|
274
|
+
complete: false
|
|
275
|
+
};
|
|
132
276
|
}
|
|
133
|
-
function completeUpload(id) {
|
|
277
|
+
async function completeUpload(id) {
|
|
134
278
|
const asset = get(id);
|
|
135
279
|
if (!asset)
|
|
136
280
|
throw new Error('Image not found');
|
|
@@ -145,38 +289,64 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
145
289
|
}
|
|
146
290
|
if (session.status === 'expired')
|
|
147
291
|
throw new Error('Upload expired. Upload this file again.');
|
|
148
|
-
if (session.status === '
|
|
149
|
-
|
|
150
|
-
|
|
292
|
+
if (session.status === 'pending')
|
|
293
|
+
return asset;
|
|
294
|
+
// Hold the lease while assembling so neither expiry nor deletion removes the chunks.
|
|
295
|
+
const claimed = db.transaction(() => {
|
|
296
|
+
const current = db.select().from(uploadSessions).where(eq(uploadSessions.mediaId, id)).get();
|
|
297
|
+
const now = Date.now();
|
|
298
|
+
if (!current || current.attempt !== session.attempt)
|
|
299
|
+
return 'gone';
|
|
300
|
+
if (current.status === 'pending')
|
|
301
|
+
return 'done';
|
|
302
|
+
if (current.status === 'expired' || (current.expiresAt < now && current.leaseUntil <= now))
|
|
303
|
+
return 'expired';
|
|
151
304
|
db.update(uploadSessions)
|
|
152
|
-
.set({
|
|
305
|
+
.set({ leaseUntil: now + chunkLease })
|
|
153
306
|
.where(eq(uploadSessions.mediaId, id))
|
|
154
307
|
.run();
|
|
308
|
+
return 'claimed';
|
|
309
|
+
});
|
|
310
|
+
if (claimed === 'done')
|
|
311
|
+
return asset;
|
|
312
|
+
if (claimed === 'gone')
|
|
313
|
+
throw new Error('Upload was interrupted. Upload this file again.');
|
|
314
|
+
if (claimed === 'expired')
|
|
315
|
+
throw new Error('Upload expired. Upload this file again.');
|
|
316
|
+
try {
|
|
317
|
+
await storage.assemble(id, session.attempt, asset.bytes);
|
|
318
|
+
}
|
|
319
|
+
catch (e) {
|
|
320
|
+
db.update(uploadSessions).set({ leaseUntil: 0 }).where(eq(uploadSessions.mediaId, id)).run();
|
|
321
|
+
throw e;
|
|
155
322
|
}
|
|
323
|
+
db.update(uploadSessions)
|
|
324
|
+
.set({ status: 'pending', leaseUntil: 0 })
|
|
325
|
+
.where(and(eq(uploadSessions.mediaId, id), eq(uploadSessions.attempt, session.attempt), inArray(uploadSessions.status, ['uploading', 'receiving'])))
|
|
326
|
+
.run();
|
|
156
327
|
return asset;
|
|
157
328
|
}
|
|
158
|
-
// Server callers
|
|
329
|
+
// Server callers stage the bytes directly and share the browser's durable finalization.
|
|
159
330
|
async function upload(file, blueprintId) {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
body: file,
|
|
165
|
-
signal: AbortSignal.timeout(120_000)
|
|
166
|
-
});
|
|
167
|
-
if (!response.ok)
|
|
168
|
-
throw new Error('Direct upload failed. Upload this file again.');
|
|
169
|
-
return completeUpload(signed.id);
|
|
331
|
+
const { id, attempt } = await createUpload(file, blueprintId);
|
|
332
|
+
for (let index = 0; index * UPLOAD_CHUNK_SIZE < file.size; index++)
|
|
333
|
+
await writeChunk(id, attempt, file.size, index, file.slice(index * UPLOAD_CHUNK_SIZE, (index + 1) * UPLOAD_CHUNK_SIZE).stream());
|
|
334
|
+
return completeUpload(id);
|
|
170
335
|
}
|
|
171
|
-
const deletePending = createDeletionRunner(store,
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
336
|
+
const deletePending = createDeletionRunner(store, storage);
|
|
337
|
+
const runner = createRunner(store, {
|
|
338
|
+
storage,
|
|
339
|
+
processor,
|
|
340
|
+
maxPixels: settings.maxPixels,
|
|
341
|
+
owner: crypto.randomUUID(),
|
|
342
|
+
heartbeat: options.timing?.heartbeat,
|
|
343
|
+
enqueue,
|
|
344
|
+
deletePending
|
|
345
|
+
});
|
|
346
|
+
const tick = async () => (await runner.maintain()) || (await runner.render());
|
|
175
347
|
async function drain() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
;
|
|
179
|
-
}));
|
|
348
|
+
while (await tick())
|
|
349
|
+
;
|
|
180
350
|
}
|
|
181
351
|
function retry(id) {
|
|
182
352
|
const asset = get(id);
|
|
@@ -303,14 +473,18 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
303
473
|
completeUpload,
|
|
304
474
|
tick,
|
|
305
475
|
drain,
|
|
476
|
+
receiveChunk,
|
|
477
|
+
uploadStatus,
|
|
306
478
|
retry,
|
|
307
479
|
version,
|
|
308
480
|
versionForAsset: assetVersion,
|
|
309
|
-
|
|
481
|
+
storage,
|
|
482
|
+
/** @deprecated 0.7.0: media no longer uses the Worker; kept for `sandcms media import-r2`. */
|
|
483
|
+
workerUrl: options.workerUrl ?? settings.workerUrl ?? '',
|
|
310
484
|
list: () => db.select().from(media).orderBy(desc(media.createdAt)).all(),
|
|
311
485
|
updateMetadata,
|
|
312
486
|
async health() {
|
|
313
|
-
return (await
|
|
487
|
+
return { storage: 'local', ...(await storage.usage()) };
|
|
314
488
|
},
|
|
315
489
|
rebuild: () => {
|
|
316
490
|
db.transaction(() => {
|
|
@@ -347,10 +521,7 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
347
521
|
// Persist intent before awaiting storage; processors retain their leases until finished.
|
|
348
522
|
db.transaction(() => {
|
|
349
523
|
content.clearReferences(id, 'media');
|
|
350
|
-
db.insert(mediaDeletions)
|
|
351
|
-
.values({ mediaId: id })
|
|
352
|
-
.onConflictDoNothing()
|
|
353
|
-
.run();
|
|
524
|
+
db.insert(mediaDeletions).values({ mediaId: id }).onConflictDoNothing().run();
|
|
354
525
|
db.update(media)
|
|
355
526
|
.set({
|
|
356
527
|
status: 'deleting',
|
|
@@ -367,59 +538,99 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
367
538
|
// Metadata only. Bytes still require an authenticated CMS session.
|
|
368
539
|
previewAsset: (id, overrides = {}) => assetData(id, overrides, true),
|
|
369
540
|
async deliver(key, method, preview = false, ifNoneMatch = null) {
|
|
541
|
+
const notFound = () => new Response('Not found', { status: 404 });
|
|
370
542
|
const asset = get(key.split('/')[0]);
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
543
|
+
const variant = asset?.activeVersion ? asset.variants.find((v) => v.key === key) : undefined;
|
|
544
|
+
if (!asset || !variant || (!preview && !isPublic(asset.id)))
|
|
545
|
+
return notFound();
|
|
546
|
+
// Rebuilds re-render live keys: a job that is not ready means the file is changing, so send no validator.
|
|
547
|
+
const version = key.split('/')[1];
|
|
548
|
+
const job = db
|
|
549
|
+
.select()
|
|
550
|
+
.from(jobs)
|
|
551
|
+
.where(and(eq(jobs.mediaId, asset.id), eq(jobs.version, version)))
|
|
552
|
+
.all()
|
|
553
|
+
.find((j) => (j.result?.key ?? variantKey(asset.id, j.version, j.spec)) === key);
|
|
554
|
+
const tag = job ? (job.status === 'ready' ? job.result?.etag : undefined) : variant.etag;
|
|
555
|
+
const path = storage.variantPath(key);
|
|
556
|
+
// Size and bytes come from one open file, even if a rebuild replaces the path meanwhile.
|
|
557
|
+
let fd;
|
|
375
558
|
try {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
});
|
|
380
|
-
// Publication can change while the remote read is in flight.
|
|
381
|
-
const current = get(asset.id);
|
|
382
|
-
if (!current?.variants.some((v) => v.key === key) || (!preview && !isPublic(asset.id))) {
|
|
383
|
-
await response.body?.cancel();
|
|
384
|
-
return new Response('Not found', { status: 404 });
|
|
385
|
-
}
|
|
386
|
-
return response;
|
|
559
|
+
if (!path)
|
|
560
|
+
return notFound();
|
|
561
|
+
fd = openSync(path, 'r');
|
|
387
562
|
}
|
|
388
|
-
catch
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
563
|
+
catch {
|
|
564
|
+
return notFound();
|
|
565
|
+
}
|
|
566
|
+
let size;
|
|
567
|
+
try {
|
|
568
|
+
size = fstatSync(fd).size;
|
|
392
569
|
}
|
|
570
|
+
catch {
|
|
571
|
+
closeSync(fd);
|
|
572
|
+
return notFound();
|
|
573
|
+
}
|
|
574
|
+
const headers = new Headers({
|
|
575
|
+
'Content-Type': contentTypes[variant.format] ?? 'application/octet-stream',
|
|
576
|
+
'Cache-Control': 'private, no-store',
|
|
577
|
+
'Content-Security-Policy': "default-src 'none'; style-src 'none'; sandbox",
|
|
578
|
+
'Cross-Origin-Resource-Policy': 'cross-origin',
|
|
579
|
+
'X-Content-Type-Options': 'nosniff'
|
|
580
|
+
});
|
|
581
|
+
if (tag)
|
|
582
|
+
headers.set('ETag', `"${tag}"`);
|
|
583
|
+
// GET/HEAD use weak comparison; a validator list may include quoted commas.
|
|
584
|
+
if (ifNoneMatch?.trim() === '*' ||
|
|
585
|
+
(tag &&
|
|
586
|
+
Array.from(ifNoneMatch?.matchAll(/(?:W\/)?"[^"]*"/g) ?? []).some(([value]) => value.replace(/^W\//, '') === `"${tag}"`))) {
|
|
587
|
+
closeSync(fd);
|
|
588
|
+
return new Response(null, { status: 304, headers });
|
|
589
|
+
}
|
|
590
|
+
headers.set('Content-Length', String(size));
|
|
591
|
+
if (method === 'HEAD' || !size) {
|
|
592
|
+
closeSync(fd);
|
|
593
|
+
return new Response(null, { headers });
|
|
594
|
+
}
|
|
595
|
+
const body = Readable.toWeb(createReadStream('', { fd, start: 0, end: size - 1, autoClose: true }));
|
|
596
|
+
return new Response(body, { headers });
|
|
393
597
|
},
|
|
394
598
|
publicGallery: (rows, field) => galleryData(rows, field),
|
|
395
599
|
previewGallery: (rows, field) => galleryData(rows, field, true),
|
|
600
|
+
/** Background work: one render loop (one decode at a time) plus deletions, expiry and upload validation. */
|
|
396
601
|
start: () => {
|
|
397
|
-
let
|
|
398
|
-
|
|
399
|
-
async function loop() {
|
|
400
|
-
if (stopped)
|
|
602
|
+
let stopped = false, swept = 0;
|
|
603
|
+
const running = new Set();
|
|
604
|
+
async function loop(name, step) {
|
|
605
|
+
if (stopped || running.has(name))
|
|
401
606
|
return;
|
|
402
|
-
|
|
607
|
+
running.add(name);
|
|
403
608
|
try {
|
|
404
|
-
while (!stopped && (await
|
|
405
|
-
|
|
406
|
-
void loop();
|
|
609
|
+
while (!stopped && (await step()))
|
|
610
|
+
;
|
|
407
611
|
}
|
|
408
612
|
catch (e) {
|
|
409
613
|
console.error('SandCMS media job failed', e);
|
|
410
614
|
}
|
|
411
615
|
finally {
|
|
412
|
-
|
|
616
|
+
running.delete(name);
|
|
413
617
|
}
|
|
414
618
|
}
|
|
415
619
|
const timer = setInterval(() => {
|
|
416
|
-
|
|
417
|
-
|
|
620
|
+
void loop('maintain', runner.maintain);
|
|
621
|
+
void loop('render', runner.render);
|
|
622
|
+
if (Date.now() - swept < 3_600_000)
|
|
623
|
+
return;
|
|
624
|
+
swept = Date.now();
|
|
625
|
+
storage
|
|
626
|
+
.sweepTmp(new Date(swept - 24 * 3_600_000))
|
|
627
|
+
.catch((e) => console.error('SandCMS media cleanup failed', e));
|
|
418
628
|
}, 1000);
|
|
419
629
|
timer.unref();
|
|
420
630
|
return () => {
|
|
421
631
|
stopped = true;
|
|
422
632
|
clearInterval(timer);
|
|
633
|
+
void loaded?.then((p) => p.close()).catch(() => { });
|
|
423
634
|
};
|
|
424
635
|
}
|
|
425
636
|
};
|