Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(cloud): deliver private BYOS media and Docker processing pipeline

Timothy Yin799c014409f1
diff --git a/.github/workflows/deploy-saas.yml b/.github/workflows/deploy-saas.yml
index 2d548547..fccc8b99 100644
--- a/.github/workflows/deploy-saas.yml
+++ b/.github/workflows/deploy-saas.yml
@@ -138,6 +138,9 @@ jobs:
PLATFORM_EMAIL_FROM: ${{ vars.PLATFORM_EMAIL_FROM }}
CLOUD_PLATFORM_SECRET: ${{ secrets.CLOUD_PLATFORM_SECRET }}
SITE_PLATFORM_SECRET: ${{ secrets.SITE_PLATFORM_SECRET }}
+ PLATFORM_STORAGE_SECRET: ${{ secrets.PLATFORM_STORAGE_SECRET }}
+ PLATFORM_WORKER_SECRET: ${{ secrets.PLATFORM_WORKER_SECRET }}
+ PLATFORM_S3_ENDPOINT_HOSTS: ${{ vars.PLATFORM_S3_ENDPOINT_HOSTS }}
run: |
node --input-type=module <<'NODE'
if (process.env.PLATFORM_D1_DATABASE_ID) {
@@ -255,6 +258,9 @@ jobs:
PLATFORM_EMAIL_FROM: ${{ vars.PLATFORM_EMAIL_FROM }}
CLOUD_PLATFORM_SECRET: ${{ secrets.CLOUD_PLATFORM_SECRET }}
SITE_PLATFORM_SECRET: ${{ secrets.SITE_PLATFORM_SECRET }}
+ PLATFORM_STORAGE_SECRET: ${{ secrets.PLATFORM_STORAGE_SECRET }}
+ PLATFORM_WORKER_SECRET: ${{ secrets.PLATFORM_WORKER_SECRET }}
+ PLATFORM_S3_ENDPOINT_HOSTS: ${{ vars.PLATFORM_S3_ENDPOINT_HOSTS }}
run: |
set -euo pipefail
if [[ -z "$CLOUDFLARE_API_TOKEN" || -z "$CLOUDFLARE_ACCOUNT_ID" ]]; then
@@ -284,8 +290,14 @@ jobs:
node --input-type=module <<'NODE'
import fs from 'node:fs'
const secret = process.env[process.env.ENTRY === 'cloud' ? 'CLOUD_PLATFORM_SECRET' : 'SITE_PLATFORM_SECRET']
- fs.writeFileSync(process.env.WORKER_SECRET_FILE, JSON.stringify(secret ? { NUXT_PLATFORM_SECRET: secret } : {}), { mode: 0o600 })
+ const values = secret ? { NUXT_PLATFORM_SECRET: secret } : {}
+ if (process.env.PLATFORM_STORAGE_SECRET) values.NUXT_PLATFORM_STORAGE_SECRET = process.env.PLATFORM_STORAGE_SECRET
+ if (process.env.ENTRY === 'cloud' && process.env.PLATFORM_WORKER_SECRET) values.NUXT_PLATFORM_WORKER_SECRET = process.env.PLATFORM_WORKER_SECRET
+ fs.writeFileSync(process.env.WORKER_SECRET_FILE, JSON.stringify(values), { mode: 0o600 })
NODE
+ if [[ -n "$PLATFORM_S3_ENDPOINT_HOSTS" ]]; then
+ command+=(--var "NUXT_PLATFORM_S3_ENDPOINT_HOSTS:$PLATFORM_S3_ENDPOINT_HOSTS")
+ fi
command+=(--secrets-file "$secret_file")
echo "### $target" >> "$GITHUB_STEP_SUMMARY"
if pnpm exec wrangler "${command[@]}" --config "apps/$entry/.output/server/wrangler.json" \
diff --git a/apps/cloud/app/assets/cloud.css b/apps/cloud/app/assets/cloud.css
index 1e2fcef6..af1472b2 100644
--- a/apps/cloud/app/assets/cloud.css
+++ b/apps/cloud/app/assets/cloud.css
@@ -1615,3 +1615,130 @@
min-height: 40px;
}
}
+
+.cf-media-workbench {
+ display: grid;
+ gap: 16px;
+ margin-top: 20px;
+}
+.cf-media-controls {
+ display: grid;
+ gap: 12px;
+}
+.cf-media-heading {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+}
+.cf-media-controls label,
+.cf-storage-grid label {
+ display: grid;
+ gap: 5px;
+ font-size: 12px;
+}
+.cf-storage-grid {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 12px;
+ padding-top: 14px;
+}
+.cf-storage-grid p {
+ grid-column: 1/-1;
+ font-size: 12px;
+ line-height: 1.6;
+}
+.cf-storage-details summary,
+.cf-media-workbench details summary {
+ font-size: 12px;
+ cursor: pointer;
+ padding: 8px 0;
+}
+.cf-inline-controls {
+ display: flex;
+ align-items: end;
+ gap: 8px;
+ flex-wrap: wrap;
+}
+.cf-inline-controls > .cf-input,
+.cf-inline-controls > label {
+ flex: 1;
+ min-width: 150px;
+}
+.cf-upload-control {
+ position: relative;
+ overflow: hidden;
+ display: inline-flex !important;
+ max-width: max-content;
+ cursor: pointer;
+}
+.cf-upload-control input {
+ position: absolute;
+ inset: 0;
+ opacity: 0;
+ cursor: pointer;
+}
+.cf-upload-control[aria-disabled='true'] {
+ opacity: 0.55;
+}
+.cf-upload-progress {
+ display: grid;
+ gap: 6px;
+ max-width: 100%;
+ min-width: 0;
+ font-size: 12px;
+}
+.cf-upload-progress span {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+.cf-upload-progress progress {
+ width: 100%;
+ height: 6px;
+ accent-color: var(--cf-primary);
+}
+.cf-job-list {
+ margin: 12px 0 0;
+ list-style: none;
+ padding: 0;
+ display: grid;
+ gap: 6px;
+}
+.cf-job-list li {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ flex-wrap: wrap;
+ font-size: 12px;
+ border-top: 1px solid var(--cf-border);
+ padding-top: 8px;
+}
+.cf-job-list small {
+ margin-left: auto;
+}
+.cf-media-workbench .cf-muted {
+ font-size: 12px;
+}
+.cf-empty-note {
+ font-size: 13px;
+ opacity: 0.7;
+ padding: 8px 0;
+}
+@media (max-width: 600px) {
+ .cf-storage-grid {
+ grid-template-columns: minmax(0, 1fr);
+ }
+ .cf-inline-controls {
+ align-items: stretch;
+ }
+ .cf-inline-controls > .cf-input {
+ min-width: 0;
+ }
+ .cf-media-controls .cf-input {
+ font-size: 16px;
+ }
+ .cf-job-list small {
+ margin-left: 0;
+ }
+}
diff --git a/apps/cloud/app/components/CloudMediaWorkbench.vue b/apps/cloud/app/components/CloudMediaWorkbench.vue
new file mode 100644
index 00000000..771ee250
--- /dev/null
+++ b/apps/cloud/app/components/CloudMediaWorkbench.vue
@@ -0,0 +1,905 @@
+<script setup lang="ts">
+import type { Photo } from '@chronoframe/contracts/resources'
+const api = $fetch as <T = unknown>(
+ path: string,
+ options?: { method?: 'GET' | 'POST' | 'DELETE' | 'PATCH'; body?: unknown },
+) => Promise<T>
+const props = defineProps<{ siteId: number; role?: string }>()
+const context = useEntryContext(),
+ base = computed(() => `/api/sites/${props.siteId}/media`)
+const connections = ref<
+ {
+ id: string
+ current_revision: number
+ healthy: number
+ config: { bucket: string }
+ }[]
+ >([]),
+ photos = ref<Photo[]>([]),
+ jobs = ref<
+ {
+ id: string
+ photo_id: string
+ state: string
+ attempts: number
+ error_code: string | null
+ }[]
+ >([]),
+ albums = ref<
+ { id: number; title: string; isHidden: boolean; hasPassword: boolean }[]
+ >([])
+const usage = ref<{ photos: number; pending: number; bytes: number } | null>(
+ null,
+)
+const rotation = reactive({ accessKeyId: '', secretAccessKey: '' })
+const editPhoto = reactive({ id: '', title: '', description: '', revision: 0 })
+const renameAlbum = reactive({ title: '', revision: 0 })
+const connectionId = ref(''),
+ message = ref(''),
+ failed = ref(false),
+ busy = ref(false),
+ uploading = ref(false),
+ progress = ref(0),
+ currentFile = ref('')
+const storage = reactive({
+ endpoint: '',
+ region: 'auto',
+ bucket: '',
+ accessKeyId: '',
+ secretAccessKey: '',
+})
+const albumTitle = ref(''),
+ albumId = ref<number | ''>(''),
+ albumPassword = ref(''),
+ albumHidden = ref(false),
+ photoId = ref('')
+const admin = computed(() => props.role === 'admin')
+let timer: ReturnType<typeof setInterval> | undefined,
+ controller: AbortController | undefined,
+ activeUpload: string | undefined,
+ activeResumeKey: string | undefined
+const canPoll = ref(true)
+async function reload() {
+ try {
+ const [s, p, a, u] = await Promise.all([
+ api<{ connections: typeof connections.value }>(base.value + '/storage'),
+ api<{ photos: Photo[]; jobs: typeof jobs.value }>(base.value + '/photos'),
+ api<{ albums: typeof albums.value }>(base.value + '/albums'),
+ api<{ usage: NonNullable<typeof usage.value> }>(base.value + '/usage'),
+ ])
+ usage.value = u.usage
+ connections.value = s.connections
+ photos.value = p.photos
+ jobs.value = p.jobs
+ albums.value = a.albums
+ if (!connectionId.value) connectionId.value = s.connections[0]?.id ?? ''
+ } catch (error) {
+ failed.value = true
+ message.value =
+ (error as { statusCode?: number }).statusCode === 503
+ ? 'Media services need deployment configuration. Your existing site settings are available.'
+ : 'Could not refresh media. Check your access and retry.'
+ canPoll.value = false
+ }
+}
+function refreshMedia() {
+ canPoll.value = true
+ void reload()
+}
+async function action(run: () => Promise<unknown>, success: string) {
+ if (busy.value) return
+ busy.value = true
+ message.value = ''
+ failed.value = false
+ try {
+ await run()
+ message.value = success
+ canPoll.value = true
+ await reload()
+ } catch (error) {
+ failed.value = true
+ const code = (error as { statusCode?: number }).statusCode
+ message.value =
+ code === 409
+ ? 'This item changed. Refresh and try again.'
+ : code === 429
+ ? 'The preview resource limit has been reached.'
+ : code === 403
+ ? 'Administrator access is required.'
+ : 'The operation failed. Check storage permissions and CORS, then retry.'
+ } finally {
+ busy.value = false
+ }
+}
+async function saveStorage() {
+ await action(async () => {
+ await api(base.value + '/storage', { method: 'POST', body: storage })
+ storage.accessKeyId = ''
+ storage.secretAccessKey = ''
+ }, 'Storage connected and verified.')
+}
+async function rotateStorage() {
+ const selected = connections.value.find((c) => c.id === connectionId.value)
+ if (!selected) return
+ await action(async () => {
+ await api(base.value + '/storage/' + selected.id + '/rotate', {
+ method: 'POST',
+ body: { revision: selected.current_revision, ...rotation },
+ })
+ rotation.accessKeyId = ''
+ rotation.secretAccessKey = ''
+ }, 'Credentials rotated. Existing media references remain available.')
+}
+async function loadPhoto() {
+ if (!editPhoto.id) return
+ const data = await api<{ photo: Photo; revision: number }>(
+ base.value + '/photos/' + editPhoto.id,
+ )
+ editPhoto.title = data.photo.title
+ editPhoto.description = data.photo.description ?? ''
+ editPhoto.revision = data.revision
+}
+async function savePhoto() {
+ await action(
+ () =>
+ api(base.value + '/photos/' + editPhoto.id, {
+ method: 'PATCH',
+ body: {
+ title: editPhoto.title,
+ description: editPhoto.description || null,
+ revision: editPhoto.revision,
+ },
+ }),
+ 'Photograph updated.',
+ )
+ await loadPhoto()
+}
+async function loadAlbum() {
+ if (!albumId.value) return
+ const data = await api<{ album: { title: string }; revision: number }>(
+ base.value + '/albums/' + albumId.value + '/settings',
+ )
+ renameAlbum.title = data.album.title
+ renameAlbum.revision = data.revision
+}
+async function saveAlbum() {
+ await action(
+ () =>
+ api(base.value + '/albums/' + albumId.value + '/settings', {
+ method: 'PATCH',
+ body: renameAlbum,
+ }),
+ 'Album renamed.',
+ )
+ await loadAlbum()
+}
+function resumeKey(file: File) {
+ return [
+ 'chronoframe-upload',
+ context.value?.scope,
+ props.siteId,
+ file.name,
+ file.size,
+ file.lastModified,
+ connectionId.value,
+ ].join(':')
+}
+async function sendFile(file: File, liveVideo?: File) {
+ const cache = resumeKey(file),
+ id = localStorage.getItem(cache) || crypto.randomUUID()
+ await api(base.value + '/uploads', {
+ method: 'POST',
+ body: {
+ requestId: id,
+ connectionId: connectionId.value,
+ fileName: file.name,
+ bytes: file.size,
+ ...(liveVideo
+ ? { liveVideo: { fileName: liveVideo.name, bytes: liveVideo.size } }
+ : {}),
+ },
+ })
+ localStorage.setItem(cache, id)
+ activeResumeKey = cache
+ if (controller?.signal.aborted) {
+ await api(base.value + '/uploads/' + id, {
+ method: 'DELETE',
+ body: {},
+ }).catch(() => {})
+ localStorage.removeItem(cache)
+ throw new Error('cancelled')
+ }
+ activeUpload = id
+ const plan = await api<{
+ state: string
+ mode?: string
+ url?: string
+ contentType?: string
+ videoUrl?: string | null
+ partBytes?: number
+ parts?: { partNumber: number; bytes: number }[]
+ urls?: string[]
+ }>(base.value + '/uploads/' + id + '/plan', { method: 'POST', body: {} })
+ if (plan.state === 'completed') {
+ localStorage.removeItem(cache)
+ return
+ }
+ if (plan.state !== 'pending' || !plan.mode)
+ throw new Error('Upload unavailable')
+ async function put(url: string, body: Blob, type?: string) {
+ for (let attempt = 0; attempt < 3; attempt++) {
+ controller?.signal.throwIfAborted()
+ const response = await fetch(url, {
+ method: 'PUT',
+ body,
+ headers: type ? { 'Content-Type': type } : {},
+ signal: controller?.signal,
+ redirect: 'error',
+ })
+ if (response.ok) return
+ if (attempt === 2) throw new Error('Upload failed')
+ await new Promise((resolve) => setTimeout(resolve, 500 * 2 ** attempt))
+ }
+ }
+ if (plan.mode === 'single') await put(plan.url!, file, plan.contentType)
+ else {
+ const partBytes = plan.partBytes!,
+ count = plan.urls!.length
+ for (let i = 0; i < count; i++) {
+ const start = i * partBytes,
+ end = Math.min(file.size, start + partBytes)
+ if (
+ !plan.parts?.some(
+ (p) => p.partNumber === i + 1 && p.bytes === end - start,
+ )
+ )
+ await put(plan.urls![i]!, file.slice(start, end))
+ progress.value = Math.round((end / file.size) * 100)
+ }
+ }
+ if (liveVideo && plan.videoUrl)
+ await put(plan.videoUrl, liveVideo, 'video/quicktime')
+ progress.value = 100
+ await api(base.value + '/uploads/' + id + '/complete', {
+ method: 'POST',
+ body: {},
+ })
+ localStorage.removeItem(cache)
+ activeUpload = undefined
+}
+async function upload(event: Event) {
+ const input = event.target as HTMLInputElement,
+ files = Array.from(input.files ?? [])
+ if (!files.length || uploading.value || !connectionId.value) return
+ if (
+ files.some((f) => f.size > 100 * 1024 * 1024) ||
+ files.every((f) => f.name.toLowerCase().endsWith('.mov'))
+ ) {
+ failed.value = true
+ message.value =
+ 'Choose photographs under 100 MB. A Live Photo MOV must accompany a photograph with the same filename.'
+ input.value = ''
+ return
+ }
+ uploading.value = true
+ failed.value = false
+ message.value = ''
+ controller = new AbortController()
+ try {
+ for (const file of files.filter(
+ (f) => !f.name.toLowerCase().endsWith('.mov'),
+ )) {
+ const companion = files.find(
+ (f) =>
+ f.name.toLowerCase().endsWith('.mov') &&
+ f.name.replace(/\.[^.]+$/, '').toLowerCase() ===
+ file.name.replace(/\.[^.]+$/, '').toLowerCase(),
+ )
+ currentFile.value = file.name
+ progress.value = 0
+ await sendFile(file, companion)
+ }
+ message.value = 'Upload verified. Processing continues in the background.'
+ } catch (error) {
+ failed.value = true
+ message.value = controller.signal.aborted
+ ? 'Upload cancelled.'
+ : 'Upload interrupted. Select the same file to resume its uploaded parts.'
+ } finally {
+ uploading.value = false
+ input.value = ''
+ currentFile.value = ''
+ await reload()
+ }
+}
+async function cancel() {
+ controller?.abort()
+ if (activeResumeKey) localStorage.removeItem(activeResumeKey)
+ activeResumeKey = undefined
+ if (activeUpload)
+ await api(base.value + '/uploads/' + activeUpload, {
+ method: 'DELETE',
+ body: {},
+ }).catch(() => {})
+ activeUpload = undefined
+}
+async function removePhoto(id: string) {
+ if (
+ !window.confirm(
+ 'Delete this photograph and its managed original, thumbnail and Live Photo files from storage?',
+ )
+ )
+ return
+ await action(
+ () => api(base.value + '/photos/' + id, { method: 'DELETE', body: {} }),
+ 'Photograph deleted. Managed storage files are queued for cleanup.',
+ )
+}
+function createAlbum() {
+ return action(
+ () =>
+ api(base.value + '/albums', {
+ method: 'POST',
+ body: {
+ id: crypto.getRandomValues(new Uint32Array(1))[0] || 1,
+ title: albumTitle.value,
+ },
+ }),
+ 'Album created.',
+ )
+}
+async function removeAlbum() {
+ if (
+ !window.confirm(
+ 'Delete this album? Photographs remain in your library and may become public if no other protected album contains them.',
+ )
+ )
+ return
+ await action(
+ () =>
+ api(base.value + '/albums/' + albumId.value, {
+ method: 'DELETE',
+ body: {},
+ }),
+ 'Album deleted. Photographs remain in your library.',
+ )
+ albumId.value = ''
+}
+async function removeFromAlbum() {
+ if (
+ !window.confirm(
+ 'Remove this photograph from the album? It may become public if no other protected album contains it.',
+ )
+ )
+ return
+ await action(
+ () =>
+ api(base.value + '/albums/' + albumId.value + '/photos', {
+ method: 'DELETE',
+ body: { photoId: photoId.value },
+ }),
+ 'Photograph removed from album.',
+ )
+}
+async function publish(value: boolean) {
+ await action(
+ async () => {
+ const settings = await api<{ site: { revision: number } }>(
+ `/api/sites/${props.siteId}/settings`,
+ )
+ const result = await api<{ published: boolean }>(
+ base.value + '/publish',
+ {
+ method: 'POST',
+ body: { revision: settings.site.revision, published: value },
+ },
+ )
+ if (context.value?.siteId === props.siteId)
+ context.value.published = result.published
+ },
+ value ? 'Gallery published.' : 'Gallery unpublished.',
+ )
+}
+onMounted(() => {
+ void reload()
+ timer = setInterval(() => {
+ if (canPoll.value && !busy.value && !uploading.value) void reload()
+ }, 5000)
+})
+onBeforeUnmount(() => {
+ if (timer) clearInterval(timer)
+ controller?.abort()
+})
+</script>
+<template>
+ <section
+ class="cf-media-workbench"
+ aria-label="Gallery media"
+ >
+ <p
+ v-if="message"
+ :role="failed ? 'alert' : 'status'"
+ class="cf-notice"
+ >
+ {{ message }}
+ </p>
+ <CloudCard>
+ <div class="cf-panel-heading">
+ <h2>Storage &amp; uploads</h2>
+ <p>Your files stay in your private S3-compatible bucket.</p>
+ </div>
+ <div class="cf-panel-body cf-media-controls">
+ <label class="cf-field-label"
+ >Storage connection<select
+ v-model="connectionId"
+ class="cf-input"
+ :disabled="!admin"
+ >
+ <option value="">Choose a connection</option>
+ <option
+ v-for="c in connections"
+ :key="c.id"
+ :value="c.id"
+ >
+ {{ c.config.bucket }} · revision {{ c.current_revision }}
+ </option>
+ </select></label
+ >
+ <label
+ v-if="admin"
+ class="cf-upload-control cf-button cf-button-primary"
+ :aria-disabled="!connectionId || uploading"
+ >Upload photographs<input
+ type="file"
+ multiple
+ accept=".jpg,.jpeg,.png,.webp,.heic,.heif,.avif,.gif,.mov"
+ :disabled="!connectionId || uploading"
+ @change="upload"
+ /></label>
+ <CloudButton
+ v-if="uploading"
+ variant="secondary"
+ @click="cancel"
+ >Cancel upload</CloudButton
+ >
+ <div
+ v-if="uploading"
+ role="status"
+ class="cf-upload-progress"
+ >
+ <span>{{ currentFile }}</span
+ ><progress
+ :value="progress"
+ max="100"
+ aria-label="Upload progress"
+ />
+ </div>
+ <p
+ v-if="usage"
+ class="cf-muted"
+ >
+ {{ usage.photos }} / 1,000 photographs · {{ usage.pending }} pending ·
+ {{ (usage.bytes / 1024 ** 3).toFixed(2) }} / 5 GiB uploaded in 30 days
+ </p>
+ <details
+ v-if="admin && connectionId"
+ class="cf-storage-details"
+ >
+ <summary>Rotate storage credentials</summary>
+ <form
+ class="cf-storage-grid"
+ @submit.prevent="rotateStorage"
+ >
+ <label
+ >New access key ID<input
+ v-model="rotation.accessKeyId"
+ class="cf-input"
+ autocomplete="off"
+ required
+ /></label>
+ <label
+ >New secret access key<input
+ v-model="rotation.secretAccessKey"
+ class="cf-input"
+ type="password"
+ autocomplete="new-password"
+ required
+ /></label>
+ <CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Test &amp; rotate</CloudButton
+ >
+ </form>
+ </details>
+ <details
+ v-if="admin"
+ class="cf-storage-details"
+ >
+ <summary>Connect storage</summary>
+ <form
+ class="cf-storage-grid"
+ @submit.prevent="saveStorage"
+ >
+ <label
+ >Endpoint<input
+ v-model="storage.endpoint"
+ class="cf-input"
+ type="url"
+ placeholder="https://s3.us-east-1.amazonaws.com"
+ required
+ /></label>
+ <label
+ >Region<input
+ v-model="storage.region"
+ class="cf-input"
+ required
+ /></label>
+ <label
+ >Bucket<input
+ v-model="storage.bucket"
+ class="cf-input"
+ required
+ /></label>
+ <label
+ >Access key ID<input
+ v-model="storage.accessKeyId"
+ class="cf-input"
+ autocomplete="off"
+ required
+ /></label>
+ <label
+ >Secret access key<input
+ v-model="storage.secretAccessKey"
+ class="cf-input"
+ type="password"
+ autocomplete="new-password"
+ required
+ /></label>
+ <p>
+ Use a dedicated bucket with private access. Allow this portal
+ origin in CORS for GET, PUT and HEAD; allow Content-Type and
+ expose ETag. Other endpoint hosts require operator approval.
+ </p>
+ <CloudButton
+ type="submit"
+ :disabled="busy"
+ >Test &amp; connect</CloudButton
+ >
+ </form>
+ </details>
+ </div>
+ </CloudCard>
+ <CloudCard>
+ <div class="cf-panel-heading cf-media-heading">
+ <div>
+ <h2>Photographs</h2>
+ <p>
+ {{ photos.length }} ready ·
+ {{
+ jobs.filter((j) =>
+ ['queued', 'running', 'retry_wait'].includes(j.state),
+ ).length
+ }}
+ processing
+ </p>
+ </div>
+ <CloudButton
+ variant="secondary"
+ @click="refreshMedia"
+ >Refresh</CloudButton
+ >
+ </div>
+ <div class="cf-panel-body">
+ <GalleryGrid
+ v-if="photos.length"
+ :photos="photos"
+ />
+ <p
+ v-else
+ class="cf-empty-note"
+ >
+ Upload your first photograph to begin.
+ </p>
+ <ul
+ v-if="jobs.some((j) => j.state !== 'succeeded')"
+ class="cf-job-list"
+ >
+ <li
+ v-for="job in jobs.filter((j) => j.state !== 'succeeded')"
+ :key="job.id"
+ >
+ <code>{{ job.id.slice(0, 8) }}</code
+ ><span>{{ job.state.replace('_', ' ') }}</span
+ ><small
+ >Attempt {{ job.attempts
+ }}<template v-if="job.error_code">
+ · {{ job.error_code.replaceAll('_', ' ') }}</template
+ ></small
+ >
+ <CloudButton
+ v-if="admin && job.state === 'failed'"
+ size="sm"
+ variant="secondary"
+ :disabled="busy"
+ @click="
+ action(
+ () =>
+ api(base + '/jobs/' + job.id + '/retry', {
+ method: 'POST',
+ body: {},
+ }),
+ 'Job queued again.',
+ )
+ "
+ >Retry</CloudButton
+ >
+ <CloudButton
+ v-if="admin && job.state !== 'cancelled'"
+ size="sm"
+ variant="ghost"
+ :disabled="busy"
+ @click="removePhoto(job.photo_id)"
+ >Remove</CloudButton
+ >
+ </li>
+ </ul>
+ <details v-if="admin && photos.length">
+ <summary>Edit a photograph</summary>
+ <form
+ class="cf-storage-grid"
+ @submit.prevent="savePhoto"
+ >
+ <label
+ >Photograph<select
+ v-model="editPhoto.id"
+ class="cf-input"
+ required
+ @change="loadPhoto"
+ >
+ <option value="">Choose a photograph</option>
+ <option
+ v-for="p in photos"
+ :key="p.id"
+ :value="p.id"
+ >
+ {{ p.title }}
+ </option>
+ </select></label
+ >
+ <label
+ >Title<input
+ v-model="editPhoto.title"
+ class="cf-input"
+ maxlength="255"
+ required
+ /></label>
+ <label
+ >Description<textarea
+ v-model="editPhoto.description"
+ class="cf-input"
+ maxlength="2048"
+ />
+ </label>
+ <CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy || !editPhoto.id"
+ >Save photograph</CloudButton
+ >
+ </form>
+ </details>
+ <details v-if="admin && photos.length">
+ <summary>Remove a photograph</summary>
+ <form
+ class="cf-inline-controls"
+ @submit.prevent="removePhoto(photoId)"
+ >
+ <label
+ class="sr-only"
+ for="cf-delete-photo"
+ >Photograph</label
+ ><select
+ id="cf-delete-photo"
+ v-model="photoId"
+ class="cf-input"
+ required
+ >
+ <option value="">Choose a photograph</option>
+ <option
+ v-for="p in photos"
+ :key="p.id"
+ :value="p.id"
+ >
+ {{ p.title }}
+ </option></select
+ ><CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Remove</CloudButton
+ >
+ </form>
+ </details>
+ </div>
+ </CloudCard>
+ <CloudCard>
+ <div class="cf-panel-heading">
+ <h2>Albums &amp; publication</h2>
+ <p>
+ Hidden and password-protected albums also protect every media variant.
+ </p>
+ </div>
+ <div class="cf-panel-body cf-media-controls">
+ <form
+ v-if="admin"
+ class="cf-inline-controls"
+ @submit.prevent="createAlbum()"
+ >
+ <label
+ class="sr-only"
+ for="cf-album-name"
+ >Album name</label
+ ><input
+ id="cf-album-name"
+ v-model="albumTitle"
+ class="cf-input"
+ placeholder="Album name"
+ maxlength="80"
+ required
+ /><CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Create album</CloudButton
+ >
+ </form>
+ <label class="cf-field-label"
+ >Album<select
+ v-model="albumId"
+ @change="loadAlbum"
+ class="cf-input"
+ >
+ <option value="">Choose an album</option>
+ <option
+ v-for="a in albums"
+ :key="a.id"
+ :value="a.id"
+ >
+ {{ a.title }}{{ a.isHidden ? ' · hidden' : ''
+ }}{{ a.hasPassword ? ' · protected' : '' }}
+ </option>
+ </select></label
+ >
+ <form
+ v-if="admin && albumId"
+ class="cf-inline-controls"
+ @submit.prevent="saveAlbum"
+ >
+ <label
+ >Album name<input
+ v-model="renameAlbum.title"
+ class="cf-input"
+ maxlength="80"
+ required /></label
+ ><CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Rename album</CloudButton
+ >
+ </form>
+ <form
+ v-if="admin && albumId"
+ class="cf-inline-controls"
+ @submit.prevent="
+ action(
+ () =>
+ api(base + '/albums/' + albumId + '/photos', {
+ method: 'POST',
+ body: { photoId },
+ }),
+ 'Photograph added.',
+ )
+ "
+ >
+ <label
+ class="sr-only"
+ for="cf-add-photo"
+ >Photograph</label
+ ><select
+ id="cf-add-photo"
+ v-model="photoId"
+ class="cf-input"
+ required
+ >
+ <option value="">Choose a photograph</option>
+ <option
+ v-for="p in photos"
+ :key="p.id"
+ :value="p.id"
+ >
+ {{ p.title }}
+ </option></select
+ ><CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Add to album</CloudButton
+ >
+ </form>
+ <form
+ v-if="admin && albumId"
+ class="cf-inline-controls"
+ @submit.prevent="
+ action(
+ () =>
+ api(base + '/albums/' + albumId + '/access', {
+ method: 'POST',
+ body: {
+ hidden: albumHidden,
+ password: albumPassword || null,
+ },
+ }),
+ 'Album access updated.',
+ )
+ "
+ >
+ <label
+ ><input
+ v-model="albumHidden"
+ type="checkbox"
+ />
+ Hidden</label
+ ><label
+ >Album password<input
+ v-model="albumPassword"
+ type="password"
+ class="cf-input"
+ minlength="8"
+ autocomplete="new-password"
+ placeholder="Empty removes protection" /></label
+ ><CloudButton
+ type="submit"
+ variant="secondary"
+ :disabled="busy"
+ >Save access</CloudButton
+ >
+ </form>
+ <div
+ v-if="admin && albumId"
+ class="cf-inline-controls"
+ >
+ <CloudButton
+ variant="secondary"
+ :disabled="busy || !photoId"
+ @click="removeFromAlbum"
+ >Remove selected photograph from album</CloudButton
+ >
+ <CloudButton
+ variant="ghost"
+ :disabled="busy"
+ @click="removeAlbum"
+ >Delete album</CloudButton
+ >
+ </div>
+ <div
+ v-if="admin"
+ class="cf-inline-controls"
+ >
+ <CloudButton
+ :disabled="busy || (!photos.length && !context?.published)"
+ @click="publish(!context?.published)"
+ >{{
+ context?.published ? 'Unpublish gallery' : 'Publish gallery'
+ }}</CloudButton
+ ><span class="cf-muted"
+ >Publishing makes unprotected photographs public at your reserved
+ .site.chronoframe.art address.</span
+ >
+ </div>
+ </div>
+ </CloudCard>
+ </section>
+</template>
diff --git a/apps/cloud/app/pages/sites/[siteId]/index.vue b/apps/cloud/app/pages/sites/[siteId]/index.vue
index 1f55fa4e..8deb28a3 100644
--- a/apps/cloud/app/pages/sites/[siteId]/index.vue
+++ b/apps/cloud/app/pages/sites/[siteId]/index.vue
@@ -105,6 +105,10 @@ onBeforeUnmount(() => stream?.close())
>Read the documentation<ArrowUpRight :size="13"
/></a>
</aside>
- </div> </motion.div
+ </div>
+ <CloudMediaWorkbench
+ :site-id="context.siteId"
+ :role="context.role"
+ /> </motion.div
></CloudPortalShell>
</template>
diff --git a/apps/cloud/nuxt.config.ts b/apps/cloud/nuxt.config.ts
index 6812e8dc..f3e50a3a 100644
--- a/apps/cloud/nuxt.config.ts
+++ b/apps/cloud/nuxt.config.ts
@@ -38,6 +38,9 @@ export default defineNuxtConfig({
portalOrigin: 'https://app.chronoframe.art',
secret: '',
emailFrom: '',
+ storageSecret: '',
+ s3EndpointHosts: '',
+ workerSecret: '',
},
},
css: ['~/assets/cloud.css'],
diff --git a/apps/cloud/package.json b/apps/cloud/package.json
index a082cfcb..c6ffe643 100644
--- a/apps/cloud/package.json
+++ b/apps/cloud/package.json
@@ -44,6 +44,8 @@
"reka-ui": "^2.11.0",
"shadcn-nuxt": "^2.8.2",
"tailwind-merge": "^3.7.0",
- "vue": "^3.5.43"
+ "vue": "^3.5.43",
+ "@chronoframe/database": "workspace:*",
+ "@chronoframe/storage": "workspace:*"
}
}
diff --git a/apps/cloud/server/api/internal/worker/[action].post.ts b/apps/cloud/server/api/internal/worker/[action].post.ts
new file mode 100644
index 00000000..b41c23e2
--- /dev/null
+++ b/apps/cloud/server/api/internal/worker/[action].post.ts
@@ -0,0 +1,402 @@
+import { createError, getHeader, getRequestWebStream, getRouterParam } from 'h3'
+import { createCloudExecutionRepository } from '@chronoframe/database/cloud-execution'
+import type { MediaDatabase } from '@chronoframe/database/cloud-media'
+import type { CloudLease, CloudMediaResult } from '@chronoframe/core'
+import { createCloudS3 } from '@chronoframe/storage/s3-edge'
+import { createStorageCipher } from '@chronoframe/storage/credential-cipher'
+import { createStorageVault } from '../../../../../../packages/platform/src/index'
+import {
+ definePlatformHandler,
+ platformDatabase,
+ platformPortalOrigin,
+} from '../../../../../shared/platform/runtime'
+import { readSiteBody } from '../../../utils/site-body'
+export default definePlatformHandler(async (event) => {
+ const config = useRuntimeConfig(event).platform
+ platformPortalOrigin(event, config.portalOrigin)
+ if (
+ config.workerSecret.length < 32 ||
+ !getHeader(event, 'content-type')?.startsWith('application/json')
+ )
+ throw createError({ statusCode: 503 })
+ const supplied =
+ getHeader(event, 'authorization')?.replace(/^Bearer /, '') ?? ''
+ if (supplied.length > 256) throw createError({ statusCode: 401 })
+ const encoder = new TextEncoder(),
+ key = await crypto.subtle.importKey(
+ 'raw',
+ encoder.encode(config.workerSecret),
+ { name: 'HMAC', hash: 'SHA-256' },
+ false,
+ ['sign', 'verify'],
+ )
+ const proof = await crypto.subtle.sign(
+ 'HMAC',
+ key,
+ encoder.encode(config.workerSecret),
+ )
+ if (
+ !(await crypto.subtle.verify('HMAC', key, proof, encoder.encode(supplied)))
+ )
+ throw createError({ statusCode: 401 })
+ const body = await readSiteBody(getRequestWebStream(event), 98304),
+ action = getRouterParam(event, 'action'),
+ now = Date.now()
+ const raw = platformDatabase(event),
+ db = raw as unknown as MediaDatabase
+ const allowedHosts = config.s3EndpointHosts
+ .split(',')
+ .map((h: string) => h.trim())
+ .filter(Boolean)
+ async function storage(siteId: number, id: string, revision: number) {
+ const item = await createStorageVault(
+ raw,
+ siteId,
+ await createStorageCipher(config.storageSecret),
+ ).get(id, revision)
+ if (!item) throw createError({ statusCode: 503 })
+ return createCloudS3(
+ { ...item.config, ...item.credentials },
+ { allowedHosts },
+ )
+ }
+ if (action === 'claim') {
+ if (
+ typeof body.workerId !== 'string' ||
+ !/^[a-zA-Z0-9_-]{1,128}$/.test(body.workerId)
+ )
+ throw createError({ statusCode: 400 })
+ const candidates = (
+ await db
+ .prepare(
+ `SELECT j.site_id,MIN(j.created_at) oldest FROM cloud_processing_jobs j LEFT JOIN cloud_site_dispatch d ON d.site_id=j.site_id WHERE ((state IN ('queued','retry_wait') AND available_at<=?) OR (state='running' AND lease_until<=?)) AND NOT EXISTS(SELECT 1 FROM cloud_processing_jobs active WHERE active.site_id=j.site_id AND active.state='running' AND active.lease_until>?) GROUP BY j.site_id ORDER BY COALESCE(d.last_claim_at,0),oldest,j.site_id LIMIT 10`,
+ )
+ .bind(now, now, now)
+ .all<{ site_id: number }>()
+ ).results
+ for (const candidate of candidates) {
+ const lease = await createCloudExecutionRepository(db, {
+ id: candidate.site_id,
+ }).claim(body.workerId, crypto.randomUUID(), now, 120000)
+ if (!lease) continue
+ const s3 = await storage(
+ lease.siteId,
+ lease.connectionId,
+ lease.connectionRevision,
+ )
+ const detail = await raw
+ .prepare(
+ 'SELECT file_name,content_type,verified_etag,video_key,video_etag,expected_video_bytes FROM cloud_uploads WHERE site_id=? AND photo_id=? AND source_version=?',
+ )
+ .bind(lease.siteId, lease.photoId, lease.sourceVersion)
+ .first<{
+ file_name: string
+ content_type: string
+ verified_etag: string | null
+ video_key: string | null
+ video_etag: string | null
+ expected_video_bytes: number
+ }>()
+ return {
+ lease,
+ sourceUrl: await s3.signGet(lease.objectKey),
+ fileName: detail?.file_name,
+ contentType: detail?.content_type,
+ etag: detail?.verified_etag,
+ companion: detail?.video_key
+ ? {
+ key: detail.video_key,
+ url: await s3.signGet(detail.video_key),
+ bytes: detail.expected_video_bytes,
+ etag: detail.video_etag,
+ }
+ : null,
+ }
+ }
+ return { lease: null }
+ }
+ if (action === 'cleanup') {
+ const expired = (
+ await db
+ .prepare(
+ `SELECT site_id,id,connection_id,connection_revision,object_key,video_key,provider_upload_id FROM cloud_uploads WHERE state='pending' AND expires_at<=? ORDER BY expires_at LIMIT 10`,
+ )
+ .bind(now)
+ .all<{
+ site_id: number
+ id: string
+ connection_id: string
+ connection_revision: number
+ object_key: string
+ video_key: string | null
+ provider_upload_id: string | null
+ }>()
+ ).results
+ for (const u of expired)
+ await db.batch([
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,provider_upload_id,available_at) SELECT ?,?,?,?,?,?,? WHERE EXISTS(SELECT 1 FROM cloud_uploads WHERE site_id=? AND id=? AND state='pending' AND expires_at<=?) ON CONFLICT DO NOTHING`,
+ )
+ .bind(
+ u.site_id,
+ 'upload-' + u.id,
+ u.connection_id,
+ u.connection_revision,
+ u.object_key,
+ u.provider_upload_id,
+ now + 360000,
+ u.site_id,
+ u.id,
+ now,
+ ),
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,available_at) SELECT ?,?,?,?,?,? WHERE ? IS NOT NULL AND EXISTS(SELECT 1 FROM cloud_uploads WHERE site_id=? AND id=? AND state='pending' AND expires_at<=?) ON CONFLICT DO NOTHING`,
+ )
+ .bind(
+ u.site_id,
+ 'upload-video-' + u.id,
+ u.connection_id,
+ u.connection_revision,
+ u.video_key,
+ now + 360000,
+ u.video_key,
+ u.site_id,
+ u.id,
+ now,
+ ),
+ db
+ .prepare(
+ `UPDATE cloud_uploads SET state='cancelled' WHERE site_id=? AND id=? AND state='pending' AND expires_at<=?`,
+ )
+ .bind(u.site_id, u.id, now),
+ ])
+ const abandoned = (
+ await db
+ .prepare(
+ `SELECT a.site_id,a.attempt_id,j.photo_id,j.source_version,j.pipeline_version,u.connection_id,u.connection_revision FROM cloud_job_attempts a JOIN cloud_processing_jobs j ON j.site_id=a.site_id AND j.id=a.job_id JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE (a.state IN ('expired','failed','cancelled') OR j.state='cancelled') AND NOT EXISTS(SELECT 1 FROM cloud_cleanup_requests c WHERE c.site_id=a.site_id AND c.id='attempt-'||a.attempt_id||'-thumbnail.webp') LIMIT 10`,
+ )
+ .all<{
+ site_id: number
+ attempt_id: string
+ photo_id: string
+ source_version: string
+ pipeline_version: string
+ connection_id: string
+ connection_revision: number
+ }>()
+ ).results
+ for (const old of abandoned)
+ for (const name of [
+ 'thumbnail.webp',
+ 'display.jpeg',
+ 'live-video.mp4',
+ 'live-video.mov',
+ ])
+ await raw
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,available_at) VALUES(?,?,?,?,?,?) ON CONFLICT DO NOTHING`,
+ )
+ .bind(
+ old.site_id,
+ 'attempt-' + old.attempt_id + '-' + name,
+ old.connection_id,
+ old.connection_revision,
+ `chronoframe/${old.site_id}/derived/${old.photo_id}/${old.source_version}/${old.pipeline_version}/${old.attempt_id}/${name}`,
+ now + 360000,
+ )
+ .run()
+ const pending = (
+ await db
+ .prepare(
+ `SELECT * FROM cloud_cleanup_requests WHERE state='pending' AND available_at<=? AND attempts<10 LIMIT 2`,
+ )
+ .bind(now)
+ .all<{
+ site_id: number
+ id: string
+ connection_id: string
+ connection_revision: number
+ object_key: string
+ provider_upload_id: string | null
+ attempts: number
+ }>()
+ ).results
+ let done = 0
+ for (const item of pending) {
+ try {
+ const s3 = await storage(
+ item.site_id,
+ item.connection_id,
+ item.connection_revision,
+ )
+ if (!item.provider_upload_id && item.id.startsWith('upload-'))
+ for (const orphan of await s3.recoverMultipart(item.object_key))
+ await s3.abort(item.object_key, orphan)
+ if (item.provider_upload_id)
+ await s3
+ .abort(item.object_key, item.provider_upload_id)
+ .catch((error) => {
+ if (error?.name !== 'NoSuchUpload') throw error
+ })
+ await s3.remove(item.object_key)
+ await raw
+ .prepare(
+ "UPDATE cloud_cleanup_requests SET state='done',attempts=attempts+1 WHERE site_id=? AND id=?",
+ )
+ .bind(item.site_id, item.id)
+ .run()
+ done++
+ } catch {
+ await raw
+ .prepare(
+ 'UPDATE cloud_cleanup_requests SET attempts=attempts+1,available_at=? WHERE site_id=? AND id=?',
+ )
+ .bind(
+ now + Math.min(60000 * 2 ** item.attempts, 86400000),
+ item.site_id,
+ item.id,
+ )
+ .run()
+ }
+ }
+ return { done }
+ }
+ const lease = body.lease as CloudLease
+ if (
+ !lease ||
+ !Number.isSafeInteger(lease.siteId) ||
+ typeof lease.id !== 'string'
+ )
+ throw createError({ statusCode: 400 })
+ const queue = createCloudExecutionRepository(db, { id: lease.siteId })
+ if (action === 'heartbeat')
+ return { ok: await queue.heartbeat(lease, now, 120000) }
+ if (action === 'fail')
+ return {
+ ok: await queue.fail(
+ lease,
+ typeof body.code === 'string' ? body.code : 'processing_error',
+ body.retryable === true,
+ now,
+ ),
+ }
+ if (action === 'grant') {
+ if (!(await queue.heartbeat(lease, now, 120000)))
+ throw createError({ statusCode: 409 })
+ const canonical = await raw
+ .prepare(
+ 'SELECT j.photo_id,j.source_version,j.pipeline_version,u.connection_id,u.connection_revision FROM cloud_processing_jobs j JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE j.site_id=? AND j.id=? AND j.attempt_id=?',
+ )
+ .bind(lease.siteId, lease.id, lease.attemptId)
+ .first<{
+ photo_id: string
+ source_version: string
+ pipeline_version: string
+ connection_id: string
+ connection_revision: number
+ }>()
+ if (
+ !canonical ||
+ canonical.photo_id !== lease.photoId ||
+ canonical.source_version !== lease.sourceVersion ||
+ canonical.pipeline_version !== lease.pipelineVersion ||
+ canonical.connection_id !== lease.connectionId ||
+ canonical.connection_revision !== lease.connectionRevision
+ )
+ throw createError({ statusCode: 403 })
+ if (
+ !Array.isArray(body.assets) ||
+ body.assets.length > 3 ||
+ body.assets.length < 1
+ )
+ throw createError({ statusCode: 400 })
+ const s3 = await storage(
+ lease.siteId,
+ lease.connectionId,
+ lease.connectionRevision,
+ )
+ const outputs = []
+ for (const asset of body.assets as {
+ role: string
+ bytes: number
+ contentType: string
+ }[]) {
+ let name: string
+ if (asset.role === 'thumbnail' && asset.contentType === 'image/webp')
+ name = 'thumbnail.webp'
+ else if (asset.role === 'display' && asset.contentType === 'image/jpeg')
+ name = 'display.jpeg'
+ else if (asset.role === 'live-video' && asset.contentType === 'video/mp4')
+ name = 'live-video.mp4'
+ else if (
+ asset.role === 'live-video' &&
+ asset.contentType === 'video/quicktime'
+ )
+ name = 'live-video.mov'
+ else throw createError({ statusCode: 400 })
+ const objectKey = `chronoframe/${lease.siteId}/derived/${lease.photoId}/${lease.sourceVersion}/${lease.pipelineVersion}/${lease.attemptId}/${name}`
+ outputs.push({
+ ...asset,
+ objectKey,
+ url: await s3.signPut(objectKey, asset.bytes, asset.contentType),
+ })
+ }
+ return { outputs }
+ }
+ if (action === 'complete') {
+ if (!(await queue.heartbeat(lease, now, 120000))) return { ok: false }
+ const result = body.result as CloudMediaResult
+ if (!result || !Array.isArray(result.assets))
+ throw createError({ statusCode: 400 })
+ const canonical = await raw
+ .prepare(
+ 'SELECT j.photo_id,j.source_version,j.pipeline_version,u.connection_id,u.connection_revision FROM cloud_processing_jobs j JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE j.site_id=? AND j.id=? AND j.attempt_id=?',
+ )
+ .bind(lease.siteId, lease.id, lease.attemptId)
+ .first<{
+ photo_id: string
+ source_version: string
+ pipeline_version: string
+ connection_id: string
+ connection_revision: number
+ }>()
+ if (
+ !canonical ||
+ canonical.photo_id !== lease.photoId ||
+ canonical.source_version !== lease.sourceVersion ||
+ canonical.pipeline_version !== lease.pipelineVersion ||
+ canonical.connection_id !== lease.connectionId ||
+ canonical.connection_revision !== lease.connectionRevision
+ )
+ throw createError({ statusCode: 403 })
+ const prefix = `chronoframe/${lease.siteId}/derived/${lease.photoId}/${lease.sourceVersion}/${lease.pipelineVersion}/${lease.attemptId}/`
+ if (
+ result.assets.length < 1 ||
+ result.assets.length > 3 ||
+ result.assets.some(
+ (a) =>
+ ![
+ 'thumbnail.webp',
+ 'display.jpeg',
+ 'live-video.mp4',
+ 'live-video.mov',
+ ].some((name) => a.objectKey === prefix + name),
+ )
+ )
+ throw createError({ statusCode: 400 })
+ const s3 = await storage(
+ lease.siteId,
+ lease.connectionId,
+ lease.connectionRevision,
+ )
+ for (const asset of result.assets) {
+ const meta = await s3.head(asset.objectKey)
+ if (meta.bytes !== asset.bytes || meta.contentType !== asset.contentType)
+ throw createError({ statusCode: 400 })
+ }
+ return { ok: await queue.complete(lease, result, now) }
+ }
+ throw createError({ statusCode: 404 })
+})
diff --git a/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts b/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts
new file mode 100644
index 00000000..c929d157
--- /dev/null
+++ b/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts
@@ -0,0 +1,586 @@
+import { getRouterParam, createError } from 'h3'
+import { definePlatformHandler } from '../../../../../../shared/platform/runtime'
+import { cloudMedia, checkStorage } from '../../../../utils/cloud-media'
+import { siteManagementBody } from '../../../../utils/site-management'
+import { createResourceReader } from '@chronoframe/core'
+import { visitorPasswordHash } from '../../../../../../shared/platform/crypto'
+
+const types: Record<string, string> = {
+ jpg: 'image/jpeg',
+ jpeg: 'image/jpeg',
+ png: 'image/png',
+ webp: 'image/webp',
+ heic: 'image/heic',
+ heif: 'image/heif',
+ avif: 'image/avif',
+ gif: 'image/gif',
+}
+const text = (v: unknown, max = 128) => {
+ if (typeof v !== 'string' || !v.trim() || v.length > max)
+ throw createError({ statusCode: 400 })
+ return v.trim()
+}
+const number = (v: unknown) => {
+ if (!Number.isSafeInteger(v) || Number(v) < 1)
+ throw createError({ statusCode: 400 })
+ return Number(v)
+}
+const ID = (v: unknown) => {
+ const s = text(v)
+ if (!/^[a-zA-Z0-9_-]+$/.test(s)) throw createError({ statusCode: 400 })
+ return s
+}
+export default definePlatformHandler(async (event) => {
+ const mutation = event.method !== 'GET',
+ ctx = await cloudMedia(event, mutation)
+ const { siteId, userId, site, db, raw, sql, repository, vault } = ctx
+ const operation = getRouterParam(event, 'operation') ?? ''
+ const [kind, id, action] = operation.split('/')
+ const body = mutation ? await siteManagementBody(event) : {}
+ const now = Date.now()
+ if (kind === 'storage' && !id) {
+ if (event.method === 'GET') return { connections: await vault.list() }
+ if (event.method !== 'POST') throw createError({ statusCode: 405 })
+ if ((await vault.list()).length >= 20)
+ throw createError({ statusCode: 429 })
+ const config = {
+ endpoint: text(body.endpoint, 512),
+ region: text(body.region, 40),
+ bucket: text(body.bucket, 63),
+ accessKeyId: text(body.accessKeyId, 256),
+ secretAccessKey: text(body.secretAccessKey, 256),
+ }
+ await checkStorage(
+ config,
+ useRuntimeConfig(event).platform.portalOrigin,
+ ctx.allowedHosts,
+ )
+ const connectionId = crypto.randomUUID(),
+ { accessKeyId, secretAccessKey, ...publicConfig } = config
+ await vault.create(
+ connectionId,
+ publicConfig,
+ { accessKeyId, secretAccessKey },
+ now,
+ )
+ return { id: connectionId, revision: 1 }
+ }
+ if (
+ kind === 'storage' &&
+ id &&
+ action === 'rotate' &&
+ event.method === 'POST'
+ ) {
+ const old = await vault.get(ID(id))
+ if (!old) throw createError({ statusCode: 404 })
+ const credentials = {
+ accessKeyId: text(body.accessKeyId, 256),
+ secretAccessKey: text(body.secretAccessKey, 256),
+ }
+ await checkStorage(
+ { ...old.config, ...credentials },
+ useRuntimeConfig(event).platform.portalOrigin,
+ ctx.allowedHosts,
+ )
+ if (!(await vault.rotate(id, number(body.revision), credentials, now)))
+ throw createError({ statusCode: 409 })
+ return { ok: true }
+ }
+ if (kind === 'usage' && event.method === 'GET') {
+ const usage = (
+ await sql<{ photos: number; pending: number; bytes: number }>(
+ `SELECT (SELECT COUNT(*) FROM cloud_photos WHERE site_id=? AND deleted=0) AS photos,(SELECT COUNT(*) FROM cloud_uploads WHERE site_id=? AND state='pending' AND expires_at>?) AS pending,COALESCE((SELECT SUM(bytes) FROM cloud_usage_events WHERE site_id=? AND created_at>?),0) AS bytes`,
+ siteId,
+ siteId,
+ now,
+ siteId,
+ now - 30 * 86400000,
+ )
+ )[0]
+ return {
+ usage,
+ limits: { fileBytes: 104857600, photos: 1000, monthlyBytes: 5368709120 },
+ }
+ }
+ if (kind === 'uploads' && !id && event.method === 'POST') {
+ const uploadId = ID(body.requestId),
+ connectionId = ID(body.connectionId),
+ bytes = number(body.bytes),
+ fileName = text(body.fileName, 255)
+ const liveVideo = body.liveVideo as
+ | { fileName?: unknown; bytes?: unknown }
+ | undefined
+ const videoBytes = liveVideo ? number(liveVideo.bytes) : 0
+ if (
+ liveVideo &&
+ (typeof liveVideo.fileName !== 'string' ||
+ !liveVideo.fileName.toLowerCase().endsWith('.mov') ||
+ liveVideo.fileName.replace(/\.[^.]+$/, '').toLowerCase() !==
+ fileName.replace(/\.[^.]+$/, '').toLowerCase())
+ )
+ throw createError({ statusCode: 400 })
+ const ext = fileName.split('.').pop()?.toLowerCase() ?? '',
+ contentType = types[ext]
+ if (!contentType || bytes + videoBytes > 104857600)
+ throw createError({ statusCode: 400 })
+ let upload = await repository.upload(uploadId, userId)
+ if (!upload) {
+ const stored = await vault.get(connectionId)
+ if (!stored) throw createError({ statusCode: 404 })
+ const objectKey = `chronoframe/${siteId}/originals/${uploadId}/1/source.${ext}`
+ await db.batch([
+ db
+ .prepare(`INSERT INTO cloud_uploads(site_id,id,owner_id,photo_id,source_version,connection_id,connection_revision,object_key,expected_bytes,expires_at,content_type,file_name,expected_video_bytes,video_key)
+ SELECT ?,?,?,?,'1',?,?,?,?,?,?,?,?,? WHERE
+ (SELECT COUNT(*) FROM cloud_photos WHERE site_id=? AND deleted=0)+(SELECT COUNT(*) FROM cloud_uploads WHERE site_id=? AND state='pending' AND expires_at>?)<1000 AND
+ COALESCE((SELECT SUM(bytes) FROM cloud_usage_events WHERE site_id=? AND created_at>?),0)+COALESCE((SELECT SUM(expected_bytes+expected_video_bytes) FROM cloud_uploads WHERE site_id=? AND state='pending' AND expires_at>?),0)+?<=5368709120
+ ON CONFLICT DO NOTHING`)
+ .bind(
+ siteId,
+ uploadId,
+ userId,
+ uploadId,
+ connectionId,
+ stored.revision,
+ objectKey,
+ bytes,
+ now + 86400000,
+ contentType,
+ fileName,
+ videoBytes,
+ liveVideo
+ ? `chronoframe/${siteId}/originals/${uploadId}/1/source.MOV`
+ : null,
+ siteId,
+ siteId,
+ now,
+ siteId,
+ now - 30 * 86400000,
+ siteId,
+ now,
+ bytes + videoBytes,
+ ),
+ ])
+ upload = await repository.upload(uploadId, userId)
+ }
+ if (!upload) throw createError({ statusCode: 429 })
+ if (
+ upload.expected_bytes !== bytes ||
+ upload.expected_video_bytes !== videoBytes ||
+ upload.connection_id !== connectionId
+ )
+ throw createError({ statusCode: 409 })
+ return { id: uploadId }
+ }
+ if (kind === 'uploads' && id) {
+ const upload = await repository.upload(ID(id), userId)
+ if (!upload) throw createError({ statusCode: 404 })
+ const detail = (
+ await sql<{
+ connection_revision: number
+ content_type: string
+ file_name: string
+ expected_video_bytes: number
+ video_key: string | null
+ }>(
+ 'SELECT connection_revision,content_type,file_name,expected_video_bytes,video_key FROM cloud_uploads WHERE site_id=? AND id=?',
+ siteId,
+ id,
+ )
+ )[0]!
+ const { s3 } = await ctx.storage(
+ upload.connection_id,
+ detail.connection_revision,
+ )
+ if (event.method === 'GET')
+ return { state: upload.state, expired: upload.expires_at <= now }
+ if (event.method === 'POST' && action === 'plan') {
+ if (upload.state !== 'pending' || upload.expires_at <= now)
+ return { state: upload.state, expired: upload.expires_at <= now }
+
+ const videoUrl = detail.video_key
+ ? await s3.signPut(
+ detail.video_key,
+ detail.expected_video_bytes,
+ 'video/quicktime',
+ )
+ : null
+ if (upload.expected_bytes <= 10485760)
+ return {
+ state: 'pending',
+ mode: 'single',
+ videoUrl,
+ url: await s3.signPut(
+ upload.object_key,
+ upload.expected_bytes,
+ detail.content_type,
+ ),
+ contentType: detail.content_type,
+ }
+ let providerId = upload.provider_upload_id
+ if (!providerId) {
+ const token = crypto.randomUUID()
+ const lock = await raw
+ .prepare(
+ `UPDATE cloud_uploads SET transport_token=?,transport_lease_until=? WHERE site_id=? AND id=? AND state='pending' AND provider_upload_id IS NULL AND (transport_lease_until IS NULL OR transport_lease_until<=?) RETURNING id`,
+ )
+ .bind(token, now + 30000, siteId, id, now)
+ .first()
+ if (!lock) throw createError({ statusCode: 409 })
+ const recovered = await s3.recoverMultipart(upload.object_key)
+ providerId =
+ recovered[0] ??
+ (await s3.multipart(upload.object_key, detail.content_type))
+ for (const abandoned of recovered.slice(1))
+ await s3.abort(upload.object_key, abandoned)
+ const updated = await raw
+ .prepare(
+ `UPDATE cloud_uploads SET provider_upload_id=?,transport_token=NULL,transport_lease_until=NULL WHERE site_id=? AND id=? AND transport_token=? AND state='pending' RETURNING id`,
+ )
+ .bind(providerId, siteId, id, token)
+ .first()
+ if (!updated) {
+ await s3.abort(upload.object_key, providerId)
+ throw createError({ statusCode: 409 })
+ }
+ }
+ const parts = await s3.parts(upload.object_key, providerId),
+ count = Math.ceil(upload.expected_bytes / 10485760)
+ const urls = await Promise.all(
+ Array.from({ length: count }, (_, i) =>
+ s3.signPart(
+ upload.object_key,
+ providerId!,
+ i + 1,
+ Math.min(10485760, upload.expected_bytes - i * 10485760),
+ ),
+ ),
+ )
+ return {
+ state: 'pending',
+ mode: 'multipart',
+ videoUrl,
+ partBytes: 10485760,
+ parts,
+ urls,
+ }
+ }
+ if (event.method === 'DELETE') {
+ if (upload.state !== 'pending') throw createError({ statusCode: 409 })
+ await repository.cancelUpload(id, userId)
+ await db.batch([
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,provider_upload_id,available_at) VALUES(?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`,
+ )
+ .bind(
+ siteId,
+ 'upload-' + id,
+ upload.connection_id,
+ detail.connection_revision,
+ upload.object_key,
+ upload.provider_upload_id,
+ now + 360000,
+ ),
+ ])
+ if (detail.video_key)
+ await raw
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,available_at) VALUES(?,?,?,?,?,?) ON CONFLICT DO NOTHING`,
+ )
+ .bind(
+ siteId,
+ 'upload-video-' + id,
+ upload.connection_id,
+ detail.connection_revision,
+ detail.video_key,
+ now + 360000,
+ )
+ .run()
+ return { ok: true }
+ }
+ if (event.method === 'POST' && action === 'complete') {
+ if (upload.state === 'completed') return { jobId: id }
+ if (upload.state !== 'pending' || upload.expires_at <= now)
+ throw createError({ statusCode: 409 })
+ if (upload.provider_upload_id) {
+ // After a lost completion response, an already-created exact-size object is
+ // authoritative. Otherwise recover provider parts and complete once more.
+ const existing = await s3.head(upload.object_key).catch(() => null)
+ if (!existing) {
+ const parts = await s3.parts(
+ upload.object_key,
+ upload.provider_upload_id,
+ )
+ const expected = Math.ceil(upload.expected_bytes / 10485760)
+ if (
+ parts.length !== expected ||
+ parts.some(
+ (p, i) =>
+ p.partNumber !== i + 1 ||
+ p.bytes !==
+ (i === expected - 1
+ ? upload.expected_bytes - i * 10485760
+ : 10485760),
+ )
+ )
+ throw createError({ statusCode: 409 })
+ await s3.finish(upload.object_key, upload.provider_upload_id, parts)
+ }
+ }
+ const head = await s3.head(upload.object_key)
+ if (
+ head.bytes !== upload.expected_bytes ||
+ head.contentType !== detail.content_type
+ )
+ throw createError({ statusCode: 400 })
+ let videoEtag: string | null = null
+ if (detail.video_key) {
+ const video = await s3.head(detail.video_key)
+ if (
+ video.bytes !== detail.expected_video_bytes ||
+ video.contentType !== 'video/quicktime'
+ )
+ throw createError({ statusCode: 400 })
+ videoEtag = video.etag
+ }
+ await raw
+ .prepare(
+ "UPDATE cloud_uploads SET video_etag=?,verified_etag=? WHERE site_id=? AND id=? AND state='pending'",
+ )
+ .bind(videoEtag, head.etag, siteId, id)
+ .run()
+ await repository.completeUpload({
+ id,
+ ownerId: userId,
+ jobId: id,
+ pipelineVersion: '1',
+ title: detail.file_name,
+ now,
+ })
+ return { jobId: id }
+ }
+ }
+ if (kind === 'photos' && !id && event.method === 'GET')
+ return {
+ photos: await ctx.resources.listPhotos(),
+ jobs: await sql(
+ 'SELECT id,state,attempts,error_code,photo_id FROM cloud_processing_jobs WHERE site_id=? ORDER BY created_at DESC LIMIT 100',
+ siteId,
+ ),
+ }
+ if (kind === 'photos' && id && event.method === 'GET') {
+ const photo = (await ctx.resources.listPhotos()).find(
+ (p) => p.id === ID(id),
+ )
+ const revision = await raw
+ .prepare(
+ 'SELECT revision FROM cloud_photos WHERE site_id=? AND id=? AND deleted=0',
+ )
+ .bind(siteId, id)
+ .first<{ revision: number }>()
+ if (!photo || !revision) throw createError({ statusCode: 404 })
+ return { photo, revision: revision.revision }
+ }
+ if (kind === 'photos' && id && event.method === 'PATCH') {
+ const title = text(body.title, 255),
+ description =
+ body.description === null ? null : text(body.description, 2048),
+ revision = number(body.revision)
+ const changed = await raw
+ .prepare(
+ `UPDATE cloud_photos SET title=?,metadata_json=json_set(metadata_json,'$.description',?),revision=revision+1 WHERE site_id=? AND id=? AND revision=? AND deleted=0 AND metadata_json IS NOT NULL RETURNING id`,
+ )
+ .bind(title, description, siteId, ID(id), revision)
+ .first()
+ if (!changed) throw createError({ statusCode: 409 })
+ return { revision: revision + 1 }
+ }
+ if (kind === 'photos' && id && event.method === 'DELETE') {
+ const found = await raw
+ .prepare('SELECT id FROM cloud_photos WHERE site_id=? AND id=?')
+ .bind(siteId, ID(id))
+ .first()
+ if (!found) throw createError({ statusCode: 404 })
+ await repository.deletePhoto(id)
+ await db.batch([
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,provider_upload_id,available_at) SELECT site_id,'original-'||id,connection_id,connection_revision,object_key,NULL,? FROM cloud_uploads WHERE site_id=? AND photo_id=? ON CONFLICT DO NOTHING`,
+ )
+ .bind(now + 360000, siteId, id),
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,provider_upload_id,available_at) SELECT site_id,'video-'||id,connection_id,connection_revision,video_key,NULL,? FROM cloud_uploads WHERE site_id=? AND photo_id=? AND video_key IS NOT NULL ON CONFLICT DO NOTHING`,
+ )
+ .bind(now + 360000, siteId, id),
+ db
+ .prepare(
+ `INSERT INTO cloud_cleanup_requests(site_id,id,connection_id,connection_revision,object_key,provider_upload_id,available_at) SELECT site_id,'asset-'||photo_id||'-'||role,connection_id,connection_revision,object_key,NULL,? FROM cloud_media_assets WHERE site_id=? AND photo_id=? ON CONFLICT DO NOTHING`,
+ )
+ .bind(now + 360000, siteId, id),
+ ])
+ return { ok: true }
+ }
+ if (kind === 'jobs' && id && action === 'retry' && event.method === 'POST') {
+ const changed = await raw
+ .prepare(
+ `UPDATE cloud_processing_jobs SET state='queued',available_at=?,max_attempts=attempts+3,error_code=NULL WHERE site_id=? AND id=? AND state='failed' AND attempts<=6 AND EXISTS(SELECT 1 FROM cloud_photos WHERE site_id=? AND id=cloud_processing_jobs.photo_id AND deleted=0) RETURNING id`,
+ )
+ .bind(now, siteId, ID(id), siteId)
+ .first()
+ if (!changed) throw createError({ statusCode: 409 })
+ return { ok: true }
+ }
+ if (kind === 'albums' && !id) {
+ if (event.method === 'GET')
+ return {
+ albums: await createResourceReader(ctx.resources).listAlbums({
+ admin: true,
+ }),
+ }
+ if (event.method === 'POST') {
+ const albumId = String(number(body.id))
+ await repository.createAlbum(albumId, text(body.title, 80))
+ return { id: Number(albumId) }
+ }
+ }
+ if (kind === 'albums' && id && event.method === 'DELETE') {
+ const albumId = String(number(Number(id)))
+ if (!(await repository.album(albumId)))
+ throw createError({ statusCode: 404 })
+ if (action === 'photos') {
+ await raw
+ .prepare(
+ 'DELETE FROM cloud_album_photos WHERE site_id=? AND album_id=? AND photo_id=?',
+ )
+ .bind(siteId, albumId, ID(body.photoId))
+ .run()
+ } else if (!action) {
+ await raw.batch([
+ raw
+ .prepare(
+ 'DELETE FROM platform_album_access WHERE site_id=? AND album_id=?',
+ )
+ .bind(siteId, Number(albumId)),
+ raw
+ .prepare(
+ 'DELETE FROM cloud_album_photos WHERE site_id=? AND album_id=?',
+ )
+ .bind(siteId, albumId),
+ raw
+ .prepare('DELETE FROM cloud_albums WHERE site_id=? AND id=?')
+ .bind(siteId, albumId),
+ ])
+ } else throw createError({ statusCode: 404 })
+ return { ok: true }
+ }
+ if (
+ kind === 'albums' &&
+ id &&
+ action === 'settings' &&
+ event.method === 'GET'
+ ) {
+ const album = await ctx.resources.getAlbum(number(Number(id)))
+ const revision = await raw
+ .prepare('SELECT revision FROM cloud_albums WHERE site_id=? AND id=?')
+ .bind(siteId, id)
+ .first<{ revision: number }>()
+ if (!album || !revision) throw createError({ statusCode: 404 })
+ return { album: album.resource, revision: revision.revision }
+ }
+ if (
+ kind === 'albums' &&
+ id &&
+ action === 'settings' &&
+ event.method === 'PATCH'
+ ) {
+ const revision = number(body.revision),
+ title = text(body.title, 80)
+ const changed = await raw
+ .prepare(
+ 'UPDATE cloud_albums SET title=?,revision=revision+1,updated_at=? WHERE site_id=? AND id=? AND revision=? RETURNING id',
+ )
+ .bind(title, now, siteId, String(number(Number(id))), revision)
+ .first()
+ if (!changed) throw createError({ statusCode: 409 })
+ return { revision: revision + 1 }
+ }
+ if (kind === 'albums' && id && event.method === 'POST') {
+ const albumId = String(number(Number(id))),
+ album = await repository.album(albumId)
+ if (!album) throw createError({ statusCode: 404 })
+ if (action === 'photos') {
+ await repository.addToAlbum(albumId, ID(body.photoId))
+ return { ok: true }
+ }
+ if (action === 'access') {
+ const hidden = body.hidden === true,
+ password = body.password
+ if (
+ password !== null &&
+ (typeof password !== 'string' ||
+ password.length < 8 ||
+ password.length > 256)
+ )
+ throw createError({ statusCode: 400 })
+ const salt = crypto.randomUUID(),
+ revision = crypto.randomUUID()
+ await db.batch([
+ db
+ .prepare(
+ 'UPDATE cloud_albums SET hidden=?,updated_at=? WHERE site_id=? AND id=?',
+ )
+ .bind(hidden ? 1 : 0, now, siteId, albumId),
+ password === null
+ ? db
+ .prepare(
+ 'DELETE FROM platform_album_access WHERE site_id=? AND album_id=?',
+ )
+ .bind(siteId, Number(albumId))
+ : db
+ .prepare(
+ `INSERT INTO platform_album_access(site_id,album_id,revision,salt,password_hash) VALUES(?,?,?,?,?) ON CONFLICT(site_id,album_id) DO UPDATE SET revision=excluded.revision,salt=excluded.salt,password_hash=excluded.password_hash`,
+ )
+ .bind(
+ siteId,
+ Number(albumId),
+ revision,
+ salt,
+ await visitorPasswordHash(password as string, salt),
+ ),
+ ])
+ return { ok: true }
+ }
+ }
+ if (kind === 'publish' && event.method === 'POST') {
+ if (!site.slug) throw createError({ statusCode: 400 })
+ const revision = number(body.revision),
+ published = body.published === true
+ if (published && !(await ctx.resources.listPhotos()).length)
+ throw createError({ statusCode: 400 })
+ const hostname = site.slug + '.site.chronoframe.art'
+ const changes = await raw.batch([
+ raw
+ .prepare(
+ `INSERT INTO platform_domains(hostname,site_id,verified,active) SELECT ?,id,1,? FROM platform_sites WHERE id=? AND revision=? AND EXISTS(SELECT 1 FROM platform_memberships WHERE workspace_id=platform_sites.workspace_id AND user_id=? AND role='admin') ON CONFLICT(hostname) DO UPDATE SET active=excluded.active WHERE platform_domains.site_id=excluded.site_id`,
+ )
+ .bind(hostname, published ? 1 : 0, siteId, revision, userId),
+ raw
+ .prepare(
+ `UPDATE platform_sites SET published=?,revision=revision+1 WHERE id=? AND revision=? AND EXISTS(SELECT 1 FROM platform_memberships WHERE workspace_id=platform_sites.workspace_id AND user_id=? AND role='admin') AND (?=0 OR EXISTS(SELECT 1 FROM platform_domains WHERE hostname=? AND site_id=platform_sites.id))`,
+ )
+ .bind(
+ published ? 1 : 0,
+ siteId,
+ revision,
+ userId,
+ published ? 1 : 0,
+ hostname,
+ ),
+ ])
+ if (changes[1]?.meta.changes !== 1) throw createError({ statusCode: 409 })
+ return { published, hostname, revision: revision + 1 }
+ }
+ throw createError({ statusCode: 404 })
+})
diff --git a/apps/cloud/server/api/sites/[siteId]/media/file/[photoId]/[role].get.ts b/apps/cloud/server/api/sites/[siteId]/media/file/[photoId]/[role].get.ts
new file mode 100644
index 00000000..49983f22
--- /dev/null
+++ b/apps/cloud/server/api/sites/[siteId]/media/file/[photoId]/[role].get.ts
@@ -0,0 +1,13 @@
+import { getRouterParam } from 'h3'
+import { cloudMedia } from '../../../../../../utils/cloud-media'
+import { definePlatformHandler } from '../../../../../../../../shared/platform/runtime'
+import { privateMedia } from '../../../../../../../../shared/platform/media'
+export default definePlatformHandler(async (event) => {
+ const { siteId } = await cloudMedia(event)
+ return privateMedia(
+ event,
+ siteId,
+ getRouterParam(event, 'photoId') ?? '',
+ getRouterParam(event, 'role') ?? '',
+ )
+})
diff --git a/apps/cloud/server/middleware/platform-boundary.ts b/apps/cloud/server/middleware/platform-boundary.ts
index ac3c4ea8..b1f3612e 100644
--- a/apps/cloud/server/middleware/platform-boundary.ts
+++ b/apps/cloud/server/middleware/platform-boundary.ts
@@ -21,6 +21,13 @@ export default defineEventHandler((event) => {
'/api/workspaces/default',
].includes(path) ||
/^\/api\/sites\/[1-9][0-9]*\/(?:context|events|settings)$/.test(path)
+ if (
+ /^\/api\/sites\/[1-9][0-9]*\/media(?:\/|$)/.test(path) ||
+ /^\/api\/internal\/worker\/(?:claim|heartbeat|grant|complete|fail|cleanup)$/.test(
+ path,
+ )
+ )
+ return
if (platformRoute || accountRouteAllowed(path, event.method)) return
if (path === '/api' || (path.startsWith('/api/') && path !== '/api/health')) {
throw createError({ statusCode: 503, statusMessage: 'Service unavailable' })
diff --git a/apps/cloud/server/utils/cloud-media.ts b/apps/cloud/server/utils/cloud-media.ts
new file mode 100644
index 00000000..44e52bce
--- /dev/null
+++ b/apps/cloud/server/utils/cloud-media.ts
@@ -0,0 +1,135 @@
+import { createError, getRouterParam } from 'h3'
+import type { H3Event } from 'h3'
+import {
+ createCloudMediaRepository,
+ type MediaDatabase,
+} from '@chronoframe/database/cloud-media'
+import { createCloudResourceRepository } from '@chronoframe/database/cloud-resources'
+import {
+ createCloudS3,
+ validateCloudS3,
+ type CloudS3Config,
+} from '@chronoframe/storage/s3-edge'
+import { createStorageCipher } from '@chronoframe/storage/credential-cipher'
+import { createStorageVault } from '../../../../packages/platform/src/index'
+import { platformDatabase } from '../../../shared/platform/runtime'
+import { siteManagement } from './site-management'
+
+export async function cloudMedia(event: H3Event, mutation = false) {
+ const { userId, store } = await siteManagement(event, mutation)
+ const siteId = Number(getRouterParam(event, 'siteId'))
+ const site = await store.get(userId, siteId)
+ if (mutation && site.role !== 'admin') throw createError({ statusCode: 403 })
+ const raw = platformDatabase(event),
+ db = raw as unknown as MediaDatabase
+ if (mutation) {
+ const kind = (getRouterParam(event, 'operation') ?? '').split('/')[0]
+ const bucket = [
+ 'storage',
+ 'uploads',
+ 'photos',
+ 'albums',
+ 'publish',
+ ].includes(kind ?? '')
+ ? kind
+ : 'other'
+ const window = Math.floor(Date.now() / 60000)
+ const rate = await raw
+ .prepare(
+ `INSERT INTO platform_auth_attempts(key,window_start,attempts) VALUES(?,?,1) ON CONFLICT(key) DO UPDATE SET attempts=CASE WHEN window_start=excluded.window_start THEN attempts+1 ELSE 1 END,window_start=excluded.window_start RETURNING attempts`,
+ )
+ .bind('cloud-media:' + userId + ':' + bucket, window)
+ .first<{ attempts: number }>()
+ if (
+ !rate ||
+ rate.attempts >
+ (bucket === 'storage' ? 4 : bucket === 'uploads' ? 600 : 240)
+ )
+ throw createError({ statusCode: 429 })
+ }
+ const cfg = useRuntimeConfig(event).platform
+ const vault = createStorageVault(
+ raw,
+ siteId,
+ await createStorageCipher(cfg.storageSecret),
+ )
+ const allowedHosts = cfg.s3EndpointHosts
+ .split(',')
+ .map((h) => h.trim())
+ .filter(Boolean)
+ async function storage(id: string, revision?: number) {
+ const item = await vault.get(id, revision)
+ if (!item) throw createError({ statusCode: 404 })
+ return {
+ revision: item.revision,
+ s3: createCloudS3(
+ { ...item.config, ...item.credentials },
+ { allowedHosts },
+ ),
+ }
+ }
+ const sql = async <T>(query: string, ...values: (string | number | null)[]) =>
+ (
+ await db
+ .prepare(query)
+ .bind(...values)
+ .all<T>()
+ ).results
+ return {
+ siteId,
+ userId,
+ site,
+ raw,
+ db,
+ vault,
+ allowedHosts,
+ storage,
+ sql,
+ repository: createCloudMediaRepository(db, { id: siteId }),
+ resources: createCloudResourceRepository(
+ db,
+ { id: siteId },
+ `/api/sites/${siteId}/media/file`,
+ ),
+ }
+}
+export async function checkStorage(
+ config: CloudS3Config,
+ origin: string,
+ allowedHosts: string[],
+) {
+ validateCloudS3(config, allowedHosts)
+ const s3 = createCloudS3(config, { allowedHosts }),
+ key = 'chronoframe/connection-check/' + crypto.randomUUID()
+ const sample = crypto.getRandomValues(new Uint8Array(32))
+ let written = false
+ try {
+ await s3.put(key, sample, 'application/octet-stream')
+ written = true
+ const head = await s3.head(key),
+ bytes = await s3.getProbe(key)
+ if (
+ head.bytes !== sample.length ||
+ !bytes ||
+ bytes.some((v, i) => v !== sample[i])
+ )
+ throw new Error('Storage read/write check failed')
+ // Probe the presigned path too; raw responses and URLs are never returned/logged.
+ const response = await fetch(await s3.signGet(key), {
+ redirect: 'manual',
+ signal: AbortSignal.timeout(15000),
+ })
+ if (!response.ok) throw new Error('Storage signed access failed')
+ await response.body?.cancel()
+ const multipartId = await s3.multipart(key, 'application/octet-stream')
+ try {
+ if (!(await s3.recoverMultipart(key)).includes(multipartId))
+ throw new Error('Multipart recovery check failed')
+ } finally {
+ await s3.abort(key, multipartId)
+ }
+ if (!(await s3.cors(origin))) throw new Error('Storage CORS check failed')
+ } finally {
+ if (written) await s3.remove(key)
+ }
+}
diff --git a/apps/cloud/server/utils/site-body.ts b/apps/cloud/server/utils/site-body.ts
index 6b64db26..58ea4032 100644
--- a/apps/cloud/server/utils/site-body.ts
+++ b/apps/cloud/server/utils/site-body.ts
@@ -1,36 +1 @@
-import { createError } from 'h3'
-
-/** Bound bytes before decoding or parsing, including requests with no length header. */
-export async function readSiteBody(
- stream: ReadableStream<Uint8Array> | null | undefined,
-): Promise<Record<string, unknown>> {
- if (!stream) throw createError({ statusCode: 400 })
- const reader = stream.getReader()
- const bytes = new Uint8Array(4096)
- let size = 0
- try {
- while (true) {
- const { done, value } = await reader.read()
- if (done) break
- if (size + value.byteLength > bytes.byteLength) {
- await reader.cancel().catch(() => undefined)
- throw createError({ statusCode: 400 })
- }
- bytes.set(value, size)
- size += value.byteLength
- }
- } finally {
- reader.releaseLock()
- }
- let body: unknown
- try {
- body = JSON.parse(
- new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(0, size)),
- )
- } catch {
- throw createError({ statusCode: 400 })
- }
- if (!body || typeof body !== 'object' || Array.isArray(body))
- throw createError({ statusCode: 400 })
- return body as Record<string, unknown>
-}
+export { readSiteBody } from '../../../shared/platform/json.ts'
diff --git a/apps/shared/platform/json.ts b/apps/shared/platform/json.ts
new file mode 100644
index 00000000..2fd4ce96
--- /dev/null
+++ b/apps/shared/platform/json.ts
@@ -0,0 +1,39 @@
+import { createError } from 'h3'
+
+/** Bound bytes before decoding or parsing, including requests with no length header. */
+export async function readSiteBody(
+ stream: ReadableStream<Uint8Array> | null | undefined,
+ limit = 4096,
+): Promise<Record<string, unknown>> {
+ if (!Number.isSafeInteger(limit) || limit < 1 || limit > 131072)
+ throw createError({ statusCode: 400 })
+ if (!stream) throw createError({ statusCode: 400 })
+ const reader = stream.getReader()
+ const bytes = new Uint8Array(limit)
+ let size = 0
+ try {
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+ if (size + value.byteLength > bytes.byteLength) {
+ await reader.cancel().catch(() => undefined)
+ throw createError({ statusCode: 400 })
+ }
+ bytes.set(value, size)
+ size += value.byteLength
+ }
+ } finally {
+ reader.releaseLock()
+ }
+ let body: unknown
+ try {
+ body = JSON.parse(
+ new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(0, size)),
+ )
+ } catch {
+ throw createError({ statusCode: 400 })
+ }
+ if (!body || typeof body !== 'object' || Array.isArray(body))
+ throw createError({ statusCode: 400 })
+ return body as Record<string, unknown>
+}
diff --git a/apps/shared/platform/media.ts b/apps/shared/platform/media.ts
new file mode 100644
index 00000000..3c88ea42
--- /dev/null
+++ b/apps/shared/platform/media.ts
@@ -0,0 +1,152 @@
+import { createError, getHeader } from 'h3'
+import type { H3Event } from 'h3'
+import { createCloudS3 } from '@chronoframe/storage/s3-edge'
+import { createStorageCipher } from '@chronoframe/storage/credential-cipher'
+import { createStorageVault } from '../../../packages/platform/src/index'
+import { createCloudResourceRepository } from '@chronoframe/database/cloud-resources'
+import type { MediaDatabase } from '@chronoframe/database/cloud-media'
+import { photoAccessDenied } from '../../../packages/core/src/access'
+import { platformDatabase } from './runtime'
+import { visitorAccess } from './visitor'
+import type { PublicSiteContext } from '../../../packages/core/src/platform'
+
+/** Check album policy on every role: thumbnail, original and Live Photo share it. */
+export async function authorizePublicPhoto(
+ event: H3Event,
+ context: PublicSiteContext,
+ id: string,
+) {
+ const repository = createCloudResourceRepository(
+ platformDatabase(event) as unknown as MediaDatabase,
+ context.site,
+ '/api/public/media',
+ )
+ const memberships = repository.membershipsForPhoto
+ ? await repository.membershipsForPhoto(id)
+ : (await repository.listMemberships()).filter((m) => m.photoId === id)
+ const grants = new Map<number, boolean>()
+ for (const m of memberships)
+ if (m.protected && !m.hidden)
+ grants.set(
+ m.albumId,
+ (
+ await visitorAccess(
+ event,
+ context,
+ m.albumId,
+ useRuntimeConfig(event).platform.secret,
+ )
+ ).unlocked,
+ )
+ if (
+ photoAccessDenied(
+ false,
+ memberships.map((m) => ({
+ album: {
+ id: m.albumId,
+ isHidden: m.hidden,
+ passwordHash: m.protected ? 'protected' : null,
+ },
+ })),
+ (album) => grants.get(album.id) === true,
+ )
+ )
+ throw createError({ statusCode: 404 })
+}
+/** Stream authorized objects, keeping signed URLs out of HTML, redirects and caches. */
+export async function privateMedia(
+ event: H3Event,
+ siteId: number,
+ photoId: string,
+ role: string,
+) {
+ if (
+ !/^[a-zA-Z0-9_-]{1,128}$/.test(photoId) ||
+ !['original', 'thumbnail', 'live-video', 'display'].includes(role)
+ )
+ throw createError({ statusCode: 404 })
+ const raw = platformDatabase(event)
+ const source = await raw
+ .prepare(
+ `SELECT u.connection_id,u.connection_revision,u.object_key,u.expected_bytes,u.content_type,u.verified_etag FROM cloud_uploads u JOIN cloud_photos p ON p.site_id=u.site_id AND p.id=u.photo_id AND p.source_version=u.source_version WHERE u.site_id=? AND p.id=? AND p.deleted=0 AND p.metadata_json IS NOT NULL AND u.state='completed' AND EXISTS(SELECT 1 FROM cloud_processing_jobs j WHERE j.site_id=p.site_id AND j.photo_id=p.id AND j.source_version=p.source_version AND j.state='succeeded') LIMIT 1`,
+ )
+ .bind(siteId, photoId)
+ .first<{
+ connection_id: string
+ connection_revision: number
+ object_key: string
+ expected_bytes: number
+ content_type: string
+ verified_etag: string | null
+ }>()
+ if (!source) throw createError({ statusCode: 404 })
+ let object = {
+ connection_id: source.connection_id,
+ connection_revision: source.connection_revision,
+ object_key: source.object_key,
+ bytes: source.expected_bytes,
+ content_type: source.content_type,
+ }
+ if (role !== 'original') {
+ const asset = await raw
+ .prepare(
+ `SELECT connection_id,connection_revision,object_key,bytes,content_type FROM cloud_media_assets a WHERE site_id=? AND photo_id=? AND role=? AND source_version=(SELECT source_version FROM cloud_photos WHERE site_id=? AND id=?) LIMIT 1`,
+ )
+ .bind(siteId, photoId, role, siteId, photoId)
+ .first<typeof object>()
+ if (!asset && role !== 'display') throw createError({ statusCode: 404 })
+ if (asset) object = asset
+ }
+ const config = useRuntimeConfig(event).platform
+ const vault = createStorageVault(
+ raw,
+ siteId,
+ await createStorageCipher(config.storageSecret),
+ )
+ const stored = await vault.get(
+ object.connection_id,
+ object.connection_revision,
+ )
+ if (!stored) throw createError({ statusCode: 503 })
+ const s3 = createCloudS3(
+ { ...stored.config, ...stored.credentials },
+ {
+ allowedHosts: config.s3EndpointHosts
+ .split(',')
+ .map((v: string) => v.trim())
+ .filter(Boolean),
+ },
+ )
+ const range = getHeader(event, 'range')
+ if (range && !/^bytes=(?:[0-9]+-[0-9]*|-[0-9]+)$/.test(range))
+ throw createError({ statusCode: 400 })
+ const headers: Record<string, string> = {}
+ if (range) headers.Range = range
+ if (object.object_key === source.object_key && source.verified_etag)
+ headers['If-Match'] = source.verified_etag
+ const response = await fetch(await s3.signGet(object.object_key), {
+ headers,
+ redirect: 'manual',
+ signal: AbortSignal.timeout(30000),
+ })
+ if (!response.ok) {
+ await response.body?.cancel()
+ throw createError({ statusCode: 404 })
+ }
+ const length = Number(response.headers.get('content-length'))
+ if (length > object.bytes) {
+ await response.body?.cancel()
+ throw createError({ statusCode: 503 })
+ }
+ const out = new Headers({
+ 'Cache-Control': 'private, no-store',
+ 'Content-Type': object.content_type,
+ 'X-Content-Type-Options': 'nosniff',
+ 'Content-Disposition': 'inline',
+ })
+ for (const name of ['content-length', 'content-range', 'accept-ranges']) {
+ const value = response.headers.get(name)
+ if (value) out.set(name, value)
+ }
+ return new Response(response.body, { status: response.status, headers: out })
+}
diff --git a/apps/site/app/layouts/default.vue b/apps/site/app/layouts/default.vue
new file mode 100644
index 00000000..e933f51a
--- /dev/null
+++ b/apps/site/app/layouts/default.vue
@@ -0,0 +1,126 @@
+<script setup lang="ts">
+const context = useEntryContext()
+</script>
+<template>
+ <div class="site-frame">
+ <header>
+ <NuxtLink
+ to="/"
+ class="site-title"
+ >{{ context?.name || 'ChronoFrame' }}</NuxtLink
+ >
+ <nav aria-label="Gallery navigation">
+ <NuxtLink to="/">Photographs</NuxtLink
+ ><NuxtLink to="/albums">Albums</NuxtLink>
+ </nav>
+ </header>
+ <main><slot /></main>
+ <footer>
+ Made with <a href="https://chronoframe.art/">ChronoFrame</a>
+ </footer>
+ </div>
+</template>
+<style>
+.site-frame {
+ max-width: 1280px;
+ margin: auto;
+ padding: 24px;
+ color: #243128;
+ font-family: system-ui, sans-serif;
+}
+.site-frame header {
+ display: flex;
+ gap: 20px;
+ align-items: center;
+ justify-content: space-between;
+ margin-bottom: 32px;
+ border-bottom: 1px solid #d9dfd5;
+ padding-bottom: 18px;
+}
+.site-title {
+ font-size: 20px;
+ font-weight: 650;
+ min-width: 0;
+ overflow-wrap: anywhere;
+}
+.site-frame nav {
+ display: flex;
+ gap: 16px;
+ flex-shrink: 0;
+ font-size: 14px;
+}
+.site-frame a {
+ color: inherit;
+}
+.site-frame h1 {
+ font-size: 28px;
+ font-weight: 500;
+ margin-bottom: 24px;
+}
+.site-frame footer {
+ border-top: 1px solid #d9dfd5;
+ margin-top: 40px;
+ padding-top: 18px;
+ font-size: 12px;
+ opacity: 0.7;
+}
+.site-frame input {
+ font-size: 16px;
+ min-height: 42px;
+ padding: 8px;
+ border: 1px solid #c5cec0;
+ border-radius: 5px;
+ max-width: 100%;
+}
+.site-frame button {
+ font: inherit;
+}
+.site-album-list {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
+ gap: 12px;
+}
+.site-album-list a {
+ display: block;
+ padding: 18px;
+ border: 1px solid #d9dfd5;
+ border-radius: 6px;
+ text-decoration: none;
+}
+.site-unlock {
+ display: flex;
+ gap: 10px;
+ flex-wrap: wrap;
+}
+.site-unlock button {
+ padding: 10px 16px;
+ background: #344d38;
+ color: #fff;
+ border: 0;
+ border-radius: 5px;
+}
+@media (max-width: 480px) {
+ .site-frame {
+ padding: 16px;
+ }
+ .site-frame header {
+ gap: 12px;
+ }
+ .site-title {
+ font-size: 17px;
+ }
+ .site-frame nav {
+ gap: 10px;
+ font-size: 12px;
+ }
+}
+@media (prefers-color-scheme: dark) {
+ .site-frame {
+ color: #d9e1d6;
+ }
+ .site-frame input {
+ background: #202b22;
+ color: inherit;
+ }
+}
+</style>
diff --git a/apps/site/app/middleware/site.global.ts b/apps/site/app/middleware/site.global.ts
index 33254c6b..3892e062 100644
--- a/apps/site/app/middleware/site.global.ts
+++ b/apps/site/app/middleware/site.global.ts
@@ -15,14 +15,10 @@ export default defineNuxtRouteMiddleware(async (to) => {
context.value = null
return abortNavigation(error as Error)
}
- // Photo repositories are delivered in P4; never fall back to Self-hosted data.
- if (to.path !== '/') {
- context.value = null
- return abortNavigation(
- createError({
- statusCode: 503,
- statusMessage: 'Gallery content is not available yet',
- }),
- )
- }
+ if (
+ to.path !== '/' &&
+ to.path !== '/albums' &&
+ !/^\/albums\/[1-9][0-9]*$/.test(to.path)
+ )
+ return abortNavigation(createError({ statusCode: 404 }))
})
diff --git a/apps/site/app/pages/albums/[albumId].vue b/apps/site/app/pages/albums/[albumId].vue
new file mode 100644
index 00000000..6b7146bf
--- /dev/null
+++ b/apps/site/app/pages/albums/[albumId].vue
@@ -0,0 +1,75 @@
+<script setup lang="ts">
+import type { Album, Photo } from '@chronoframe/contracts/resources'
+const route = useRoute(),
+ context = useEntryContext(),
+ id = Number(route.params.albumId)
+const password = ref(''),
+ failed = ref(false),
+ busy = ref(false)
+const { data, error, refresh } = await useFetch<{
+ album: Album & { photos: Photo[] }
+}>(`/api/public/albums/${id}`, {
+ key: `public-album-${context.value?.scope}-${id}`,
+})
+async function unlock() {
+ busy.value = true
+ failed.value = false
+ try {
+ await $fetch(`/api/public/albums/${id}/unlock`, {
+ method: 'POST',
+ body: { password: password.value },
+ })
+ password.value = ''
+ await refresh()
+ } catch {
+ failed.value = true
+ } finally {
+ busy.value = false
+ }
+}
+useSeoMeta({
+ title: () => data.value?.album.title ?? 'Album',
+ robots: 'noindex',
+})
+</script>
+<template>
+ <section>
+ <h1>{{ data?.album.title || 'Album' }}</h1>
+ <template v-if="error"
+ ><form
+ v-if="error.statusCode === 403"
+ class="site-unlock"
+ @submit.prevent="unlock"
+ >
+ <label
+ >Album password
+ <input
+ v-model="password"
+ type="password"
+ required
+ autocomplete="current-password" /></label
+ ><button
+ type="submit"
+ :disabled="busy"
+ >
+ Unlock album
+ </button>
+ </form>
+ <p
+ v-else
+ role="alert"
+ >
+ This album is unavailable.
+ </p>
+ <p
+ v-if="failed"
+ role="alert"
+ >
+ The password was not accepted. Please try again.
+ </p></template
+ ><GalleryGrid
+ v-else-if="data"
+ :photos="data.album.photos"
+ />
+ </section>
+</template>
diff --git a/apps/site/app/pages/albums/index.vue b/apps/site/app/pages/albums/index.vue
new file mode 100644
index 00000000..3cbdf6e3
--- /dev/null
+++ b/apps/site/app/pages/albums/index.vue
@@ -0,0 +1,35 @@
+<script setup lang="ts">
+import type { Album } from '@chronoframe/contracts/resources'
+const context = useEntryContext()
+const { data, error } = await useFetch<{ albums: Album[] }>(
+ '/api/public/albums',
+ { key: `public-albums-${context.value?.scope}` },
+)
+useSeoMeta({
+ title: () => `Albums — ${context.value?.name ?? 'ChronoFrame'}`,
+ robots: 'noindex',
+})
+</script>
+<template>
+ <section>
+ <h1>Albums</h1>
+ <p
+ v-if="error"
+ role="alert"
+ >
+ Albums could not be loaded.
+ </p>
+ <div
+ v-else
+ class="site-album-list"
+ >
+ <NuxtLink
+ v-for="album in data?.albums"
+ :key="album.id"
+ :to="'/albums/' + album.id"
+ ><h2>{{ album.title }}</h2>
+ <p v-if="album.hasPassword">Password protected</p></NuxtLink
+ >
+ </div>
+ </section>
+</template>
diff --git a/apps/site/app/pages/index.vue b/apps/site/app/pages/index.vue
index db268eb8..d674e526 100644
--- a/apps/site/app/pages/index.vue
+++ b/apps/site/app/pages/index.vue
@@ -1,10 +1,28 @@
<script setup lang="ts">
+import type { Photo } from '@chronoframe/contracts/resources'
const context = useEntryContext()
useSeoMeta({
title: () => context.value?.name ?? 'ChronoFrame',
robots: 'noindex',
})
+const { data, error } = await useFetch<{ photos: Photo[] }>(
+ '/api/public/photos',
+ { key: `public-photos-${context.value?.scope}` },
+)
</script>
<template>
- <main class="mx-auto max-w-3xl p-8"><SiteContextCard /></main>
+ <section>
+ <h1>Your photographs. Your perspective.</h1>
+ <p
+ v-if="error"
+ role="alert"
+ >
+ Photographs could not be loaded.
+ </p>
+ <GalleryGrid
+ v-else-if="data?.photos.length"
+ :photos="data.photos"
+ />
+ <p v-else>No public photographs yet.</p>
+ </section>
</template>
diff --git a/apps/site/nuxt.config.ts b/apps/site/nuxt.config.ts
index 6aa385d4..ab83ef09 100644
--- a/apps/site/nuxt.config.ts
+++ b/apps/site/nuxt.config.ts
@@ -18,6 +18,6 @@ export default defineNuxtConfig({
capabilities: SITE_CAPABILITIES,
},
session: { name: 'chronoframe-site' },
- platform: { secret: '' },
+ platform: { secret: '', storageSecret: '', s3EndpointHosts: '' },
},
})
diff --git a/apps/site/package.json b/apps/site/package.json
index 2999706e..b0db1c1a 100644
--- a/apps/site/package.json
+++ b/apps/site/package.json
@@ -33,6 +33,9 @@
"@unhead/vue": "3.4.2",
"@chronoframe/platform": "workspace:*",
"@chronoframe/core": "workspace:*",
- "h3": "1.15.11"
+ "h3": "1.15.11",
+ "@chronoframe/database": "workspace:*",
+ "@chronoframe/storage": "workspace:*",
+ "@chronoframe/contracts": "workspace:*"
}
}
diff --git a/apps/site/server/api/public/albums/[albumId].get.ts b/apps/site/server/api/public/albums/[albumId].get.ts
new file mode 100644
index 00000000..71361953
--- /dev/null
+++ b/apps/site/server/api/public/albums/[albumId].get.ts
@@ -0,0 +1,39 @@
+import { createError, getRouterParam } from 'h3'
+import { createResourceReader } from '@chronoframe/core'
+import { createCloudResourceRepository } from '@chronoframe/database/cloud-resources'
+import type { MediaDatabase } from '@chronoframe/database/cloud-media'
+import {
+ definePlatformHandler,
+ platformDatabase,
+} from '../../../../../shared/platform/runtime'
+import { publicSiteContext } from '../../../../../shared/platform/context'
+import { visitorAccess } from '../../../../../shared/platform/visitor'
+export default definePlatformHandler(async (event) => {
+ const context = await publicSiteContext(event),
+ id = Number(getRouterParam(event, 'albumId'))
+ if (!Number.isSafeInteger(id) || id < 1)
+ throw createError({ statusCode: 404 })
+ const repository = createCloudResourceRepository(
+ platformDatabase(event) as unknown as MediaDatabase,
+ context.site,
+ '/api/public/media',
+ )
+ const album = await repository.getAlbum(id)
+ if (!album || album.resource.isHidden) throw createError({ statusCode: 404 })
+ const unlocked =
+ !album.passwordHash ||
+ (
+ await visitorAccess(
+ event,
+ context,
+ id,
+ useRuntimeConfig(event).platform.secret,
+ )
+ ).unlocked
+ return {
+ album: await createResourceReader(repository).getAlbum(id, {
+ admin: false,
+ canAccess: () => unlocked,
+ }),
+ }
+})
diff --git a/apps/site/server/api/public/albums/[albumId]/unlock.post.ts b/apps/site/server/api/public/albums/[albumId]/unlock.post.ts
index fc5e937a..5288bd8e 100644
--- a/apps/site/server/api/public/albums/[albumId]/unlock.post.ts
+++ b/apps/site/server/api/public/albums/[albumId]/unlock.post.ts
@@ -1,9 +1,11 @@
+import { getRequestWebStream } from 'h3'
+import { readSiteBody } from '../../../../../../shared/platform/json'
import { definePlatformHandler } from '../../../../../../shared/platform/runtime'
import { publicSiteContext } from '../../../../../../shared/platform/context'
import { unlockVisitor } from '../../../../../../shared/platform/visitor'
export default definePlatformHandler(async (event) => {
const context = await publicSiteContext(event)
- const body = await readBody(event)
+ const body = await readSiteBody(getRequestWebStream(event))
return unlockVisitor(
event,
context,
diff --git a/apps/site/server/api/public/albums/index.get.ts b/apps/site/server/api/public/albums/index.get.ts
new file mode 100644
index 00000000..e705d1d1
--- /dev/null
+++ b/apps/site/server/api/public/albums/index.get.ts
@@ -0,0 +1,20 @@
+import { createResourceReader } from '@chronoframe/core'
+import { createCloudResourceRepository } from '@chronoframe/database/cloud-resources'
+import type { MediaDatabase } from '@chronoframe/database/cloud-media'
+import {
+ definePlatformHandler,
+ platformDatabase,
+} from '../../../../../shared/platform/runtime'
+import { publicSiteContext } from '../../../../../shared/platform/context'
+export default definePlatformHandler(async (event) => {
+ const context = await publicSiteContext(event)
+ return {
+ albums: await createResourceReader(
+ createCloudResourceRepository(
+ platformDatabase(event) as unknown as MediaDatabase,
+ context.site,
+ '/api/public/media',
+ ),
+ ).listAlbums({ admin: false }),
+ }
+})
diff --git a/apps/site/server/api/public/media/[photoId]/[role].get.ts b/apps/site/server/api/public/media/[photoId]/[role].get.ts
new file mode 100644
index 00000000..9a019a98
--- /dev/null
+++ b/apps/site/server/api/public/media/[photoId]/[role].get.ts
@@ -0,0 +1,18 @@
+import { getRouterParam } from 'h3'
+import { definePlatformHandler } from '../../../../../../shared/platform/runtime'
+import { publicSiteContext } from '../../../../../../shared/platform/context'
+import {
+ authorizePublicPhoto,
+ privateMedia,
+} from '../../../../../../shared/platform/media'
+export default definePlatformHandler(async (event) => {
+ const context = await publicSiteContext(event),
+ id = getRouterParam(event, 'photoId') ?? ''
+ await authorizePublicPhoto(event, context, id)
+ return privateMedia(
+ event,
+ context.site.id,
+ id,
+ getRouterParam(event, 'role') ?? '',
+ )
+})
diff --git a/apps/site/server/api/public/photos.get.ts b/apps/site/server/api/public/photos.get.ts
new file mode 100644
index 00000000..343fc15f
--- /dev/null
+++ b/apps/site/server/api/public/photos.get.ts
@@ -0,0 +1,20 @@
+import { createResourceReader } from '@chronoframe/core'
+import { createCloudResourceRepository } from '@chronoframe/database/cloud-resources'
+import type { MediaDatabase } from '@chronoframe/database/cloud-media'
+import {
+ definePlatformHandler,
+ platformDatabase,
+} from '../../../../shared/platform/runtime'
+import { publicSiteContext } from '../../../../shared/platform/context'
+export default definePlatformHandler(async (event) => {
+ const context = await publicSiteContext(event)
+ return {
+ photos: await createResourceReader(
+ createCloudResourceRepository(
+ platformDatabase(event) as unknown as MediaDatabase,
+ context.site,
+ '/api/public/media',
+ ),
+ ).listVisiblePhotos(),
+ }
+})
diff --git a/apps/site/server/middleware/platform-boundary.ts b/apps/site/server/middleware/platform-boundary.ts
index 9a2b89b4..7760fb6a 100644
--- a/apps/site/server/middleware/platform-boundary.ts
+++ b/apps/site/server/middleware/platform-boundary.ts
@@ -11,7 +11,15 @@ export default defineEventHandler((event) => {
})
}
if (
- path === '/api/public/context' ||
+ [
+ '/api/public/context',
+ '/api/public/photos',
+ '/api/public/albums',
+ ].includes(path) ||
+ /^\/api\/public\/albums\/[1-9][0-9]*$/.test(path) ||
+ /^\/api\/public\/media\/[a-z0-9_-]+\/(?:original|thumbnail|live-video|display)$/.test(
+ path,
+ ) ||
/^\/api\/public\/albums\/[1-9][0-9]*\/(?:access|unlock)$/.test(path)
)
return
diff --git a/apps/worker/Dockerfile b/apps/worker/Dockerfile
new file mode 100644
index 00000000..bafd733a
--- /dev/null
+++ b/apps/worker/Dockerfile
@@ -0,0 +1,15 @@
+# syntax=docker/dockerfile:1
+FROM node:22.23.3-alpine
+RUN --mount=type=secret,id=proxy_ca --mount=type=secret,id=ca_bundle \
+ if [ -f /run/secrets/ca_bundle ]; then export SSL_CERT_FILE=/run/secrets/ca_bundle; fi; \
+ if [ -f /run/secrets/proxy_ca ]; then export NODE_EXTRA_CA_CERTS=/run/secrets/proxy_ca; fi; \
+ apk add --no-cache ca-certificates perl exiftool && corepack enable
+WORKDIR /app
+COPY . .
+RUN --mount=type=secret,id=proxy_ca \
+ if [ -f /run/secrets/proxy_ca ]; then export NODE_EXTRA_CA_CERTS=/run/secrets/proxy_ca; fi; \
+ NODE_USE_ENV_PROXY=1 pnpm install --frozen-lockfile --prod --ignore-scripts --filter @chronoframe/worker... \
+ && chown -R node:node /app
+USER node
+ENV NODE_ENV=production
+CMD ["node", "--experimental-strip-types", "apps/worker/src/index.ts"]
diff --git a/apps/worker/compose.yaml b/apps/worker/compose.yaml
new file mode 100644
index 00000000..b58363dc
--- /dev/null
+++ b/apps/worker/compose.yaml
@@ -0,0 +1,18 @@
+services:
+ worker:
+ build:
+ context: ../..
+ dockerfile: apps/worker/Dockerfile
+ env_file:
+ - ${CHRONOFRAME_WORKER_ENV_FILE:?Set the path to the generated worker.env}
+ restart: unless-stopped
+ init: true
+ read_only: true
+ tmpfs:
+ - /tmp:size=768m,mode=1777
+ mem_limit: 2g
+ cpus: 2
+ pids_limit: 128
+ cap_drop: [ALL]
+ security_opt: [no-new-privileges:true]
+ stop_grace_period: 210s
diff --git a/apps/worker/package.json b/apps/worker/package.json
new file mode 100644
index 00000000..63a8e6eb
--- /dev/null
+++ b/apps/worker/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "@chronoframe/worker",
+ "version": "0.1.0",
+ "private": true,
+ "type": "module",
+ "license": "AGPL-3.0-only",
+ "scripts": {
+ "start": "node --experimental-strip-types src/index.ts",
+ "type-check": "tsc --noEmit"
+ },
+ "dependencies": {
+ "@chronoframe/core": "workspace:*",
+ "@chronoframe/contracts": "workspace:*",
+ "@chronoframe/processing": "workspace:*",
+ "@chronoframe/storage": "workspace:*",
+ "@chronoframe/media": "workspace:*",
+ "exiftool-vendored": "^35.21.0",
+ "sharp": "^0.35.5"
+ },
+ "devDependencies": {
+ "@types/node": "^22.20.5",
+ "typescript": "^5.9.3"
+ }
+}
diff --git a/apps/worker/src/index.ts b/apps/worker/src/index.ts
new file mode 100644
index 00000000..f249206e
--- /dev/null
+++ b/apps/worker/src/index.ts
@@ -0,0 +1,142 @@
+import { randomUUID } from 'node:crypto'
+import { setTimeout as delay } from 'node:timers/promises'
+import { processWork, WorkerProcessingError, type WorkItem } from './process.ts'
+import type { CloudMediaResult } from '@chronoframe/core'
+const origin = process.env.CHRONOFRAME_PORTAL_ORIGIN ?? ''
+const secret = process.env.CHRONOFRAME_WORKER_SECRET ?? ''
+const workerId = process.env.CHRONOFRAME_WORKER_ID ?? randomUUID()
+if (
+ !origin ||
+ new URL(origin).protocol !== 'https:' ||
+ new URL(origin).origin !== origin ||
+ secret.length < 32
+)
+ throw new Error(
+ 'Configure an HTTPS portal origin and generated worker credential',
+ )
+let stopping = false
+const shutdown = new AbortController()
+for (const signal of ['SIGINT', 'SIGTERM'] as const)
+ process.on(signal, () => {
+ stopping = true
+ shutdown.abort()
+ })
+async function rpc<T>(
+ action: string,
+ body: unknown,
+ signal?: AbortSignal,
+): Promise<T> {
+ const response = await fetch(origin + '/api/internal/worker/' + action, {
+ method: 'POST',
+ redirect: 'error',
+ headers: {
+ Authorization: 'Bearer ' + secret,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ signal: signal ?? AbortSignal.timeout(30000),
+ })
+ if (!response.ok) throw new Error('portal_request_failed')
+ return response.json() as Promise<T>
+}
+while (!stopping) {
+ try {
+ const item = await rpc<WorkItem | { lease: null }>('claim', { workerId })
+ if (!item.lease) {
+ await rpc('cleanup', {})
+ await delay(3000, undefined, { signal: shutdown.signal }).catch(() => {})
+ continue
+ }
+ const work = item as WorkItem,
+ controller = new AbortController(),
+ timeout = setTimeout(() => controller.abort('processing_timeout'), 180000)
+ let heartbeatRunning = false
+ const timer = setInterval(() => {
+ if (heartbeatRunning) return
+ heartbeatRunning = true
+ void rpc<{ ok: boolean }>('heartbeat', { lease: work.lease })
+ .then((result) => {
+ if (!result.ok) controller.abort('lease_lost')
+ })
+ .catch(() => controller.abort('heartbeat_unavailable'))
+ .finally(() => {
+ heartbeatRunning = false
+ })
+ }, 30000)
+ try {
+ const processed = await processWork(work, controller.signal)
+ controller.signal.throwIfAborted()
+ const grants = await rpc<{
+ outputs: (CloudMediaResult['assets'][number] & { url: string })[]
+ }>('grant', {
+ lease: work.lease,
+ assets: processed.derivatives.map(
+ ({ buffer: _buffer, ...asset }) => asset,
+ ),
+ })
+ for (const grant of grants.outputs) {
+ controller.signal.throwIfAborted()
+ const derivative = processed.derivatives.find(
+ (d) => d.role === grant.role,
+ )
+ if (!derivative) throw new Error('invalid_output_grant')
+ const response = await fetch(grant.url, {
+ method: 'PUT',
+ headers: { 'Content-Type': grant.contentType },
+ body: new Uint8Array(derivative.buffer),
+ redirect: 'error',
+ signal: controller.signal,
+ })
+ if (!response.ok) throw new Error('derivative_upload_failed')
+ }
+ const completed = await rpc<{ ok: boolean }>('complete', {
+ lease: work.lease,
+ result: {
+ ...processed.result,
+ assets: grants.outputs.map(({ url: _url, ...asset }) => asset),
+ },
+ })
+ console.log(
+ JSON.stringify({
+ event: completed.ok ? 'job.completed' : 'job.discarded',
+ jobId: work.lease.id,
+ siteId: work.lease.siteId,
+ attemptId: work.lease.attemptId,
+ }),
+ )
+ } catch (error) {
+ const aborted = controller.signal.aborted
+ await rpc('fail', {
+ lease: work.lease,
+ code: aborted
+ ? String(controller.signal.reason || 'lease_lost')
+ : error instanceof WorkerProcessingError
+ ? error.message
+ : 'processing_error',
+ retryable: aborted
+ ? controller.signal.reason === 'processing_timeout'
+ : error instanceof WorkerProcessingError
+ ? error.retryable
+ : true,
+ }).catch(() => {})
+ console.log(
+ JSON.stringify({
+ event: 'job.failed',
+ jobId: work.lease.id,
+ siteId: work.lease.siteId,
+ attemptId: work.lease.attemptId,
+ code: aborted ? 'lease_or_timeout' : 'processing_error',
+ }),
+ )
+ } finally {
+ clearInterval(timer)
+ clearTimeout(timeout)
+ }
+ await rpc('cleanup', {}).catch(() => {})
+ } catch {
+ if (!stopping) {
+ console.log(JSON.stringify({ event: 'worker.connection_retry' }))
+ await delay(5000, undefined, { signal: shutdown.signal }).catch(() => {})
+ }
+ }
+}
diff --git a/apps/worker/src/process.ts b/apps/worker/src/process.ts
new file mode 100644
index 00000000..fc5f5151
--- /dev/null
+++ b/apps/worker/src/process.ts
@@ -0,0 +1,244 @@
+import { mkdtemp, rm, readFile, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import path from 'node:path'
+import sharp from 'sharp'
+import { ExifTool } from 'exiftool-vendored'
+import { createPhotoProcessor } from '@chronoframe/processing'
+import { createExifService } from '@chronoframe/media'
+import type { StorageProvider } from '@chronoframe/storage'
+import type { CloudLease, CloudMediaResult } from '@chronoframe/core'
+export interface WorkItem {
+ lease: CloudLease
+ sourceUrl: string
+ fileName: string
+ contentType: string
+ companion?: { key: string; url: string; bytes: number; etag?: string } | null
+ etag?: string
+}
+export interface Derivative {
+ role: 'thumbnail' | 'live-video' | 'display'
+ bytes: number
+ contentType: string
+ buffer: Buffer
+}
+export class WorkerProcessingError extends Error {
+ readonly retryable: boolean
+ constructor(code: string, retryable: boolean) {
+ super(code)
+ this.retryable = retryable
+ }
+}
+const quiet = { info() {}, warn() {}, error() {}, debug() {}, success() {} }
+/** Bound download bytes before allocating a complete input buffer. */
+export async function boundedDownload(
+ url: string,
+ expected: number,
+ signal: AbortSignal,
+ etag?: string,
+) {
+ if (!Number.isSafeInteger(expected) || expected < 1 || expected > 104857600)
+ throw new WorkerProcessingError('invalid_source_size', false)
+ const parsed = new URL(url)
+ if (parsed.protocol !== 'https:')
+ throw new WorkerProcessingError('invalid_source_url', false)
+ const response = await fetch(url, {
+ signal,
+ redirect: 'error',
+ headers: etag ? { 'If-Match': etag } : {},
+ })
+ if (!response.ok || !response.body)
+ throw new WorkerProcessingError(
+ response.status === 403
+ ? 'storage_access_denied'
+ : response.status === 404
+ ? 'source_not_found'
+ : 'storage_unavailable',
+ response.status >= 500 || response.status === 429,
+ )
+ const reader = response.body.getReader(),
+ parts: Uint8Array[] = []
+ let size = 0
+ try {
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+ size += value.length
+ if (size > expected) {
+ await reader.cancel()
+ throw new WorkerProcessingError('source_size_changed', false)
+ }
+ parts.push(value)
+ }
+ } finally {
+ reader.releaseLock()
+ }
+ if (size !== expected)
+ throw new WorkerProcessingError('source_size_changed', false)
+ return Buffer.concat(parts, size)
+}
+/** Reuse the shared processing pipeline. Its storage dependency can access only
+ * the assigned source and in-memory derivatives, never a bucket or credential.
+ */
+export async function processWork(
+ item: WorkItem,
+ signal: AbortSignal,
+): Promise<{
+ result: Omit<CloudMediaResult, 'assets'>
+ derivatives: Derivative[]
+}> {
+ const input = await boundedDownload(
+ item.sourceUrl,
+ item.lease.expectedBytes,
+ signal,
+ item.etag,
+ )
+ const companion = item.companion
+ ? await boundedDownload(
+ item.companion.url,
+ item.companion.bytes,
+ signal,
+ item.companion.etag,
+ )
+ : null
+ const ext = path.extname(item.lease.objectKey).toLowerCase()
+ if (!['.heic', '.heif', '.hif'].includes(ext))
+ await sharp(input, { limitInputPixels: 100000000 }).metadata()
+ const directory = await mkdtemp(path.join(tmpdir(), 'chronoframe-worker-'))
+ const exiftool = new ExifTool({ maxProcs: 1, taskTimeoutMillis: 30000 })
+ const derivatives: Derivative[] = []
+ try {
+ if (['.heic', '.heif', '.hif'].includes(ext)) {
+ const sourceFile = path.join(directory, 'source' + ext)
+ await writeFile(sourceFile, input)
+ const tags = await exiftool.read(sourceFile),
+ width = Number(tags.ImageWidth ?? tags.ExifImageWidth),
+ height = Number(tags.ImageHeight ?? tags.ExifImageHeight)
+ if (
+ !Number.isSafeInteger(width) ||
+ !Number.isSafeInteger(height) ||
+ width < 1 ||
+ height < 1 ||
+ width * height > 100000000
+ )
+ throw new WorkerProcessingError('invalid_dimensions', false)
+ }
+ const storage: StorageProvider = {
+ async get(key) {
+ return key === item.lease.objectKey
+ ? input
+ : key === item.companion?.key
+ ? companion
+ : null
+ },
+ async getFileMeta(key) {
+ return key === item.lease.objectKey
+ ? { key, size: input.length }
+ : key === item.companion?.key && companion
+ ? { key, size: companion.length }
+ : null
+ },
+ async create(key, buffer, contentType) {
+ signal.throwIfAborted()
+ const role =
+ contentType === 'image/webp'
+ ? 'thumbnail'
+ : contentType === 'image/jpeg'
+ ? 'display'
+ : contentType === 'video/mp4'
+ ? 'live-video'
+ : null
+ if (!role || buffer.length > 104857600)
+ throw new Error('invalid_derivative')
+ derivatives.push({
+ role,
+ bytes: buffer.length,
+ contentType: contentType!,
+ buffer,
+ })
+ return { key: 'derived/' + role, size: buffer.length }
+ },
+ async createFromFile(key, file, contentType) {
+ return this.create(key, await readFile(file), contentType)
+ },
+ async delete() {
+ throw new Error('operation_not_authorized')
+ },
+ getPublicUrl(key) {
+ return key
+ },
+ async listAll() {
+ return []
+ },
+ async listImages() {
+ return []
+ },
+ }
+ const process = createPhotoProcessor({
+ logger: quiet,
+ exif: createExifService({ exiftool, tempDir: directory, logger: quiet }),
+ parseGPS() {
+ return {}
+ },
+ async reverseGeocode() {
+ return null
+ },
+ })
+ const { photo } = await process({
+ photoId: item.lease.photoId,
+ storageKey: item.lease.objectKey,
+ eraseLocation: false,
+ storage,
+ async stage() {
+ signal.throwIfAborted()
+ },
+ signal,
+ })
+ if (
+ photo.isLivePhoto &&
+ companion &&
+ photo.livePhotoVideoKey === item.companion?.key
+ )
+ derivatives.push({
+ role: 'live-video',
+ bytes: companion.length,
+ contentType: 'video/quicktime',
+ buffer: companion,
+ })
+ if (!photo.width || !photo.height)
+ throw new WorkerProcessingError('invalid_dimensions', false)
+ // Persist only JSON-safe shared photo metadata; URLs/keys are resolved by APIs.
+ const {
+ originalUrl: _originalUrl,
+ thumbnailUrl: _thumbnailUrl,
+ livePhotoVideoUrl: _livePhotoVideoUrl,
+ storageKey: _storageKey,
+ thumbnailKey: _thumbnailKey,
+ livePhotoVideoKey: _livePhotoVideoKey,
+ originalDerivedKey: _originalDerivedKey,
+ ...metadata
+ } = photo
+ const cleanMetadata = JSON.parse(JSON.stringify(metadata))
+ if (Buffer.byteLength(JSON.stringify(cleanMetadata)) > 60 * 1024)
+ throw new WorkerProcessingError('metadata_too_large', false)
+ return {
+ result: {
+ width: photo.width,
+ height: photo.height,
+ metadata: cleanMetadata,
+ },
+ derivatives,
+ }
+ } catch (error) {
+ if (error instanceof WorkerProcessingError) throw error
+ if (
+ /unsupported|corrupt|invalid|pixel|decode|dimensions/i.test(
+ error instanceof Error ? error.message : '',
+ )
+ )
+ throw new WorkerProcessingError('invalid_media', false)
+ throw new WorkerProcessingError('processing_error', true)
+ } finally {
+ await exiftool.end()
+ await rm(directory, { recursive: true, force: true })
+ }
+}
diff --git a/apps/worker/tsconfig.json b/apps/worker/tsconfig.json
new file mode 100644
index 00000000..e79cde15
--- /dev/null
+++ b/apps/worker/tsconfig.json
@@ -0,0 +1,15 @@
+{
+ "compilerOptions": {
+ "target": "ES2022",
+ "module": "ESNext",
+ "moduleResolution": "Bundler",
+ "strict": true,
+ "noEmit": true,
+ "allowImportingTsExtensions": true,
+ "types": ["node"],
+ "lib": ["ES2022", "DOM"],
+ "skipLibCheck": true,
+ "noUncheckedIndexedAccess": true
+ },
+ "include": ["src/**/*.ts"]
+}
diff --git a/package.json b/package.json
index ce0973c1..ffdf95f7 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,7 @@
"db:generate": "drizzle-kit generate",
"db:migrate": "node --experimental-strip-types scripts/migrate-database.ts",
"build": "node scripts/self-hosted.mjs build",
- "build:deps": "pnpm --filter @chronoframe/webgl-image build",
+ "build:deps": "pnpm --filter @chronoframe/storage build && pnpm --filter @chronoframe/webgl-image build",
"dev": "run-p dev:dep dev:wait-only",
"dev:dep": "pnpm --filter @chronoframe/webgl-image dev",
"dev:only": "node scripts/self-hosted.mjs dev",
@@ -33,7 +33,7 @@
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
- "check:packages": "node scripts/check-ui-boundaries.mjs && node scripts/check-package-boundaries.mjs && pnpm --filter @chronoframe/jobs --filter @chronoframe/processing --filter @chronoframe/database --filter @chronoframe/platform --filter @chronoframe/core --filter @chronoframe/contracts --filter @chronoframe/storage --filter @chronoframe/media --filter @chronoframe/geocoding type-check",
+ "check:packages": "node scripts/check-ui-boundaries.mjs && node scripts/check-package-boundaries.mjs && pnpm --filter @chronoframe/worker --filter @chronoframe/jobs --filter @chronoframe/processing --filter @chronoframe/database --filter @chronoframe/platform --filter @chronoframe/core --filter @chronoframe/contracts --filter @chronoframe/storage --filter @chronoframe/media --filter @chronoframe/geocoding type-check",
"test": "node --experimental-strip-types --test tests/*.test.ts packages/gallery/app/utils/*.test.ts",
"check:ui": "node scripts/check-ui-boundaries.mjs",
"prepare": "node scripts/self-hosted.mjs prepare",
diff --git a/packages/contracts/src/entry.ts b/packages/contracts/src/entry.ts
index 551fc267..f9e9b339 100644
--- a/packages/contracts/src/entry.ts
+++ b/packages/contracts/src/entry.ts
@@ -23,9 +23,9 @@ export const CLOUD_CAPABILITIES: EntryCapabilities = {
edition: 'cloud',
identity: 'platform',
database: 'd1',
- storageProviders: [],
+ storageProviders: ['s3'],
quota: 'plan',
- mediaManagement: false,
+ mediaManagement: true,
subscriptions: true,
customScripts: false,
}
@@ -33,6 +33,8 @@ export const SITE_CAPABILITIES: EntryCapabilities = {
...CLOUD_CAPABILITIES,
edition: 'site',
identity: 'visitor',
+ mediaManagement: false,
+ storageProviders: [],
subscriptions: false,
}
export interface EntrySiteContext {
diff --git a/packages/core/src/cloud-execution.ts b/packages/core/src/cloud-execution.ts
new file mode 100644
index 00000000..6b751ca2
--- /dev/null
+++ b/packages/core/src/cloud-execution.ts
@@ -0,0 +1,47 @@
+/** Internal worker protocol; never carries long-lived storage credentials. */
+export interface CloudLease {
+ id: string
+ siteId: number
+ photoId: string
+ sourceVersion: string
+ pipelineVersion: string
+ attemptId: string
+ workerId: string
+ attempts: number
+ connectionRevision: number
+ connectionId: string
+ objectKey: string
+ expectedBytes: number
+}
+export interface CloudMediaResult {
+ width: number
+ height: number
+ /** JSON-safe metadata, independently size-limited before persistence. */
+ metadata: Record<string, unknown>
+ assets: {
+ role: 'thumbnail' | 'live-video' | 'display'
+ objectKey: string
+ bytes: number
+ contentType: string
+ }[]
+}
+export interface CloudExecutionRepository {
+ claim(
+ workerId: string,
+ attemptId: string,
+ now: number,
+ leaseMs: number,
+ ): Promise<CloudLease | null>
+ heartbeat(lease: CloudLease, now: number, leaseMs: number): Promise<boolean>
+ complete(
+ lease: CloudLease,
+ result: CloudMediaResult,
+ now: number,
+ ): Promise<boolean>
+ fail(
+ lease: CloudLease,
+ code: string,
+ retryable: boolean,
+ now: number,
+ ): Promise<boolean>
+}
diff --git a/packages/core/src/cloud-media.ts b/packages/core/src/cloud-media.ts
index 6af53930..70e645ec 100644
--- a/packages/core/src/cloud-media.ts
+++ b/packages/core/src/cloud-media.ts
@@ -25,6 +25,7 @@ export interface MediaUpload {
connection_id: string
object_key: string
provider_upload_id: string | null
+ expected_video_bytes: number
expected_bytes: number
expires_at: number
state: 'pending' | 'completed' | 'cancelled'
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index ebef0df5..1385a466 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -15,6 +15,7 @@ export interface PhotoMembership {
/** Async boundary allows both embedded SQLite and future asynchronous adapters. */
export interface ResourceReadRepository {
+ protectedPhoto?(photo: Photo): Photo
listPhotos(
excludedIds?: string[],
page?: { limit: number; offset: number },
@@ -23,6 +24,7 @@ export interface ResourceReadRepository {
getAlbum(id: number): Promise<AlbumReadRecord | null>
listAlbumPhotos(id: number): Promise<Photo[]>
listAlbumPhotoIds(id: number): Promise<string[]>
+ membershipsForPhoto?(id: string): Promise<PhotoMembership[]>
listMemberships(): Promise<PhotoMembership[]>
}
@@ -117,6 +119,8 @@ export function createResourceReader(repository: ResourceReadRepository) {
...resource,
photos: passwordHash
? photos.map((photo) => {
+ if (repository.protectedPhoto)
+ return repository.protectedPhoto(photo)
const {
storageKey,
thumbnailKey: _thumbnailKey,
@@ -151,3 +155,4 @@ export * from './platform.ts'
export * from './email-auth.ts'
export type * from './cloud-media.ts'
+export type * from './cloud-execution.ts'
diff --git a/packages/database/package.json b/packages/database/package.json
index 1f74ace1..c96eb17b 100644
--- a/packages/database/package.json
+++ b/packages/database/package.json
@@ -10,7 +10,9 @@
"./dto": "./src/dto.ts",
"./album-membership": "./src/album-membership.ts",
"./album-photos-add": "./src/album-photos-add.ts",
- "./cloud-media": "./src/cloud-media.ts"
+ "./cloud-media": "./src/cloud-media.ts",
+ "./cloud-execution": "./src/cloud-execution.ts",
+ "./cloud-resources": "./src/cloud-resources.ts"
},
"scripts": {
"type-check": "tsc --noEmit"
diff --git a/packages/database/src/cloud-execution.ts b/packages/database/src/cloud-execution.ts
new file mode 100644
index 00000000..f4c8086d
--- /dev/null
+++ b/packages/database/src/cloud-execution.ts
@@ -0,0 +1,310 @@
+import {
+ assertSiteContext,
+ CoreError,
+ type SiteContext,
+ type CloudLease,
+ type CloudExecutionRepository,
+} from '@chronoframe/core'
+import type { MediaDatabase, MediaStatement } from './cloud-media.ts'
+
+function positive(value: number) {
+ if (!Number.isSafeInteger(value) || value < 1)
+ throw new CoreError(400, 'Invalid clock or size')
+ return value
+}
+function identifier(value: string) {
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(value))
+ throw new CoreError(400, 'Invalid worker identifier')
+ return value
+}
+function leaseDuration(value: number) {
+ if (!Number.isSafeInteger(value) || value < 1000 || value > 300_000)
+ throw new CoreError(400, 'Invalid lease duration')
+ return value
+}
+/** Source existence/version and deletion are checked on every worker mutation. */
+const currentSource = `EXISTS(SELECT 1 FROM cloud_photos p WHERE p.site_id=cloud_processing_jobs.site_id AND p.id=cloud_processing_jobs.photo_id AND p.source_version=cloud_processing_jobs.source_version AND p.deleted=0)`
+
+/** Database polling is intentional: job creation and enqueue are the same D1
+ * transaction. No broker dual-write, public database endpoint or CF credential.
+ */
+export function createCloudExecutionRepository(
+ db: MediaDatabase,
+ site: SiteContext,
+): CloudExecutionRepository {
+ assertSiteContext(site)
+ const siteId = site.id
+ const stmt = (sql: string, ...values: (string | number | null)[]) =>
+ db.prepare(sql).bind(...values)
+ const rows = async <T>(s: MediaStatement) => (await s.all<T>()).results
+ function own(lease: CloudLease) {
+ if (lease.siteId !== siteId) throw new CoreError(403, 'Wrong lease Site')
+ identifier(lease.attemptId)
+ identifier(lease.workerId)
+ }
+ const valid = `site_id=? AND id=? AND attempt_id=? AND worker_id=? AND state='running' AND lease_until>? AND ${currentSource}`
+ const validValues = (lease: CloudLease, now: number) =>
+ [lease.id, lease.attemptId, lease.workerId, positive(now)] as const
+ async function changed(s: MediaStatement) {
+ return (await rows<{ id: string }>(s)).length === 1
+ }
+ return {
+ async claim(workerId, attemptId, now, leaseMs) {
+ identifier(workerId)
+ identifier(attemptId)
+ positive(now)
+ leaseDuration(leaseMs)
+ if (!Number.isSafeInteger(now + leaseMs))
+ throw new CoreError(400, 'Invalid lease expiry')
+ await db.batch([
+ stmt(
+ `UPDATE cloud_processing_jobs SET state='failed',error_code='attempts_exhausted',lease_until=NULL WHERE site_id=? AND state='running' AND lease_until<=? AND attempts>=max_attempts`,
+ siteId,
+ now,
+ ),
+ stmt(
+ `UPDATE cloud_processing_jobs SET state='running',worker_id=?,attempt_id=?,lease_until=?,attempts=attempts+1,error_code=NULL
+ WHERE site_id=? AND id=(SELECT id FROM cloud_processing_jobs WHERE site_id=? AND attempts<max_attempts AND ${currentSource}
+ AND ((state IN ('queued','retry_wait') AND available_at<=?) OR (state='running' AND lease_until<=?)) AND NOT EXISTS(SELECT 1 FROM cloud_processing_jobs active WHERE active.site_id=? AND active.state='running' AND active.lease_until>?) ORDER BY created_at,id LIMIT 1)`,
+ workerId,
+ attemptId,
+ now + leaseMs,
+ siteId,
+ siteId,
+ now,
+ now,
+ siteId,
+ now,
+ ),
+ stmt(
+ `UPDATE cloud_job_attempts SET state='expired',finished_at=? WHERE site_id=? AND state='running' AND EXISTS(SELECT 1 FROM cloud_processing_jobs j WHERE j.site_id=cloud_job_attempts.site_id AND j.id=cloud_job_attempts.job_id AND (j.attempt_id!=cloud_job_attempts.attempt_id OR j.state='failed'))`,
+ now,
+ siteId,
+ ),
+ stmt(
+ `INSERT INTO cloud_job_attempts(site_id,job_id,attempt_id,worker_id,started_at,state)
+ SELECT ?,id,attempt_id,worker_id,?,'running' FROM cloud_processing_jobs WHERE site_id=? AND attempt_id=? AND worker_id=? AND state='running'`,
+ siteId,
+ now,
+ siteId,
+ attemptId,
+ workerId,
+ ),
+ stmt(
+ `INSERT INTO cloud_site_dispatch(site_id,last_claim_at) SELECT ?,? WHERE EXISTS(SELECT 1 FROM cloud_processing_jobs WHERE site_id=? AND attempt_id=? AND worker_id=? AND state='running') ON CONFLICT(site_id) DO UPDATE SET last_claim_at=excluded.last_claim_at`,
+ siteId,
+ now,
+ siteId,
+ attemptId,
+ workerId,
+ ),
+ ])
+ const found = (
+ await rows<Record<string, unknown>>(
+ stmt(
+ `SELECT j.*,u.connection_id,u.connection_revision,u.object_key,u.expected_bytes FROM cloud_processing_jobs j JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE j.site_id=? AND j.attempt_id=? AND j.worker_id=? AND j.state='running'`,
+ siteId,
+ attemptId,
+ workerId,
+ ),
+ )
+ )[0]
+ if (!found) return null
+ return {
+ id: String(found.id),
+ siteId,
+ photoId: String(found.photo_id),
+ sourceVersion: String(found.source_version),
+ pipelineVersion: String(found.pipeline_version),
+ attemptId,
+ workerId,
+ attempts: Number(found.attempts),
+ connectionId: String(found.connection_id),
+ connectionRevision: Number(found.connection_revision),
+ objectKey: String(found.object_key),
+ expectedBytes: Number(found.expected_bytes),
+ }
+ },
+ async heartbeat(lease, now, leaseMs) {
+ own(lease)
+ leaseDuration(leaseMs)
+ return changed(
+ stmt(
+ `UPDATE cloud_processing_jobs SET lease_until=? WHERE ${valid} RETURNING id`,
+ now + leaseMs,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ )
+ },
+ async complete(lease, result, now) {
+ own(lease)
+ positive(now)
+ const canonical = (
+ await rows<{
+ photo_id: string
+ source_version: string
+ pipeline_version: string
+ connection_id: string
+ connection_revision: number
+ }>(
+ stmt(
+ `SELECT j.photo_id,j.source_version,j.pipeline_version,u.connection_id,u.connection_revision FROM cloud_processing_jobs j JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE j.site_id=? AND j.id=? AND j.attempt_id=? AND j.worker_id=?`,
+ siteId,
+ lease.id,
+ lease.attemptId,
+ lease.workerId,
+ ),
+ )
+ )[0]
+ if (
+ !canonical ||
+ canonical.photo_id !== lease.photoId ||
+ canonical.source_version !== lease.sourceVersion ||
+ canonical.pipeline_version !== lease.pipelineVersion ||
+ canonical.connection_id !== lease.connectionId ||
+ canonical.connection_revision !== lease.connectionRevision
+ )
+ return false
+ positive(result.width)
+ positive(result.height)
+ if (
+ result.width > 65535 ||
+ result.height > 65535 ||
+ result.width * result.height > 100_000_000
+ )
+ throw new CoreError(400, 'Image dimensions exceed limits')
+ const json = JSON.stringify({
+ ...result.metadata,
+ width: result.width,
+ height: result.height,
+ })
+ if (
+ new TextEncoder().encode(json).length > 65536 ||
+ result.assets.length < 1 ||
+ result.assets.length > 3 ||
+ new Set(result.assets.map((a) => a.role)).size !== result.assets.length
+ )
+ throw new CoreError(400, 'Invalid media result')
+ const prefix = `chronoframe/${siteId}/derived/${lease.photoId}/${lease.sourceVersion}/${lease.pipelineVersion}/${lease.attemptId}/`
+ for (const asset of result.assets) {
+ positive(asset.bytes)
+ const suffix =
+ asset.role === 'thumbnail'
+ ? 'thumbnail.webp'
+ : asset.role === 'display'
+ ? 'display.jpeg'
+ : asset.contentType === 'video/mp4'
+ ? 'live-video.mp4'
+ : 'live-video.mov'
+ if (
+ asset.objectKey !== prefix + suffix ||
+ asset.contentType !==
+ (asset.role === 'thumbnail'
+ ? 'image/webp'
+ : asset.role === 'display'
+ ? 'image/jpeg'
+ : asset.objectKey.endsWith('.mp4')
+ ? 'video/mp4'
+ : 'video/quicktime')
+ )
+ throw new CoreError(400, 'Unexpected media object')
+ }
+ if (!result.assets.some((a) => a.role === 'thumbnail'))
+ throw new CoreError(400, 'Thumbnail required')
+ // Each write repeats the active attempt/source predicate; the final state
+ // change comes last, so rejected or stale callbacks cannot publish assets.
+ await db.batch([
+ stmt(
+ `UPDATE cloud_photos SET metadata_json=? WHERE site_id=? AND id=? AND source_version=? AND deleted=0 AND EXISTS(SELECT 1 FROM cloud_processing_jobs WHERE ${valid})`,
+ json,
+ siteId,
+ lease.photoId,
+ lease.sourceVersion,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ ...result.assets.map((asset) =>
+ stmt(
+ `INSERT INTO cloud_media_assets(site_id,photo_id,source_version,pipeline_version,role,connection_id,connection_revision,object_key,bytes,content_type)
+ SELECT ?,photo_id,source_version,pipeline_version,?, ?,?,?,?,? FROM cloud_processing_jobs WHERE ${valid}
+ ON CONFLICT DO UPDATE SET object_key=excluded.object_key,bytes=excluded.bytes,content_type=excluded.content_type`,
+ siteId,
+ asset.role,
+ lease.connectionId,
+ lease.connectionRevision,
+ asset.objectKey,
+ asset.bytes,
+ asset.contentType,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ ),
+ stmt(
+ `UPDATE cloud_job_attempts SET state='succeeded',finished_at=? WHERE site_id=? AND job_id=? AND attempt_id=? AND state='running' AND EXISTS(SELECT 1 FROM cloud_processing_jobs WHERE ${valid})`,
+ now,
+ siteId,
+ lease.id,
+ lease.attemptId,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ stmt(
+ `UPDATE cloud_processing_jobs SET state='succeeded',lease_until=NULL WHERE ${valid}`,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ ])
+ return (
+ (
+ await rows<{ id: string }>(
+ stmt(
+ `SELECT id FROM cloud_processing_jobs WHERE site_id=? AND id=? AND attempt_id=? AND worker_id=? AND state='succeeded' AND ${currentSource}`,
+ siteId,
+ lease.id,
+ lease.attemptId,
+ lease.workerId,
+ ),
+ )
+ ).length === 1
+ )
+ },
+ async fail(lease, code, retryable, now) {
+ own(lease)
+ positive(now)
+ if (!/^[a-z_]{1,64}$/.test(code))
+ throw new CoreError(400, 'Invalid error code')
+ await db.batch([
+ stmt(
+ `UPDATE cloud_job_attempts SET state='failed',finished_at=? WHERE site_id=? AND job_id=? AND attempt_id=? AND EXISTS(SELECT 1 FROM cloud_processing_jobs WHERE ${valid})`,
+ now,
+ siteId,
+ lease.id,
+ lease.attemptId,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ stmt(
+ `UPDATE cloud_processing_jobs SET state=CASE WHEN ?=1 AND attempts<max_attempts THEN 'retry_wait' ELSE 'failed' END,available_at=?,error_code=?,lease_until=NULL WHERE ${valid}`,
+ retryable ? 1 : 0,
+ now + Math.min(1000 * 2 ** Math.max(0, lease.attempts - 1), 30000),
+ code,
+ siteId,
+ ...validValues(lease, now),
+ ),
+ ])
+ return (
+ (
+ await rows<{ id: string }>(
+ stmt(
+ `SELECT id FROM cloud_processing_jobs WHERE site_id=? AND id=? AND attempt_id=? AND state IN ('retry_wait','failed')`,
+ siteId,
+ lease.id,
+ lease.attemptId,
+ ),
+ )
+ ).length === 1
+ )
+ },
+ }
+}
diff --git a/packages/database/src/cloud-media.ts b/packages/database/src/cloud-media.ts
index 33d7e1d2..b43483c5 100644
--- a/packages/database/src/cloud-media.ts
+++ b/packages/database/src/cloud-media.ts
@@ -76,13 +76,28 @@ export function createCloudMediaRepository(
async createAlbum(id: string, title: string) {
await db.batch([
statement(
- 'INSERT INTO cloud_albums(site_id,id,title) VALUES(?,?,?)',
+ 'INSERT INTO cloud_albums(site_id,id,title) SELECT ?,?,? WHERE (SELECT COUNT(*) FROM cloud_albums WHERE site_id=?)<100 ON CONFLICT DO NOTHING',
text(id),
text(title),
+ siteId,
),
])
+ const existing = await first<{ title: string }>(
+ 'SELECT title FROM cloud_albums WHERE site_id=? AND id=?',
+ id,
+ )
+ if (!existing) throw new CoreError(429, 'Album limit reached')
+ if (existing.title !== title)
+ throw new CoreError(409, 'Album identifier already used')
},
async addToAlbum(albumId: string, photoId: string) {
+ if (
+ !(await first<{ id: string }>(
+ 'SELECT id FROM cloud_albums WHERE site_id=? AND id=?',
+ text(albumId),
+ ))
+ )
+ throw new CoreError(404, 'Album not found')
await db.batch([
statement(
`INSERT INTO cloud_album_photos(site_id,album_id,photo_id)
@@ -130,10 +145,22 @@ export function createCloudMediaRepository(
await db.batch([
statement(
`INSERT INTO cloud_uploads(site_id,id,owner_id,photo_id,source_version,connection_id,object_key,provider_upload_id,expected_bytes,expires_at)
- VALUES(?,?,?,?,?,?,?,?,?,?)`,
+ VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`,
...values,
),
])
+ const existing = await upload(input.id, input.ownerId)
+ if (
+ !existing ||
+ existing.photo_id !== input.photoId ||
+ existing.source_version !== input.sourceVersion ||
+ existing.connection_id !== input.connectionId ||
+ existing.object_key !== input.objectKey ||
+ existing.provider_upload_id !== input.providerUploadId ||
+ existing.expected_bytes !== input.expectedBytes ||
+ existing.expires_at !== input.expiresAt
+ )
+ throw new CoreError(409, 'Upload identifier or object already used')
},
/** Only call after provider-side object verification. Completion and durable
* queued job are one batch; queue delivery is a separate later adapter.
@@ -175,6 +202,14 @@ export function createCloudMediaRepository(
ownerId,
now,
),
+ statement(
+ `INSERT INTO cloud_usage_events(site_id,upload_id,bytes,created_at) SELECT ?,id,expected_bytes+expected_video_bytes,? FROM cloud_uploads WHERE ${condition} ON CONFLICT DO NOTHING`,
+ now,
+ siteId,
+ id,
+ ownerId,
+ now,
+ ),
statement(
`UPDATE cloud_uploads SET state='completed' WHERE site_id=? AND id=? AND owner_id=? AND state='pending' AND expires_at>?`,
id,
diff --git a/packages/database/src/cloud-resources.ts b/packages/database/src/cloud-resources.ts
new file mode 100644
index 00000000..d2db6c6d
--- /dev/null
+++ b/packages/database/src/cloud-resources.ts
@@ -0,0 +1,151 @@
+import {
+ assertSiteContext,
+ type SiteContext,
+ type ResourceReadRepository,
+ type AlbumReadRecord,
+} from '@chronoframe/core'
+import type { Photo } from '@chronoframe/contracts/resources'
+import type { MediaDatabase } from './cloud-media.ts'
+/** Shared read contract; private media endpoints are emitted, never signed URLs.
+ * Cloud HTTP assembly applies the same core visibility/access rules as SQLite.
+ */
+export function createCloudResourceRepository(
+ db: MediaDatabase,
+ site: SiteContext,
+ mediaPrefix: string,
+): ResourceReadRepository {
+ assertSiteContext(site)
+ const all = async <T>(sql: string, ...values: (string | number | null)[]) =>
+ (
+ await db
+ .prepare(sql)
+ .bind(site.id, ...values)
+ .all<T>()
+ ).results
+ const photo = (row: Record<string, unknown>): Photo => {
+ const m = JSON.parse(String(row.metadata_json)) as Partial<Photo>
+ const id = String(row.id),
+ base = mediaPrefix + '/' + encodeURIComponent(id)
+ return {
+ id,
+ title: String(row.title),
+ description: typeof m.description === 'string' ? m.description : null,
+ width: typeof m.width === 'number' ? m.width : null,
+ height: typeof m.height === 'number' ? m.height : null,
+ aspectRatio:
+ typeof m.width === 'number' && typeof m.height === 'number'
+ ? m.width / m.height
+ : null,
+ dateTaken: typeof m.dateTaken === 'string' ? m.dateTaken : null,
+ fileSize: typeof m.fileSize === 'number' ? m.fileSize : null,
+ lastModified: null,
+ originalUrl: base + '/display',
+ thumbnailUrl: base + '/thumbnail',
+ thumbnailHash:
+ typeof m.thumbnailHash === 'string' ? m.thumbnailHash : null,
+ tags: Array.isArray(m.tags)
+ ? m.tags.filter((t) => typeof t === 'string')
+ : null,
+ exif: m.exif ?? null,
+ latitude: typeof m.latitude === 'number' ? m.latitude : null,
+ longitude: typeof m.longitude === 'number' ? m.longitude : null,
+ country: typeof m.country === 'string' ? m.country : null,
+ city: typeof m.city === 'string' ? m.city : null,
+ locationName: typeof m.locationName === 'string' ? m.locationName : null,
+ geocoding: null,
+ isLivePhoto: row.has_video ? 1 : 0,
+ livePhotoVideoUrl: row.has_video ? base + '/live-video' : null,
+ }
+ }
+ const columns = `p.*,EXISTS(SELECT 1 FROM cloud_media_assets a WHERE a.site_id=p.site_id AND a.photo_id=p.id AND a.source_version=p.source_version AND a.role='live-video') AS has_video`
+ const ready = `p.deleted=0 AND p.metadata_json IS NOT NULL AND EXISTS(SELECT 1 FROM cloud_processing_jobs j WHERE j.site_id=p.site_id AND j.photo_id=p.id AND j.source_version=p.source_version AND j.state='succeeded')`
+ const album = (r: Record<string, unknown>): AlbumReadRecord => ({
+ resource: {
+ id: Number(r.id),
+ title: String(r.title),
+ description: r.description ? String(r.description) : null,
+ coverPhotoId: r.cover_photo_id ? String(r.cover_photo_id) : null,
+ isHidden: !!r.hidden,
+ position: Number(r.position),
+ createdAt: new Date(Number(r.created_at)).toISOString(),
+ updatedAt: new Date(Number(r.updated_at)).toISOString(),
+ hasPassword: !!r.password_hash,
+ },
+ passwordHash: r.password_hash ? String(r.password_hash) : null,
+ })
+ const albumSelect = `SELECT a.*,v.password_hash FROM cloud_albums a LEFT JOIN platform_album_access v ON v.site_id=a.site_id AND CAST(v.album_id AS TEXT)=a.id WHERE a.site_id=?`
+ async function memberships(photoId?: string) {
+ return (
+ await all<{
+ photoId: string
+ albumId: string
+ hidden: number
+ protected: number
+ }>(
+ `SELECT m.photo_id AS photoId,m.album_id AS albumId,a.hidden,EXISTS(SELECT 1 FROM platform_album_access v WHERE v.site_id=a.site_id AND CAST(v.album_id AS TEXT)=a.id) AS protected FROM cloud_album_photos m JOIN cloud_albums a ON a.site_id=m.site_id AND a.id=m.album_id WHERE m.site_id=?${photoId ? ' AND m.photo_id=?' : ''}`,
+ ...(photoId ? [photoId] : []),
+ )
+ ).map((r) => ({
+ photoId: r.photoId,
+ albumId: Number(r.albumId),
+ hidden: !!r.hidden,
+ protected: !!r.protected,
+ }))
+ }
+ return {
+ protectedPhoto: (value: Photo) => value,
+ async listPhotos(excluded = [], page) {
+ if (
+ page &&
+ (!Number.isSafeInteger(page.limit) ||
+ page.limit < 1 ||
+ page.limit > 1000 ||
+ !Number.isSafeInteger(page.offset) ||
+ page.offset < 0)
+ )
+ throw new RangeError('Invalid pagination')
+ return (
+ await all<Record<string, unknown>>(
+ `SELECT ${columns} FROM cloud_photos p WHERE p.site_id=? AND ${ready} ${excluded.length ? 'AND p.id NOT IN (SELECT value FROM json_each(?))' : ''} ORDER BY json_extract(p.metadata_json,'$.dateTaken') DESC,p.id LIMIT ? OFFSET ?`,
+ ...(excluded.length ? [JSON.stringify(excluded)] : []),
+ page?.limit ?? 1000,
+ page?.offset ?? 0,
+ )
+ ).map(photo)
+ },
+ async listAlbums() {
+ return (
+ await all<Record<string, unknown>>(
+ albumSelect + ' ORDER BY a.position,a.created_at DESC LIMIT 100',
+ )
+ ).map(album)
+ },
+ async getAlbum(id) {
+ const row = (
+ await all<Record<string, unknown>>(
+ albumSelect + ' AND a.id=?',
+ String(id),
+ )
+ )[0]
+ return row ? album(row) : null
+ },
+ async listAlbumPhotoIds(id) {
+ return (
+ await all<{ photo_id: string }>(
+ 'SELECT photo_id FROM cloud_album_photos WHERE site_id=? AND album_id=? ORDER BY position,photo_id LIMIT 1000',
+ String(id),
+ )
+ ).map((r) => r.photo_id)
+ },
+ async listAlbumPhotos(id) {
+ return (
+ await all<Record<string, unknown>>(
+ `SELECT ${columns} FROM cloud_photos p JOIN cloud_album_photos a ON a.site_id=p.site_id AND a.photo_id=p.id WHERE p.site_id=? AND a.album_id=? AND ${ready} ORDER BY a.position,p.id LIMIT 1000`,
+ String(id),
+ )
+ ).map(photo)
+ },
+ membershipsForPhoto: memberships,
+ listMemberships: () => memberships(),
+ }
+}
diff --git a/packages/gallery/app/components/GalleryGrid.vue b/packages/gallery/app/components/GalleryGrid.vue
new file mode 100644
index 00000000..fa2d6665
--- /dev/null
+++ b/packages/gallery/app/components/GalleryGrid.vue
@@ -0,0 +1,165 @@
+<script setup lang="ts">
+import type { Photo } from '@chronoframe/contracts/resources'
+defineProps<{ photos: Photo[] }>()
+const selected = ref<Photo | null>(null)
+const dialog = ref<HTMLDialogElement>()
+const trigger = ref<HTMLElement | null>(null)
+function open(photo: Photo, event: MouseEvent) {
+ trigger.value = event.currentTarget as HTMLElement
+ selected.value = photo
+ nextTick(() => dialog.value?.showModal())
+}
+function close() {
+ dialog.value?.close()
+ selected.value = null
+ trigger.value?.focus()
+}
+</script>
+<template>
+ <div class="cf-gallery-grid">
+ <button
+ v-for="photo in photos"
+ :key="photo.id"
+ class="cf-gallery-tile"
+ type="button"
+ :aria-label="'View ' + (photo.title || 'photograph')"
+ @click="open(photo, $event)"
+ >
+ <img
+ :src="photo.thumbnailUrl || undefined"
+ :alt="photo.title || 'Photograph'"
+ loading="lazy"
+ :width="photo.width || undefined"
+ :height="photo.height || undefined"
+ />
+ <span>{{ photo.title || 'Untitled' }}</span>
+ </button>
+ </div>
+ <dialog
+ ref="dialog"
+ class="cf-gallery-dialog"
+ aria-label="Photograph viewer"
+ @cancel.prevent="close"
+ @click="$event.target === dialog && close()"
+ >
+ <template v-if="selected">
+ <div class="cf-gallery-dialog-tools">
+ <span>{{ selected.title }}</span
+ ><a
+ :href="selected.originalUrl?.replace(/\/display$/, '/original')"
+ target="_blank"
+ rel="noopener"
+ >Original</a
+ ><button
+ type="button"
+ @click="close"
+ >
+ Close
+ </button>
+ </div>
+ <img
+ :src="selected.originalUrl || undefined"
+ :alt="selected.title || 'Photograph'"
+ />
+ <video
+ v-if="selected.livePhotoVideoUrl"
+ :src="selected.livePhotoVideoUrl"
+ controls
+ playsinline
+ preload="none"
+ aria-label="Live Photo video"
+ />
+ </template>
+ </dialog>
+</template>
+<style scoped>
+.cf-gallery-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr));
+ gap: 12px;
+}
+.cf-gallery-tile {
+ border: 0;
+ background: transparent;
+ padding: 0;
+ cursor: pointer;
+ text-align: left;
+ color: inherit;
+ min-width: 0;
+}
+.cf-gallery-tile img {
+ width: 100%;
+ height: 190px;
+ object-fit: cover;
+ border-radius: 6px;
+ background: #eceee7;
+}
+.cf-gallery-tile span {
+ display: block;
+ font-size: 12px;
+ padding: 6px 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+.cf-gallery-dialog {
+ border: 0;
+ border-radius: 8px;
+ padding: 12px;
+ background: #171d18;
+ color: white;
+ max-width: 96vw;
+ max-height: 96vh;
+}
+.cf-gallery-dialog::backdrop {
+ background: #101710e8;
+}
+.cf-gallery-dialog img {
+ display: block;
+ object-fit: contain;
+ max-width: 90vw;
+ max-height: 78vh;
+ width: auto;
+ height: auto;
+ margin: auto;
+}
+.cf-gallery-dialog video {
+ max-width: 100%;
+ max-height: 60vh;
+ margin: auto;
+ display: block;
+}
+.cf-gallery-dialog-tools {
+ display: flex;
+ align-items: center;
+ gap: 16px;
+ margin-bottom: 10px;
+ font-size: 14px;
+}
+.cf-gallery-dialog-tools span {
+ flex: 1;
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+.cf-gallery-dialog-tools button,
+.cf-gallery-dialog-tools a {
+ color: inherit;
+ background: transparent;
+ min-height: 40px;
+ padding: 8px;
+ border: 1px solid #ffffff55;
+ border-radius: 5px;
+ font: inherit;
+ cursor: pointer;
+}
+@media (max-width: 480px) {
+ .cf-gallery-grid {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 8px;
+ }
+ .cf-gallery-tile img {
+ height: 160px;
+ }
+}
+</style>
diff --git a/packages/platform/migrations/0005_media_catalog.sql b/packages/platform/migrations/0005_media_catalog.sql
index 35ed0e85..db36bc33 100644
--- a/packages/platform/migrations/0005_media_catalog.sql
+++ b/packages/platform/migrations/0005_media_catalog.sql
@@ -31,6 +31,7 @@ CREATE TABLE cloud_uploads (
object_key TEXT NOT NULL,
provider_upload_id TEXT,
expected_bytes INTEGER NOT NULL CHECK (expected_bytes > 0),
+ expected_video_bytes INTEGER NOT NULL DEFAULT 0 CHECK(expected_video_bytes>=0),
expires_at INTEGER NOT NULL,
state TEXT NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','completed','cancelled')),
PRIMARY KEY (site_id,id),
@@ -52,3 +53,12 @@ CREATE TABLE cloud_processing_jobs (
);
CREATE INDEX cloud_jobs_dispatch ON cloud_processing_jobs(site_id,state,created_at,id);
CREATE INDEX cloud_uploads_expiry ON cloud_uploads(site_id,state,expires_at);
+
+CREATE TABLE cloud_usage_events (
+ site_id INTEGER NOT NULL,
+ upload_id TEXT NOT NULL,
+ bytes INTEGER NOT NULL,
+ created_at INTEGER NOT NULL,
+ PRIMARY KEY(site_id,upload_id),
+ FOREIGN KEY(site_id,upload_id) REFERENCES cloud_uploads(site_id,id)
+);
diff --git a/packages/platform/migrations/0006_cloud_execution.sql b/packages/platform/migrations/0006_cloud_execution.sql
new file mode 100644
index 00000000..2dcc6f27
--- /dev/null
+++ b/packages/platform/migrations/0006_cloud_execution.sql
@@ -0,0 +1,40 @@
+-- D1 is the durable transactional work queue. External processing uses leases.
+ALTER TABLE cloud_processing_jobs ADD COLUMN attempts INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_processing_jobs ADD COLUMN max_attempts INTEGER NOT NULL DEFAULT 3;
+ALTER TABLE cloud_processing_jobs ADD COLUMN available_at INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_processing_jobs ADD COLUMN lease_until INTEGER;
+ALTER TABLE cloud_processing_jobs ADD COLUMN attempt_id TEXT;
+ALTER TABLE cloud_processing_jobs ADD COLUMN worker_id TEXT;
+ALTER TABLE cloud_processing_jobs ADD COLUMN error_code TEXT;
+ALTER TABLE cloud_photos ADD COLUMN metadata_json TEXT;
+CREATE TABLE cloud_job_attempts (
+ site_id INTEGER NOT NULL,
+ job_id TEXT NOT NULL,
+ attempt_id TEXT NOT NULL,
+ worker_id TEXT NOT NULL,
+ started_at INTEGER NOT NULL,
+ finished_at INTEGER,
+ state TEXT NOT NULL CHECK (state IN ('running','expired','succeeded','failed','cancelled')),
+ PRIMARY KEY(site_id,job_id,attempt_id),
+ UNIQUE(site_id,attempt_id),
+ FOREIGN KEY(site_id,job_id) REFERENCES cloud_processing_jobs(site_id,id)
+);
+CREATE TABLE cloud_media_assets (
+ site_id INTEGER NOT NULL,
+ photo_id TEXT NOT NULL,
+ source_version TEXT NOT NULL,
+ pipeline_version TEXT NOT NULL,
+ role TEXT NOT NULL CHECK(role IN ('thumbnail','live-video','display')),
+ connection_id TEXT NOT NULL,
+ object_key TEXT NOT NULL,
+ bytes INTEGER NOT NULL CHECK(bytes>0),
+ content_type TEXT NOT NULL,
+ PRIMARY KEY(site_id,photo_id,source_version,pipeline_version,role),
+ FOREIGN KEY(site_id,photo_id) REFERENCES cloud_photos(site_id,id)
+);
+CREATE INDEX cloud_jobs_available ON cloud_processing_jobs(site_id,state,available_at,lease_until,created_at);
+CREATE INDEX cloud_jobs_global_poll ON cloud_processing_jobs(state,available_at,lease_until,created_at,site_id);
+CREATE TABLE cloud_site_dispatch (
+ site_id INTEGER PRIMARY KEY REFERENCES platform_sites(id),
+ last_claim_at INTEGER NOT NULL
+);
diff --git a/packages/platform/migrations/0007_cloud_storage.sql b/packages/platform/migrations/0007_cloud_storage.sql
new file mode 100644
index 00000000..6f02b00f
--- /dev/null
+++ b/packages/platform/migrations/0007_cloud_storage.sql
@@ -0,0 +1,26 @@
+CREATE TABLE cloud_storage_connections (
+ site_id INTEGER NOT NULL REFERENCES platform_sites(id),
+ id TEXT NOT NULL,
+ current_revision INTEGER NOT NULL,
+ checked_at INTEGER,
+ healthy INTEGER NOT NULL DEFAULT 0 CHECK(healthy IN (0,1)),
+ PRIMARY KEY(site_id,id)
+);
+CREATE TABLE cloud_storage_versions (
+ site_id INTEGER NOT NULL,
+ connection_id TEXT NOT NULL,
+ revision INTEGER NOT NULL,
+ public_json TEXT NOT NULL,
+ secret_ciphertext TEXT NOT NULL,
+ PRIMARY KEY(site_id,connection_id,revision),
+ FOREIGN KEY(site_id,connection_id) REFERENCES cloud_storage_connections(site_id,id)
+);
+ALTER TABLE cloud_uploads ADD COLUMN connection_revision INTEGER NOT NULL DEFAULT 1;
+ALTER TABLE cloud_uploads ADD COLUMN content_type TEXT NOT NULL DEFAULT 'image/jpeg';
+ALTER TABLE cloud_uploads ADD COLUMN file_name TEXT NOT NULL DEFAULT 'photo';
+ALTER TABLE cloud_uploads ADD COLUMN transport_token TEXT;
+ALTER TABLE cloud_uploads ADD COLUMN transport_lease_until INTEGER;
+ALTER TABLE cloud_media_assets ADD COLUMN connection_revision INTEGER NOT NULL DEFAULT 1;
+ALTER TABLE cloud_uploads ADD COLUMN verified_etag TEXT;
+ALTER TABLE cloud_uploads ADD COLUMN video_key TEXT;
+ALTER TABLE cloud_uploads ADD COLUMN video_etag TEXT;
diff --git a/packages/platform/migrations/0008_cloud_albums.sql b/packages/platform/migrations/0008_cloud_albums.sql
new file mode 100644
index 00000000..0673660f
--- /dev/null
+++ b/packages/platform/migrations/0008_cloud_albums.sql
@@ -0,0 +1,23 @@
+ALTER TABLE cloud_albums ADD COLUMN description TEXT;
+ALTER TABLE cloud_albums ADD COLUMN hidden INTEGER NOT NULL DEFAULT 0 CHECK(hidden IN (0,1));
+ALTER TABLE cloud_albums ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_albums ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_albums ADD COLUMN updated_at INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_albums ADD COLUMN cover_photo_id TEXT;
+CREATE INDEX cloud_album_order ON cloud_albums(site_id,position,created_at);
+CREATE TABLE cloud_cleanup_requests (
+ site_id INTEGER NOT NULL,
+ id TEXT NOT NULL,
+ connection_id TEXT NOT NULL,
+ connection_revision INTEGER NOT NULL,
+ object_key TEXT NOT NULL,
+ provider_upload_id TEXT,
+ state TEXT NOT NULL DEFAULT 'pending' CHECK(state IN ('pending','done')),
+ attempts INTEGER NOT NULL DEFAULT 0,
+ available_at INTEGER NOT NULL DEFAULT 0,
+ PRIMARY KEY(site_id,id)
+);
+ALTER TABLE cloud_album_photos ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
+ALTER TABLE cloud_photos ADD COLUMN revision INTEGER NOT NULL DEFAULT 1;
+CREATE INDEX cloud_album_membership_photo ON cloud_album_photos(site_id,photo_id,album_id);
+ALTER TABLE cloud_albums ADD COLUMN revision INTEGER NOT NULL DEFAULT 1;
diff --git a/packages/platform/src/index.ts b/packages/platform/src/index.ts
index 38febdbd..b8414400 100644
--- a/packages/platform/src/index.ts
+++ b/packages/platform/src/index.ts
@@ -187,3 +187,5 @@ export {
ProvisioningError,
} from './provisioning.ts'
export type { ManagedSite } from './provisioning.ts'
+export { createStorageVault } from './storage-vault.ts'
+export type { CloudStoragePublic, CloudStorageSecret } from './storage-vault.ts'
diff --git a/packages/platform/src/storage-vault.ts b/packages/platform/src/storage-vault.ts
new file mode 100644
index 00000000..afec465b
--- /dev/null
+++ b/packages/platform/src/storage-vault.ts
@@ -0,0 +1,145 @@
+import { CoreError } from '@chronoframe/core'
+import type { PlatformD1 } from './index.ts'
+export interface CloudStoragePublic {
+ endpoint: string
+ region: string
+ bucket: string
+}
+export interface CloudStorageSecret {
+ accessKeyId: string
+ secretAccessKey: string
+}
+export interface StorageCipher {
+ seal(value: CloudStorageSecret, context: string): Promise<string>
+ open(envelope: string, context: string): Promise<CloudStorageSecret>
+}
+export function createStorageVault(
+ db: PlatformD1,
+ siteId: number,
+ cipher: StorageCipher,
+) {
+ if (!Number.isSafeInteger(siteId) || siteId < 1)
+ throw new RangeError('Site required')
+ const aad = (id: string, revision: number) =>
+ JSON.stringify(['cloud-storage', siteId, id, revision])
+ function validId(id: string) {
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(id))
+ throw new RangeError('Invalid connection ID')
+ }
+ return {
+ async list() {
+ const result = (await db
+ .prepare(
+ 'SELECT c.id,c.current_revision,c.healthy,c.checked_at,v.public_json FROM cloud_storage_connections c JOIN cloud_storage_versions v ON v.site_id=c.site_id AND v.connection_id=c.id AND v.revision=c.current_revision WHERE c.site_id=? ORDER BY c.id LIMIT 20',
+ )
+ .bind(siteId)) as unknown as { all<T>(): Promise<{ results: T[] }> }
+ return (
+ await result.all<{
+ id: string
+ current_revision: number
+ healthy: number
+ checked_at: number | null
+ public_json: string
+ }>()
+ ).results.map(({ public_json, ...record }) => ({
+ ...record,
+ config: JSON.parse(public_json) as CloudStoragePublic,
+ }))
+ },
+ async get(id: string, revision?: number) {
+ validId(id)
+ const row = await db
+ .prepare(
+ `SELECT v.revision,v.public_json,v.secret_ciphertext FROM cloud_storage_versions v JOIN cloud_storage_connections c ON c.site_id=v.site_id AND c.id=v.connection_id WHERE v.site_id=? AND v.connection_id=? AND v.revision=COALESCE(?,c.current_revision)`,
+ )
+ .bind(siteId, id, revision ?? null)
+ .first<{
+ revision: number
+ public_json: string
+ secret_ciphertext: string
+ }>()
+ if (!row) return null
+ return {
+ revision: row.revision,
+ config: JSON.parse(row.public_json) as CloudStoragePublic,
+ credentials: await cipher.open(
+ row.secret_ciphertext,
+ aad(id, row.revision),
+ ),
+ }
+ },
+ async create(
+ id: string,
+ config: CloudStoragePublic,
+ credentials: CloudStorageSecret,
+ now: number,
+ ) {
+ validId(id)
+ const encrypted = await cipher.seal(credentials, aad(id, 1))
+ const results = await db.batch([
+ db
+ .prepare(
+ 'INSERT INTO cloud_storage_connections(site_id,id,current_revision,healthy,checked_at) SELECT ?,?,1,1,? WHERE (SELECT COUNT(*) FROM cloud_storage_connections WHERE site_id=?)<20',
+ )
+ .bind(siteId, id, now, siteId),
+ db
+ .prepare(
+ 'INSERT INTO cloud_storage_versions(site_id,connection_id,revision,public_json,secret_ciphertext) SELECT ?,?,1,?,? WHERE EXISTS(SELECT 1 FROM cloud_storage_connections WHERE site_id=? AND id=?)',
+ )
+ .bind(siteId, id, JSON.stringify(config), encrypted, siteId, id),
+ ])
+ if (!(results[0] as { meta?: { changes?: number } }).meta?.changes)
+ throw new CoreError(429, 'Storage connection limit reached')
+ },
+ async rotate(
+ id: string,
+ expectedRevision: number,
+ credentials: CloudStorageSecret,
+ now: number,
+ ) {
+ validId(id)
+ if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 1)
+ throw new RangeError('Revision required')
+ const revision = expectedRevision + 1
+ const encrypted = await cipher.seal(credentials, aad(id, revision))
+ await db.batch([
+ db
+ .prepare(
+ `INSERT INTO cloud_storage_versions(site_id,connection_id,revision,public_json,secret_ciphertext) SELECT ?,?, ?,public_json,? FROM cloud_storage_versions WHERE site_id=? AND connection_id=? AND revision=? AND EXISTS(SELECT 1 FROM cloud_storage_connections WHERE site_id=? AND id=? AND current_revision=?)`,
+ )
+ .bind(
+ siteId,
+ id,
+ revision,
+ encrypted,
+ siteId,
+ id,
+ expectedRevision,
+ siteId,
+ id,
+ expectedRevision,
+ ),
+ db
+ .prepare(
+ 'UPDATE cloud_storage_connections SET current_revision=?,healthy=1,checked_at=? WHERE site_id=? AND id=? AND current_revision=?',
+ )
+ .bind(revision, now, siteId, id, expectedRevision),
+ ])
+ const row = await db
+ .prepare(
+ 'SELECT secret_ciphertext FROM cloud_storage_versions WHERE site_id=? AND connection_id=? AND revision=?',
+ )
+ .bind(siteId, id, revision)
+ .first<{ secret_ciphertext: string }>()
+ return row?.secret_ciphertext === encrypted
+ },
+ async health(id: string, healthy: boolean, now: number) {
+ await db
+ .prepare(
+ 'UPDATE cloud_storage_connections SET healthy=?,checked_at=? WHERE site_id=? AND id=?',
+ )
+ .bind(healthy ? 1 : 0, now, siteId, id)
+ .run()
+ },
+ }
+}
diff --git a/packages/storage/package.json b/packages/storage/package.json
index 91c643aa..d6a595b5 100644
--- a/packages/storage/package.json
+++ b/packages/storage/package.json
@@ -9,19 +9,27 @@
"./manager": "./src/manager.ts",
"./providers/local": "./src/providers/local.ts",
"./providers/s3": "./src/providers/s3.ts",
- "./providers/openlist": "./src/providers/openlist.ts"
+ "./providers/openlist": "./src/providers/openlist.ts",
+ "./s3-edge": {
+ "types": "./src/s3-edge.ts",
+ "default": "./dist/s3-edge.mjs"
+ },
+ "./credential-cipher": "./src/credential-cipher.ts"
},
"scripts": {
- "type-check": "tsc --noEmit"
+ "type-check": "tsc --noEmit",
+ "build": "node scripts/build-edge.mjs"
},
"dependencies": {
- "@chronoframe/contracts": "workspace:*",
"@aws-sdk/client-s3": "^3.1149.0",
- "@aws-sdk/s3-request-presigner": "^3.1149.0"
+ "@aws-sdk/s3-request-presigner": "^3.1149.0",
+ "@chronoframe/contracts": "workspace:*",
+ "@smithy/fetch-http-handler": "5.8.0"
},
"devDependencies": {
"@types/node": "^22.20.5",
- "typescript": "^5.9.3"
+ "typescript": "^5.9.3",
+ "esbuild": "0.28.0"
},
"license": "AGPL-3.0-only"
}
diff --git a/packages/storage/scripts/build-edge.mjs b/packages/storage/scripts/build-edge.mjs
new file mode 100644
index 00000000..d0fd1b84
--- /dev/null
+++ b/packages/storage/scripts/build-edge.mjs
@@ -0,0 +1,45 @@
+import { build } from 'esbuild'
+import { fileURLToPath } from 'node:url'
+import { mkdir, readFile } from 'node:fs/promises'
+const root = fileURLToPath(new URL('..', import.meta.url))
+await mkdir(root + '/dist', { recursive: true })
+const result = await build({
+ entryPoints: [root + '/src/s3-edge.ts'],
+ outfile: root + '/dist/s3-edge.mjs',
+ bundle: true,
+ platform: 'browser',
+ format: 'esm',
+ target: 'es2022',
+ minify: true,
+ metafile: true,
+ // Workers have no DOMParser. Use the SDK's own dependency-free XML parser
+ // while keeping the rest of the SDK on its browser/Fetch runtime.
+ plugins: [
+ {
+ name: 'worker-sdk-xml',
+ setup(builder) {
+ builder.onLoad(
+ { filter: /xml-parser\.browser\.js$/ },
+ async (args) => ({
+ contents: await readFile(
+ args.path.replace(/xml-parser\.browser\.js$/, 'xml-parser.js'),
+ 'utf8',
+ ),
+ loader: 'js',
+ }),
+ )
+ },
+ },
+ ],
+})
+const prohibited = Object.keys(result.metafile.inputs).filter((name) =>
+ /credential-provider-node|node-http-handler|shared-ini-file-loader/.test(
+ name,
+ ),
+)
+if (/\bDOMParser\b/.test(await readFile(root + '/dist/s3-edge.mjs', 'utf8')))
+ throw new Error('DOM-only XML dependency in edge build')
+if (prohibited.length) throw new Error('Node-only S3 dependency in edge build')
+console.log(
+ 'Built browser S3 transport without Node credential files or HTTP agents.',
+)
diff --git a/packages/storage/src/credential-cipher.ts b/packages/storage/src/credential-cipher.ts
new file mode 100644
index 00000000..6a8c28e5
--- /dev/null
+++ b/packages/storage/src/credential-cipher.ts
@@ -0,0 +1,68 @@
+export interface CloudStorageSecret {
+ accessKeyId: string
+ secretAccessKey: string
+}
+const encoder = new TextEncoder()
+function encode(value: Uint8Array) {
+ return btoa(String.fromCharCode(...value))
+}
+function decode(value: string) {
+ return Uint8Array.from(atob(value), (c) => c.charCodeAt(0))
+}
+/** AES-GCM authenticates tenant, connection and immutable credential revision.
+ * HKDF separates the encryption key from platform session/authentication keys.
+ */
+export async function createStorageCipher(secret: string) {
+ if (secret.length < 32) throw new Error('Storage encryption unavailable')
+ const material = await crypto.subtle.importKey(
+ 'raw',
+ encoder.encode(secret),
+ 'HKDF',
+ false,
+ ['deriveKey'],
+ )
+ const key = await crypto.subtle.deriveKey(
+ {
+ name: 'HKDF',
+ hash: 'SHA-256',
+ salt: encoder.encode('chronoframe-storage-v1'),
+ info: encoder.encode('credential-encryption'),
+ },
+ material,
+ { name: 'AES-GCM', length: 256 },
+ false,
+ ['encrypt', 'decrypt'],
+ )
+ return {
+ async seal(value: CloudStorageSecret, context: string) {
+ const nonce = crypto.getRandomValues(new Uint8Array(12))
+ const cipher = await crypto.subtle.encrypt(
+ { name: 'AES-GCM', iv: nonce, additionalData: encoder.encode(context) },
+ key,
+ encoder.encode(JSON.stringify(value)),
+ )
+ return 'v1.' + encode(nonce) + '.' + encode(new Uint8Array(cipher))
+ },
+ async open(envelope: string, context: string): Promise<CloudStorageSecret> {
+ const [version, iv, body, ...extra] = envelope.split('.')
+ if (
+ version !== 'v1' ||
+ !iv ||
+ !body ||
+ extra.length ||
+ envelope.length > 4096
+ )
+ throw new Error('Invalid credential envelope')
+ const plain = await crypto.subtle.decrypt(
+ {
+ name: 'AES-GCM',
+ iv: decode(iv),
+ additionalData: encoder.encode(context),
+ },
+ key,
+ decode(body),
+ )
+ return JSON.parse(new TextDecoder().decode(plain))
+ },
+ }
+}
diff --git a/packages/storage/src/s3-edge.ts b/packages/storage/src/s3-edge.ts
new file mode 100644
index 00000000..98c9dc82
--- /dev/null
+++ b/packages/storage/src/s3-edge.ts
@@ -0,0 +1,303 @@
+import {
+ S3Client,
+ PutObjectCommand,
+ GetObjectCommand,
+ HeadObjectCommand,
+ DeleteObjectCommand,
+ CreateMultipartUploadCommand,
+ UploadPartCommand,
+ CompleteMultipartUploadCommand,
+ AbortMultipartUploadCommand,
+ GetBucketCorsCommand,
+ ListPartsCommand,
+ ListMultipartUploadsCommand,
+} from '@aws-sdk/client-s3'
+import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
+import { FetchHttpHandler } from '@smithy/fetch-http-handler'
+
+export interface CloudS3Config {
+ endpoint: string
+ region: string
+ bucket: string
+ accessKeyId: string
+ secretAccessKey: string
+}
+/** Trusted provider endpoints are still one S3 implementation. Custom S3 hosts
+ * require an operator allowlist: arbitrary user DNS cannot bypass egress policy.
+ */
+export function validateCloudS3(
+ config: CloudS3Config,
+ allowedHosts: readonly string[] = [],
+) {
+ const endpoint = new URL(config.endpoint)
+ const host = endpoint.hostname
+ const trusted =
+ /^(?:s3(?:[.-][a-z0-9-]+)?\.amazonaws\.com(?:\.cn)?|[a-f0-9]{32}(?:\.(?:eu|fedramp))?\.r2\.cloudflarestorage\.com)$/.test(
+ host,
+ )
+ if (
+ endpoint.protocol !== 'https:' ||
+ endpoint.username ||
+ endpoint.password ||
+ endpoint.port ||
+ endpoint.pathname !== '/' ||
+ endpoint.search ||
+ endpoint.hash ||
+ (!trusted && !allowedHosts.includes(host)) ||
+ !/^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/.test(config.bucket) ||
+ /\.\.|^\d+\.\d+\.\d+\.\d+$/.test(config.bucket) ||
+ !/^[a-z0-9-]{1,40}$/.test(config.region) ||
+ !config.accessKeyId ||
+ config.accessKeyId.length > 256 ||
+ !config.secretAccessKey ||
+ config.secretAccessKey.length > 256
+ )
+ throw new Error('Unsupported S3 configuration')
+ // Even operator configuration cannot authorize loopback, literal/private IPs,
+ // ambiguous numeric addresses, local names or DNS metadata endpoints.
+ if (
+ !/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(host) ||
+ !host.includes('.') ||
+ /(?:^|\.)(?:localhost|local|internal|metadata|invalid)$/.test(host) ||
+ /^(?:\d+\.)*\d+$/.test(host) ||
+ host === 'metadata.google.internal'
+ )
+ throw new Error('Unsafe S3 endpoint')
+ return endpoint.origin
+}
+export function createCloudS3(
+ config: CloudS3Config,
+ options: { allowedHosts?: readonly string[]; fetch?: typeof fetch } = {},
+) {
+ const endpoint = validateCloudS3(config, options.allowedHosts)
+ const transport = options.fetch ?? fetch
+ const client = new S3Client({
+ endpoint,
+ region: config.region,
+ forcePathStyle: true,
+ maxAttempts: 2,
+ credentials: {
+ accessKeyId: config.accessKeyId,
+ secretAccessKey: config.secretAccessKey,
+ },
+ requestChecksumCalculation: 'WHEN_REQUIRED',
+ responseChecksumValidation: 'WHEN_REQUIRED',
+ requestHandler: new FetchHttpHandler({
+ requestTimeout: 15000,
+ requestInit: () => ({ redirect: 'manual' }),
+ customFetch: async (input, init) => {
+ const url = new URL(
+ input instanceof Request ? input.url : String(input),
+ )
+ if (url.origin !== endpoint)
+ throw new Error('Unexpected S3 destination')
+ const response = await transport(input, { ...init, redirect: 'manual' })
+ if (response.status >= 300 && response.status < 400) {
+ await response.body?.cancel()
+ throw new Error('S3 redirect refused')
+ }
+ return response
+ },
+ }),
+ })
+ const Bucket = config.bucket
+ const safeKey = (key: string) => {
+ if (
+ !key ||
+ key.length > 1024 ||
+ key.startsWith('/') ||
+ key.split('/').some((p) => !p || p === '.' || p === '..') ||
+ Array.from(key).some((c) => c.charCodeAt(0) < 32 || c === '\\')
+ )
+ throw new Error('Invalid object key')
+ return key
+ }
+ return {
+ async signGet(key: string, expiresIn = 60) {
+ return getSignedUrl(
+ client,
+ new GetObjectCommand({ Bucket, Key: safeKey(key) }),
+ { expiresIn },
+ )
+ },
+ async signPut(
+ key: string,
+ bytes: number,
+ contentType: string,
+ expiresIn = 300,
+ ) {
+ if (
+ !Number.isSafeInteger(bytes) ||
+ bytes < 1 ||
+ bytes > 100 * 1024 * 1024
+ )
+ throw new Error('Invalid upload size')
+ return getSignedUrl(
+ client,
+ new PutObjectCommand({
+ Bucket,
+ Key: safeKey(key),
+ ContentLength: bytes,
+ ContentType: contentType,
+ }),
+ { expiresIn },
+ )
+ },
+ async head(key: string) {
+ const r = await client.send(
+ new HeadObjectCommand({ Bucket, Key: safeKey(key) }),
+ )
+ return {
+ bytes: r.ContentLength ?? 0,
+ contentType: r.ContentType ?? '',
+ etag: r.ETag ?? '',
+ }
+ },
+ async remove(key: string) {
+ await client.send(new DeleteObjectCommand({ Bucket, Key: safeKey(key) }))
+ },
+ async put(key: string, body: Uint8Array, contentType: string) {
+ await client.send(
+ new PutObjectCommand({
+ Bucket,
+ Key: safeKey(key),
+ Body: body,
+ ContentType: contentType,
+ }),
+ )
+ },
+ async getProbe(key: string) {
+ const result = await client.send(
+ new GetObjectCommand({ Bucket, Key: safeKey(key) }),
+ )
+ const stream = result.Body?.transformToWebStream()
+ if (!stream) return undefined
+ const reader = stream.getReader(),
+ bytes = new Uint8Array(4096)
+ let size = 0
+ try {
+ while (true) {
+ const next = await reader.read()
+ if (next.done) break
+ if (size + next.value.length > bytes.length) {
+ await reader.cancel()
+ throw new Error('Storage probe too large')
+ }
+ bytes.set(next.value, size)
+ size += next.value.length
+ }
+ } finally {
+ reader.releaseLock()
+ }
+ return bytes.subarray(0, size)
+ },
+ async cors(origin: string) {
+ const r = await client.send(new GetBucketCorsCommand({ Bucket }))
+ return (r.CORSRules ?? []).some(
+ (rule) =>
+ (rule.AllowedOrigins ?? []).includes(origin) &&
+ ['GET', 'PUT', 'HEAD'].every((method) =>
+ rule.AllowedMethods?.includes(method),
+ ) &&
+ (rule.ExposeHeaders ?? []).some((h) => h.toLowerCase() === 'etag') &&
+ (rule.AllowedHeaders ?? []).some((h) =>
+ ['*', 'content-type'].includes(h.toLowerCase()),
+ ),
+ )
+ },
+ async recoverMultipart(key: string) {
+ const result = await client.send(
+ new ListMultipartUploadsCommand({
+ Bucket,
+ Prefix: safeKey(key),
+ MaxUploads: 100,
+ }),
+ )
+ if (result.IsTruncated)
+ throw new Error('Too many multipart sessions for one object')
+ return (result.Uploads ?? [])
+ .filter((upload) => upload.Key === key && upload.UploadId)
+ .map((upload) => upload.UploadId!)
+ },
+ async multipart(key: string, contentType: string) {
+ const r = await client.send(
+ new CreateMultipartUploadCommand({
+ Bucket,
+ Key: safeKey(key),
+ ContentType: contentType,
+ }),
+ )
+ if (!r.UploadId) throw new Error('Missing multipart identity')
+ return r.UploadId
+ },
+ async signPart(
+ key: string,
+ uploadId: string,
+ partNumber: number,
+ bytes: number,
+ ) {
+ if (!Number.isSafeInteger(bytes) || bytes < 1 || bytes > 10485760)
+ throw new Error('Invalid upload part size')
+ if (
+ !Number.isSafeInteger(partNumber) ||
+ partNumber < 1 ||
+ partNumber > 10000
+ )
+ throw new Error('Invalid part')
+ return getSignedUrl(
+ client,
+ new UploadPartCommand({
+ Bucket,
+ Key: safeKey(key),
+ UploadId: uploadId,
+ PartNumber: partNumber,
+ ContentLength: bytes,
+ }),
+ { expiresIn: 300 },
+ )
+ },
+ async parts(key: string, uploadId: string) {
+ const r = await client.send(
+ new ListPartsCommand({
+ Bucket,
+ Key: safeKey(key),
+ UploadId: uploadId,
+ MaxParts: 1000,
+ }),
+ )
+ return (r.Parts ?? []).map((p) => ({
+ partNumber: p.PartNumber!,
+ etag: p.ETag!,
+ bytes: p.Size!,
+ }))
+ },
+ async finish(
+ key: string,
+ uploadId: string,
+ parts: { partNumber: number; etag: string }[],
+ ) {
+ await client.send(
+ new CompleteMultipartUploadCommand({
+ Bucket,
+ Key: safeKey(key),
+ UploadId: uploadId,
+ MultipartUpload: {
+ Parts: parts.map((p) => ({
+ PartNumber: p.partNumber,
+ ETag: p.etag,
+ })),
+ },
+ }),
+ )
+ },
+ async abort(key: string, uploadId: string) {
+ await client.send(
+ new AbortMultipartUploadCommand({
+ Bucket,
+ Key: safeKey(key),
+ UploadId: uploadId,
+ }),
+ )
+ },
+ }
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bf02ec85..cfc4eab8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -264,6 +264,37 @@ importers:
specifier: 2.0.0-alpha.20
+ apps/worker:
+ dependencies:
+ '@chronoframe/core':
+ specifier: workspace:*
+ version: link:../../packages/core
+ '@chronoframe/contracts':
+ specifier: workspace:*
+ version: link:../../packages/contracts
+ '@chronoframe/processing':
+ specifier: workspace:*
+ version: link:../../packages/processing
+ '@chronoframe/storage':
+ specifier: workspace:*
+ version: link:../../packages/storage
+ '@chronoframe/media':
+ specifier: workspace:*
+ version: link:../../packages/media
+ 'exiftool-vendored':
+ specifier: ^35.21.0
+ version: 35.21.0
+ 'sharp':
+ specifier: 0.35.5
+ version: 0.35.5(@types/[email protected])
+ devDependencies:
+ '@types/node':
+ specifier: ^22.20.5
+ version: 22.20.5
+ 'typescript':
+ specifier: ^5.9.3
+ version: 5.9.3
+
packages/console:
dependencies:
"@chronoframe/gallery":
@@ -487,6 +518,9 @@ importers:
version: 5.9.3
packages/storage:
dependencies:
+ '@smithy/fetch-http-handler':
+ specifier: 5.8.0
+ version: 5.8.0
"@aws-sdk/client-s3":
specifier: ^3.1149.0
version: 3.1149.0
@@ -497,6 +531,9 @@ importers:
specifier: workspace:*
version: link:../contracts
devDependencies:
+ esbuild:
+ specifier: 0.28.0
+ version: 0.28.0
"@types/node":
specifier: ^22.20.5
version: 22.20.5
@@ -709,6 +746,12 @@ importers:
apps/cloud:
dependencies:
+ '@chronoframe/database':
+ specifier: workspace:*
+ version: link:../../packages/database
+ '@chronoframe/storage':
+ specifier: workspace:*
+ version: link:../../packages/storage
better-auth:
specifier: 1.7.12
@@ -811,6 +854,15 @@ importers:
apps/site:
dependencies:
+ '@chronoframe/contracts':
+ specifier: workspace:*
+ version: link:../../packages/contracts
+ '@chronoframe/database':
+ specifier: workspace:*
+ version: link:../../packages/database
+ '@chronoframe/storage':
+ specifier: workspace:*
+ version: link:../../packages/storage
h3:
specifier: 1.15.11
version: 1.15.11
diff --git a/scripts/check-entry-boundaries.mjs b/scripts/check-entry-boundaries.mjs
index 194bd132..2073959f 100644
--- a/scripts/check-entry-boundaries.mjs
+++ b/scripts/check-entry-boundaries.mjs
@@ -40,7 +40,15 @@ for (const entry of ['cloud', 'site']) {
/^(?:node:)?(?:fs(?:\/|$)|child_process|worker_threads)/.test(specifier)
)
failures.push(`${file}: ${specifier}`)
+ const edgeSafe = [
+ '@chronoframe/database/cloud-media',
+ '@chronoframe/database/cloud-execution',
+ '@chronoframe/database/cloud-resources',
+ '@chronoframe/storage/s3-edge',
+ '@chronoframe/storage/credential-cipher',
+ ].includes(specifier)
if (
+ !edgeSafe &&
/self-hosted|@chronoframe\/(?:database|storage|media|processing|jobs)(?:\/|$)|better-sqlite3|exiftool|sharp|heic-convert/.test(
specifier,
)
diff --git a/scripts/saas-entry.mjs b/scripts/saas-entry.mjs
index 1722c26c..43eb822c 100644
--- a/scripts/saas-entry.mjs
+++ b/scripts/saas-entry.mjs
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createRequire } from 'node:module'
-import { spawn } from 'node:child_process'
+import { spawn, execFileSync } from 'node:child_process'
const root = fileURLToPath(new URL('../', import.meta.url))
const [entry, command = 'dev', ...args] = process.argv.slice(2)
if (
@@ -14,6 +14,12 @@ const app = resolve(root, 'apps', entry)
if (command === 'prepare' && !existsSync(resolve(app, 'nuxt.config.ts'))) {
console.log('Application preparation deferred until sources are available.')
} else {
+ if (command === 'build')
+ execFileSync(
+ process.execPath,
+ [resolve(root, 'packages/storage/scripts/build-edge.mjs')],
+ { cwd: root, stdio: 'inherit' },
+ )
const require = createRequire(import.meta.url)
const cli = resolve(
dirname(require.resolve('nuxt/package.json')),
diff --git a/scripts/setup-cloud-media.mjs b/scripts/setup-cloud-media.mjs
new file mode 100644
index 00000000..1f2df448
--- /dev/null
+++ b/scripts/setup-cloud-media.mjs
@@ -0,0 +1,100 @@
+import { randomBytes, randomUUID } from 'node:crypto'
+import { mkdir, readFile, writeFile, realpath, chmod } from 'node:fs/promises'
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { spawnSync } from 'node:child_process'
+const args = process.argv.slice(2)
+const options = {}
+for (let i = 0; i < args.length; i += 2) {
+ if (
+ !['--environment', '--portal', '--directory'].includes(args[i]) ||
+ !args[i + 1]
+ )
+ throw new Error(
+ 'Use --environment staging|production --portal https://portal.origin --directory /private/path',
+ )
+ options[args[i].slice(2)] = args[i + 1]
+}
+const { environment, portal, directory } = options
+if (
+ !['staging', 'production'].includes(environment) ||
+ !portal ||
+ new URL(portal).protocol !== 'https:' ||
+ new URL(portal).origin !== portal ||
+ !directory ||
+ !path.isAbsolute(directory)
+)
+ throw new Error(
+ 'Explicit environment, HTTPS origin and absolute private output directory are required',
+ )
+await mkdir(directory, { recursive: true, mode: 0o700 })
+const target = await realpath(directory),
+ root = await realpath(fileURLToPath(new URL('..', import.meta.url)))
+if (target === root || target.startsWith(root + path.sep))
+ throw new Error('Keep generated secrets outside the repository')
+const envName = 'saas-' + environment
+function gh(args, input) {
+ const result = spawnSync(
+ 'gh',
+ [...args, '--repo', 'HoshinoSuzumi/chronoframe'],
+ { input, encoding: 'utf8' },
+ )
+ if (result.status !== 0)
+ throw new Error(
+ 'GitHub secret configuration failed; inspect CLI authorization without logging secret values',
+ )
+ return result.stdout
+}
+const existing = JSON.parse(
+ gh(['secret', 'list', '--env', envName, '--json', 'name']),
+)
+const file = path.join(target, environment + '-media-secrets.json')
+let secrets
+try {
+ secrets = JSON.parse(await readFile(file, 'utf8'))
+} catch (error) {
+ if (error.code !== 'ENOENT') throw error
+}
+if (
+ !secrets &&
+ existing.some((s) =>
+ ['PLATFORM_STORAGE_SECRET', 'PLATFORM_WORKER_SECRET'].includes(s.name),
+ )
+)
+ throw new Error(
+ 'Secrets already exist remotely. Use the original protected output directory; do not silently rotate a storage encryption key',
+ )
+if (!secrets) {
+ secrets = {
+ storage: randomBytes(48).toString('base64url'),
+ worker: randomBytes(48).toString('base64url'),
+ workerId: randomUUID(),
+ environment,
+ }
+ await writeFile(file, JSON.stringify(secrets), { mode: 0o600, flag: 'wx' })
+}
+if (
+ secrets.environment !== environment ||
+ secrets.storage?.length < 32 ||
+ secrets.worker?.length < 32
+)
+ throw new Error('Invalid protected configuration')
+await chmod(file, 0o600)
+gh(
+ ['secret', 'set', 'PLATFORM_STORAGE_SECRET', '--env', envName],
+ secrets.storage,
+)
+gh(
+ ['secret', 'set', 'PLATFORM_WORKER_SECRET', '--env', envName],
+ secrets.worker,
+)
+const workerEnv = path.join(target, environment + '-worker.env')
+await writeFile(
+ workerEnv,
+ `CHRONOFRAME_PORTAL_ORIGIN=${portal}\nCHRONOFRAME_WORKER_SECRET=${secrets.worker}\nCHRONOFRAME_WORKER_ID=${secrets.workerId}\n`,
+ { mode: 0o600 },
+)
+await chmod(workerEnv, 0o600)
+console.log(
+ `Configured ${envName}. Protected worker configuration: ${workerEnv}. Back up the media-secrets.json file securely; losing its storage key makes stored credentials unreadable. No deployment has been started.`,
+)
diff --git a/tests/cloud-execution.test.ts b/tests/cloud-execution.test.ts
new file mode 100644
index 00000000..5bbee050
--- /dev/null
+++ b/tests/cloud-execution.test.ts
@@ -0,0 +1,177 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { createRequire } from 'node:module'
+import { readFile } from 'node:fs/promises'
+import { createCloudMediaRepository } from '../packages/database/src/cloud-media.ts'
+import { createCloudExecutionRepository } from '../packages/database/src/cloud-execution.ts'
+import type {
+ CloudLease,
+ CloudMediaResult,
+} from '../packages/core/src/cloud-execution.ts'
+const require = createRequire(import.meta.url)
+const { Miniflare, convertV4MiniflareOptions } = createRequire(
+ require.resolve('wrangler/package.json'),
+)('miniflare')
+test('D1 queue serializes claims, fences expired attempts, retries and refuses deleted/foreign source results', async () => {
+ const mf = new Miniflare(
+ convertV4MiniflareOptions({
+ modules: true,
+ compatibilityDate: '2026-10-22',
+ script: 'export default {fetch(){return new Response("test")}}',
+ d1Databases: ['DB'],
+ }),
+ )
+ try {
+ const db = await mf.getD1Database('DB')
+ for (const sql of [
+ 'CREATE TABLE platform_sites(id INTEGER PRIMARY KEY)',
+ 'CREATE TABLE platform_users(id INTEGER PRIMARY KEY)',
+ 'INSERT INTO platform_sites VALUES(1),(2)',
+ 'INSERT INTO platform_users VALUES(1)',
+ ])
+ await db.prepare(sql).run()
+ for (const name of [
+ '0005_media_catalog.sql',
+ '0006_cloud_execution.sql',
+ '0007_cloud_storage.sql',
+ ]) {
+ const migration = await readFile(
+ new URL('../packages/platform/migrations/' + name, import.meta.url),
+ 'utf8',
+ )
+ for (const sql of migration
+ .replace(/^--.*$/gm, '')
+ .split(';')
+ .map((s) => s.trim())
+ .filter(Boolean))
+ await db.prepare(sql).run()
+ }
+ const media = createCloudMediaRepository(db, { id: 1 }),
+ queue = createCloudExecutionRepository(db, { id: 1 })
+ async function enqueue(id: string) {
+ await media.reserveUpload({
+ id,
+ ownerId: 1,
+ photoId: id,
+ sourceVersion: 'v1',
+ connectionId: 'storage',
+ objectKey: 'originals/' + id,
+ providerUploadId: null,
+ expectedBytes: 10,
+ expiresAt: 100000,
+ })
+ await media.completeUpload({
+ id,
+ ownerId: 1,
+ jobId: id,
+ pipelineVersion: 'p1',
+ title: id,
+ now: 1,
+ })
+ }
+ const result = (lease: CloudLease): CloudMediaResult => ({
+ width: 10,
+ height: 20,
+ metadata: {},
+ assets: [
+ {
+ role: 'thumbnail',
+ objectKey: `chronoframe/1/derived/${lease.photoId}/v1/p1/${lease.attemptId}/thumbnail.webp`,
+ bytes: 32,
+ contentType: 'image/webp',
+ },
+ ],
+ })
+ await enqueue('first')
+ const claimed = await Promise.all([
+ queue.claim('worker-a', 'attempt-a', 10, 1000),
+ queue.claim('worker-b', 'attempt-b', 10, 1000),
+ ])
+ assert.equal(claimed.filter(Boolean).length, 1)
+ const first = claimed.find(Boolean)!
+ assert.equal(first.attempts, 1)
+ assert.equal(
+ await createCloudExecutionRepository(db, { id: 1 }).claim(
+ 'restart',
+ 'attempt-restart',
+ 500,
+ 1000,
+ ),
+ null,
+ )
+ assert.equal(await queue.heartbeat(first, 500, 1000), true)
+ assert.equal(await queue.claim('other', 'attempt-other', 1100, 1000), null)
+ const second = (await queue.claim('other', 'attempt-next', 1600, 1000))!
+ assert.equal(second.attempts, 2)
+ assert.equal(await queue.heartbeat(first, 1601, 1000), false)
+ assert.equal(await queue.complete(first, result(first), 1601), false)
+ assert.equal(
+ await queue.complete(
+ { ...second, connectionId: 'foreign' },
+ result(second),
+ 1601,
+ ),
+ false,
+ )
+ await assert.rejects(
+ queue.complete(
+ second,
+ {
+ ...result(second),
+ assets: [
+ { ...result(second).assets[0]!, objectKey: 'other-site/key' },
+ ],
+ },
+ 1601,
+ ),
+ { statusCode: 400 },
+ )
+ assert.equal(await queue.complete(second, result(second), 1601), true)
+ assert.equal(await queue.complete(first, result(first), 1602), false)
+ assert.equal((await media.job('first'))?.state, 'succeeded')
+ assert.equal(
+ (
+ await db
+ .prepare('SELECT state FROM cloud_job_attempts WHERE attempt_id=?')
+ .bind(first.attemptId)
+ .first()
+ ).state,
+ 'expired',
+ )
+ await enqueue('retry')
+ const retry = (await queue.claim('worker', 'retry-1', 2000, 1000))!
+ assert.equal(await queue.fail(retry, 'network_error', true, 2001), true)
+ assert.equal(await queue.claim('worker', 'retry-early', 2500, 1000), null)
+ const retry2 = (await queue.claim('worker', 'retry-2', 3100, 1000))!
+ assert.equal(retry2.attempts, 2)
+ await queue.fail(retry2, 'network_error', true, 3101)
+ const retry3 = (await queue.claim('worker', 'retry-3', 5200, 1000))!
+ assert.equal(retry3.attempts, 3)
+ assert.equal(await queue.claim('worker', 'retry-4', 6300, 1000), null)
+ assert.equal((await media.job('retry'))?.state, 'failed')
+ await enqueue('deleted')
+ const deleted = (await queue.claim('worker', 'deleted-1', 7000, 1000))!
+ await media.deletePhoto('deleted')
+ assert.equal(await queue.complete(deleted, result(deleted), 7001), false)
+ assert.equal(await queue.heartbeat(deleted, 7001, 1000), false)
+ assert.equal(
+ (
+ await db
+ .prepare('SELECT COUNT(*) n FROM cloud_media_assets WHERE photo_id=?')
+ .bind('deleted')
+ .first()
+ ).n,
+ 0,
+ )
+ await assert.rejects(
+ createCloudExecutionRepository(db, { id: 2 }).heartbeat(
+ deleted,
+ 7001,
+ 1000,
+ ),
+ { statusCode: 403 },
+ )
+ } finally {
+ await mf.dispose()
+ }
+})
diff --git a/tests/cloud-media-repositories.test.ts b/tests/cloud-media-repositories.test.ts
index 5f8d0b91..6ad083cd 100644
--- a/tests/cloud-media-repositories.test.ts
+++ b/tests/cloud-media-repositories.test.ts
@@ -134,6 +134,11 @@ for (const backend of ['SQLite', 'D1'] as const) {
now: 10,
})
await a.reserveUpload(reserve('same'))
+ await a.reserveUpload(reserve('same'))
+ await assert.rejects(
+ a.reserveUpload({ ...reserve('same'), expectedBytes: 99 }),
+ { statusCode: 409 },
+ )
await b.reserveUpload(reserve('same'))
assert.equal(await a.upload('same', 2), null)
assert.equal(
@@ -155,6 +160,8 @@ for (const backend of ['SQLite', 'D1'] as const) {
assert.equal((await a.photo('same'))?.title, 'Photo')
assert.equal((await b.photo('same'))?.title, 'Other tenant')
await a.createAlbum('album', 'My album')
+ await a.createAlbum('album', 'My album')
+ await assert.rejects(a.createAlbum('album', 'Other'), { statusCode: 409 })
await a.addToAlbum('album', 'same')
await a.addToAlbum('album', 'same')
assert.equal((await a.albumPhotos('album')).length, 1)
diff --git a/tests/cloud-resource-parity.test.ts b/tests/cloud-resource-parity.test.ts
new file mode 100644
index 00000000..40e3611c
--- /dev/null
+++ b/tests/cloud-resource-parity.test.ts
@@ -0,0 +1,204 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { createRequire } from 'node:module'
+import { readFile, readdir } from 'node:fs/promises'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
+import { createResourceReader } from '@chronoframe/core'
+import * as schema from '../packages/database/src/schema.ts'
+import { createSqliteResourceRepository } from '../packages/database/src/sqlite-resources.ts'
+import { createCloudResourceRepository } from '../packages/database/src/cloud-resources.ts'
+import { createCloudMediaRepository } from '../packages/database/src/cloud-media.ts'
+import { createCloudExecutionRepository } from '../packages/database/src/cloud-execution.ts'
+const require = createRequire(import.meta.url)
+const { Miniflare, convertV4MiniflareOptions } = createRequire(
+ require.resolve('wrangler/package.json'),
+)('miniflare')
+test('Cloud D1 and Self-hosted SQLite share ordering, visibility and protected-album behavior', async () => {
+ const sqlite = new Database(':memory:'),
+ legacy = drizzle(sqlite, { schema })
+ migrate(legacy, { migrationsFolder: './packages/database/migrations' })
+ const mf = new Miniflare(
+ convertV4MiniflareOptions({
+ modules: true,
+ compatibilityDate: '2026-10-22',
+ script: 'export default {fetch(){return new Response("test")}}',
+ d1Databases: ['DB'],
+ }),
+ )
+ try {
+ const db = await mf.getD1Database('DB')
+ for (const name of (await readdir('packages/platform/migrations'))
+ .filter((n) => n.endsWith('.sql'))
+ .sort()) {
+ const sql = await readFile('packages/platform/migrations/' + name, 'utf8')
+ for (const statement of sql
+ .replace(/^--.*$/gm, '')
+ .split(';')
+ .map((s) => s.trim())
+ .filter(Boolean))
+ await db.prepare(statement).run()
+ }
+ await db
+ .prepare(
+ "INSERT INTO platform_users(id,email,created_at) VALUES(1,'[email protected]',1)",
+ )
+ .run()
+ await db
+ .prepare("INSERT INTO platform_workspaces(id,name) VALUES(1,'Test')")
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_sites(id,workspace_id,name,published) VALUES(1,1,'One',0),(2,1,'Two',0)",
+ )
+ .run()
+ const media = createCloudMediaRepository(db, { id: 1 }),
+ queue = createCloudExecutionRepository(db, { id: 1 })
+ const fixtures = [
+ { id: 'old', dateTaken: '2020-01-01', title: 'Old' },
+ { id: 'new', dateTaken: '2026-01-01', title: 'New' },
+ { id: 'hidden', dateTaken: null, title: 'Hidden photo' },
+ { id: 'locked', dateTaken: null, title: 'Locked photo' },
+ ]
+ legacy.insert(schema.photos).values(fixtures).run()
+ for (const p of fixtures) {
+ await media.reserveUpload({
+ id: p.id,
+ ownerId: 1,
+ photoId: p.id,
+ sourceVersion: '1',
+ connectionId: 'storage',
+ objectKey: 'source/' + p.id,
+ providerUploadId: null,
+ expectedBytes: 32,
+ expiresAt: 10000,
+ })
+ await media.completeUpload({
+ id: p.id,
+ ownerId: 1,
+ jobId: p.id,
+ pipelineVersion: '1',
+ title: p.title,
+ now: 1,
+ })
+ const lease = (await queue.claim('worker', 'attempt-' + p.id, 2, 1000))!
+ await queue.complete(
+ lease,
+ {
+ width: 32,
+ height: 16,
+ metadata: { dateTaken: p.dateTaken },
+ assets: [
+ {
+ role: 'thumbnail',
+ objectKey: `chronoframe/1/derived/${p.id}/1/1/${lease.attemptId}/thumbnail.webp`,
+ bytes: 32,
+ contentType: 'image/webp',
+ },
+ ],
+ },
+ 3,
+ )
+ }
+ legacy
+ .insert(schema.albums)
+ .values([
+ { id: 1, title: 'Public', position: 2 },
+ { id: 2, title: 'Hidden', isHidden: true, position: 1 },
+ { id: 3, title: 'Locked', passwordHash: 'password', position: 0 },
+ ])
+ .run()
+ for (const a of [
+ { id: '1', title: 'Public', position: 2, hidden: 0 },
+ { id: '2', title: 'Hidden', position: 1, hidden: 1 },
+ { id: '3', title: 'Locked', position: 0, hidden: 0 },
+ ]) {
+ await media.createAlbum(a.id, a.title)
+ await db
+ .prepare(
+ 'UPDATE cloud_albums SET position=?,hidden=? WHERE site_id=1 AND id=?',
+ )
+ .bind(a.position, a.hidden, a.id)
+ .run()
+ }
+ await db
+ .prepare('INSERT INTO platform_album_access VALUES(1,3,?,?,?)')
+ .bind('revision', 'salt', 'a'.repeat(64))
+ .run()
+ for (const m of [
+ { albumId: 1, photoId: 'old', position: 30 },
+ { albumId: 1, photoId: 'new', position: 10 },
+ { albumId: 1, photoId: 'hidden', position: 5 },
+ { albumId: 1, photoId: 'locked', position: 20 },
+ { albumId: 2, photoId: 'hidden', position: 0 },
+ { albumId: 3, photoId: 'locked', position: 0 },
+ ]) {
+ legacy.insert(schema.albumPhotos).values(m).run()
+ await media.addToAlbum(String(m.albumId), m.photoId)
+ await db
+ .prepare(
+ 'UPDATE cloud_album_photos SET position=? WHERE site_id=1 AND album_id=? AND photo_id=?',
+ )
+ .bind(m.position, String(m.albumId), m.photoId)
+ .run()
+ }
+ const readers = [
+ createResourceReader(createSqliteResourceRepository(legacy, { id: 1 })),
+ createResourceReader(
+ createCloudResourceRepository(db, { id: 1 }, '/api/public/media'),
+ ),
+ ]
+ for (const reader of readers) {
+ assert.deepEqual(
+ (await reader.listPhotos({ admin: true })).slice(0, 2).map((p) => p.id),
+ ['new', 'old'],
+ )
+ assert.deepEqual(
+ (await reader.listVisiblePhotos()).map((p) => p.id),
+ ['new', 'old'],
+ )
+ const albums = await reader.listAlbums({ admin: false })
+ assert.deepEqual(
+ albums.map((a) => a.id),
+ [3, 1],
+ )
+ assert.deepEqual(albums[0]?.photoIds, [])
+ assert.deepEqual(albums[1]?.photoIds, ['new', 'old'])
+ await assert.rejects(
+ reader.getAlbum(2, { admin: false, canAccess: () => false }),
+ { statusCode: 404 },
+ )
+ await assert.rejects(
+ reader.getAlbum(3, { admin: false, canAccess: () => false }),
+ { statusCode: 403 },
+ )
+ assert.deepEqual(
+ (
+ await reader.getAlbum(3, { admin: false, canAccess: () => true })
+ ).photos.map((p) => p.id),
+ ['locked'],
+ )
+ }
+ assert.deepEqual(
+ await createCloudResourceRepository(
+ db,
+ { id: 2 },
+ '/api/public/media',
+ ).listPhotos(),
+ [],
+ )
+ const unlocked = await readers[1]!.getAlbum(3, {
+ admin: false,
+ canAccess: () => true,
+ })
+ assert.equal(
+ unlocked.photos[0]?.originalUrl,
+ '/api/public/media/locked/display',
+ )
+ assert(!JSON.stringify(unlocked).includes('source/locked'))
+ } finally {
+ sqlite.close()
+ await mf.dispose()
+ }
+})
diff --git a/tests/cloud-storage.test.ts b/tests/cloud-storage.test.ts
new file mode 100644
index 00000000..579066e5
--- /dev/null
+++ b/tests/cloud-storage.test.ts
@@ -0,0 +1,187 @@
+import { createStorageCipher } from '../packages/storage/src/credential-cipher.ts'
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { createRequire } from 'node:module'
+import { readFile } from 'node:fs/promises'
+import {
+ validateCloudS3,
+ createCloudS3,
+} from '../packages/storage/src/s3-edge.ts'
+import { createStorageVault } from '../packages/platform/src/storage-vault.ts'
+import type { PlatformD1 } from '../packages/platform/src/index.ts'
+const require = createRequire(import.meta.url)
+const { Miniflare, convertV4MiniflareOptions } = createRequire(
+ require.resolve('wrangler/package.json'),
+)('miniflare')
+const config = {
+ endpoint: 'https://s3.us-east-1.amazonaws.com',
+ region: 'us-east-1',
+ bucket: 'test-bucket',
+ accessKeyId: 'example-access',
+ secretAccessKey: 'example-secret',
+}
+test('One S3 implementation rejects SSRF/redirect destinations and signs bounded upload grants', async () => {
+ for (const endpoint of [
+ 'http://s3.us-east-1.amazonaws.com',
+ 'https://127.0.0.1',
+ 'https://[::1]',
+ 'https://2130706433',
+ 'https://metadata.google.internal',
+ 'https://user:[email protected]',
+ 'https://s3.us-east-1.amazonaws.com:8443',
+ 'https://s3.us-east-1.amazonaws.com/path',
+ 'https://s3.us-east-1.amazonaws.com.evil.test',
+ 'https://user-controlled.test',
+ ])
+ assert.throws(() => validateCloudS3({ ...config, endpoint }))
+ assert.throws(() =>
+ validateCloudS3({ ...config, endpoint: 'https://localhost' }, [
+ 'localhost',
+ ]),
+ )
+ assert.equal(
+ validateCloudS3({
+ ...config,
+ endpoint: 'https://' + 'a'.repeat(32) + '.r2.cloudflarestorage.com',
+ region: 'auto',
+ }),
+ 'https://' + 'a'.repeat(32) + '.r2.cloudflarestorage.com',
+ )
+ assert.equal(
+ validateCloudS3({ ...config, endpoint: 'https://storage.operator.test' }, [
+ 'storage.operator.test',
+ ]),
+ 'https://storage.operator.test',
+ )
+ const s3 = createCloudS3(config)
+ const url = new URL(
+ await s3.signPut('chronoframe/1/originals/image', 42, 'image/jpeg'),
+ )
+ assert.equal(url.hostname, 's3.us-east-1.amazonaws.com')
+ assert.equal(url.searchParams.get('X-Amz-Expires'), '300')
+ assert.equal(url.searchParams.get('X-Amz-Algorithm'), 'AWS4-HMAC-SHA256')
+ assert.equal(url.pathname, '/test-bucket/chronoframe/1/originals/image')
+ assert(!url.href.includes(config.secretAccessKey))
+ await assert.rejects(s3.signPut('../outside', 42, 'image/jpeg'))
+ await assert.rejects(s3.signPut('valid', 101 * 1024 * 1024, 'image/jpeg'))
+ let redirect: string | undefined
+ const transport = createCloudS3(config, {
+ fetch: async (input, init) => {
+ redirect = init?.redirect
+ return new Response(null, {
+ status: 302,
+ headers: { location: 'http://127.0.0.1' },
+ })
+ },
+ })
+ await assert.rejects(transport.head('probe'))
+ assert.equal(redirect, 'manual')
+})
+test('Credential vault authenticates Site/revision, keeps old objects usable and never stores plaintext', async () => {
+ const mf = new Miniflare(
+ convertV4MiniflareOptions({
+ modules: true,
+ compatibilityDate: '2026-10-22',
+ script: 'export default {fetch(){return new Response("test")}}',
+ d1Databases: ['DB'],
+ }),
+ )
+ try {
+ const raw = await mf.getD1Database('DB')
+ const db = raw as PlatformD1
+ for (const sql of [
+ 'CREATE TABLE platform_sites(id INTEGER PRIMARY KEY)',
+ 'CREATE TABLE platform_users(id INTEGER PRIMARY KEY)',
+ 'INSERT INTO platform_sites VALUES(1),(2)',
+ ])
+ await raw.prepare(sql).run()
+ for (const name of [
+ '0005_media_catalog.sql',
+ '0006_cloud_execution.sql',
+ '0007_cloud_storage.sql',
+ ]) {
+ const migration = await readFile(
+ new URL('../packages/platform/migrations/' + name, import.meta.url),
+ 'utf8',
+ )
+ for (const sql of migration
+ .replace(/^--.*$/gm, '')
+ .split(';')
+ .map((s) => s.trim())
+ .filter(Boolean))
+ await raw.prepare(sql).run()
+ }
+ const cipher = await createStorageCipher('platform-secret-'.repeat(4)),
+ vault = createStorageVault(db, 1, cipher)
+ const { accessKeyId, secretAccessKey, ...publicConfig } = config
+ await vault.create(
+ 'storage',
+ publicConfig,
+ { accessKeyId, secretAccessKey },
+ 1,
+ )
+ const stored = await raw
+ .prepare(
+ 'SELECT secret_ciphertext,public_json FROM cloud_storage_versions',
+ )
+ .first()
+ assert(!JSON.stringify(stored).includes(secretAccessKey))
+ assert(!JSON.stringify(stored).includes(accessKeyId))
+ await assert.rejects(
+ cipher.open(
+ stored.secret_ciphertext,
+ JSON.stringify(['cloud-storage', 2, 'storage', 1]),
+ ),
+ )
+ assert.equal(await createStorageVault(db, 2, cipher).get('storage'), null)
+ assert.equal(
+ (await vault.get('storage'))?.credentials.secretAccessKey,
+ secretAccessKey,
+ )
+ const rotated = await Promise.all([
+ vault.rotate(
+ 'storage',
+ 1,
+ { accessKeyId: 'new', secretAccessKey: 'new-secret' },
+ 2,
+ ),
+ vault.rotate(
+ 'storage',
+ 1,
+ { accessKeyId: 'other', secretAccessKey: 'other-secret' },
+ 2,
+ ),
+ ])
+ assert.equal(rotated.filter(Boolean).length, 1)
+ assert.equal((await vault.get('storage'))?.revision, 2)
+ assert.equal(
+ (await vault.get('storage', 1))?.credentials.secretAccessKey,
+ secretAccessKey,
+ )
+ assert(!JSON.stringify(await vault.list()).includes('secret'))
+ const capacity = await Promise.allSettled(
+ Array.from({ length: 20 }, (_, i) =>
+ vault.create(
+ 'capacity-' + i,
+ publicConfig,
+ { accessKeyId, secretAccessKey },
+ 3,
+ ),
+ ),
+ )
+ assert.equal(capacity.filter((r) => r.status === 'fulfilled').length, 19)
+ assert.equal((await vault.list()).length, 20)
+ assert.equal(
+ (
+ await raw
+ .prepare(
+ 'SELECT COUNT(*) AS n FROM cloud_storage_versions WHERE site_id=1',
+ )
+ .first()
+ ).n,
+ 21,
+ )
+ } finally {
+ await mf.dispose()
+ }
+})
diff --git a/tests/s3-worker-runtime.test.ts b/tests/s3-worker-runtime.test.ts
new file mode 100644
index 00000000..6a8f2302
--- /dev/null
+++ b/tests/s3-worker-runtime.test.ts
@@ -0,0 +1,70 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { createRequire } from 'node:module'
+import { execFileSync } from 'node:child_process'
+import { readFile } from 'node:fs/promises'
+const require = createRequire(import.meta.url)
+const wrangler = createRequire(require.resolve('wrangler/package.json'))
+const { Miniflare, convertV4MiniflareOptions } = wrangler('miniflare')
+const { build } = wrangler('esbuild')
+test('S3 browser distribution parses multipart/CORS XML in real workerd without DOMParser or Node loaders', async () => {
+ execFileSync(process.execPath, ['packages/storage/scripts/build-edge.mjs'], {
+ stdio: 'pipe',
+ })
+ const sdk = await readFile('packages/storage/dist/s3-edge.mjs', 'utf8')
+ assert(!/\bDOMParser\b|node:fs/.test(sdk))
+ const bundle = await build({
+ stdin: {
+ resolveDir: process.cwd(),
+ contents: `import {createCloudS3} from '@chronoframe/storage/s3-edge';export default {async fetch(){const s=createCloudS3({endpoint:'https://s3.us-east-1.amazonaws.com',region:'us-east-1',bucket:'test-bucket',accessKeyId:'test-access',secretAccessKey:'test-secret'});const id=await s.multipart('test/file','image/jpeg');const recover=await s.recoverMultipart('test/file');const parts=await s.parts('test/file',id);const cors=await s.cors('https://app.chronoframe.art');const grant=await s.signPart('test/file',id,1,32);await s.abort('test/file',id);return Response.json({id,recover,parts,cors,signed:new URL(grant).searchParams.get('X-Amz-Algorithm')})}}`,
+ },
+ bundle: true,
+ platform: 'browser',
+ format: 'esm',
+ write: false,
+ })
+ const mf = new Miniflare(
+ convertV4MiniflareOptions({
+ modules: true,
+ compatibilityDate: '2026-10-22',
+ script: bundle.outputFiles[0].text,
+ outboundService: async (request: Request) => {
+ const u = new URL(request.url)
+ const xml = (body: string) =>
+ new Response(body, { headers: { 'content-type': 'application/xml' } })
+ if (request.method === 'POST')
+ return xml(
+ '<InitiateMultipartUploadResult><UploadId>test-upload</UploadId></InitiateMultipartUploadResult>',
+ )
+ if (request.method === 'DELETE')
+ return new Response(null, { status: 204 })
+ if (u.searchParams.has('uploads'))
+ return xml(
+ '<ListMultipartUploadsResult><Upload><Key>test/file</Key><UploadId>test-upload</UploadId></Upload></ListMultipartUploadsResult>',
+ )
+ if (u.searchParams.has('uploadId'))
+ return xml(
+ '<ListPartsResult><Part><PartNumber>1</PartNumber><ETag>test-etag</ETag><Size>32</Size></Part></ListPartsResult>',
+ )
+ if (u.searchParams.has('cors'))
+ return xml(
+ '<CORSConfiguration><CORSRule><AllowedOrigin>https://app.chronoframe.art</AllowedOrigin><AllowedMethod>GET</AllowedMethod><AllowedMethod>PUT</AllowedMethod><AllowedMethod>HEAD</AllowedMethod><AllowedHeader>*</AllowedHeader><ExposeHeader>ETag</ExposeHeader></CORSRule></CORSConfiguration>',
+ )
+ return new Response(null, { status: 404 })
+ },
+ }),
+ )
+ try {
+ const response = await mf.dispatchFetch('https://test/')
+ assert.equal(response.status, 200, await response.clone().text())
+ assert.deepEqual(await response.json(), {
+ id: 'test-upload',
+ recover: ['test-upload'],
+ parts: [{ partNumber: 1, etag: 'test-etag', bytes: 32 }],
+ cors: true,
+ signed: 'AWS4-HMAC-SHA256',
+ })
+ } finally {
+ await mf.dispose()
+ }
+})