@aphexcms/cms-core 9.10.0 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/assets.d.ts +62 -7
- package/dist/api/assets.d.ts.map +1 -1
- package/dist/api/assets.js +116 -9
- package/dist/api/client.d.ts +1 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +39 -9
- package/dist/api/limits.d.ts +55 -0
- package/dist/api/limits.d.ts.map +1 -0
- package/dist/api/limits.js +58 -0
- package/dist/api/schemas/assets.d.ts +94 -4
- package/dist/api/schemas/assets.d.ts.map +1 -1
- package/dist/api/schemas/assets.js +82 -5
- package/dist/api/types.d.ts +25 -0
- package/dist/api/types.d.ts.map +1 -1
- package/dist/api/upload-timeout.d.ts +17 -0
- package/dist/api/upload-timeout.d.ts.map +1 -0
- package/dist/api/upload-timeout.js +35 -0
- package/dist/api/upload.d.ts +42 -0
- package/dist/api/upload.d.ts.map +1 -0
- package/dist/api/upload.js +144 -0
- package/dist/components/AdminApp.svelte +130 -26
- package/dist/components/AdminApp.svelte.d.ts.map +1 -1
- package/dist/components/admin/AssetBrowserModal.svelte +4 -3
- package/dist/components/admin/AssetBrowserModal.svelte.d.ts +2 -1
- package/dist/components/admin/AssetBrowserModal.svelte.d.ts.map +1 -1
- package/dist/components/admin/MediaBrowser.svelte +1859 -281
- package/dist/components/admin/MediaBrowser.svelte.d.ts +21 -1
- package/dist/components/admin/MediaBrowser.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/ArrayField.svelte +21 -10
- package/dist/components/admin/fields/ArrayField.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/FileField.svelte +9 -2
- package/dist/components/admin/fields/FileField.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/ImageField.svelte +47 -12
- package/dist/components/admin/fields/ImageField.svelte.d.ts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +14 -0
- package/dist/db/interfaces/asset.d.ts +114 -5
- package/dist/db/interfaces/asset.d.ts.map +1 -1
- package/dist/db/interfaces/document.d.ts +98 -3
- package/dist/db/interfaces/document.d.ts.map +1 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +2 -1
- package/dist/image/Image.svelte +72 -0
- package/dist/image/Image.svelte.d.ts +40 -0
- package/dist/image/Image.svelte.d.ts.map +1 -0
- package/dist/image/index.d.ts +3 -0
- package/dist/image/index.d.ts.map +1 -0
- package/dist/image/index.js +11 -0
- package/dist/image/types.d.ts +28 -0
- package/dist/image/types.d.ts.map +1 -0
- package/dist/image/types.js +1 -0
- package/dist/images/generate.d.ts +55 -0
- package/dist/images/generate.d.ts.map +1 -0
- package/dist/images/generate.js +243 -0
- package/dist/images/index.d.ts +2 -0
- package/dist/images/index.d.ts.map +1 -0
- package/dist/images/index.js +7 -0
- package/dist/images/variants.d.ts +82 -0
- package/dist/images/variants.d.ts.map +1 -0
- package/dist/images/variants.js +124 -0
- package/dist/jobs/asset-reference-jobs.d.ts +49 -0
- package/dist/jobs/asset-reference-jobs.d.ts.map +1 -0
- package/dist/jobs/asset-reference-jobs.js +74 -0
- package/dist/jobs/run-batch.d.ts.map +1 -1
- package/dist/jobs/run-batch.js +5 -0
- package/dist/lib/api/assets.d.ts +62 -7
- package/dist/lib/api/assets.d.ts.map +1 -1
- package/dist/lib/api/assets.js +116 -9
- package/dist/lib/api/assets.js.map +1 -1
- package/dist/lib/api/client.d.ts +1 -1
- package/dist/lib/api/client.d.ts.map +1 -1
- package/dist/lib/api/client.js +39 -9
- package/dist/lib/api/client.js.map +1 -1
- package/dist/lib/api/limits.d.ts +55 -0
- package/dist/lib/api/limits.d.ts.map +1 -0
- package/dist/lib/api/limits.js +59 -0
- package/dist/lib/api/limits.js.map +1 -0
- package/dist/lib/api/schemas/assets.d.ts +94 -4
- package/dist/lib/api/schemas/assets.d.ts.map +1 -1
- package/dist/lib/api/schemas/assets.js +82 -5
- package/dist/lib/api/schemas/assets.js.map +1 -1
- package/dist/lib/api/types.d.ts +25 -0
- package/dist/lib/api/types.d.ts.map +1 -1
- package/dist/lib/api/upload-timeout.d.ts +17 -0
- package/dist/lib/api/upload-timeout.d.ts.map +1 -0
- package/dist/lib/api/upload-timeout.js +36 -0
- package/dist/lib/api/upload-timeout.js.map +1 -0
- package/dist/lib/api/upload.d.ts +42 -0
- package/dist/lib/api/upload.d.ts.map +1 -0
- package/dist/lib/api/upload.js +145 -0
- package/dist/lib/api/upload.js.map +1 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +14 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/db/interfaces/asset.d.ts +114 -5
- package/dist/lib/db/interfaces/asset.d.ts.map +1 -1
- package/dist/lib/db/interfaces/document.d.ts +98 -3
- package/dist/lib/db/interfaces/document.d.ts.map +1 -1
- package/dist/lib/hooks.d.ts.map +1 -1
- package/dist/lib/hooks.js +2 -1
- package/dist/lib/hooks.js.map +1 -1
- package/dist/lib/image/index.d.ts +3 -0
- package/dist/lib/image/index.d.ts.map +1 -0
- package/dist/lib/image/index.js +12 -0
- package/dist/lib/image/index.js.map +1 -0
- package/dist/lib/image/types.d.ts +28 -0
- package/dist/lib/image/types.d.ts.map +1 -0
- package/dist/lib/image/types.js +2 -0
- package/dist/lib/image/types.js.map +1 -0
- package/dist/lib/images/generate.d.ts +55 -0
- package/dist/lib/images/generate.d.ts.map +1 -0
- package/dist/lib/images/generate.js +244 -0
- package/dist/lib/images/generate.js.map +1 -0
- package/dist/lib/images/index.d.ts +2 -0
- package/dist/lib/images/index.d.ts.map +1 -0
- package/dist/lib/images/index.js +8 -0
- package/dist/lib/images/index.js.map +1 -0
- package/dist/lib/images/variants.d.ts +82 -0
- package/dist/lib/images/variants.d.ts.map +1 -0
- package/dist/lib/images/variants.js +125 -0
- package/dist/lib/images/variants.js.map +1 -0
- package/dist/lib/jobs/asset-reference-jobs.d.ts +49 -0
- package/dist/lib/jobs/asset-reference-jobs.d.ts.map +1 -0
- package/dist/lib/jobs/asset-reference-jobs.js +75 -0
- package/dist/lib/jobs/asset-reference-jobs.js.map +1 -0
- package/dist/lib/jobs/run-batch.d.ts.map +1 -1
- package/dist/lib/jobs/run-batch.js +5 -0
- package/dist/lib/jobs/run-batch.js.map +1 -1
- package/dist/lib/local-api/collection-api.d.ts +19 -2
- package/dist/lib/local-api/collection-api.d.ts.map +1 -1
- package/dist/lib/local-api/collection-api.js +65 -17
- package/dist/lib/local-api/collection-api.js.map +1 -1
- package/dist/lib/preview/assets.d.ts +19 -0
- package/dist/lib/preview/assets.d.ts.map +1 -1
- package/dist/lib/preview/assets.js +6 -0
- package/dist/lib/preview/assets.js.map +1 -1
- package/dist/lib/routes/assets-cdn.d.ts +19 -0
- package/dist/lib/routes/assets-cdn.d.ts.map +1 -1
- package/dist/lib/routes/assets-cdn.js +295 -17
- package/dist/lib/routes/assets-cdn.js.map +1 -1
- package/dist/lib/server/api/index.d.ts.map +1 -1
- package/dist/lib/server/api/index.js +87 -8
- package/dist/lib/server/api/index.js.map +1 -1
- package/dist/lib/server/api/routes/assets-bulk.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-bulk.js +36 -7
- package/dist/lib/server/api/routes/assets-bulk.js.map +1 -1
- package/dist/lib/server/api/routes/assets-by-id.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-by-id.js +32 -15
- package/dist/lib/server/api/routes/assets-by-id.js.map +1 -1
- package/dist/lib/server/api/routes/assets-direct-upload.d.ts +4 -0
- package/dist/lib/server/api/routes/assets-direct-upload.d.ts.map +1 -0
- package/dist/lib/server/api/routes/assets-direct-upload.js +164 -0
- package/dist/lib/server/api/routes/assets-direct-upload.js.map +1 -0
- package/dist/lib/server/api/routes/assets-poster.d.ts +4 -0
- package/dist/lib/server/api/routes/assets-poster.d.ts.map +1 -0
- package/dist/lib/server/api/routes/assets-poster.js +120 -0
- package/dist/lib/server/api/routes/assets-poster.js.map +1 -0
- package/dist/lib/server/api/routes/assets-references.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-references.js +64 -5
- package/dist/lib/server/api/routes/assets-references.js.map +1 -1
- package/dist/lib/server/api/routes/assets.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets.js +116 -12
- package/dist/lib/server/api/routes/assets.js.map +1 -1
- package/dist/lib/server/api/routes/clear-asset-references.d.ts +18 -0
- package/dist/lib/server/api/routes/clear-asset-references.d.ts.map +1 -0
- package/dist/lib/server/api/routes/clear-asset-references.js +32 -0
- package/dist/lib/server/api/routes/clear-asset-references.js.map +1 -0
- package/dist/lib/server/api/routes/documents.d.ts.map +1 -1
- package/dist/lib/server/api/routes/documents.js +27 -1
- package/dist/lib/server/api/routes/documents.js.map +1 -1
- package/dist/lib/server/index.d.ts +2 -0
- package/dist/lib/server/index.d.ts.map +1 -1
- package/dist/lib/server/index.js +4 -0
- package/dist/lib/server/index.js.map +1 -1
- package/dist/lib/services/asset-references-service.d.ts +93 -0
- package/dist/lib/services/asset-references-service.d.ts.map +1 -0
- package/dist/lib/services/asset-references-service.js +198 -0
- package/dist/lib/services/asset-references-service.js.map +1 -0
- package/dist/lib/services/asset-service.d.ts +115 -14
- package/dist/lib/services/asset-service.d.ts.map +1 -1
- package/dist/lib/services/asset-service.js +312 -31
- package/dist/lib/services/asset-service.js.map +1 -1
- package/dist/lib/services/references-service.d.ts +19 -15
- package/dist/lib/services/references-service.d.ts.map +1 -1
- package/dist/lib/services/references-service.js +59 -31
- package/dist/lib/services/references-service.js.map +1 -1
- package/dist/lib/services/version-service.d.ts +10 -1
- package/dist/lib/services/version-service.d.ts.map +1 -1
- package/dist/lib/services/version-service.js +26 -4
- package/dist/lib/services/version-service.js.map +1 -1
- package/dist/lib/storage/adapters/local-storage-adapter.d.ts +38 -0
- package/dist/lib/storage/adapters/local-storage-adapter.d.ts.map +1 -1
- package/dist/lib/storage/adapters/local-storage-adapter.js +86 -19
- package/dist/lib/storage/adapters/local-storage-adapter.js.map +1 -1
- package/dist/lib/storage/index.d.ts +1 -0
- package/dist/lib/storage/index.d.ts.map +1 -1
- package/dist/lib/storage/index.js +2 -0
- package/dist/lib/storage/index.js.map +1 -1
- package/dist/lib/storage/interfaces/storage.d.ts +135 -5
- package/dist/lib/storage/interfaces/storage.d.ts.map +1 -1
- package/dist/lib/storage/keys.d.ts +96 -0
- package/dist/lib/storage/keys.d.ts.map +1 -0
- package/dist/lib/storage/keys.js +160 -0
- package/dist/lib/storage/keys.js.map +1 -0
- package/dist/lib/types/asset.d.ts +92 -12
- package/dist/lib/types/asset.d.ts.map +1 -1
- package/dist/lib/types/capabilities.d.ts +11 -0
- package/dist/lib/types/capabilities.d.ts.map +1 -1
- package/dist/lib/types/capabilities.js +20 -6
- package/dist/lib/types/capabilities.js.map +1 -1
- package/dist/lib/types/config.d.ts +127 -0
- package/dist/lib/types/config.d.ts.map +1 -1
- package/dist/lib/utils/asset-reference-walk.d.ts +44 -0
- package/dist/lib/utils/asset-reference-walk.d.ts.map +1 -0
- package/dist/lib/utils/asset-reference-walk.js +144 -0
- package/dist/lib/utils/asset-reference-walk.js.map +1 -0
- package/dist/lib/utils/image-url.d.ts +61 -26
- package/dist/lib/utils/image-url.d.ts.map +1 -1
- package/dist/lib/utils/image-url.js +143 -26
- package/dist/lib/utils/image-url.js.map +1 -1
- package/dist/lib/utils/video-metadata.d.ts +39 -0
- package/dist/lib/utils/video-metadata.d.ts.map +1 -0
- package/dist/lib/utils/video-metadata.js +161 -0
- package/dist/lib/utils/video-metadata.js.map +1 -0
- package/dist/local-api/collection-api.d.ts +19 -2
- package/dist/local-api/collection-api.d.ts.map +1 -1
- package/dist/local-api/collection-api.js +65 -17
- package/dist/preview/assets.d.ts +19 -0
- package/dist/preview/assets.d.ts.map +1 -1
- package/dist/preview/assets.js +6 -0
- package/dist/routes/assets-cdn.d.ts +19 -0
- package/dist/routes/assets-cdn.d.ts.map +1 -1
- package/dist/routes/assets-cdn.js +295 -17
- package/dist/server/api/index.d.ts.map +1 -1
- package/dist/server/api/index.js +87 -8
- package/dist/server/api/routes/assets-bulk.d.ts.map +1 -1
- package/dist/server/api/routes/assets-bulk.js +36 -7
- package/dist/server/api/routes/assets-by-id.d.ts.map +1 -1
- package/dist/server/api/routes/assets-by-id.js +32 -15
- package/dist/server/api/routes/assets-direct-upload.d.ts +4 -0
- package/dist/server/api/routes/assets-direct-upload.d.ts.map +1 -0
- package/dist/server/api/routes/assets-direct-upload.js +163 -0
- package/dist/server/api/routes/assets-poster.d.ts +4 -0
- package/dist/server/api/routes/assets-poster.d.ts.map +1 -0
- package/dist/server/api/routes/assets-poster.js +119 -0
- package/dist/server/api/routes/assets-references.d.ts.map +1 -1
- package/dist/server/api/routes/assets-references.js +64 -5
- package/dist/server/api/routes/assets.d.ts.map +1 -1
- package/dist/server/api/routes/assets.js +116 -12
- package/dist/server/api/routes/clear-asset-references.d.ts +18 -0
- package/dist/server/api/routes/clear-asset-references.d.ts.map +1 -0
- package/dist/server/api/routes/clear-asset-references.js +31 -0
- package/dist/server/api/routes/documents.d.ts.map +1 -1
- package/dist/server/api/routes/documents.js +27 -1
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -0
- package/dist/services/asset-references-service.d.ts +93 -0
- package/dist/services/asset-references-service.d.ts.map +1 -0
- package/dist/services/asset-references-service.js +197 -0
- package/dist/services/asset-service.d.ts +115 -14
- package/dist/services/asset-service.d.ts.map +1 -1
- package/dist/services/asset-service.js +312 -31
- package/dist/services/references-service.d.ts +19 -15
- package/dist/services/references-service.d.ts.map +1 -1
- package/dist/services/references-service.js +59 -31
- package/dist/services/version-service.d.ts +10 -1
- package/dist/services/version-service.d.ts.map +1 -1
- package/dist/services/version-service.js +26 -4
- package/dist/storage/adapters/local-storage-adapter.d.ts +38 -0
- package/dist/storage/adapters/local-storage-adapter.d.ts.map +1 -1
- package/dist/storage/adapters/local-storage-adapter.js +86 -19
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +2 -0
- package/dist/storage/interfaces/storage.d.ts +135 -5
- package/dist/storage/interfaces/storage.d.ts.map +1 -1
- package/dist/storage/keys.d.ts +96 -0
- package/dist/storage/keys.d.ts.map +1 -0
- package/dist/storage/keys.js +159 -0
- package/dist/types/asset.d.ts +92 -12
- package/dist/types/asset.d.ts.map +1 -1
- package/dist/types/capabilities.d.ts +11 -0
- package/dist/types/capabilities.d.ts.map +1 -1
- package/dist/types/capabilities.js +20 -6
- package/dist/types/config.d.ts +127 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/utils/asset-reference-walk.d.ts +44 -0
- package/dist/utils/asset-reference-walk.d.ts.map +1 -0
- package/dist/utils/asset-reference-walk.js +143 -0
- package/dist/utils/image-url.d.ts +61 -26
- package/dist/utils/image-url.d.ts.map +1 -1
- package/dist/utils/image-url.js +143 -26
- package/dist/utils/video-metadata.d.ts +39 -0
- package/dist/utils/video-metadata.d.ts.map +1 -0
- package/dist/utils/video-metadata.js +160 -0
- package/package.json +6 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// local-api/collection-api.ts
|
|
2
2
|
//
|
|
3
3
|
// Collection API - provides type-safe CRUD operations for a single collection
|
|
4
|
+
import { AssetReferencesService } from '../services/asset-references-service.js';
|
|
4
5
|
import { singletonId } from '../schema-utils/singleton.js';
|
|
5
6
|
import { resolveSearchPaths, buildSearchText } from '../schema-utils/utils.js';
|
|
6
7
|
import { validateDocumentData } from '../field-validation/utils.js';
|
|
@@ -151,13 +152,35 @@ export class CollectionAPI {
|
|
|
151
152
|
}
|
|
152
153
|
/**
|
|
153
154
|
* Refresh the back-reference index for this doc using the freshly-saved
|
|
154
|
-
* draftData.
|
|
155
|
-
*
|
|
155
|
+
* draftData.
|
|
156
|
+
*
|
|
157
|
+
* Takes the adapter to write through — always a `withTransaction` handle on
|
|
158
|
+
* the write paths, so the index rows commit or roll back with the document
|
|
159
|
+
* itself. It throws now, which is the point: this index backs the publish and
|
|
160
|
+
* unpublish guards, and a guard that quietly weakens when its write failed is
|
|
161
|
+
* more dangerous than no guard.
|
|
156
162
|
*/
|
|
157
|
-
async syncReferences(organizationId, documentId, data) {
|
|
163
|
+
async syncReferences(db, organizationId, documentId, data) {
|
|
158
164
|
if (!this.referencesService)
|
|
159
165
|
return;
|
|
160
|
-
await this.referencesService.syncReferencesFor(organizationId, documentId, data, this._schema, this.schemaRegistry ?? []);
|
|
166
|
+
await this.referencesService.syncReferencesFor(db, organizationId, documentId, data, this._schema, this.schemaRegistry ?? []);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Refresh the asset-reference index for this doc — which assets its draft and
|
|
170
|
+
* published data use. Same contract as {@link syncReferences}: written through
|
|
171
|
+
* the caller's transaction handle, and throws.
|
|
172
|
+
*
|
|
173
|
+
* Built here rather than injected: it needs nothing but the adapter this class
|
|
174
|
+
* already holds, and threading a tenth constructor argument through every call
|
|
175
|
+
* site would be the only other option.
|
|
176
|
+
*/
|
|
177
|
+
get assetReferencesService() {
|
|
178
|
+
this._assetReferencesService ??= new AssetReferencesService(this.databaseAdapter);
|
|
179
|
+
return this._assetReferencesService;
|
|
180
|
+
}
|
|
181
|
+
_assetReferencesService;
|
|
182
|
+
async syncAssetReferences(db, organizationId, documentId, draftData, publishedData) {
|
|
183
|
+
await this.assetReferencesService.syncAssetReferencesFor(db, organizationId, documentId, this.collectionName, draftData, publishedData);
|
|
161
184
|
}
|
|
162
185
|
/**
|
|
163
186
|
* Recompute the document's full-text search index from freshly-saved
|
|
@@ -478,11 +501,18 @@ export class CollectionAPI {
|
|
|
478
501
|
if (published)
|
|
479
502
|
await emitDocumentPublished(tx, context.organizationId, published);
|
|
480
503
|
}
|
|
504
|
+
// Both reference indexes commit with the document. They used to run
|
|
505
|
+
// after the transaction, best-effort, which meant a document could
|
|
506
|
+
// exist with no record of what it points at — and the "Unused" asset
|
|
507
|
+
// filter and the publish guards both read those records as fact.
|
|
508
|
+
await this.syncReferences(tx, context.organizationId, document.id, validationResult.normalizedData);
|
|
509
|
+
// Created and published in one call, so both planes hold this same data.
|
|
510
|
+
await this.syncAssetReferences(tx, context.organizationId, document.id, validationResult.normalizedData, published ? validationResult.normalizedData : null);
|
|
481
511
|
return { document, published };
|
|
482
512
|
});
|
|
483
|
-
// Post-commit
|
|
513
|
+
// Post-commit: search text is genuinely best-effort — a missed row costs a
|
|
514
|
+
// search result, never a deletion decision — and retention is one pass
|
|
484
515
|
// covering both the draft and publish snapshots written inside the tx.
|
|
485
|
-
await this.syncReferences(context.organizationId, document.id, validationResult.normalizedData);
|
|
486
516
|
await this.syncSearchText(context.organizationId, document.id, validationResult.normalizedData);
|
|
487
517
|
if (versionService) {
|
|
488
518
|
await versionService.enforceRetentionFor(this.databaseAdapter, context.organizationId, document.id);
|
|
@@ -506,15 +536,22 @@ export class CollectionAPI {
|
|
|
506
536
|
validation: validationResult
|
|
507
537
|
};
|
|
508
538
|
}
|
|
509
|
-
// Draft-only path.
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
539
|
+
// Draft-only path. Wrapped in a transaction it did not previously need, so
|
|
540
|
+
// the row and its reference index arrive together — the alternative is a
|
|
541
|
+
// document that exists while the index insists it references nothing.
|
|
542
|
+
const document = await this.databaseAdapter.withTransaction(async (tx) => {
|
|
543
|
+
const document = await tx.createDocument({
|
|
544
|
+
organizationId: context.organizationId,
|
|
545
|
+
type: this.collectionName,
|
|
546
|
+
draftData: validationResult.normalizedData,
|
|
547
|
+
createdBy: context.user?.id,
|
|
548
|
+
id: options?.id
|
|
549
|
+
});
|
|
550
|
+
await this.syncReferences(tx, context.organizationId, document.id, validationResult.normalizedData);
|
|
551
|
+
// Freshly created — draft only, nothing published yet.
|
|
552
|
+
await this.syncAssetReferences(tx, context.organizationId, document.id, validationResult.normalizedData, null);
|
|
553
|
+
return document;
|
|
516
554
|
});
|
|
517
|
-
await this.syncReferences(context.organizationId, document.id, validationResult.normalizedData);
|
|
518
555
|
await this.syncSearchText(context.organizationId, document.id, validationResult.normalizedData);
|
|
519
556
|
// Create initial draft version
|
|
520
557
|
if (versionService) {
|
|
@@ -585,13 +622,24 @@ export class CollectionAPI {
|
|
|
585
622
|
// RevisionConflictError propagates un-swallowed — a stale caller (a second
|
|
586
623
|
// tab, an agent that read the doc seconds ago) gets a surfaced conflict
|
|
587
624
|
// instead of silently overwriting a change made after it read the document.
|
|
625
|
+
// Both indexes are refreshed inside whichever transaction does the write, so
|
|
626
|
+
// a saved draft can never disagree with the record of what it references.
|
|
627
|
+
const indexInTx = async (tx, saved) => {
|
|
628
|
+
await this.syncReferences(tx, context.organizationId, id, validationResult.normalizedData);
|
|
629
|
+
// The published plane is whatever is currently live — untouched by a draft save.
|
|
630
|
+
await this.syncAssetReferences(tx, context.organizationId, id, validationResult.normalizedData, saved.publishedData ?? null);
|
|
631
|
+
};
|
|
588
632
|
const document = this.versionService && !options?.skipVersioning
|
|
589
|
-
? await this.versionService.saveWithVersion(this.databaseAdapter, context.organizationId, id, validationResult.normalizedData, context.user?.id, options?.expectedRevision)
|
|
590
|
-
: await this.databaseAdapter.
|
|
633
|
+
? await this.versionService.saveWithVersion(this.databaseAdapter, context.organizationId, id, validationResult.normalizedData, context.user?.id, options?.expectedRevision, indexInTx)
|
|
634
|
+
: await this.databaseAdapter.withTransaction(async (tx) => {
|
|
635
|
+
const saved = await tx.updateDocDraft(context.organizationId, id, validationResult.normalizedData, context.user?.id, options?.expectedRevision);
|
|
636
|
+
if (saved)
|
|
637
|
+
await indexInTx(tx, saved);
|
|
638
|
+
return saved;
|
|
639
|
+
});
|
|
591
640
|
if (!document) {
|
|
592
641
|
return null;
|
|
593
642
|
}
|
|
594
|
-
await this.syncReferences(context.organizationId, id, validationResult.normalizedData);
|
|
595
643
|
await this.syncSearchText(context.organizationId, id, validationResult.normalizedData);
|
|
596
644
|
if (options?.publish) {
|
|
597
645
|
await this.permissions.canPublish(context, this.collectionName, document);
|
package/dist/preview/assets.d.ts
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
export interface ResolvedAsset {
|
|
3
3
|
url: string;
|
|
4
4
|
alt?: string;
|
|
5
|
+
/**
|
|
6
|
+
* Intrinsic dimensions of the original, so `<Image>` can set width/height and
|
|
7
|
+
* reserve layout space before the image loads.
|
|
8
|
+
*/
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Pre-built `srcset` of generated widths.
|
|
13
|
+
*
|
|
14
|
+
* Built here, on the server, rather than in the component: constructing a
|
|
15
|
+
* variant URL needs the width ladder and the config hash, and the choice is
|
|
16
|
+
* between shipping both to the browser or shipping the finished string. The
|
|
17
|
+
* string is smaller, keeps the hashing in one place, and means the rule for
|
|
18
|
+
* how a variant is addressed lives entirely server-side.
|
|
19
|
+
*
|
|
20
|
+
* Absent when the pipeline is disabled or the asset isn't an image, in which
|
|
21
|
+
* case `<Image>` renders a plain `src` and behaves exactly as before.
|
|
22
|
+
*/
|
|
23
|
+
srcset?: string;
|
|
5
24
|
}
|
|
6
25
|
/**
|
|
7
26
|
* Collect every asset `_ref` reachable in a value. Image and file fields, and
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../../src/lib/preview/assets.ts"],"names":[],"mappings":"AAYA,0CAA0C;AAC1C,MAAM,WAAW,aAAa;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../../src/lib/preview/assets.ts"],"names":[],"mappings":"AAYA,0CAA0C;AAC1C,MAAM,WAAW,aAAa;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,GAAE,GAAG,CAAC,MAAM,CAAa,GAAG,GAAG,CAAC,MAAM,CAAC,CAW1F;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,GAC1C,IAAI,CA4BN"}
|
package/dist/preview/assets.js
CHANGED
|
@@ -52,6 +52,12 @@ export function injectAssetData(value, resolved) {
|
|
|
52
52
|
asset.url = hit.url;
|
|
53
53
|
if (hit.alt != null)
|
|
54
54
|
asset.alt = hit.alt;
|
|
55
|
+
if (hit.width != null)
|
|
56
|
+
asset.width = hit.width;
|
|
57
|
+
if (hit.height != null)
|
|
58
|
+
asset.height = hit.height;
|
|
59
|
+
if (hit.srcset)
|
|
60
|
+
asset.srcset = hit.srcset;
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
for (const key in obj)
|
|
@@ -1,3 +1,22 @@
|
|
|
1
1
|
import type { RequestHandler } from '@sveltejs/kit';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a single byte range against a known object size.
|
|
4
|
+
*
|
|
5
|
+
* Returns `null` when there is nothing to honour — no header, a form we don't
|
|
6
|
+
* serve, or an unknown size — in which case the caller answers `200` with the
|
|
7
|
+
* whole body. That is a legal response to any `Range` request, which is what
|
|
8
|
+
* makes ignoring multipart ranges (`bytes=0-99,200-299`) acceptable: they are
|
|
9
|
+
* fiddly to emit, essentially nothing sends them, and a full body is correct.
|
|
10
|
+
*
|
|
11
|
+
* `'unsatisfiable'` is different from `null`: the range is well-formed but lies
|
|
12
|
+
* outside the object, which must be answered `416`, not `200`. A client that
|
|
13
|
+
* seeks past the end otherwise receives a full file it did not ask for.
|
|
14
|
+
*
|
|
15
|
+
* Both bounds in the result are **inclusive**, as in the header itself.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseByteRange(header: string | null, size: number | null): {
|
|
18
|
+
start: number;
|
|
19
|
+
end: number;
|
|
20
|
+
} | 'unsatisfiable' | null;
|
|
2
21
|
export declare const GET: RequestHandler;
|
|
3
22
|
//# sourceMappingURL=assets-cdn.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assets-cdn.d.ts","sourceRoot":"","sources":["../../src/lib/routes/assets-cdn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"assets-cdn.d.ts","sourceRoot":"","sources":["../../src/lib/routes/assets-cdn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAyBpD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,IAAI,EAAE,MAAM,GAAG,IAAI,GACjB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,eAAe,GAAG,IAAI,CA8BzD;AAkBD,eAAO,MAAM,GAAG,EAAE,cA+XjB,CAAC"}
|
|
@@ -1,4 +1,77 @@
|
|
|
1
1
|
import { cmsLogger } from '../utils/logger.js';
|
|
2
|
+
import { parseVariantFilename, VARIANT_FORMAT, POSTER_FILENAME, buildPosterKey } from '../storage/keys.js';
|
|
3
|
+
import { configHashFor, pickVariant, resolveImageConfig } from '../images/variants.js';
|
|
4
|
+
import { generateVariant } from '../images/generate.js';
|
|
5
|
+
/**
|
|
6
|
+
* HTTP headers are ByteString-restricted, so a raw non-ASCII character in a
|
|
7
|
+
* filename throws when the Response is constructed. Callers pair this with a
|
|
8
|
+
* `filename*=UTF-8''` parameter for clients that understand it.
|
|
9
|
+
*/
|
|
10
|
+
function asciiFilename(name) {
|
|
11
|
+
return name.replace(/[^\x20-\x7E]/g, '_').replace(/["\\]/g, '');
|
|
12
|
+
}
|
|
13
|
+
function stripExtension(name) {
|
|
14
|
+
const lastDot = name.lastIndexOf('.');
|
|
15
|
+
return lastDot > 0 ? name.slice(0, lastDot) : name;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parse a single byte range against a known object size.
|
|
19
|
+
*
|
|
20
|
+
* Returns `null` when there is nothing to honour — no header, a form we don't
|
|
21
|
+
* serve, or an unknown size — in which case the caller answers `200` with the
|
|
22
|
+
* whole body. That is a legal response to any `Range` request, which is what
|
|
23
|
+
* makes ignoring multipart ranges (`bytes=0-99,200-299`) acceptable: they are
|
|
24
|
+
* fiddly to emit, essentially nothing sends them, and a full body is correct.
|
|
25
|
+
*
|
|
26
|
+
* `'unsatisfiable'` is different from `null`: the range is well-formed but lies
|
|
27
|
+
* outside the object, which must be answered `416`, not `200`. A client that
|
|
28
|
+
* seeks past the end otherwise receives a full file it did not ask for.
|
|
29
|
+
*
|
|
30
|
+
* Both bounds in the result are **inclusive**, as in the header itself.
|
|
31
|
+
*/
|
|
32
|
+
export function parseByteRange(header, size) {
|
|
33
|
+
if (!header || size == null || size <= 0)
|
|
34
|
+
return null;
|
|
35
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
36
|
+
if (!match)
|
|
37
|
+
return null;
|
|
38
|
+
const [, rawStart, rawEnd] = match;
|
|
39
|
+
if (rawStart === '' && rawEnd === '')
|
|
40
|
+
return null;
|
|
41
|
+
// `bytes=-500` is a *suffix* range: the last 500 bytes, not "from 500 on".
|
|
42
|
+
// Reading it as a start offset is the classic way to serve the wrong half of
|
|
43
|
+
// a file to a player seeking near the end.
|
|
44
|
+
if (rawStart === '') {
|
|
45
|
+
const suffixLength = Number(rawEnd);
|
|
46
|
+
if (!Number.isFinite(suffixLength) || suffixLength <= 0)
|
|
47
|
+
return 'unsatisfiable';
|
|
48
|
+
return { start: Math.max(0, size - suffixLength), end: size - 1 };
|
|
49
|
+
}
|
|
50
|
+
const start = Number(rawStart);
|
|
51
|
+
if (!Number.isFinite(start) || start >= size)
|
|
52
|
+
return 'unsatisfiable';
|
|
53
|
+
// An open-ended `bytes=500-` runs to the last byte, and an end past the object
|
|
54
|
+
// is clamped rather than rejected — RFC 9110 treats an over-long end as the
|
|
55
|
+
// whole remainder, and players routinely ask for more than exists.
|
|
56
|
+
const requestedEnd = rawEnd === '' ? size - 1 : Number(rawEnd);
|
|
57
|
+
if (!Number.isFinite(requestedEnd))
|
|
58
|
+
return 'unsatisfiable';
|
|
59
|
+
const end = Math.min(requestedEnd, size - 1);
|
|
60
|
+
if (end < start)
|
|
61
|
+
return 'unsatisfiable';
|
|
62
|
+
return { start, end };
|
|
63
|
+
}
|
|
64
|
+
function toArrayBuffer(buffer) {
|
|
65
|
+
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Lifetime of a signed URL when `signedDownloads.expiresIn` isn't set.
|
|
69
|
+
*
|
|
70
|
+
* Long enough to start and finish a large download, short enough that a leaked
|
|
71
|
+
* URL stops working quickly — the redirect is the one path where the file is
|
|
72
|
+
* reachable without passing back through this route's access checks.
|
|
73
|
+
*/
|
|
74
|
+
const DEFAULT_SIGNED_URL_TTL_SECONDS = 900;
|
|
2
75
|
export const GET = async ({ params, locals, setHeaders, request }) => {
|
|
3
76
|
try {
|
|
4
77
|
const { assetService, databaseAdapter, storageAdapter, cmsEngine, config } = locals.aphexCMS;
|
|
@@ -90,19 +163,217 @@ export const GET = async ({ params, locals, setHeaders, request }) => {
|
|
|
90
163
|
return new Response('Forbidden', { status: 403 });
|
|
91
164
|
}
|
|
92
165
|
}
|
|
93
|
-
//
|
|
94
|
-
|
|
166
|
+
// --- Serving ---------------------------------------------------------
|
|
167
|
+
//
|
|
168
|
+
// Everything above decided whether this caller may read the file. Only
|
|
169
|
+
// now do we fetch it, and by default we fetch it *through* the app rather
|
|
170
|
+
// than redirecting to the bucket.
|
|
171
|
+
//
|
|
172
|
+
// This route used to 302 straight to `asset.url` whenever it looked like
|
|
173
|
+
// an absolute URL — which meant the privacy checks above decided nothing
|
|
174
|
+
// for S3/R2-backed assets (anyone with the id got the public URL) and
|
|
175
|
+
// broke outright against a private bucket, where the redirect target
|
|
176
|
+
// isn't readable. Proxying is what makes those checks real.
|
|
177
|
+
if (!storageAdapter) {
|
|
178
|
+
cmsLogger.error('[Asset CDN]', 'No storage adapter configured');
|
|
179
|
+
return new Response('No storage adapter configured', { status: 500 });
|
|
180
|
+
}
|
|
181
|
+
// Opt out per-file: large downloads shouldn't tie up a server process, so
|
|
182
|
+
// they can be handed to the bucket as a short-lived signed URL. The
|
|
183
|
+
// access checks have already passed, so this only ever signs a request
|
|
184
|
+
// that was allowed. Falls through to the proxy when the adapter can't
|
|
185
|
+
// sign, since serving the file beats refusing to.
|
|
186
|
+
const signedDownloads = config.signedDownloads;
|
|
187
|
+
if (signedDownloads && storageAdapter.getSignedUrl) {
|
|
188
|
+
let useSigned = false;
|
|
189
|
+
try {
|
|
190
|
+
useSigned = await signedDownloads.shouldUseSignedURL(asset);
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
cmsLogger.warn('[Asset CDN]', 'shouldUseSignedURL threw; proxying instead:', err);
|
|
194
|
+
}
|
|
195
|
+
if (useSigned) {
|
|
196
|
+
const signedUrl = await storageAdapter.getSignedUrl(asset.path, signedDownloads.expiresIn ?? DEFAULT_SIGNED_URL_TTL_SECONDS);
|
|
197
|
+
return new Response(null, {
|
|
198
|
+
status: 302,
|
|
199
|
+
headers: {
|
|
200
|
+
Location: signedUrl,
|
|
201
|
+
// The URL expires, so it must never be cached by a shared proxy.
|
|
202
|
+
'Cache-Control': 'private, no-store'
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Stream when the adapter can, buffer when it can't.
|
|
208
|
+
//
|
|
209
|
+
// Not just an optimisation: a serverless host caps the *response body* it
|
|
210
|
+
// will return (Vercel Functions: 4.5 MB, then a 413), and a streamed
|
|
211
|
+
// response is exempt. Buffering an ordinary 5 MB photo there is a hard
|
|
212
|
+
// failure, not a slow request — so the streaming path is the correct one
|
|
213
|
+
// wherever it's available, and buffering is the fallback.
|
|
214
|
+
// --- Variants --------------------------------------------------------
|
|
215
|
+
//
|
|
216
|
+
// Reached only after every check above has passed, which is the entire
|
|
217
|
+
// reason derivatives live on this route instead of their own. A separate
|
|
218
|
+
// image endpoint would have to repeat the privacy and organization logic,
|
|
219
|
+
// and if it ever drifted the result would be a public derivative of a
|
|
220
|
+
// private original, cached at the edge under a guessable key.
|
|
221
|
+
// A video's poster frame. Served from this route, behind the same checks,
|
|
222
|
+
// for the reason stated above: a private video must not have a public
|
|
223
|
+
// thumbnail sitting at a guessable URL.
|
|
224
|
+
if (filename === POSTER_FILENAME) {
|
|
225
|
+
try {
|
|
226
|
+
const poster = await storageAdapter.getObject(buildPosterKey(asset.id));
|
|
227
|
+
setHeaders({
|
|
228
|
+
'Content-Type': 'image/webp',
|
|
229
|
+
'Content-Length': String(poster.length),
|
|
230
|
+
'Cache-Control': isPrivate ? 'private, no-store' : 'public, max-age=31536000, immutable',
|
|
231
|
+
'X-Content-Type-Options': 'nosniff'
|
|
232
|
+
});
|
|
233
|
+
return new Response(toArrayBuffer(poster));
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
// No poster (an API upload, or a codec the browser couldn't decode).
|
|
237
|
+
// 404 rather than falling through to the video: a <img> asking for a
|
|
238
|
+
// poster must not receive 30MB of MP4.
|
|
239
|
+
return new Response('No poster for this asset', { status: 404 });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const variantRequest = filename ? parseVariantFilename(filename) : null;
|
|
243
|
+
const imageConfig = resolveImageConfig(config.images);
|
|
244
|
+
if (variantRequest && imageConfig && asset.assetType === 'image') {
|
|
245
|
+
const configHash = configHashFor(imageConfig);
|
|
246
|
+
// A width outside the ladder, or a request under a superseded config,
|
|
247
|
+
// falls through and serves the original. The ladder being a closed set
|
|
248
|
+
// is what stops arbitrary dimensions turning into unbounded CPU and
|
|
249
|
+
// storage, so an unknown width must never trigger generation.
|
|
250
|
+
const servable = variantRequest.configHash === configHash &&
|
|
251
|
+
imageConfig.widths.includes(variantRequest.width);
|
|
252
|
+
if (servable) {
|
|
253
|
+
const existing = pickVariant(asset, variantRequest.width, configHash);
|
|
254
|
+
// A saved copy should get a readable name, not `w800-a1b2c3.webp`.
|
|
255
|
+
// The variant's *URL* keeps the generated name — it has to, since
|
|
256
|
+
// that's what makes it addressable and immutable — but what the
|
|
257
|
+
// browser writes to disk is this route's business.
|
|
258
|
+
const downloadName = `${stripExtension(asset.originalFilename || asset.filename)}.${VARIANT_FORMAT}`;
|
|
259
|
+
setHeaders({
|
|
260
|
+
'Content-Type': `image/${VARIANT_FORMAT}`,
|
|
261
|
+
// Every variant URL embeds the config hash, so a change of ladder
|
|
262
|
+
// or quality produces a different URL rather than new bytes at the
|
|
263
|
+
// same one. That is what makes a year-long immutable cache safe.
|
|
264
|
+
'Cache-Control': isPrivate ? 'private, no-store' : 'public, max-age=31536000, immutable',
|
|
265
|
+
'Content-Disposition': `inline; filename="${asciiFilename(downloadName)}"; filename*=UTF-8''${encodeURIComponent(downloadName)}`,
|
|
266
|
+
'X-Content-Type-Options': 'nosniff'
|
|
267
|
+
});
|
|
268
|
+
if (existing) {
|
|
269
|
+
try {
|
|
270
|
+
const buffer = await storageAdapter.getObject(existing.path);
|
|
271
|
+
return new Response(toArrayBuffer(buffer), {
|
|
272
|
+
headers: { 'Content-Length': String(buffer.length) }
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
catch (err) {
|
|
276
|
+
// Recorded but unreadable — the object was pruned, or the
|
|
277
|
+
// record outlived it. Fall through and regenerate rather
|
|
278
|
+
// than 404 on something we promised exists.
|
|
279
|
+
cmsLogger.warn('[Asset CDN]', 'Recorded variant unreadable; regenerating:', err);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const { buffer } = await generateVariant({
|
|
284
|
+
asset,
|
|
285
|
+
width: variantRequest.width,
|
|
286
|
+
config: imageConfig,
|
|
287
|
+
configHash,
|
|
288
|
+
storage: storageAdapter,
|
|
289
|
+
database: databaseAdapter
|
|
290
|
+
});
|
|
291
|
+
return new Response(toArrayBuffer(buffer), {
|
|
292
|
+
headers: { 'Content-Length': String(buffer.length) }
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
catch (err) {
|
|
296
|
+
// A derivative that can't be produced must not break the page.
|
|
297
|
+
// Serving the original is heavier but correct, and the next
|
|
298
|
+
// request retries — which is the whole retry story for
|
|
299
|
+
// generate-on-miss.
|
|
300
|
+
cmsLogger.warn('[Asset CDN]', 'Variant generation failed; serving original:', err);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
//
|
|
305
|
+
// The body and its length are resolved together because they're only
|
|
306
|
+
// knowable together: a stream doesn't carry a length, so it has to borrow
|
|
307
|
+
// the row's `size`, while a buffer knows its own and shouldn't trust the
|
|
308
|
+
// row. Omit the header rather than guess when neither is available — a
|
|
309
|
+
// wrong Content-Length truncates the response or hangs the client, both
|
|
310
|
+
// worse than sending none.
|
|
311
|
+
let body;
|
|
312
|
+
let contentLength;
|
|
313
|
+
// Byte ranges, which is what makes media usable. Without them a browser can
|
|
314
|
+
// only reach the middle of a recording by transferring everything before
|
|
315
|
+
// it, so seeking a large video costs a full download and the egress to
|
|
316
|
+
// match. Small files hide this — a few MB over localhost feels instant —
|
|
317
|
+
// which is why it reads as "fine in dev, expensive in production".
|
|
318
|
+
//
|
|
319
|
+
// The total has to be the object's real size, since it goes out in
|
|
320
|
+
// `Content-Range` and a client trusts it. `asset.size` is the row's claim
|
|
321
|
+
// and can be stale, so ask storage when it can tell us.
|
|
322
|
+
let totalSize = asset.size ?? null;
|
|
323
|
+
if (storageAdapter.getObjectMetadata) {
|
|
324
|
+
try {
|
|
325
|
+
const metadata = await storageAdapter.getObjectMetadata(asset.path);
|
|
326
|
+
if (typeof metadata?.size === 'number')
|
|
327
|
+
totalSize = metadata.size;
|
|
328
|
+
}
|
|
329
|
+
catch (err) {
|
|
330
|
+
// Reporting-only: a metadata failure must not fail the download.
|
|
331
|
+
cmsLogger.debug('[Asset CDN]', 'Could not read object metadata for range:', err);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const range = parseByteRange(request.headers.get('range'), totalSize);
|
|
335
|
+
if (range === 'unsatisfiable') {
|
|
336
|
+
// 416 must carry the true size so the client can correct itself.
|
|
95
337
|
return new Response(null, {
|
|
96
|
-
status:
|
|
97
|
-
headers: {
|
|
338
|
+
status: 416,
|
|
339
|
+
headers: {
|
|
340
|
+
'Content-Range': `bytes */${totalSize}`,
|
|
341
|
+
'Accept-Ranges': 'bytes'
|
|
342
|
+
}
|
|
98
343
|
});
|
|
99
344
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
345
|
+
if (range) {
|
|
346
|
+
const rangeLength = range.end - range.start + 1;
|
|
347
|
+
if (storageAdapter.getObjectRange) {
|
|
348
|
+
body = await storageAdapter.getObjectRange(asset.path, range.start, range.end);
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
// Correct, but reads the whole object to return part of it. Adapters
|
|
352
|
+
// should implement getObjectRange; both first-party ones do.
|
|
353
|
+
const fileBuffer = await storageAdapter.getObject(asset.path);
|
|
354
|
+
body = toArrayBuffer(fileBuffer.subarray(range.start, range.end + 1));
|
|
355
|
+
}
|
|
356
|
+
setHeaders({
|
|
357
|
+
'Content-Type': asset.mimeType || 'application/octet-stream',
|
|
358
|
+
// The *range* length, never the file's. A Content-Length that
|
|
359
|
+
// disagrees with the body truncates the response or hangs the client.
|
|
360
|
+
'Content-Length': String(rangeLength),
|
|
361
|
+
'Content-Range': `bytes ${range.start}-${range.end}/${totalSize}`,
|
|
362
|
+
'Accept-Ranges': 'bytes',
|
|
363
|
+
'Cache-Control': isPrivate ? 'private, no-store' : 'public, max-age=31536000, immutable',
|
|
364
|
+
'X-Content-Type-Options': 'nosniff'
|
|
365
|
+
});
|
|
366
|
+
return new Response(body, { status: 206 });
|
|
367
|
+
}
|
|
368
|
+
if (storageAdapter.getStream) {
|
|
369
|
+
body = await storageAdapter.getStream(asset.path);
|
|
370
|
+
contentLength = asset.size ?? null;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
const fileBuffer = await storageAdapter.getObject(asset.path);
|
|
374
|
+
body = toArrayBuffer(fileBuffer);
|
|
375
|
+
contentLength = fileBuffer.length;
|
|
104
376
|
}
|
|
105
|
-
const fileBuffer = await storageAdapter.getObject(asset.path);
|
|
106
377
|
// RFC 6266 Content-Disposition: dual-encode the filename so it survives
|
|
107
378
|
// non-ASCII characters in HTTP headers. HTTP headers are ByteString-
|
|
108
379
|
// restricted (codepoints 0–255), so a raw character like U+202F NARROW
|
|
@@ -123,17 +394,24 @@ export const GET = async ({ params, locals, setHeaders, request }) => {
|
|
|
123
394
|
const disposition = canInline ? 'inline' : 'attachment';
|
|
124
395
|
setHeaders({
|
|
125
396
|
'Content-Type': asset.mimeType || 'application/octet-stream',
|
|
126
|
-
'Content-Length':
|
|
127
|
-
|
|
397
|
+
...(contentLength != null && { 'Content-Length': contentLength.toString() }),
|
|
398
|
+
// A private asset must never land in a shared cache. Now that these
|
|
399
|
+
// bytes flow through the app instead of coming from the bucket, an
|
|
400
|
+
// unconditional `public, immutable` would let any CDN or proxy in front
|
|
401
|
+
// of the app hand the file to the next caller without the checks above
|
|
402
|
+
// ever running again.
|
|
403
|
+
'Cache-Control': isPrivate ? 'private, no-store' : 'public, max-age=31536000, immutable',
|
|
128
404
|
'Content-Disposition': `${disposition}; filename="${asciiFallback}"; filename*=UTF-8''${utf8Encoded}`,
|
|
129
405
|
'X-Content-Type-Options': 'nosniff',
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
406
|
+
// Advertised for every type, not just images. It used to be image-only
|
|
407
|
+
// while the route ignored `Range` entirely — so it was both a promise
|
|
408
|
+
// nothing kept and a promise withheld from video, the one type that
|
|
409
|
+
// actually needs it. Ranges are served above for anything whose size we
|
|
410
|
+
// know, falling back to a buffered slice when the adapter has no ranged
|
|
411
|
+
// read, so this is now true wherever it is sent.
|
|
412
|
+
...(totalSize != null && { 'Accept-Ranges': 'bytes' })
|
|
133
413
|
});
|
|
134
|
-
|
|
135
|
-
const arrayBuffer = fileBuffer.buffer.slice(fileBuffer.byteOffset, fileBuffer.byteOffset + fileBuffer.byteLength);
|
|
136
|
-
return new Response(arrayBuffer);
|
|
414
|
+
return new Response(body);
|
|
137
415
|
}
|
|
138
416
|
catch (error) {
|
|
139
417
|
cmsLogger.error('[Asset CDN]', 'Error serving asset:', error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/server/api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/server/api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AA6B7C;;;;;;;;;;GAUG;AACH,MAAM,MAAM,QAAQ,GAAG;IACtB,SAAS,EAAE;QACV,QAAQ,EAAE,YAAY,CAAC;QACvB,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;KAClB,CAAC;IACF,QAAQ,EAAE;QACT,QAAQ,EAAE,YAAY,CAAC;QACvB,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;KAClB,CAAC;CACF,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,cAAc,kGAqC7B;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,QAmGrD;AAED,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AA0C1D;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,IACtE,GAAG,OAAO,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAC,uBAejD"}
|