Mediabibliotheek: prullenbak, delete met tweede bevestiging, laatste gebruik, bestandsnaam (backend)
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 17s
Continuous Integration / config-asf (pull_request) Successful in 9s
Continuous Integration / frontend-prepare (pull_request) Successful in 30s
Continuous Integration / compile-sites (pull_request) Successful in 1m40s
Continuous Integration / frontend-build (pull_request) Successful in 1m9s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m48s
Continuous Integration / frontend-lint (pull_request) Successful in 41s
Continuous Integration / frontend-test (pull_request) Successful in 2m26s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-build (pull_request) Canceled after 10m10s
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 17s
Continuous Integration / config-asf (pull_request) Successful in 9s
Continuous Integration / frontend-prepare (pull_request) Successful in 30s
Continuous Integration / compile-sites (pull_request) Successful in 1m40s
Continuous Integration / frontend-build (pull_request) Successful in 1m9s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m48s
Continuous Integration / frontend-lint (pull_request) Successful in 41s
Continuous Integration / frontend-test (pull_request) Successful in 2m26s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-build (pull_request) Canceled after 10m10s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,17 +6,17 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
|---|---|---|---|---|
|
||||
| 1 | Which **uses** (places and ratios) exist? | Hero `wide` (3:2) and `tall` (2:3), ASF shoot-page **mosaic** tiles (a small fixed set of ratios per page, to be listed by the ASF session), **masonry** (natural ratio, no crop), **thumb** (library thumbnails, 1:1 or natural). A use is `(module, place, ratio set)`. | Other ratios only add rows to the use definitions; a free-form ratio per tile would need a ratio editor and more variants per photo. | Application design section 4 (`MediaUseDefinition`) |
|
||||
| 2 | **Several formats/variants per photo**: who decides the set? | The **use** decides the ratios and widths (as `VariantPlanner` does for Hero); the library never cuts a fixed set per photo. Formats stay WebP and AVIF as today. | A fixed set per photo costs more disk and CPU on the Pi and cuts variants nobody uses. | `VariantPlanner`, application design section 4 |
|
||||
| 3 | The **7-day automatic clean-up** of unattached Hero images until the library is built. | **Built (2026-11-17, safe: default unchanged)**: the setting `Media:AutoCleanupUnattached` exists, default `true` (today's behaviour); set `Media__AutoCleanupUnattached=false` per host to keep every upload. Plan: switch it off (default `false`) once the library screen and delete are accepted by Bryan. Switch it off behind a setting (`Media:AutoCleanupUnattached`, default `true` until the library screen exists, then default `false`), so nothing is lost silently once the library can show and delete photos. Retired-file removal (after a replaced set) stays: that is housekeeping of generated files, not a person's photos. | Keep it on until phase C; or switch off now (risk: unused uploads pile up with no screen to remove them). | `HeroMediaCleanup`, `MediaOptions` |
|
||||
| 4 | **Rights per module**: who may see, use and delete which photos? | One shared library for Owner and Administrator (like Hero and Offerings). No per-module visibility yet. | Per-module visibility needs an owner field on a photo and a filter; the contract has room for it (`ModuleKey` on a use). | `[Authorize(Policy = "AdminOnly")]`, `MediaUse.Module` |
|
||||
| 3 | The **7-day automatic clean-up** of unattached Hero images until the library is built. **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | **Built (2026-11-17, safe: default unchanged)**: the setting `Media:AutoCleanupUnattached` exists, default `true` (today's behaviour); set `Media__AutoCleanupUnattached=false` per host to keep every upload. Plan: switch it off (default `false`) once the library screen and delete are accepted by Bryan. Switch it off behind a setting (`Media:AutoCleanupUnattached`, default `true` until the library screen exists, then default `false`), so nothing is lost silently once the library can show and delete photos. Retired-file removal (after a replaced set) stays: that is housekeeping of generated files, not a person's photos. | Keep it on until phase C; or switch off now (risk: unused uploads pile up with no screen to remove them). | `HeroMediaCleanup`, `MediaOptions` |
|
||||
| 4 | **Rights per module**: who may see, use and delete which photos? **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | One shared library for Owner and Administrator (like Hero and Offerings). No per-module visibility yet. | Per-module visibility needs an owner field on a photo and a filter; the contract has room for it (`ModuleKey` on a use). | `[Authorize(Policy = "AdminOnly")]`, `MediaUse.Module` |
|
||||
| 5 | **Public addresses**: per module (`/media/<module>/...`) or a shared path? | Keep `/media/{area}/{setHash}/{name}`; `area` stays the module (`hero`; a gallery would use `gallery`). The set hash still comes from the original's hash plus the crops of that **use**. A photo used in two modules is two sets (two addresses), cached separately. | A shared path would change addresses and golden files: not allowed without alignment. | `MediaPaths`, `MediaEndpoint` |
|
||||
| 6 | **Usage register**: do modules report, or does the library keep it centrally? | Modules report: `IMediaUsageProvider` per module answers "where is this photo used" and "detach this photo" from the module's own data. Computed, never copied, so it cannot go stale. The library warns when a module has no provider. | A central register table keeps the answer fast but must be kept in step on every module save (risk of drift); can be added later as a cache. | Application design section 3, `IMediaUsageProvider` |
|
||||
| 7 | **What counts as "unused"** in the clean-up? | A photo is unused when **no provider** reports a use. A photo used only by an inactive or unpublished slide still counts as used (it is in a slide). | Treating an inactive slide as unused would delete photos a person may want to switch on again; stricter would be safer than looser. | `MediaUsageService` |
|
||||
| 8 | **Clean-up default period** and slider range. | Default 7 days, slider 1 to 365 days, step 1; "Only unused photos" on. The period is measured from the upload date. | Measured from last use, or another range: a one-line change in the filter. | Clean-up component, `CleanupSelection` |
|
||||
| 7 | **What counts as "unused"** in the clean-up? **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | A photo is unused when **no provider** reports a use. A photo used only by an inactive or unpublished slide still counts as used (it is in a slide). | Treating an inactive slide as unused would delete photos a person may want to switch on again; stricter would be safer than looser. | `MediaUsageService` |
|
||||
| 8 | **Clean-up default period** and slider range. **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | Default 7 days, slider 1 to 365 days, step 1; "Only unused photos" on. The period is measured from the upload date. | Measured from last use, or another range: a one-line change in the filter. | Clean-up component, `CleanupSelection` |
|
||||
| 9 | **Storage limit and warning**. | Limit = existing `Media:MaxTotalBytes` (2 GB, decided by Bryan, Hero assumption 12). Warning at 80 percent (amber) and at 100 percent (red, uploads refused with a clear message). Used bytes = originals plus all variants, cached 60 s. | Another threshold or limit: configuration only. | `MediaOptions`, `MediaStorageUsage` |
|
||||
| 10 | **Delete of a photo that is in use**. | Not possible from Clean-up (those rows are not selectable); possible from the library after a second confirmation that names the uses; the module detaches it first (Hero: the slide loses its image and is unpublished until a new one is chosen, as today). | Block deleting in-use photos completely: the extra confirmation path is removed. | `IMediaUsageProvider.DetachAsync`, library screen |
|
||||
| 10 | **Delete of a photo that is in use**. **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | Not possible from Clean-up (those rows are not selectable); possible from the library after a second confirmation that names the uses; the module detaches it first (Hero: the slide loses its image and is unpublished until a new one is chosen, as today). | Block deleting in-use photos completely: the extra confirmation path is removed. | `IMediaUsageProvider.DetachAsync`, library screen |
|
||||
| 11 | **Shared upload service**: does Hero's upload move into the library? | In phase A Hero keeps its own upload endpoint; the library also lists those photos. In phase B both call one upload service in Core. | Moving Hero upload earlier gives a single path sooner but touches Hero before the data model exists. | `HeroImageService`, `MediaUploadGuard` |
|
||||
| 12 | **Trash delay** (deleted photos recoverable for a while)? | No trash in phase A: the confirmation naming the count is the safety. A 7-day trash can be added in phase B (a `DeletedAt` and the existing retired-file mechanism). | Trash from the start: reuse `HeroRetiredFile` logic generalised in Core; about 1 day extra. | `HeroRetiredFile`, application design section 5 |
|
||||
| 13 | **Migration of existing `HeroImages`** (phase C). | Expand then contract: new `MediaAsset` and `MediaUse` tables are filled from `HeroImages` and `HeroSlides` with the **same ids, hashes and set hashes**; Hero reads the library; old columns are dropped in a later release. Needs a back-up and alignment: **not built**. | A one-step move is smaller but cannot be rolled back; or no migration at all and the library keeps reading through Hero (the provider approach) until the gallery module needs own tables. | Requirements section 9, application design section 6 |
|
||||
| 12 | **Trash delay** (deleted photos recoverable for a while)? **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | No trash in phase A: the confirmation naming the count is the safety. A 7-day trash can be added in phase B (a `DeletedAt` and the existing retired-file mechanism). | Trash from the start: reuse `HeroRetiredFile` logic generalised in Core; about 1 day extra. | `HeroRetiredFile`, application design section 5 |
|
||||
| 13 | **Migration of existing `HeroImages`** (phase C). **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | Expand then contract: new `MediaAsset` and `MediaUse` tables are filled from `HeroImages` and `HeroSlides` with the **same ids, hashes and set hashes**; Hero reads the library; old columns are dropped in a later release. Needs a back-up and alignment: **not built**. | A one-step move is smaller but cannot be rolled back; or no migration at all and the library keeps reading through Hero (the provider approach) until the gallery module needs own tables. | Requirements section 9, application design section 6 |
|
||||
| 14 | **Crop per use**: data per use or per photo? | A use owns its crop (focus x, y and zoom per ratio); the photo owns the default focus new uses start from. The set hash of a use is `SetHash(originalSha256, crops of that use)` exactly as today, so existing Hero addresses are unchanged. | Crop per photo only (today's model) cannot serve a mosaic with several tiles of the same photo. | `MediaPaths.SetHash`, `MediaUse` |
|
||||
| 15 | **Selection range** with shift-click and "select all": across pages or the loaded page? | The loaded list (the clean-up list is loaded fully, the library list incrementally; "select all" says how many are loaded and offers "select all N" for a filtered list). | Strictly the current page: simpler, less useful for a clean-up of hundreds. | Selection component |
|
||||
| 16 | **Deleting files from the store** when a photo is deleted. | The original and the set directories of that photo are deleted through `IMediaStore` after the database rows; a set shared with another use (same hash) is kept until no use references it. | Soft delete first (see 12). | `IMediaStore.DeleteSetAsync`, `HeroRepository.GetReferencedSetHashesAsync` |
|
||||
@@ -24,7 +24,7 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
| 18 | **Phase A built (2026-11-17, on assumptions)**: what is in the first step? | Read-only only: the usage contract (`IMediaUsageProvider`, `MediaLibraryService`, pure `CleanupSelection`), the Hero provider and the selection components in the frontend (`PhotoSelectionList`, `BulkActionBar`, selection rules). **No endpoint, no screen, no delete, no migration, no address change yet.** | Wire the screen and endpoints next (list, storage, clean-up preview) and then delete (needs a Hero delete path and Bryan's check of assumptions 3, 7 and 10). | `Core/Media/Library`, `Modules.Hero/Services/HeroMediaUsageProvider.cs`, `frontend/src/features/media` |
|
||||
| 19 | The **position** of a slide in "Hero, slide 3". | Rank by `SortOrder` within its hero, counting slides that are switched off, 1-based; a slide that is off still counts as a use and is shown "(uit/off)". | Count only active slides, or show the slide's title instead. | `HeroMediaUsageProvider.ListUsesAsync` |
|
||||
| 20 | The **thumbnail** of a Hero photo in the library. | The smallest published WebP variant of the current set; none yet (a grey "no preview") while the set is not cut. | Use the full-frame preview (`full-*.webp`), which shows the whole picture instead of the wide crop. | `HeroMediaUsageProvider.Thumbnail` |
|
||||
| 21 | A **name** for a photo. | Photos have no file name today (`HeroImage` does not store it), so the library shows a neutral label (date and size); a name field comes with the library's own tables (phase B). | Store the original file name at upload now (a small Hero migration, additive). | `PhotoItem.name`, phase B |
|
||||
| 21 | A **name** for a photo. **[Decided by Bryan, 2026-11-17, second round: see "Decisions of the second round"]** | Photos have no file name today (`HeroImage` does not store it), so the library shows a neutral label (date and size); a name field comes with the library's own tables (phase B). | Store the original file name at upload now (a small Hero migration, additive). | `PhotoItem.name`, phase B |
|
||||
| 22 | **Select all** and ranges with photos that cannot be selected (in use during a clean-up). | They are skipped by "select all" and by shift-ranges; the header box shows "some" when only part is selected. | Include them but refuse at confirmation. | `PhotoSelectionList`, `selection.ts` |
|
||||
| 23 | Where does Hero **list all photos** (performance)? | In memory, newest first, as the rest of the Hero repository does (a few dozen photos; the MySQL provider has limits with Guid lists). Paging comes with the screen when the count grows. | Server-side paging by `CreatedAt` (needs an index). | `HeroRepository.GetAllImagesReadOnlyAsync` |
|
||||
| 24 | **Tile ratios of the ASF mosaic** (assumption 1). | **Answered by the ASF session (2026-11-17, from its code)**: the mosaic shows at most 4 tiles per shoot page, from 2 photos up (layouts for 2, 3, 4). The tile shape is fixed per page by the photo count and order, not chosen per photo: **large** (photo 1) 2x2, desktop about 1:1 (max about 656x632), phone 4:3; **wide** (photo 2 with 3 or 4 photos) 2x1, desktop about 2.2:1 (max about 656x300), phone 3:2; **small** (photos 3 and 4 with 4 photos) 1x1, about 1.04:1 (max about 312x300), phone 1:1. With 2 photos: two large side by side. All object-fit cover (a crop). Widths: large and wide up to about 656 px, small about 312 px; phone half width about 175-300, full width about 350-600; retina variants 480, 640, 960, 1280. **Masonry** (portfolio): natural ratio only, no crop (rounding below half a row of 1/40 of the column width); 1, 2 or 3 columns, column width up to about 427 px; thumb now 800 px wide with the height following the ratio, variants about 480 and 800; the lightbox uses a separate `full`. Used in the design as uses `mosaic-large`, `mosaic-wide`, `mosaic-small` (each with a focus point and a desktop and phone shape) and `masonry` (no crop). | Each answer only adds use definitions. A ratio per photo (not per tile shape) would need a ratio editor. | Application design section 4 |
|
||||
@@ -32,3 +32,25 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
| 26 | **Choose or upload everywhere (ML-17)**: how does the Hero upload relate to the library? | Now the Hero keeps its own upload and table; its photos are already in the library list through the provider. When the library owns uploads (phase B) every upload creates exactly one library record (same file = same record by hash). The Hero switches to the shared picker with the phase C migration. Until then the Hero upload flow does not change. | Moving the Hero to the picker earlier (before phase B) would give a single upload path sooner but touches the Hero flow and needs the data model first. | Application design section 10 |
|
||||
| 27 | **Scope of the shared picker**: single choice or several, and what does it include? | One photo per picker (single choice, radio group). It includes the library photos and one upload button (one file at a time); the Hero's multi-file upload and drag and drop stay on the Hero screen. The picker is pure: the module passes the list and the upload callback. | Several photos at once or drag and drop in the picker: a larger component, added when a module needs it (the gallery will probably need multiple). | `features/media/components/ImagePicker.tsx` |
|
||||
| 28 | **Uploaded photo not ready yet** in the picker. | It is chosen at once but shows "no preview" until the background renderer has cut its set; an image without a set is not published (as in the Hero today). | Block choosing until it is ready: simpler to explain, slower to work with. | `ImagePicker`, `HeroRenderService` |
|
||||
|
||||
## Decisions of the second round (Bryan, 2026-11-17, via SLP Software - SLS - Main)
|
||||
|
||||
| # | Decision | Consequence in design and code |
|
||||
|---|---|---|
|
||||
| 3 | The automatic removal of unused Hero uploads is switched off once Bryan has approved the screen (PR #22). | `Media:AutoCleanupUnattached` keeps its default `true`; it becomes `false` only at or after his approval of PR #22. |
|
||||
| 4 | One shared library for Owner and Administrator, no rights per module. | As designed (`AdminOnly`). |
|
||||
| 7 | A photo used only by a switched-off slide counts as **used**. | As designed (`HeroMediaUsageProvider` reports it as a use with `Active = false`). |
|
||||
| 8 | The clean-up period counts from the **last use**, not the upload date. | Each photo gets "unused since" (the moment it was last detached from a slide; the upload date when it was never used). Details in assumption 29. |
|
||||
| 10 | Deleting a photo that is in use is possible after a **second confirmation that names the places**. | Delete refuses an in-use photo unless the request says it was confirmed twice; the module detaches it (Hero: the slides using it are removed). Assumption 30. |
|
||||
| 12 | There is a **trash**: recoverable for 7 days (`DeletedAt` plus the existing retired-file mechanism). | Delete means "to the trash"; the files go 7 days later. A trash view lists them with Restore. Assumption 31. |
|
||||
| 13 | The migration of `HeroImages` to the library's own tables may be built next (expand then contract, same ids, hashes and addresses). | Conditions: document a back-up and rollback plan first; roll out only to TEST, never production; tell SLS-Main before deploying to test. Assumption 32. |
|
||||
| 21 | The original file name is stored at every upload from now on (additive); existing photos keep the neutral label. | `OriginalFileName` on the Hero image (additive migration); the library shows it. |
|
||||
|
||||
### New assumptions from these decisions
|
||||
|
||||
| # | Question | Assumption made | If Bryan chooses differently | Where |
|
||||
|---|---|---|---|---|
|
||||
| 29 | **Reference date of the clean-up** (decision 8). | For a photo that is not in use: the moment it was last detached from a slide (`LastUsedAt`), else its upload date. For a photo that is in use right now (clean-up with "Only unused photos" off): its upload date, since "last used" is "now" and nothing would ever be old enough. The period slider keeps its range (1 to 365 days, default 7). | "Last used" for a photo in use = the last save that kept it, so with the filter off only photos left alone for the whole period are offered. | `HeroImage.LastUsedAt`, `HeroService.SaveAsync`, `CleanupSelection` |
|
||||
| 30 | **Detaching a used photo when it is deleted** (decision 10). | In the Hero the slides that use the photo are removed from the hero (a slide cannot exist without an image); the second confirmation names them ("Hero, slide 3"). The hero then has fewer slides and stays published as long as it has a title. | Keep the slide and mark it "image missing" until a new image is chosen: needs a nullable slide image and a changed public contract. | `HeroMediaUsageProvider.TrashAsync` |
|
||||
| 31 | **Trash mechanics** (decision 12). | `DeletedAt` on the Hero image: a trashed photo is hidden from the Hero (slides, upload by hash, renderer) and shown in the library trash; Restore clears it. After 7 days the cleanup turns it into retired files (removed immediately) and removes the row. Uploading the same file again restores it. The trash period is `Media:TrashDays` (7). | Another period: the setting; no trash: delete straight to retired files. | `HeroImage.DeletedAt`, `HeroMediaCleanup`, `MediaOptions.TrashDays` |
|
||||
| 32 | **Migration of `HeroImages`** (decision 13): the plan and the safeguards. | Designed in `inception/application-design/migration-plan.md` (back-up, rollback, verification, test-only rollout). The additive columns of this round (`OriginalFileName`, `DeletedAt`, `LastUsedAt`) are copied with the rest. Not run anywhere until SLS-Main has been told and agrees to the test deploy. | n/a | `migration-plan.md` |
|
||||
|
||||
@@ -1140,6 +1140,13 @@
|
||||
"format": "date-time",
|
||||
"type": "string"
|
||||
},
|
||||
"deletedAt": {
|
||||
"format": "date-time",
|
||||
"type": [
|
||||
"null",
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"height": {
|
||||
"format": "int32",
|
||||
"pattern": "^-?(?:0|[1-9]\\d*)$",
|
||||
@@ -1154,12 +1161,25 @@
|
||||
"module": {
|
||||
"type": "string"
|
||||
},
|
||||
"originalFileName": {
|
||||
"type": [
|
||||
"null",
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"thumbnailUrl": {
|
||||
"type": [
|
||||
"null",
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"unusedSince": {
|
||||
"format": "date-time",
|
||||
"type": [
|
||||
"null",
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"uses": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/MediaUseDto"
|
||||
@@ -1187,6 +1207,20 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaRestoreRequest": {
|
||||
"properties": {
|
||||
"ids": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"ids"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaStorageDto": {
|
||||
"properties": {
|
||||
"level": {
|
||||
@@ -1225,6 +1259,38 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaTrashRequest": {
|
||||
"properties": {
|
||||
"confirmedInUse": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"ids": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"ids"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaTrashResponse": {
|
||||
"properties": {
|
||||
"results": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/MediaUseResultDto"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"results"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaUseDto": {
|
||||
"properties": {
|
||||
"active": {
|
||||
@@ -1254,6 +1320,28 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"MediaUseResultDto": {
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"type": "string"
|
||||
},
|
||||
"uses": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/MediaUseDto"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"status",
|
||||
"uses"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"OfferingBannerRequest": {
|
||||
"properties": {
|
||||
"endsAt": {
|
||||
@@ -3498,6 +3586,75 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/media-library/delete": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/*+json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashRequest"
|
||||
}
|
||||
},
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashRequest"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
},
|
||||
"text/plain": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "OK"
|
||||
},
|
||||
"400": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
},
|
||||
"text/plain": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Bad Request"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"MediaLibrary"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/media-library/photos": {
|
||||
"get": {
|
||||
"responses": {
|
||||
@@ -3527,6 +3684,75 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/media-library/restore": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/*+json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaRestoreRequest"
|
||||
}
|
||||
},
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaRestoreRequest"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaRestoreRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
},
|
||||
"text/plain": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaTrashResponse"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "OK"
|
||||
},
|
||||
"400": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
},
|
||||
"text/plain": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ProblemDetails"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Bad Request"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"MediaLibrary"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/media-library/storage": {
|
||||
"get": {
|
||||
"responses": {
|
||||
@@ -3556,6 +3782,35 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/media-library/trash": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaLibraryDto"
|
||||
}
|
||||
},
|
||||
"text/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaLibraryDto"
|
||||
}
|
||||
},
|
||||
"text/plain": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/MediaLibraryDto"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "OK"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"MediaLibrary"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/offerings": {
|
||||
"get": {
|
||||
"responses": {
|
||||
|
||||
@@ -79,10 +79,114 @@ public class MediaLibraryTests
|
||||
library.Modules.Should().Equal("gallery", "hero");
|
||||
}
|
||||
|
||||
private sealed class FakeProvider(string module, MediaPhoto[] photos, MediaUse[] uses) : IMediaUsageProvider
|
||||
[Fact]
|
||||
public async Task Trash_refuses_a_photo_in_use_until_the_person_confirmed_and_then_names_the_places()
|
||||
{
|
||||
var photo = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-40), null);
|
||||
var use = new MediaUse("hero", photo.Id, "slide", 3, true);
|
||||
var provider = new FakeProvider("hero", [photo], [use]);
|
||||
var library = new MediaLibraryService([provider]);
|
||||
|
||||
var refused = await library.TrashAsync([$"hero:{photo.Id}"], confirmedInUse: false, Guid.Empty, CancellationToken.None);
|
||||
var done = await library.TrashAsync([$"hero:{photo.Id}"], confirmedInUse: true, Guid.Empty, CancellationToken.None);
|
||||
|
||||
refused.Should().ContainSingle().Which.Status.Should().Be("inUse");
|
||||
refused.Single().Uses.Should().Equal(use);
|
||||
provider.Trashed.Should().Equal(photo.Id);
|
||||
done.Single().Status.Should().Be("trashed");
|
||||
done.Single().Uses.Should().Equal(use);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Trash_reports_every_photo_on_its_own_and_one_failure_does_not_stop_the_others()
|
||||
{
|
||||
var ok = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now, null);
|
||||
var broken = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now, null);
|
||||
var provider = new FakeProvider("hero", [ok, broken], []) { FailFor = broken.Id };
|
||||
var library = new MediaLibraryService([provider]);
|
||||
|
||||
var results = await library.TrashAsync([$"hero:{broken.Id}", $"hero:{ok.Id}", "hero:not-a-guid", $"other:{Guid.NewGuid()}", $"hero:{Guid.NewGuid()}"], false, Guid.Empty, CancellationToken.None);
|
||||
|
||||
results.Select(r => r.Status).Should().Equal("failed", "trashed", "notFound", "notFound", "notFound");
|
||||
provider.Trashed.Should().Equal(ok.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Trash_and_restore_accept_at_most_100_photos_and_a_photo_once()
|
||||
{
|
||||
var provider = new FakeProvider("hero", [], []);
|
||||
var library = new MediaLibraryService([provider]);
|
||||
var tooMany = Enumerable.Range(0, 101).Select(_ => $"hero:{Guid.NewGuid()}").ToList();
|
||||
|
||||
await FluentActions.Awaiting(() => library.TrashAsync(tooMany, false, Guid.Empty, CancellationToken.None)).Should().ThrowAsync<ArgumentOutOfRangeException>();
|
||||
await FluentActions.Awaiting(() => library.RestoreAsync(tooMany, Guid.Empty, CancellationToken.None)).Should().ThrowAsync<ArgumentOutOfRangeException>();
|
||||
var id = $"hero:{Guid.NewGuid()}";
|
||||
(await library.TrashAsync([id, id], false, Guid.Empty, CancellationToken.None)).Should().ContainSingle();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Restore_gives_the_photo_back_and_lists_the_trash_most_recent_first()
|
||||
{
|
||||
var older = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-9), null, DeletedAt: Now.AddDays(-3));
|
||||
var newer = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-9), null, DeletedAt: Now.AddDays(-1));
|
||||
var provider = new FakeProvider("hero", [], [], trash: [older, newer]);
|
||||
var library = new MediaLibraryService([provider]);
|
||||
|
||||
(await library.ListTrashAsync(CancellationToken.None)).Select(i => i.Photo.Id).Should().Equal(newer.Id, older.Id);
|
||||
(await library.RestoreAsync([$"hero:{older.Id}"], Guid.Empty, CancellationToken.None)).Single().Status.Should().Be("restored");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_clean_up_counts_from_the_last_use_and_falls_back_to_the_upload_date()
|
||||
{
|
||||
var detachedRecently = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-100), null, UnusedSince: Now.AddDays(-2));
|
||||
var detachedLongAgo = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-100), null, UnusedSince: Now.AddDays(-30));
|
||||
var neverUsed = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-20), null);
|
||||
var items = new[] { detachedRecently, detachedLongAgo, neverUsed }.Select(p => new MediaLibraryItem(p, [])).ToList();
|
||||
|
||||
var selected = CleanupSelection.Select(items, Now, 7, onlyUnused: true);
|
||||
|
||||
selected.Select(i => i.Photo.Id).Should().BeEquivalentTo([detachedLongAgo.Id, neverUsed.Id]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_photo_in_use_counts_from_its_upload_date()
|
||||
{
|
||||
var photo = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-20), null, UnusedSince: Now.AddDays(-1));
|
||||
var item = new MediaLibraryItem(photo, [new MediaUse("hero", photo.Id, "slide", 1, true)]);
|
||||
|
||||
CleanupSelection.ReferenceDate(item).Should().Be(photo.CreatedAt);
|
||||
CleanupSelection.Select([item], Now, 7, onlyUnused: false).Should().ContainSingle();
|
||||
}
|
||||
|
||||
private sealed class FakeProvider(string module, MediaPhoto[] photos, MediaUse[] uses, MediaPhoto[]? trash = null) : IMediaUsageProvider
|
||||
{
|
||||
public string Module => module;
|
||||
|
||||
public List<Guid> Trashed { get; } = [];
|
||||
|
||||
public Guid? FailFor { get; init; }
|
||||
|
||||
public Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaPhoto>>(trash ?? []);
|
||||
|
||||
public Task<int?> TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (photoId == FailFor)
|
||||
{
|
||||
throw new InvalidOperationException("boom");
|
||||
}
|
||||
|
||||
if (photos.All(p => p.Id != photoId))
|
||||
{
|
||||
return Task.FromResult<int?>(null);
|
||||
}
|
||||
|
||||
Trashed.Add(photoId);
|
||||
return Task.FromResult<int?>(uses.Count(u => u.PhotoId == photoId));
|
||||
}
|
||||
|
||||
public Task<bool> RestoreAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken) => Task.FromResult(trash?.Any(p => p.Id == photoId) == true);
|
||||
|
||||
public Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaPhoto>>(photos);
|
||||
|
||||
public Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken)
|
||||
|
||||
@@ -73,6 +73,9 @@ public sealed class MediaOptions
|
||||
/// Default <c>true</c> keeps today's behaviour (assumption 3 of the media-library list).
|
||||
/// </summary>
|
||||
public bool AutoCleanupUnattached { get; set; } = true;
|
||||
|
||||
/// <summary>How long a photo stays recoverable in the trash before its files are removed.</summary>
|
||||
public int TrashDays { get; set; } = 7;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,7 +2,8 @@ namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>
|
||||
/// The photos a clean-up would preselect: uploaded before the period and, by default, not used anywhere. Pure, so it is
|
||||
/// easy to test. The period is measured from the upload date (assumption 8 of the media-library list).
|
||||
/// easy to test. The period counts from the LAST USE (decision 8): for a photo that is not in use the moment it was last
|
||||
/// detached, else its upload date; for a photo in use right now its upload date (assumption 29).
|
||||
/// </summary>
|
||||
public static class CleanupSelection
|
||||
{
|
||||
@@ -18,13 +19,17 @@ public static class CleanupSelection
|
||||
{
|
||||
var cutoff = now.AddDays(-ClampDays(days));
|
||||
return items
|
||||
.Where(item => item.Photo.CreatedAt < cutoff)
|
||||
.Where(item => ReferenceDate(item) < cutoff)
|
||||
.Where(item => !onlyUnused || !item.InUse)
|
||||
.OrderByDescending(item => item.Photo.CreatedAt)
|
||||
.ThenBy(item => item.Photo.Id)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>The date the period counts from.</summary>
|
||||
public static DateTimeOffset ReferenceDate(MediaLibraryItem item)
|
||||
=> item.InUse ? item.Photo.CreatedAt : item.Photo.UnusedSince ?? item.Photo.CreatedAt;
|
||||
|
||||
/// <summary>The bytes the given items take (originals; the variants are counted by the storage service).</summary>
|
||||
public static long Bytes(IEnumerable<MediaLibraryItem> items) => items.Sum(item => item.Photo.Bytes);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -6,18 +7,40 @@ namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
public sealed record MediaUseDto(string Module, string Kind, int? Position, bool Active);
|
||||
|
||||
public sealed record MediaPhotoDto(string Id, string Module, long Bytes, int Width, int Height, DateTimeOffset CreatedAt, string? ThumbnailUrl, IReadOnlyList<MediaUseDto> Uses);
|
||||
public sealed record MediaPhotoDto(
|
||||
string Id,
|
||||
string Module,
|
||||
long Bytes,
|
||||
int Width,
|
||||
int Height,
|
||||
DateTimeOffset CreatedAt,
|
||||
string? ThumbnailUrl,
|
||||
IReadOnlyList<MediaUseDto> Uses,
|
||||
string? OriginalFileName = null,
|
||||
DateTimeOffset? UnusedSince = null,
|
||||
DateTimeOffset? DeletedAt = null);
|
||||
|
||||
public sealed record MediaLibraryDto(IReadOnlyList<MediaPhotoDto> Photos, IReadOnlyList<string> Modules);
|
||||
|
||||
public sealed record MediaStorageDto(long UsedBytes, long LimitBytes, string Level, int WarningPercent);
|
||||
|
||||
/// <summary>What a clean-up with these settings would select. A preview only: nothing is deleted.</summary>
|
||||
/// <summary>What a clean-up with these settings would select. A preview: nothing is deleted until the person confirms.</summary>
|
||||
public sealed record MediaCleanupPreviewDto(int Days, bool OnlyUnused, int Count, long Bytes, IReadOnlyList<MediaPhotoDto> Photos);
|
||||
|
||||
/// <param name="Ids">Photo ids as listed (<c>module:guid</c>), at most 100.</param>
|
||||
/// <param name="ConfirmedInUse">True when the person confirmed a second time, knowing where the photos in use are used.</param>
|
||||
public sealed record MediaTrashRequest(IReadOnlyList<string> Ids, bool ConfirmedInUse = false);
|
||||
|
||||
public sealed record MediaRestoreRequest(IReadOnlyList<string> Ids);
|
||||
|
||||
public sealed record MediaUseResultDto(string Id, string Status, IReadOnlyList<MediaUseDto> Uses);
|
||||
|
||||
public sealed record MediaTrashResponse(IReadOnlyList<MediaUseResultDto> Results);
|
||||
|
||||
/// <summary>
|
||||
/// The media library for administrators: the photos of all modules with where they are used, the storage against its
|
||||
/// limit, and a preview of the clean-up. Read-only: deleting comes after the assumptions are checked.
|
||||
/// limit, the clean-up preview, and moving photos to the trash and back. Deleting is always "to the trash": the files go
|
||||
/// after the trash period.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("media-library")]
|
||||
@@ -32,6 +55,14 @@ public class MediaLibraryController(IMediaLibraryService library, TimeProvider t
|
||||
return Ok(new MediaLibraryDto(items.Select(ToDto).ToList(), library.Modules));
|
||||
}
|
||||
|
||||
[HttpGet("trash")]
|
||||
[ProducesResponseType<MediaLibraryDto>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Trash(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await library.ListTrashAsync(cancellationToken);
|
||||
return Ok(new MediaLibraryDto(items.Select(ToDto).ToList(), library.Modules));
|
||||
}
|
||||
|
||||
[HttpGet("storage")]
|
||||
[ProducesResponseType<MediaStorageDto>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Storage(CancellationToken cancellationToken)
|
||||
@@ -50,6 +81,47 @@ public class MediaLibraryController(IMediaLibraryService library, TimeProvider t
|
||||
return Ok(new MediaCleanupPreviewDto(clamped, onlyUnused, selected.Count, CleanupSelection.Bytes(selected), selected.Select(ToDto).ToList()));
|
||||
}
|
||||
|
||||
/// <summary>Moves photos to the trash. A photo in use is refused (status <c>inUse</c>) unless <c>confirmedInUse</c> is true.</summary>
|
||||
[HttpPost("delete")]
|
||||
[ProducesResponseType<MediaTrashResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> Delete([FromBody] MediaTrashRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!ValidIds(request.Ids))
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var results = await library.TrashAsync(request.Ids, request.ConfirmedInUse, CallerId(), cancellationToken);
|
||||
return Ok(new MediaTrashResponse(results.Select(ToDto).ToList()));
|
||||
}
|
||||
|
||||
[HttpPost("restore")]
|
||||
[ProducesResponseType<MediaTrashResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> Restore([FromBody] MediaRestoreRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!ValidIds(request.Ids))
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var results = await library.RestoreAsync(request.Ids, CallerId(), cancellationToken);
|
||||
return Ok(new MediaTrashResponse(results.Select(ToDto).ToList()));
|
||||
}
|
||||
|
||||
private static bool ValidIds(IReadOnlyList<string>? ids)
|
||||
=> ids is { Count: > 0 and <= MediaLibraryService.MaxIdsPerRequest } && ids.All(id => !string.IsNullOrWhiteSpace(id) && id.Length <= 80);
|
||||
|
||||
private Guid CallerId()
|
||||
{
|
||||
var claim = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub");
|
||||
return Guid.TryParse(claim, out var id) ? id : Guid.Empty;
|
||||
}
|
||||
|
||||
private static MediaUseResultDto ToDto(MediaTrashResult result)
|
||||
=> new(result.Id, result.Status, result.Uses.Select(u => new MediaUseDto(u.Module, u.PlaceKind, u.Position, u.Active)).ToList());
|
||||
|
||||
private static MediaPhotoDto ToDto(MediaLibraryItem item)
|
||||
=> new(
|
||||
$"{item.Photo.Module}:{item.Photo.Id}",
|
||||
@@ -59,5 +131,8 @@ public class MediaLibraryController(IMediaLibraryService library, TimeProvider t
|
||||
item.Photo.Height,
|
||||
item.Photo.CreatedAt,
|
||||
item.Photo.ThumbnailUrl,
|
||||
item.Uses.Select(u => new MediaUseDto(u.Module, u.PlaceKind, u.Position, u.Active)).ToList());
|
||||
item.Uses.Select(u => new MediaUseDto(u.Module, u.PlaceKind, u.Position, u.Active)).ToList(),
|
||||
item.Photo.OriginalFileName,
|
||||
item.Photo.UnusedSince,
|
||||
item.Photo.DeletedAt);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,39 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>The library over all modules that report photos: one list, newest first, each photo with its uses. Read-only.</summary>
|
||||
/// <summary>The result for one photo of a trash or restore request.</summary>
|
||||
/// <param name="Id">The photo id as the caller sent it (<c>module:guid</c>).</param>
|
||||
/// <param name="Status"><c>trashed</c>, <c>restored</c>, <c>inUse</c> (not done: it is used and the caller did not confirm), <c>notFound</c> or <c>failed</c>.</param>
|
||||
/// <param name="Uses">Where the photo was used when it was refused or trashed.</param>
|
||||
public sealed record MediaTrashResult(string Id, string Status, IReadOnlyList<MediaUse> Uses);
|
||||
|
||||
/// <summary>The library over all modules that report photos: one list, newest first, each photo with its uses.</summary>
|
||||
public interface IMediaLibraryService
|
||||
{
|
||||
/// <summary>All photos of all modules with their uses, newest first.</summary>
|
||||
Task<IReadOnlyList<MediaLibraryItem>> ListAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The photos in the trash, most recently trashed first.</summary>
|
||||
Task<IReadOnlyList<MediaLibraryItem>> ListTrashAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The modules that report photos (the screen warns about a module that is not in this list).</summary>
|
||||
IReadOnlyList<string> Modules { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Moves photos to the trash. The usage is checked again here, at the moment of deleting: a photo that is in use is
|
||||
/// only trashed when <paramref name="confirmedInUse"/> says the person confirmed it knowing where it is used.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<MediaTrashResult>> TrashAsync(IReadOnlyCollection<string> ids, bool confirmedInUse, Guid callerId, CancellationToken cancellationToken);
|
||||
|
||||
Task<IReadOnlyList<MediaTrashResult>> RestoreAsync(IReadOnlyCollection<string> ids, Guid callerId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public sealed class MediaLibraryService(IEnumerable<IMediaUsageProvider> providers) : IMediaLibraryService
|
||||
public sealed class MediaLibraryService(IEnumerable<IMediaUsageProvider> providers, ILogger<MediaLibraryService>? logger = null) : IMediaLibraryService
|
||||
{
|
||||
/// <summary>The most photos one trash or restore request may name.</summary>
|
||||
public const int MaxIdsPerRequest = 100;
|
||||
|
||||
private readonly IMediaUsageProvider[] _providers = providers.ToArray();
|
||||
|
||||
public IReadOnlyList<string> Modules => _providers.Select(p => p.Module).OrderBy(m => m, StringComparer.Ordinal).ToList();
|
||||
@@ -29,4 +51,111 @@ public sealed class MediaLibraryService(IEnumerable<IMediaUsageProvider> provide
|
||||
|
||||
return items.OrderByDescending(i => i.Photo.CreatedAt).ThenBy(i => i.Photo.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaLibraryItem>> ListTrashAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = new List<MediaLibraryItem>();
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
items.AddRange((await provider.ListTrashAsync(cancellationToken)).Select(photo => new MediaLibraryItem(photo, [])));
|
||||
}
|
||||
|
||||
return items.OrderByDescending(i => i.Photo.DeletedAt).ThenBy(i => i.Photo.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaTrashResult>> TrashAsync(IReadOnlyCollection<string> ids, bool confirmedInUse, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinLimit(ids);
|
||||
var results = new List<MediaTrashResult>();
|
||||
foreach (var id in ids.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (!TryResolve(id, out var provider, out var photoId))
|
||||
{
|
||||
results.Add(new MediaTrashResult(id, "notFound", []));
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var uses = await provider.ListUsesAsync([photoId], cancellationToken);
|
||||
if (uses.Count > 0 && !confirmedInUse)
|
||||
{
|
||||
results.Add(new MediaTrashResult(id, "inUse", uses));
|
||||
continue;
|
||||
}
|
||||
|
||||
var detached = await provider.TrashAsync(photoId, callerId, cancellationToken);
|
||||
results.Add(new MediaTrashResult(id, detached is null ? "notFound" : "trashed", uses));
|
||||
if (detached is not null)
|
||||
{
|
||||
logger?.LogInformation("Media {Photo} moved to the trash by user {UserId} ({Uses} uses removed)", id, callerId, detached);
|
||||
}
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
// One photo failing never stops the others; the caller sees it per photo.
|
||||
logger?.LogWarning(exception, "Moving media {Photo} to the trash failed", id);
|
||||
results.Add(new MediaTrashResult(id, "failed", []));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaTrashResult>> RestoreAsync(IReadOnlyCollection<string> ids, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinLimit(ids);
|
||||
var results = new List<MediaTrashResult>();
|
||||
foreach (var id in ids.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (!TryResolve(id, out var provider, out var photoId))
|
||||
{
|
||||
results.Add(new MediaTrashResult(id, "notFound", []));
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var restored = await provider.RestoreAsync(photoId, callerId, cancellationToken);
|
||||
results.Add(new MediaTrashResult(id, restored ? "restored" : "notFound", []));
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
logger?.LogWarning(exception, "Restoring media {Photo} failed", id);
|
||||
results.Add(new MediaTrashResult(id, "failed", []));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void EnsureWithinLimit(IReadOnlyCollection<string> ids)
|
||||
{
|
||||
if (ids.Count > MaxIdsPerRequest)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(ids), $"At most {MaxIdsPerRequest} photos per request.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Splits <c>module:guid</c> and finds the provider of that module.</summary>
|
||||
private bool TryResolve(string id, out IMediaUsageProvider provider, out Guid photoId)
|
||||
{
|
||||
provider = null!;
|
||||
photoId = Guid.Empty;
|
||||
var colon = id.IndexOf(':');
|
||||
if (colon <= 0 || !Guid.TryParse(id.AsSpan(colon + 1), out photoId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var module = id[..colon];
|
||||
var found = _providers.FirstOrDefault(p => string.Equals(p.Module, module, StringComparison.Ordinal));
|
||||
if (found is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
provider = found;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,20 @@ namespace SlpModularCms.Core.Media.Library;
|
||||
/// <summary>A photo as the library shows it. The module that owns it fills it; the library only reads.</summary>
|
||||
/// <param name="Module">The module key (for example <c>hero</c>); with <paramref name="Id"/> it names the photo.</param>
|
||||
/// <param name="ThumbnailUrl">A public address of a small variant, or null while no variant exists yet.</param>
|
||||
public sealed record MediaPhoto(string Module, Guid Id, long Bytes, int Width, int Height, DateTimeOffset CreatedAt, string? ThumbnailUrl);
|
||||
/// <param name="OriginalFileName">The file name as uploaded, or null for photos uploaded before it was stored.</param>
|
||||
/// <param name="UnusedSince">The moment the photo was last detached from where it was used; null when it was never used.</param>
|
||||
/// <param name="DeletedAt">When the photo was moved to the trash; null for a photo in the library.</param>
|
||||
public sealed record MediaPhoto(
|
||||
string Module,
|
||||
Guid Id,
|
||||
long Bytes,
|
||||
int Width,
|
||||
int Height,
|
||||
DateTimeOffset CreatedAt,
|
||||
string? ThumbnailUrl,
|
||||
string? OriginalFileName = null,
|
||||
DateTimeOffset? UnusedSince = null,
|
||||
DateTimeOffset? DeletedAt = null);
|
||||
|
||||
/// <summary>One place where a photo is used. The library shows it as "module, place kind, position" (for example "Hero, slide 3").</summary>
|
||||
/// <param name="PlaceKind">What kind of place (for example <c>slide</c>); the screen translates it.</param>
|
||||
@@ -18,17 +31,29 @@ public sealed record MediaLibraryItem(MediaPhoto Photo, IReadOnlyList<MediaUse>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What a module tells the media library. Read-only: the library never changes a module's data through this contract
|
||||
/// (deleting and detaching come in a later step). The answers are computed from the module's own data, never copied.
|
||||
/// What a module tells the media library and does for it. The answers are computed from the module's own data, never copied.
|
||||
/// Deleting always means "to the trash" (recoverable for a while); the module removes the files later itself.
|
||||
/// </summary>
|
||||
public interface IMediaUsageProvider
|
||||
{
|
||||
/// <summary>The module key, lower case, equal to its media area when it has one.</summary>
|
||||
string Module { get; }
|
||||
|
||||
/// <summary>The photos of this module, newest first.</summary>
|
||||
/// <summary>The photos of this module that are not in the trash, newest first.</summary>
|
||||
Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The photos of this module in the trash, most recently trashed first.</summary>
|
||||
Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Where the given photos are used (only photos of this module).</summary>
|
||||
Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Moves a photo to the trash after detaching it from where it is used. Returns how many places lost it, or null when
|
||||
/// there is no such photo outside the trash.
|
||||
/// </summary>
|
||||
Task<int?> TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Takes a photo out of the trash. False when it is not in the trash.</summary>
|
||||
Task<bool> RestoreAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
public class HeroMediaUsageProviderTests
|
||||
{
|
||||
private static HeroMediaUsageProvider Provider(HeroTestRig rig) => new(rig.Repository, rig.Images);
|
||||
|
||||
private static Task<HeroSaveRequest> NoSlides() => Task.FromResult(Save([]));
|
||||
|
||||
private static HeroSaveRequest Save(params HeroSlideRequest[] slides) => new("Titel", null, null, "single", [], slides);
|
||||
|
||||
[Fact]
|
||||
public async Task Lists_the_images_newest_first_with_a_thumbnail_and_reports_one_use_per_slide_with_its_position()
|
||||
{
|
||||
@@ -18,10 +24,9 @@ public class HeroMediaUsageProviderTests
|
||||
rig.Time.Now = rig.Time.Now.AddMinutes(5);
|
||||
var unused = await rig.Upload(HeroTestRig.Photo(seed: 3));
|
||||
await rig.Service.SaveAsync(
|
||||
new HeroSaveRequest("Titel", null, null, "single", [],
|
||||
[new HeroSlideRequest(null, Guid.Parse(b.Id), "tweede"), new HeroSlideRequest(null, Guid.Parse(a.Id), "eerste", IsActive: false)]),
|
||||
Save(new HeroSlideRequest(null, Guid.Parse(b.Id), "tweede"), new HeroSlideRequest(null, Guid.Parse(a.Id), "eerste", IsActive: false)),
|
||||
HeroTestRig.Admin, CancellationToken.None);
|
||||
var provider = new HeroMediaUsageProvider(rig.Repository);
|
||||
var provider = Provider(rig);
|
||||
|
||||
var photos = await provider.ListPhotosAsync(CancellationToken.None);
|
||||
var uses = await provider.ListUsesAsync(photos.Select(p => p.Id).ToList(), CancellationToken.None);
|
||||
@@ -40,12 +45,150 @@ public class HeroMediaUsageProviderTests
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var image = await rig.Upload(HeroTestRig.Photo());
|
||||
var provider = new HeroMediaUsageProvider(rig.Repository);
|
||||
var provider = Provider(rig);
|
||||
|
||||
await provider.ListPhotosAsync(CancellationToken.None);
|
||||
await provider.ListUsesAsync([Guid.Parse(image.Id)], CancellationToken.None);
|
||||
await provider.ListTrashAsync(CancellationToken.None);
|
||||
|
||||
(await rig.Repository.CountImagesAsync(CancellationToken.None)).Should().Be(1);
|
||||
rig.Db.ChangeTracker.HasChanges().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task An_upload_remembers_the_cleaned_original_file_name()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var bytes = HeroTestRig.Photo();
|
||||
|
||||
await rig.Images.UploadAsync(new MemoryStream(bytes), bytes.Length, HeroTestRig.Admin, CancellationToken.None, "C:\\fotos\\zomer\u0007 2026.jpg");
|
||||
|
||||
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().OriginalFileName.Should().Be("zomer 2026.jpg");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData(" ", null)]
|
||||
[InlineData("../../etc/passwd", "passwd")]
|
||||
[InlineData("a/b\\c.jpg", "c.jpg")]
|
||||
public void File_names_are_cleaned_for_display_only(string? input, string? expected) => HeroFileNames.Clean(input).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public void A_very_long_file_name_is_cut() => HeroFileNames.Clean(new string('a', 500))!.Length.Should().Be(HeroFileNames.MaxLength);
|
||||
|
||||
[Fact]
|
||||
public async Task Trashing_a_photo_in_use_removes_its_slides_hides_it_and_remembers_the_last_use()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
|
||||
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
|
||||
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "een"), new HeroSlideRequest(null, Guid.Parse(b.Id), "twee")), HeroTestRig.Admin, CancellationToken.None);
|
||||
var provider = Provider(rig);
|
||||
rig.Time.Now = rig.Time.Now.AddDays(3);
|
||||
|
||||
var removed = await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
removed.Should().Be(1);
|
||||
(await provider.ListPhotosAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(Guid.Parse(b.Id));
|
||||
var trashed = (await provider.ListTrashAsync(CancellationToken.None)).Single();
|
||||
trashed.Id.Should().Be(Guid.Parse(a.Id));
|
||||
trashed.DeletedAt.Should().Be(rig.Time.Now);
|
||||
trashed.UnusedSince.Should().Be(rig.Time.Now);
|
||||
(await rig.Service.GetAsync(CancellationToken.None)).Slides.Should().ContainSingle().Which.Image.Id.Should().Be(b.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_trashed_photo_cannot_be_put_in_a_slide_and_is_not_cut_in_the_background()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.UploadWithoutRendering(HeroTestRig.Photo());
|
||||
var provider = Provider(rig);
|
||||
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
var save = () => rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
await save.Should().ThrowAsync<HeroSaveRejectedException>();
|
||||
(await rig.RenderPending()).Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Trashing_an_unknown_or_already_trashed_photo_gives_null()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo());
|
||||
var provider = Provider(rig);
|
||||
|
||||
(await provider.TrashAsync(Guid.NewGuid(), HeroTestRig.Admin, CancellationToken.None)).Should().BeNull();
|
||||
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
(await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Restoring_brings_the_photo_back_without_its_slides()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo());
|
||||
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
|
||||
var provider = Provider(rig);
|
||||
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
(await provider.RestoreAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeTrue();
|
||||
(await provider.RestoreAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeFalse();
|
||||
|
||||
(await provider.ListPhotosAsync(CancellationToken.None)).Should().ContainSingle();
|
||||
(await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty();
|
||||
(await provider.ListUsesAsync([Guid.Parse(a.Id)], CancellationToken.None)).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Uploading_the_same_photo_again_brings_it_back_from_the_trash()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var bytes = HeroTestRig.Photo();
|
||||
var first = await rig.Upload(bytes);
|
||||
var provider = Provider(rig);
|
||||
await provider.TrashAsync(Guid.Parse(first.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
var again = await rig.Upload(bytes);
|
||||
|
||||
again.Id.Should().Be(first.Id);
|
||||
(await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty();
|
||||
(await provider.ListPhotosAsync(CancellationToken.None)).Should().ContainSingle();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Saving_the_hero_without_a_slide_remembers_when_its_photo_was_last_used()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo());
|
||||
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
|
||||
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().UnusedSince.Should().BeNull();
|
||||
|
||||
rig.Time.Now = rig.Time.Now.AddDays(2);
|
||||
await rig.Service.SaveAsync(Save(), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().UnusedSince.Should().Be(rig.Time.Now);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_trashed_photo_is_emptied_from_the_trash_after_the_trash_period_and_its_files_go()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo());
|
||||
var row = rig.Db.HeroImages.Single();
|
||||
var (set, original) = (row.SetHash, row.OriginalKey);
|
||||
await Provider(rig).TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
|
||||
rig.Time.Now = rig.Time.Now.AddDays(6);
|
||||
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
|
||||
rig.Db.HeroImages.Should().HaveCount(1);
|
||||
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeTrue();
|
||||
|
||||
rig.Time.Now = rig.Time.Now.AddDays(2);
|
||||
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
|
||||
|
||||
rig.Db.HeroImages.Should().BeEmpty();
|
||||
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().NotContain(s => s.SetHash == set);
|
||||
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class HeroImagesController(IHeroImageService service, IOptions<MediaOptio
|
||||
try
|
||||
{
|
||||
await using var stream = file.OpenReadStream();
|
||||
var image = await service.UploadAsync(stream, file.Length, HeroCaller.Id(User), cancellationToken);
|
||||
var image = await service.UploadAsync(stream, file.Length, HeroCaller.Id(User), cancellationToken, file.FileName);
|
||||
return Created($"/api/v1/hero/images/{image.Id}", image);
|
||||
}
|
||||
catch (ImageRejectedException rejection)
|
||||
|
||||
@@ -65,6 +65,15 @@ public class HeroImage
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public Guid CreatedByUserId { get; set; }
|
||||
|
||||
/// <summary>The file name as uploaded (for display only, cleaned; never used as a path). Null for photos uploaded before it was stored.</summary>
|
||||
public string? OriginalFileName { get; set; }
|
||||
|
||||
/// <summary>The moment the photo was last detached from a slide; null while it was never used. The clean-up counts from here (decision 8).</summary>
|
||||
public DateTimeOffset? LastUsedAt { get; set; }
|
||||
|
||||
/// <summary>Set when the photo was moved to the trash; its files stay for <c>Media:TrashDays</c> and it is hidden from the hero.</summary>
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
|
||||
public FocusSetting General => new FocusSetting(FocusX, FocusY, Zoom).Normalize();
|
||||
|
||||
public FocusSetting FocusFor(string ratio) => ratio switch
|
||||
|
||||
@@ -60,6 +60,7 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(
|
||||
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
|
||||
entity.Property(e => e.CreatedAt).IsRequired();
|
||||
entity.Property(e => e.CreatedByUserId).IsRequired();
|
||||
entity.Property(e => e.OriginalFileName).HasMaxLength(200);
|
||||
entity.Ignore(e => e.General);
|
||||
|
||||
// The variants of the current set: a small list that is always replaced as a whole.
|
||||
|
||||
Generated
+257
@@ -0,0 +1,257 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
[DbContext(typeof(HeroDbContext))]
|
||||
[Migration("20261117140509_AddMediaLibraryColumns")]
|
||||
partial class AddMediaLibraryColumns
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.9")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Button1Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button1Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Button2Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button2Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Display")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<string>("Language")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<Guid>("LastModifiedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Lead")
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<string>("Tagline")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<DateTimeOffset>("UpdatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Language")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Heroes", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<long>("Bytes")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<Guid>("CreatedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<double>("FocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("FocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("OriginalFileName")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalKey")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalSha256")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<string>("SourceFormat")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<double?>("TallFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<string>("Variants")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<double?>("WideFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<int>("Width")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Zoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("HeroImages", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Kind")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<DateTimeOffset>("RetiredAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("HeroRetiredFiles", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Alt")
|
||||
.IsRequired()
|
||||
.HasMaxLength(600)
|
||||
.HasColumnType("varchar(600)");
|
||||
|
||||
b.Property<bool>("Decorative")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<Guid>("HeroId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("ImageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<int>("SortOrder")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ImageId");
|
||||
|
||||
b.HasIndex("HeroId", "SortOrder");
|
||||
|
||||
b.ToTable("HeroSlides", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
|
||||
.WithMany("Slides")
|
||||
.HasForeignKey("HeroId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
|
||||
.WithMany()
|
||||
.HasForeignKey("ImageId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Image");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Navigation("Slides");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddMediaLibraryColumns : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<DateTimeOffset>(
|
||||
name: "DeletedAt",
|
||||
table: "HeroImages",
|
||||
type: "datetime",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<DateTimeOffset>(
|
||||
name: "LastUsedAt",
|
||||
table: "HeroImages",
|
||||
type: "datetime",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "OriginalFileName",
|
||||
table: "HeroImages",
|
||||
type: "varchar(200)",
|
||||
maxLength: 200,
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "DeletedAt",
|
||||
table: "HeroImages");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "LastUsedAt",
|
||||
table: "HeroImages");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "OriginalFileName",
|
||||
table: "HeroImages");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,9 @@ namespace SlpModularCms.Modules.Hero.Migrations
|
||||
b.Property<Guid>("CreatedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<double>("FocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
@@ -102,6 +105,13 @@ namespace SlpModularCms.Modules.Hero.Migrations
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("OriginalFileName")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalKey")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
|
||||
@@ -22,6 +22,18 @@ public interface IHeroRepository
|
||||
/// <summary>Images whose set is missing or does not match their focus, oldest first (tracked).</summary>
|
||||
Task<int> CountImagesAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>An image whether or not it is in the trash, tracked (restore, trash).</summary>
|
||||
Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The photos in the trash, most recently trashed first, not tracked.</summary>
|
||||
Task<IReadOnlyList<HeroImage>> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Photos that were trashed before the cutoff, tracked (the trash is emptied for them).</summary>
|
||||
Task<IReadOnlyList<HeroImage>> GetTrashedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The slides that use an image, tracked (they are removed when the image goes to the trash).</summary>
|
||||
Task<IReadOnlyList<HeroSlide>> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>All images, newest first, not tracked (the media library lists them).</summary>
|
||||
Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken);
|
||||
|
||||
@@ -70,32 +82,44 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository
|
||||
public void RemoveSlide(HeroSlide slide) => context.HeroSlides.Remove(slide);
|
||||
|
||||
public Task<HeroImage?> GetImageAsync(Guid id, CancellationToken cancellationToken)
|
||||
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
|
||||
|
||||
public Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken)
|
||||
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).OrderByDescending(i => i.DeletedAt).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetTrashedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).Where(i => i.DeletedAt < cutoff).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroSlide>> GetSlidesOfImageAsync(Guid imageId, CancellationToken cancellationToken)
|
||||
=> await context.HeroSlides.Where(s => s.ImageId == imageId).ToListAsync(cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetImagesAsync(IReadOnlyCollection<Guid> ids, CancellationToken cancellationToken)
|
||||
{
|
||||
// The MySQL provider cannot map a Guid list parameter in an IN clause; there are only a few dozen images, so they
|
||||
// are read and filtered here.
|
||||
var wanted = ids.ToHashSet();
|
||||
return (await context.HeroImages.ToListAsync(cancellationToken)).Where(i => wanted.Contains(i.Id)).ToList();
|
||||
return (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(i => wanted.Contains(i.Id)).ToList();
|
||||
}
|
||||
|
||||
public Task<int> CountImagesAsync(CancellationToken cancellationToken) => context.HeroImages.CountAsync(cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).OrderByDescending(i => i.CreatedAt).ToList();
|
||||
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).OrderByDescending(i => i.CreatedAt).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroSlide>> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> await context.HeroSlides.AsNoTracking().ToListAsync(cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
|
||||
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
|
||||
=> (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
|
||||
|
||||
public Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
|
||||
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
|
||||
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken);
|
||||
|
||||
public Task<HeroImage?> GetImageBySha256Async(string sha256, CancellationToken cancellationToken)
|
||||
=> context.HeroImages.FirstOrDefaultAsync(i => i.OriginalSha256 == sha256, cancellationToken);
|
||||
@@ -110,7 +134,7 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository
|
||||
public async Task<IReadOnlyList<HeroImage>> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
|
||||
{
|
||||
var used = context.HeroSlides.Select(s => s.ImageId);
|
||||
var candidates = await context.HeroImages.Where(i => !used.Contains(i.Id)).ToListAsync(cancellationToken);
|
||||
var candidates = await context.HeroImages.Where(i => !used.Contains(i.Id) && i.DeletedAt == null).ToListAsync(cancellationToken);
|
||||
return candidates.Where(i => i.CreatedAt < cutoff).ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public interface IHeroImageService
|
||||
/// image. Until its set exists the image has no variants and is not published.
|
||||
/// </summary>
|
||||
/// <exception cref="ImageRejectedException">The upload is refused; see the code.</exception>
|
||||
Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken);
|
||||
Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null);
|
||||
|
||||
/// <summary>How many images exist, how many wait for their variants and how many only lack the full-frame preview (for the log).</summary>
|
||||
Task<(int Total, int NeedRender, int NeedPreview)> GetRenderBacklogAsync(CancellationToken cancellationToken);
|
||||
@@ -67,6 +67,16 @@ public interface IHeroImageService
|
||||
|
||||
/// <summary>Removes an image nothing uses; its files go after the retention time.</summary>
|
||||
Task DeleteAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Moves an image to the trash: the slides that use it are removed from the hero (and remember the moment as its last use),
|
||||
/// it disappears from the hero and its files stay for the trash period. Returns how many slides were removed.
|
||||
/// </summary>
|
||||
/// <exception cref="HeroImageNotFoundException">There is no such image outside the trash.</exception>
|
||||
Task<int> TrashAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Takes an image out of the trash again. False when it is not in the trash.</summary>
|
||||
Task<bool> RestoreAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class HeroImageService(
|
||||
@@ -83,7 +93,7 @@ public class HeroImageService(
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
|
||||
public async Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken)
|
||||
public async Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null)
|
||||
{
|
||||
using var admitted = await guard.AdmitAsync(upload, declaredLength, cancellationToken);
|
||||
var sha = Convert.ToHexStringLower(SHA256.HashData(admitted.Bytes));
|
||||
@@ -91,8 +101,18 @@ public class HeroImageService(
|
||||
// The same photo again is the same image. When its files are gone (a lost or restored media directory) the upload
|
||||
// repairs them from the file that was just sent.
|
||||
var existing = await repository.GetImageBySha256Async(sha, cancellationToken);
|
||||
var fileName = HeroFileNames.Clean(originalFileName);
|
||||
|
||||
// The same photo uploaded again also brings it back from the trash.
|
||||
if (existing is not null)
|
||||
{
|
||||
existing.DeletedAt = null;
|
||||
existing.OriginalFileName ??= fileName;
|
||||
}
|
||||
|
||||
if (existing is not null && await FilesExistAsync(existing, cancellationToken))
|
||||
{
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
// The same photo again also gives up the failed state: the renderer tries it again when it is still pending.
|
||||
signal?.Reset(existing.Id);
|
||||
signal?.Notify();
|
||||
@@ -118,6 +138,7 @@ public class HeroImageService(
|
||||
Bytes = original.Length,
|
||||
CreatedAt = _time.GetUtcNow(),
|
||||
CreatedByUserId = callerId,
|
||||
OriginalFileName = fileName,
|
||||
};
|
||||
|
||||
// The files first, the row last: an aborted request leaves at most an original nobody refers to (the clean-up
|
||||
@@ -323,6 +344,47 @@ public class HeroImageService(
|
||||
logger.LogInformation("Hero image {ImageId} removed by user {UserId}", imageId, callerId);
|
||||
}
|
||||
|
||||
public async Task<int> TrashAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
var image = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
|
||||
var now = _time.GetUtcNow();
|
||||
|
||||
// A slide cannot exist without its image, so the slides that use it go with it; the moment counts as its last use.
|
||||
var slides = await repository.GetSlidesOfImageAsync(imageId, cancellationToken);
|
||||
foreach (var slide in slides)
|
||||
{
|
||||
repository.RemoveSlide(slide);
|
||||
}
|
||||
|
||||
if (slides.Count > 0)
|
||||
{
|
||||
image.LastUsedAt = now;
|
||||
}
|
||||
|
||||
image.DeletedAt = now;
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
indexCache?.Invalidate();
|
||||
|
||||
logger.LogInformation("Hero image {ImageId} moved to the trash by user {UserId} ({Slides} slides removed)", imageId, callerId, slides.Count);
|
||||
return slides.Count;
|
||||
}
|
||||
|
||||
public async Task<bool> RestoreAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
var image = await repository.GetImageIncludingTrashedAsync(imageId, cancellationToken);
|
||||
if (image is null || image.DeletedAt is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
image.DeletedAt = null;
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
signal?.Notify();
|
||||
|
||||
logger.LogInformation("Hero image {ImageId} restored from the trash by user {UserId}", imageId, callerId);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void Apply(FocusDto? value, Action<(double?, double?, double?)> assign)
|
||||
{
|
||||
if (value is null)
|
||||
@@ -378,3 +440,26 @@ public class HeroImageService(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The file name of an upload as it is shown in the library: cleaned, for display only, never part of a path.</summary>
|
||||
public static class HeroFileNames
|
||||
{
|
||||
public const int MaxLength = 200;
|
||||
|
||||
public static string? Clean(string? name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var last = name[(name.LastIndexOfAny(['/', '\\']) + 1)..];
|
||||
var cleaned = new string(last.Where(c => !char.IsControl(c)).ToArray()).Trim();
|
||||
if (cleaned.Length > MaxLength)
|
||||
{
|
||||
cleaned = cleaned[..MaxLength];
|
||||
}
|
||||
|
||||
return cleaned.Length == 0 ? null : cleaned;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,15 @@ public class HeroMediaCleanup(
|
||||
repository.RemoveImage(image);
|
||||
}
|
||||
|
||||
// 1b. Photos that have been in the trash for the trash period: their files go now (they are retired, immediately due).
|
||||
var trashCutoff = now.AddDays(-Math.Max(0, options.Value.TrashDays));
|
||||
foreach (var image in await repository.GetTrashedBeforeAsync(trashCutoff, cancellationToken))
|
||||
{
|
||||
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) });
|
||||
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) });
|
||||
repository.RemoveImage(image);
|
||||
}
|
||||
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 2. Retired files whose retention has passed, unless an image uses them again.
|
||||
|
||||
@@ -7,21 +7,19 @@ namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the
|
||||
/// hero). Read-only: nothing of the hero changes through this class.
|
||||
/// hero), and moves them to the trash and back for it. The files are removed by <see cref="HeroMediaCleanup"/> after the trash period.
|
||||
/// </summary>
|
||||
public class HeroMediaUsageProvider(IHeroRepository repository) : IMediaUsageProvider
|
||||
public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider
|
||||
{
|
||||
public const string SlideKind = "slide";
|
||||
|
||||
public string Module => HeroMapping.Area;
|
||||
|
||||
public async Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var images = await repository.GetAllImagesReadOnlyAsync(cancellationToken);
|
||||
return images
|
||||
.Select(i => new MediaPhoto(Module, i.Id, i.Bytes, i.Width, i.Height, i.CreatedAt, Thumbnail(i)))
|
||||
.ToList();
|
||||
}
|
||||
=> (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken)
|
||||
=> (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -46,6 +44,24 @@ public class HeroMediaUsageProvider(IHeroRepository repository) : IMediaUsagePro
|
||||
return uses;
|
||||
}
|
||||
|
||||
public async Task<int?> TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await images.TrashAsync(photoId, callerId, cancellationToken);
|
||||
}
|
||||
catch (HeroImageNotFoundException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<bool> RestoreAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken)
|
||||
=> images.RestoreAsync(photoId, callerId, cancellationToken);
|
||||
|
||||
private MediaPhoto ToPhoto(HeroImage i)
|
||||
=> new(Module, i.Id, i.Bytes, i.Width, i.Height, i.CreatedAt, Thumbnail(i), i.OriginalFileName, i.LastUsedAt, i.DeletedAt);
|
||||
|
||||
/// <summary>The smallest published WebP of the set; null while the set is not cut yet.</summary>
|
||||
private static string? Thumbnail(HeroImage image)
|
||||
{
|
||||
|
||||
@@ -121,8 +121,15 @@ public class HeroService(
|
||||
slide.SortOrder = i;
|
||||
}
|
||||
|
||||
// A photo that no slide uses any more remembers when it was last used (the media library counts from there).
|
||||
var stillUsed = slideRequests.Select(r => r.ImageId).ToHashSet();
|
||||
foreach (var removed in hero.Slides.Where(s => !keep.Contains(s.Id)).ToList())
|
||||
{
|
||||
if (removed.Image is { } detached && !stillUsed.Contains(detached.Id))
|
||||
{
|
||||
detached.LastUsedAt = now;
|
||||
}
|
||||
|
||||
hero.Slides.Remove(removed);
|
||||
if (!created)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user