sandcms 0.5.2 → 0.6.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 +6 -4
- package/dist/admin/Account.svelte +4 -1
- package/dist/admin/ApiKeys.svelte +159 -0
- package/dist/admin/ApiKeys.svelte.d.ts +3 -0
- package/dist/admin/AssetAttachment.svelte +6 -4
- package/dist/admin/BlueprintFields.svelte +86 -0
- package/dist/admin/BlueprintFields.svelte.d.ts +15 -0
- package/dist/admin/Collection.svelte +40 -7
- package/dist/admin/Editor.svelte +57 -17
- package/dist/admin/Fields.svelte +217 -144
- package/dist/admin/Fields.svelte.d.ts +1 -0
- package/dist/admin/FocusPicker.svelte +136 -0
- package/dist/admin/FocusPicker.svelte.d.ts +11 -0
- package/dist/admin/Gallery.svelte +40 -34
- package/dist/admin/GlobalSearch.svelte +56 -0
- package/dist/admin/GlobalSearch.svelte.d.ts +3 -0
- package/dist/admin/Inbox.svelte +8 -5
- package/dist/admin/LinkField.svelte +116 -0
- package/dist/admin/LinkField.svelte.d.ts +17 -0
- package/dist/admin/MediaLibrary.svelte +41 -14
- package/dist/admin/MediaPicker.svelte +12 -2
- package/dist/admin/Navigation.svelte +62 -0
- package/dist/admin/Navigation.svelte.d.ts +7 -0
- package/dist/admin/NotificationSettings.svelte +2 -2
- package/dist/admin/SearchList.svelte +55 -0
- package/dist/admin/SearchList.svelte.d.ts +42 -0
- package/dist/admin/SelectField.svelte +3 -1
- package/dist/admin/SelectField.svelte.d.ts +1 -0
- package/dist/admin/Shell.svelte +16 -24
- package/dist/admin/UnsavedChanges.svelte +1 -1
- package/dist/admin/Users.svelte +11 -5
- package/dist/admin/client.js +2 -1
- package/dist/admin/generated/admin.css +145 -0
- package/dist/admin/navigation.d.ts +7 -0
- package/dist/admin/navigation.js +25 -0
- package/dist/admin/types.d.ts +5 -1
- package/dist/admin/uploads.js +27 -6
- package/dist/blueprints/conditions.d.ts +2 -0
- package/dist/blueprints/conditions.js +4 -0
- package/dist/blueprints/extends.d.ts +5 -0
- package/dist/blueprints/extends.js +76 -0
- package/dist/blueprints/files.d.ts +3 -1
- package/dist/blueprints/files.js +35 -2
- package/dist/blueprints/layout.d.ts +4 -0
- package/dist/blueprints/layout.js +12 -0
- package/dist/blueprints/load.d.ts +1 -1
- package/dist/blueprints/load.js +127 -12
- package/dist/blueprints/types.d.ts +38 -1
- package/dist/blueprints/validate.js +30 -6
- package/dist/cli/cloudflare.js +15 -3
- package/dist/cli/doctor.js +6 -3
- package/dist/cli/files.js +1 -1
- package/dist/cli/scaffold.js +8 -6
- package/dist/cli/setup.js +9 -2
- package/dist/cli/upgrade.js +2 -1
- package/dist/email-failure.d.ts +17 -0
- package/dist/email-failure.js +22 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/link.d.ts +17 -0
- package/dist/link.js +44 -0
- package/dist/notification-fields.js +1 -1
- package/dist/search.d.ts +8 -0
- package/dist/search.js +1 -0
- package/dist/server/access.d.ts +2 -0
- package/dist/server/access.js +59 -23
- package/dist/server/admin-load.d.ts +29 -2
- package/dist/server/admin-load.js +4 -2
- package/dist/server/api-access.d.ts +31 -0
- package/dist/server/api-access.js +52 -0
- package/dist/server/api-pagination.d.ts +34 -0
- package/dist/server/api-pagination.js +87 -0
- package/dist/server/api.js +224 -32
- package/dist/server/auth.d.ts +1150 -0
- package/dist/server/auth.js +11 -1
- package/dist/server/cms.d.ts +1208 -4
- package/dist/server/cms.js +2 -0
- package/dist/server/content.d.ts +17 -2
- package/dist/server/content.js +89 -14
- package/dist/server/db/migration-data.js +24 -0
- package/dist/server/db/migrations/0001_independent_password_resets.sql +3 -0
- package/dist/server/db/migrations/0002_email_failure_reason.sql +1 -0
- package/dist/server/db/migrations/0003_direct_r2_uploads.sql +10 -0
- package/dist/server/db/migrations/0004_agent_api_keys.sql +38 -0
- package/dist/server/db/migrations/0005_api_retention_and_pagination.sql +3 -0
- package/dist/server/db/migrations/0006_durable_media_deletion.sql +7 -0
- package/dist/server/db/migrations/meta/0001_snapshot.json +1197 -0
- package/dist/server/db/migrations/meta/0002_snapshot.json +1204 -0
- package/dist/server/db/migrations/meta/0003_snapshot.json +1281 -0
- package/dist/server/db/migrations/meta/0004_snapshot.json +1546 -0
- package/dist/server/db/migrations/meta/0005_snapshot.json +1572 -0
- package/dist/server/db/migrations/meta/0006_snapshot.json +1627 -0
- package/dist/server/db/migrations/meta/_journal.json +42 -0
- package/dist/server/db/schema.d.ts +742 -2
- package/dist/server/db/schema.js +77 -5
- package/dist/server/email/index.d.ts +11 -1
- package/dist/server/email/index.js +69 -6
- package/dist/server/forms.js +12 -2
- package/dist/server/hooks.d.ts +1 -1
- package/dist/server/hooks.js +37 -14
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/links.d.ts +5 -0
- package/dist/server/links.js +44 -0
- package/dist/server/media/deletions.d.ts +2 -0
- package/dist/server/media/deletions.js +58 -0
- package/dist/server/media/presets.d.ts +10 -2
- package/dist/server/media/presets.js +14 -2
- package/dist/server/media/runner.d.ts +1 -1
- package/dist/server/media/runner.js +105 -8
- package/dist/server/media/service.d.ts +41 -3
- package/dist/server/media/service.js +146 -33
- package/dist/server/notifications.d.ts +1 -0
- package/dist/server/notifications.js +4 -2
- package/dist/server/request.d.ts +2 -0
- package/dist/server/request.js +5 -0
- package/dist/server/search.d.ts +13 -0
- package/dist/server/search.js +97 -0
- package/dist/worker/index.d.ts +3 -3
- package/dist/worker/index.js +205 -64
- package/dist/worker/local.d.ts +5 -0
- package/dist/worker/local.js +57 -0
- package/dist/worker/source.d.ts +1 -1
- package/dist/worker/source.js +2 -2
- package/dist/worker/uploads.d.ts +22 -0
- package/dist/worker/uploads.js +46 -0
- package/docs/acceptance.md +1 -1
- package/docs/api.md +73 -0
- package/docs/cloudflare.md +35 -8
- package/docs/content.md +181 -5
- package/docs/deployment.md +29 -4
- package/docs/email.md +14 -10
- package/docs/forms.md +3 -1
- package/docs/media.md +34 -5
- package/docs/package.md +7 -5
- package/docs/releases.md +56 -1
- package/docs/upgrades.md +59 -3
- package/package.json +19 -10
- package/templates/hooks.server.ts +3 -3
- package/templates/integration.ts +2 -2
- package/templates/routes/admin/+page.server.ts +2 -2
- package/templates/routes/admin/+page.svelte +1 -1
- package/templates/routes/api/+server.ts +2 -2
- package/templates/sandcms/blueprints/collections/projects.yaml +16 -8
- package/templates/sandcms/blueprints/fields/seo.yaml +5 -0
- package/templates/sandcms/blueprints/files/image.yaml +1 -0
- package/templates/sandcms/blueprints/pages/home.yaml +3 -0
- package/templates/sandcms/config.yaml +1 -0
- package/templates/sandcms/worker/index.ts +1 -1
- package/templates/sandcms/worker/local.ts +1 -0
- package/templates/sandcms/worker/wrangler.jsonc +1 -2
|
@@ -1,19 +1,107 @@
|
|
|
1
|
-
import { and, eq, lte, lt, or } from 'drizzle-orm';
|
|
2
|
-
import { media, jobs, uploads } from '../db/schema.js';
|
|
1
|
+
import { and, eq, lte, lt, ne, notExists, or } from 'drizzle-orm';
|
|
2
|
+
import { media, mediaDeletions, jobs, uploads, uploadSessions } from '../db/schema.js';
|
|
3
3
|
const uploadTimeout = 120_000;
|
|
4
|
-
export function createRunner(store, worker, enqueue) {
|
|
4
|
+
export function createRunner(store, worker, enqueue, deletePending) {
|
|
5
5
|
const { db } = store;
|
|
6
|
+
const notDeleting = (id) => notExists(db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, id)));
|
|
6
7
|
let running = false;
|
|
7
8
|
async function tick() {
|
|
8
9
|
if (running)
|
|
9
10
|
return false;
|
|
10
11
|
running = true;
|
|
11
12
|
try {
|
|
13
|
+
if (await deletePending())
|
|
14
|
+
return true;
|
|
15
|
+
// Unfinished browser transfers expire without retaining file bytes in the database.
|
|
16
|
+
const expired = db.transaction(() => {
|
|
17
|
+
const pending = db
|
|
18
|
+
.select()
|
|
19
|
+
.from(uploadSessions)
|
|
20
|
+
.where(and(notDeleting(uploadSessions.mediaId), or(and(eq(uploadSessions.status, 'uploading'), lte(uploadSessions.expiresAt, Date.now())), eq(uploadSessions.status, 'expired')), lt(uploadSessions.attempts, 4), lte(uploadSessions.nextAttempt, Date.now()), lte(uploadSessions.leaseUntil, Date.now())))
|
|
21
|
+
.get();
|
|
22
|
+
if (!pending)
|
|
23
|
+
return null;
|
|
24
|
+
// Claim expiry before awaiting storage cleanup so completion cannot race it.
|
|
25
|
+
db.update(uploadSessions)
|
|
26
|
+
.set({
|
|
27
|
+
status: 'expired',
|
|
28
|
+
attempts: pending.attempts + 1,
|
|
29
|
+
leaseUntil: Date.now() + 90_000
|
|
30
|
+
})
|
|
31
|
+
.where(eq(uploadSessions.mediaId, pending.mediaId))
|
|
32
|
+
.run();
|
|
33
|
+
db.update(media)
|
|
34
|
+
.set({ status: 'failed', error: 'Upload expired. Upload this file again.' })
|
|
35
|
+
.where(eq(media.id, pending.mediaId))
|
|
36
|
+
.run();
|
|
37
|
+
return pending;
|
|
38
|
+
});
|
|
39
|
+
if (expired) {
|
|
40
|
+
try {
|
|
41
|
+
await worker('assets/' + expired.mediaId, { method: 'DELETE' });
|
|
42
|
+
db.update(uploadSessions)
|
|
43
|
+
.set({ attempts: 4, leaseUntil: 0 })
|
|
44
|
+
.where(eq(uploadSessions.mediaId, expired.mediaId))
|
|
45
|
+
.run();
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
db.update(uploadSessions)
|
|
49
|
+
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** expired.attempts * 2000 })
|
|
50
|
+
.where(eq(uploadSessions.mediaId, expired.mediaId))
|
|
51
|
+
.run();
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const session = db.transaction(() => {
|
|
56
|
+
const pending = db
|
|
57
|
+
.select()
|
|
58
|
+
.from(uploadSessions)
|
|
59
|
+
.where(and(notDeleting(uploadSessions.mediaId), eq(uploadSessions.status, 'pending'), lte(uploadSessions.nextAttempt, Date.now()), lte(uploadSessions.leaseUntil, Date.now()), lt(uploadSessions.attempts, 4)))
|
|
60
|
+
.get();
|
|
61
|
+
if (!pending)
|
|
62
|
+
return null;
|
|
63
|
+
db.update(uploadSessions)
|
|
64
|
+
.set({ leaseUntil: Date.now() + uploadTimeout + 30_000, attempts: pending.attempts + 1 })
|
|
65
|
+
.where(eq(uploadSessions.mediaId, pending.mediaId))
|
|
66
|
+
.run();
|
|
67
|
+
return pending;
|
|
68
|
+
});
|
|
69
|
+
if (session) {
|
|
70
|
+
const asset = db.select().from(media).where(eq(media.id, session.mediaId)).get();
|
|
71
|
+
if (!asset)
|
|
72
|
+
return true;
|
|
73
|
+
try {
|
|
74
|
+
await worker(`uploads/${asset.id}/complete`, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
signal: AbortSignal.timeout(uploadTimeout),
|
|
77
|
+
headers: { 'content-type': 'application/json' },
|
|
78
|
+
body: JSON.stringify({ attempt: session.attempt, mime: asset.mime, bytes: asset.bytes })
|
|
79
|
+
});
|
|
80
|
+
db.transaction(() => {
|
|
81
|
+
db.delete(uploadSessions).where(eq(uploadSessions.mediaId, asset.id)).run();
|
|
82
|
+
enqueue(asset.id, asset.mime);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
87
|
+
return true;
|
|
88
|
+
db.update(uploadSessions)
|
|
89
|
+
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** session.attempts * 2000 })
|
|
90
|
+
.where(eq(uploadSessions.mediaId, asset.id))
|
|
91
|
+
.run();
|
|
92
|
+
if (session.attempts >= 3)
|
|
93
|
+
db.update(media)
|
|
94
|
+
.set({ status: 'failed', error: e instanceof Error ? e.message : 'Upload failed' })
|
|
95
|
+
.where(eq(media.id, asset.id))
|
|
96
|
+
.run();
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
12
100
|
const original = db.transaction(() => {
|
|
13
101
|
const pending = db
|
|
14
102
|
.select()
|
|
15
103
|
.from(uploads)
|
|
16
|
-
.where(and(lte(uploads.nextAttempt, Date.now()), lte(uploads.leaseUntil, Date.now()), lt(uploads.attempts, 4)))
|
|
104
|
+
.where(and(notDeleting(uploads.mediaId), lte(uploads.nextAttempt, Date.now()), lte(uploads.leaseUntil, Date.now()), lt(uploads.attempts, 4)))
|
|
17
105
|
.get();
|
|
18
106
|
if (!pending || pending.attempts >= 4)
|
|
19
107
|
return null;
|
|
@@ -43,6 +131,8 @@ export function createRunner(store, worker, enqueue) {
|
|
|
43
131
|
});
|
|
44
132
|
}
|
|
45
133
|
catch (e) {
|
|
134
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
135
|
+
return true;
|
|
46
136
|
const message = e instanceof Error ? e.message : 'Upload failed';
|
|
47
137
|
db.update(uploads)
|
|
48
138
|
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** original.attempts * 2000 })
|
|
@@ -61,7 +151,7 @@ export function createRunner(store, worker, enqueue) {
|
|
|
61
151
|
const next = db
|
|
62
152
|
.select()
|
|
63
153
|
.from(jobs)
|
|
64
|
-
.where(or(and(eq(jobs.status, 'pending'), lte(jobs.nextAttempt, now)), and(eq(jobs.status, 'running'), lte(jobs.leaseUntil, now))))
|
|
154
|
+
.where(and(notDeleting(jobs.mediaId), or(and(eq(jobs.status, 'pending'), lte(jobs.nextAttempt, now)), and(eq(jobs.status, 'running'), lte(jobs.leaseUntil, now)))))
|
|
65
155
|
.limit(1)
|
|
66
156
|
.get();
|
|
67
157
|
if (!next)
|
|
@@ -81,8 +171,13 @@ export function createRunner(store, worker, enqueue) {
|
|
|
81
171
|
headers: { 'content-type': 'application/json' }
|
|
82
172
|
})).json());
|
|
83
173
|
db.transaction(() => {
|
|
174
|
+
const asset = db.select().from(media).where(eq(media.id, job.mediaId)).get();
|
|
175
|
+
if (!asset || asset.status === 'deleting') {
|
|
176
|
+
db.delete(jobs).where(eq(jobs.id, job.id)).run();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
84
179
|
db.update(jobs)
|
|
85
|
-
.set({ status: 'ready', result, error: null })
|
|
180
|
+
.set({ status: 'ready', result, error: null, leaseUntil: 0 })
|
|
86
181
|
.where(eq(jobs.id, job.id))
|
|
87
182
|
.run();
|
|
88
183
|
const set = db
|
|
@@ -90,7 +185,7 @@ export function createRunner(store, worker, enqueue) {
|
|
|
90
185
|
.from(jobs)
|
|
91
186
|
.where(and(eq(jobs.mediaId, job.mediaId), eq(jobs.version, job.version)))
|
|
92
187
|
.all();
|
|
93
|
-
if (set.every((j) => j.status === 'ready'))
|
|
188
|
+
if (set.length && set.every((j) => j.status === 'ready'))
|
|
94
189
|
db.update(media)
|
|
95
190
|
.set({
|
|
96
191
|
status: 'ready',
|
|
@@ -103,6 +198,8 @@ export function createRunner(store, worker, enqueue) {
|
|
|
103
198
|
});
|
|
104
199
|
}
|
|
105
200
|
catch (e) {
|
|
201
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, job.mediaId)).get())
|
|
202
|
+
return true;
|
|
106
203
|
const message = e instanceof Error ? e.message : 'Processing failed', failed = job.attempts >= 3;
|
|
107
204
|
db.update(jobs)
|
|
108
205
|
.set({
|
|
@@ -115,7 +212,7 @@ export function createRunner(store, worker, enqueue) {
|
|
|
115
212
|
if (failed)
|
|
116
213
|
db.update(media)
|
|
117
214
|
.set({ status: 'failed', error: message })
|
|
118
|
-
.where(and(eq(media.id, job.mediaId), eq(media.targetVersion, job.version)))
|
|
215
|
+
.where(and(eq(media.id, job.mediaId), eq(media.targetVersion, job.version), ne(media.status, 'deleting')))
|
|
119
216
|
.run();
|
|
120
217
|
}
|
|
121
218
|
return true;
|
|
@@ -9,7 +9,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
9
9
|
filename: string;
|
|
10
10
|
mime: string;
|
|
11
11
|
bytes: number;
|
|
12
|
-
status: "processing" | "ready" | "failed";
|
|
12
|
+
status: "processing" | "ready" | "failed" | "deleting";
|
|
13
13
|
activeVersion: string | null;
|
|
14
14
|
targetVersion: string;
|
|
15
15
|
variants: import("../db/schema.js").Variant[];
|
|
@@ -23,16 +23,54 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
23
23
|
filename: string;
|
|
24
24
|
mime: string;
|
|
25
25
|
bytes: number;
|
|
26
|
-
status: "processing" | "ready" | "failed";
|
|
26
|
+
status: "processing" | "ready" | "failed" | "deleting";
|
|
27
27
|
activeVersion: string | null;
|
|
28
28
|
targetVersion: string;
|
|
29
29
|
variants: import("../db/schema.js").Variant[];
|
|
30
30
|
error: string | null;
|
|
31
31
|
createdAt: number;
|
|
32
32
|
}>;
|
|
33
|
+
initiateUpload: (input: {
|
|
34
|
+
name: string;
|
|
35
|
+
type: string;
|
|
36
|
+
size: number;
|
|
37
|
+
}, blueprintId?: string) => Promise<{
|
|
38
|
+
url: string;
|
|
39
|
+
headers: Record<string, string>;
|
|
40
|
+
expiresAt: number;
|
|
41
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
42
|
+
}>;
|
|
43
|
+
completeUpload: (id: string) => {
|
|
44
|
+
blueprint: string;
|
|
45
|
+
metadata: Payload;
|
|
46
|
+
id: string;
|
|
47
|
+
filename: string;
|
|
48
|
+
mime: string;
|
|
49
|
+
bytes: number;
|
|
50
|
+
status: "processing" | "ready" | "failed" | "deleting";
|
|
51
|
+
activeVersion: string | null;
|
|
52
|
+
targetVersion: string;
|
|
53
|
+
variants: import("../db/schema.js").Variant[];
|
|
54
|
+
error: string | null;
|
|
55
|
+
createdAt: number;
|
|
56
|
+
};
|
|
33
57
|
tick: () => Promise<boolean>;
|
|
34
58
|
retry: (id: string) => void;
|
|
35
59
|
version: string;
|
|
60
|
+
versionForAsset: (asset: NonNullable<ReturnType<(id: string) => {
|
|
61
|
+
blueprint: string;
|
|
62
|
+
metadata: Payload;
|
|
63
|
+
id: string;
|
|
64
|
+
filename: string;
|
|
65
|
+
mime: string;
|
|
66
|
+
bytes: number;
|
|
67
|
+
status: "processing" | "ready" | "failed" | "deleting";
|
|
68
|
+
activeVersion: string | null;
|
|
69
|
+
targetVersion: string;
|
|
70
|
+
variants: import("../db/schema.js").Variant[];
|
|
71
|
+
error: string | null;
|
|
72
|
+
createdAt: number;
|
|
73
|
+
} | undefined>>) => string;
|
|
36
74
|
workerUrl: string;
|
|
37
75
|
list: () => {
|
|
38
76
|
blueprint: string;
|
|
@@ -41,7 +79,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
41
79
|
filename: string;
|
|
42
80
|
mime: string;
|
|
43
81
|
bytes: number;
|
|
44
|
-
status: "processing" | "ready" | "failed";
|
|
82
|
+
status: "processing" | "ready" | "failed" | "deleting";
|
|
45
83
|
activeVersion: string | null;
|
|
46
84
|
targetVersion: string;
|
|
47
85
|
variants: import("../db/schema.js").Variant[];
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isLinkValue } from '../../link.js';
|
|
2
|
+
import { effectiveMetadata, fileFocus, getFileBlueprint, publicMetadata } from '../../blueprints/files.js';
|
|
2
3
|
import { validatePayload, walkValues } from '../../blueprints/validate.js';
|
|
3
4
|
import { mergeDraft } from '../../blueprints/merge.js';
|
|
4
5
|
import { createRunner } from './runner.js';
|
|
5
|
-
import {
|
|
6
|
+
import { createDeletionRunner } from './deletions.js';
|
|
7
|
+
import { assetPresetVersion, presetVersion } from './presets.js';
|
|
6
8
|
import { and, eq, desc, isNotNull } from 'drizzle-orm';
|
|
7
|
-
import { media, jobs, uploads, references, documents } from '../db/schema.js';
|
|
9
|
+
import { media, mediaDeletions, jobs, uploads, uploadSessions, references, documents } from '../db/schema.js';
|
|
8
10
|
class WorkerResponseError extends Error {
|
|
9
11
|
status;
|
|
10
12
|
constructor(status, detail) {
|
|
@@ -28,14 +30,20 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
28
30
|
throw new Error('Configure SANDCMS_WORKER_TOKEN before uploading media');
|
|
29
31
|
const response = await fetch(workerUrl.replace(/\/$/, '') + '/internal/' + path, {
|
|
30
32
|
...init,
|
|
31
|
-
headers: { ...init.headers, authorization: `Bearer ${token}`, 'x-sandcms-protocol': '
|
|
33
|
+
headers: { ...init.headers, authorization: `Bearer ${token}`, 'x-sandcms-protocol': '5' },
|
|
32
34
|
signal: init.signal ?? AbortSignal.timeout(60_000)
|
|
33
35
|
});
|
|
34
36
|
if (!response.ok && !(path.startsWith('media/') && response.status === 304))
|
|
35
37
|
throw new WorkerResponseError(response.status, (await response.text().catch(() => '')).slice(0, 300));
|
|
36
38
|
return response;
|
|
37
39
|
}
|
|
40
|
+
const assetVersion = (asset) => assetPresetVersion(definition, asset);
|
|
38
41
|
function enqueue(id, mime) {
|
|
42
|
+
const asset = get(id);
|
|
43
|
+
if (!asset || asset.status === 'deleting')
|
|
44
|
+
return;
|
|
45
|
+
const version = assetVersion(asset);
|
|
46
|
+
const focus = fileFocus(getFileBlueprint(definition, asset.blueprint), asset.metadata);
|
|
39
47
|
const existing = db
|
|
40
48
|
.select()
|
|
41
49
|
.from(jobs)
|
|
@@ -65,8 +73,14 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
65
73
|
preset,
|
|
66
74
|
size,
|
|
67
75
|
format,
|
|
68
|
-
quality: quality
|
|
76
|
+
quality: quality,
|
|
77
|
+
...(p.aspectRatio
|
|
78
|
+
? { aspectRatio: p.aspectRatio, ...(focus ? { focus } : {}) }
|
|
79
|
+
: {})
|
|
69
80
|
}))));
|
|
81
|
+
if (mime !== 'image/svg+xml' &&
|
|
82
|
+
Object.values(getFileBlueprint(definition, asset.blueprint).fields).some((f) => f.type === 'focus'))
|
|
83
|
+
specs.push({ preset: 'sandcms-preview', size: 1600, format: 'webp', quality: 85 });
|
|
70
84
|
for (const spec of specs)
|
|
71
85
|
db.insert(jobs).values({ id: crypto.randomUUID(), mediaId: id, version, spec }).run();
|
|
72
86
|
db.update(media)
|
|
@@ -74,12 +88,14 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
74
88
|
.where(eq(media.id, id))
|
|
75
89
|
.run();
|
|
76
90
|
}
|
|
77
|
-
async function
|
|
91
|
+
async function initiateUpload(input, blueprintId) {
|
|
78
92
|
const blueprint = getFileBlueprint(definition, blueprintId);
|
|
79
|
-
if (!blueprint.accept.includes(
|
|
93
|
+
if (!blueprint.accept.includes(input.type))
|
|
80
94
|
throw new Error(`Choose a file accepted by ${blueprint.label}`);
|
|
81
|
-
if (!
|
|
95
|
+
if (!Number.isSafeInteger(input.size) || input.size < 1 || input.size > settings.uploadLimit)
|
|
82
96
|
throw new Error(`Choose an image under ${settings.uploadLimit / 1_000_000} MB`);
|
|
97
|
+
if (input.type === 'image/svg+xml' && input.size > 1_000_000)
|
|
98
|
+
throw new Error('Choose an SVG under 1 MB');
|
|
83
99
|
if (![
|
|
84
100
|
'image/jpeg',
|
|
85
101
|
'image/png',
|
|
@@ -87,32 +103,95 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
87
103
|
'image/avif',
|
|
88
104
|
'image/gif',
|
|
89
105
|
'image/svg+xml'
|
|
90
|
-
].includes(
|
|
106
|
+
].includes(input.type))
|
|
91
107
|
throw new Error('Choose a raster image or SVG');
|
|
92
|
-
|
|
108
|
+
if (typeof input.name !== 'string' || !input.name.trim())
|
|
109
|
+
throw new Error('Choose a file');
|
|
110
|
+
const id = crypto.randomUUID(), attempt = crypto.randomUUID();
|
|
111
|
+
const signed = (await (await worker(`uploads/${id}/sign`, {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: { 'content-type': 'application/json' },
|
|
114
|
+
body: JSON.stringify({ attempt, mime: input.type, bytes: input.size })
|
|
115
|
+
})).json());
|
|
93
116
|
db.transaction(() => {
|
|
94
117
|
db.insert(media)
|
|
95
118
|
.values({
|
|
96
119
|
id,
|
|
97
|
-
filename:
|
|
120
|
+
filename: input.name.slice(0, 255),
|
|
98
121
|
blueprint: blueprint.id,
|
|
99
|
-
mime:
|
|
100
|
-
bytes:
|
|
122
|
+
mime: input.type,
|
|
123
|
+
bytes: input.size,
|
|
101
124
|
status: 'processing',
|
|
102
125
|
targetVersion: version,
|
|
103
126
|
createdAt: Date.now()
|
|
104
127
|
})
|
|
105
128
|
.run();
|
|
106
|
-
db.insert(
|
|
129
|
+
db.insert(uploadSessions).values({ mediaId: id, attempt, expiresAt: signed.expiresAt }).run();
|
|
130
|
+
});
|
|
131
|
+
return { id, ...signed };
|
|
132
|
+
}
|
|
133
|
+
function completeUpload(id) {
|
|
134
|
+
const asset = get(id);
|
|
135
|
+
if (!asset)
|
|
136
|
+
throw new Error('Image not found');
|
|
137
|
+
if (asset.status === 'deleting')
|
|
138
|
+
throw new Error('This image is being deleted');
|
|
139
|
+
const session = db.select().from(uploadSessions).where(eq(uploadSessions.mediaId, id)).get();
|
|
140
|
+
if (!session) {
|
|
141
|
+
if (asset.activeVersion ||
|
|
142
|
+
db.select({ id: jobs.id }).from(jobs).where(eq(jobs.mediaId, id)).get())
|
|
143
|
+
return asset;
|
|
144
|
+
throw new Error('Upload was interrupted. Upload this file again.');
|
|
145
|
+
}
|
|
146
|
+
if (session.status === 'expired')
|
|
147
|
+
throw new Error('Upload expired. Upload this file again.');
|
|
148
|
+
if (session.status === 'uploading') {
|
|
149
|
+
if (session.expiresAt < Date.now())
|
|
150
|
+
throw new Error('Upload expired. Upload this file again.');
|
|
151
|
+
db.update(uploadSessions)
|
|
152
|
+
.set({ status: 'pending' })
|
|
153
|
+
.where(eq(uploadSessions.mediaId, id))
|
|
154
|
+
.run();
|
|
155
|
+
}
|
|
156
|
+
return asset;
|
|
157
|
+
}
|
|
158
|
+
// Server callers use the same direct transfer and durable finalization as the browser.
|
|
159
|
+
async function upload(file, blueprintId) {
|
|
160
|
+
const signed = await initiateUpload(file, blueprintId);
|
|
161
|
+
const response = await fetch(signed.url, {
|
|
162
|
+
method: 'PUT',
|
|
163
|
+
headers: { ...signed.headers, 'content-length': String(file.size) },
|
|
164
|
+
body: file,
|
|
165
|
+
signal: AbortSignal.timeout(120_000)
|
|
107
166
|
});
|
|
108
|
-
|
|
167
|
+
if (!response.ok)
|
|
168
|
+
throw new Error('Direct upload failed. Upload this file again.');
|
|
169
|
+
return completeUpload(signed.id);
|
|
109
170
|
}
|
|
110
|
-
const
|
|
171
|
+
const deletePending = createDeletionRunner(store, worker);
|
|
172
|
+
const tick = createRunner(store, worker, enqueue, deletePending);
|
|
111
173
|
function retry(id) {
|
|
112
174
|
const asset = get(id);
|
|
113
175
|
if (!asset)
|
|
114
176
|
throw new Error('Image not found');
|
|
115
|
-
|
|
177
|
+
if (asset.status === 'deleting')
|
|
178
|
+
throw new Error('This image is being deleted');
|
|
179
|
+
const session = db.select().from(uploadSessions).where(eq(uploadSessions.mediaId, id)).get();
|
|
180
|
+
if (session) {
|
|
181
|
+
if (session.status !== 'pending')
|
|
182
|
+
throw new Error('Upload this file again.');
|
|
183
|
+
db.update(uploadSessions)
|
|
184
|
+
.set({ attempts: 0, leaseUntil: 0, nextAttempt: 0 })
|
|
185
|
+
.where(eq(uploadSessions.mediaId, id))
|
|
186
|
+
.run();
|
|
187
|
+
db.update(media).set({ status: 'processing', error: null }).where(eq(media.id, id)).run();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const upload = db
|
|
191
|
+
.select({ mediaId: uploads.mediaId })
|
|
192
|
+
.from(uploads)
|
|
193
|
+
.where(eq(uploads.mediaId, id))
|
|
194
|
+
.get();
|
|
116
195
|
if (upload) {
|
|
117
196
|
db.update(uploads)
|
|
118
197
|
.set({ attempts: 0, leaseUntil: 0, nextAttempt: 0 })
|
|
@@ -140,24 +219,34 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
140
219
|
const asset = get(id);
|
|
141
220
|
if (!asset)
|
|
142
221
|
throw new Error('Image not found');
|
|
222
|
+
if (asset.status === 'deleting')
|
|
223
|
+
throw new Error('This image is being deleted');
|
|
143
224
|
const blueprint = getFileBlueprint(definition, asset.blueprint);
|
|
144
225
|
const values = validatePayload(blueprint.fields, input);
|
|
145
226
|
const metadata = mergeDraft(blueprint.fields, asset.metadata, values);
|
|
146
227
|
validatePayload(blueprint.fields, metadata, true);
|
|
147
228
|
walkValues(blueprint.fields, metadata, (field, value) => {
|
|
229
|
+
if (field.type === 'link' && isLinkValue(value))
|
|
230
|
+
content.validateLinkTarget(field, value);
|
|
148
231
|
if (field.type === 'relation' && value) {
|
|
149
232
|
const entry = content.get(String(value));
|
|
150
233
|
if (!entry || entry.blueprint !== field.collection)
|
|
151
234
|
throw new Error(`Choose a valid ${field.label ?? field.collection}`);
|
|
152
235
|
}
|
|
153
236
|
});
|
|
154
|
-
db.
|
|
155
|
-
.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
237
|
+
db.transaction(() => {
|
|
238
|
+
db.update(media)
|
|
239
|
+
.set({
|
|
240
|
+
metadata,
|
|
241
|
+
blueprint: blueprint.id
|
|
242
|
+
})
|
|
243
|
+
.where(eq(media.id, id))
|
|
244
|
+
.run();
|
|
245
|
+
const updated = get(id);
|
|
246
|
+
if (assetVersion(asset) !== assetVersion(updated) &&
|
|
247
|
+
db.select({ id: jobs.id }).from(jobs).where(eq(jobs.mediaId, id)).get())
|
|
248
|
+
enqueue(id, asset.mime);
|
|
249
|
+
});
|
|
161
250
|
}
|
|
162
251
|
const assetData = (id, overrides = {}, preview = false) => {
|
|
163
252
|
const asset = get(id);
|
|
@@ -202,9 +291,12 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
202
291
|
return {
|
|
203
292
|
get,
|
|
204
293
|
upload,
|
|
294
|
+
initiateUpload,
|
|
295
|
+
completeUpload,
|
|
205
296
|
tick,
|
|
206
297
|
retry,
|
|
207
298
|
version,
|
|
299
|
+
versionForAsset: assetVersion,
|
|
208
300
|
workerUrl,
|
|
209
301
|
list: () => db.select().from(media).orderBy(desc(media.createdAt)).all(),
|
|
210
302
|
updateMetadata,
|
|
@@ -214,8 +306,26 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
214
306
|
rebuild: () => {
|
|
215
307
|
db.transaction(() => {
|
|
216
308
|
for (const asset of db.select().from(media).all()) {
|
|
217
|
-
if (asset.
|
|
218
|
-
|
|
309
|
+
if (asset.status === 'deleting')
|
|
310
|
+
continue;
|
|
311
|
+
const session = db
|
|
312
|
+
.select()
|
|
313
|
+
.from(uploadSessions)
|
|
314
|
+
.where(eq(uploadSessions.mediaId, asset.id))
|
|
315
|
+
.get();
|
|
316
|
+
if (session && session.status !== 'pending')
|
|
317
|
+
continue;
|
|
318
|
+
if (asset.activeVersion !== assetVersion(asset) &&
|
|
319
|
+
!db
|
|
320
|
+
.select({ mediaId: uploads.mediaId })
|
|
321
|
+
.from(uploads)
|
|
322
|
+
.where(eq(uploads.mediaId, asset.id))
|
|
323
|
+
.get() &&
|
|
324
|
+
!db
|
|
325
|
+
.select({ mediaId: uploadSessions.mediaId })
|
|
326
|
+
.from(uploadSessions)
|
|
327
|
+
.where(eq(uploadSessions.mediaId, asset.id))
|
|
328
|
+
.get())
|
|
219
329
|
enqueue(asset.id, asset.mime);
|
|
220
330
|
if (get(asset.id)?.status === 'failed')
|
|
221
331
|
retry(asset.id);
|
|
@@ -223,23 +333,26 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
223
333
|
});
|
|
224
334
|
},
|
|
225
335
|
async remove(id) {
|
|
226
|
-
|
|
336
|
+
if (!get(id))
|
|
337
|
+
return;
|
|
338
|
+
// Persist intent before awaiting storage; processors retain their leases until finished.
|
|
227
339
|
db.transaction(() => {
|
|
228
340
|
content.clearReferences(id, 'media');
|
|
229
|
-
db.
|
|
230
|
-
|
|
341
|
+
db.insert(mediaDeletions)
|
|
342
|
+
.values({ mediaId: id })
|
|
343
|
+
.onConflictDoNothing()
|
|
344
|
+
.run();
|
|
231
345
|
db.update(media)
|
|
232
346
|
.set({
|
|
233
|
-
status: '
|
|
347
|
+
status: 'deleting',
|
|
234
348
|
variants: [],
|
|
235
349
|
activeVersion: null,
|
|
236
|
-
error:
|
|
350
|
+
error: null
|
|
237
351
|
})
|
|
238
352
|
.where(eq(media.id, id))
|
|
239
353
|
.run();
|
|
240
354
|
});
|
|
241
|
-
await
|
|
242
|
-
db.delete(media).where(eq(media.id, id)).run();
|
|
355
|
+
await deletePending(id);
|
|
243
356
|
},
|
|
244
357
|
publicAsset: (id, overrides = {}) => assetData(id, overrides),
|
|
245
358
|
// Metadata only. Bytes still require an authenticated CMS session.
|
|
@@ -2,6 +2,7 @@ import type { Store } from './db/store.js';
|
|
|
2
2
|
import type { SiteDefinition } from '../blueprints/types.js';
|
|
3
3
|
import { type NotificationSettings } from './db/schema.js';
|
|
4
4
|
import { type createEmail } from './email/index.js';
|
|
5
|
+
/** Send selected form fields to configured recipients and persist safe outcomes. */
|
|
5
6
|
export declare function createNotifications(store: Store, definition: SiteDefinition, email: ReturnType<typeof createEmail>, origin: string): {
|
|
6
7
|
get: (formId: string) => NotificationSettings;
|
|
7
8
|
save: (formId: string, input: unknown, actor: string) => NotificationSettings;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { eq, inArray } from 'drizzle-orm';
|
|
2
2
|
import { formNotifications, submissions, user, audit } from './db/schema.js';
|
|
3
|
-
import { address } from './email/index.js';
|
|
3
|
+
import { address, emailFailureReason } from './email/index.js';
|
|
4
4
|
import { notificationFields, notificationValue } from '../notification-fields.js';
|
|
5
5
|
import { emailTemplate } from './email/template.js';
|
|
6
6
|
import { rateLimit } from './forms.js';
|
|
@@ -11,6 +11,7 @@ const defaults = () => ({
|
|
|
11
11
|
fields: [],
|
|
12
12
|
replyToField: ''
|
|
13
13
|
});
|
|
14
|
+
/** Send selected form fields to configured recipients and persist safe outcomes. */
|
|
14
15
|
export function createNotifications(store, definition, email, origin) {
|
|
15
16
|
const get = (formId) => store.db.select().from(formNotifications).where(eq(formNotifications.formId, formId)).get()
|
|
16
17
|
?.settings ?? defaults();
|
|
@@ -144,8 +145,9 @@ export function createNotifications(store, definition, email, origin) {
|
|
|
144
145
|
});
|
|
145
146
|
outcome.status = 'accepted';
|
|
146
147
|
}
|
|
147
|
-
catch {
|
|
148
|
+
catch (error) {
|
|
148
149
|
outcome.status = 'failed';
|
|
150
|
+
outcome.failureReason = emailFailureReason(error);
|
|
149
151
|
}
|
|
150
152
|
}));
|
|
151
153
|
// A process interruption leaves "sending" visible as an uncertain outcome.
|
package/dist/server/request.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
import { type RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
export declare function clientAddress(event: Pick<RequestEvent, 'request' | 'getClientAddress'>, trustedProxyHeader?: string): string;
|
|
1
3
|
export declare function boundedJson(request: Request, limit?: number): Promise<Record<string, unknown>>;
|
|
2
4
|
export declare function boundedBytes(request: Request, limit: number): Promise<Uint8Array<ArrayBuffer>>;
|
package/dist/server/request.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { error } from '@sveltejs/kit';
|
|
2
|
+
// Trust a header only when the host explicitly configures its proxy boundary.
|
|
3
|
+
export function clientAddress(event, trustedProxyHeader) {
|
|
4
|
+
const proxyClient = trustedProxyHeader ? event.request.headers.get(trustedProxyHeader) : null;
|
|
5
|
+
return proxyClient || event.getClientAddress();
|
|
6
|
+
}
|
|
2
7
|
export async function boundedJson(request, limit = 1_000_000) {
|
|
3
8
|
const bytes = await boundedBytes(request, limit);
|
|
4
9
|
let data;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Field, Payload } from '../blueprints/types.js';
|
|
2
|
+
import type { SearchResult } from '../search.js';
|
|
3
|
+
import type { CMS } from './cms.js';
|
|
4
|
+
import type { documents, media } from './db/schema.js';
|
|
5
|
+
export declare const normalizeSearch: (value: string) => string;
|
|
6
|
+
export declare function searchQuery(params: URLSearchParams): string;
|
|
7
|
+
export declare function searchableText(fields: Record<string, Field>, payload: Payload, relationLabel?: (collection: string, id: string) => string): string;
|
|
8
|
+
export declare function matchScore(query: string, title: string, body: string): number;
|
|
9
|
+
export declare function documentText(cms: CMS, row: typeof documents.$inferSelect): string;
|
|
10
|
+
export declare function assetText(cms: CMS, row: typeof media.$inferSelect, labels?: Map<string, string>): string;
|
|
11
|
+
export declare function globalSearch(cms: CMS, params: URLSearchParams): {
|
|
12
|
+
items: SearchResult[];
|
|
13
|
+
};
|