@kidecms/core 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,13 @@ import DocumentActions from "../../../admin/components/DocumentActions";
6
6
  import AiGenerateButton from "../../../admin/components/AiGenerateButton";
7
7
  import FocalPointSelector from "../../../admin/components/FocalPointSelector";
8
8
  import UnsavedGuard from "../../../admin/components/UnsavedGuard";
9
+ import ReverseReferencesList from "../../../admin/views/edit/ReverseReferencesList.astro";
9
10
  import TreeSelect, { flattenByParent } from "../../../admin/components/TreeSelect";
10
11
  import { buttonVariants } from "../../../admin/components/ui/button";
11
12
  import { Label } from "../../../admin/components/ui/label";
12
13
  import { Textarea } from "../../../admin/components/ui/textarea";
13
14
  import config from "virtual:kide/config";
14
- import { assets, folders, isAiEnabled } from "virtual:kide/runtime";
15
+ import { assets, findAssetUsage, folders, isAiEnabled } from "virtual:kide/runtime";
15
16
  import { formatDate, initDateFormat, DEFAULT_PRESETS } from "../../../core";
16
17
  import AdminLayout from "../../../admin/layouts/AdminLayout.astro";
17
18
 
@@ -43,6 +44,9 @@ for (const [name, preset] of Object.entries(mergedPresets)) {
43
44
  seenAspects.add(preset.aspect);
44
45
  cropPresets.push({ name, aspect: preset.aspect });
45
46
  }
47
+ const { refs: usage, incomplete: usageIncomplete } = await findAssetUsage(config, asset.storagePath);
48
+ const usageCount = usage.reduce((sum, ref) => sum + ref.docs.length, 0);
49
+
46
50
  const currentFolder = asset.folder ? await folders.findById(asset.folder) : null;
47
51
  const allFolders = await folders.findAll();
48
52
 
@@ -84,7 +88,14 @@ const folderItems = flattenByParent(
84
88
  <button type="submit" form="asset-form" value="save" disabled class={buttonVariants({ variant: "outline" })}>
85
89
  Save
86
90
  </button>
87
- <DocumentActions client:load formId="asset-form" showDelete={true} />
91
+ <DocumentActions
92
+ client:load
93
+ formId="asset-form"
94
+ showDelete={true}
95
+ entityLabel="asset"
96
+ referencesEndpoint={`/api/cms/asset-usage?id=${asset._id}`}
97
+ forceOnConfirm={true}
98
+ />
88
99
  </div>
89
100
 
90
101
  <UnsavedGuard client:load formId="asset-form" isNew={false} isDraft={false} />
@@ -109,41 +120,6 @@ const folderItems = flattenByParent(
109
120
  </div>
110
121
 
111
122
  <div class="space-y-6 text-sm [&_input]:text-sm [&_textarea]:text-sm">
112
- <AdminCard title="Info">
113
- <dl class="grid gap-3">
114
- <div>
115
- <dt class="text-muted-foreground text-xs">Filename</dt>
116
- <dd class="mt-0.5">{asset.filename}</dd>
117
- </div>
118
- <div>
119
- <dt class="text-muted-foreground text-xs">Type</dt>
120
- <dd class="mt-0.5">{asset.mimeType}</dd>
121
- </div>
122
- <div>
123
- <dt class="text-muted-foreground text-xs">Size</dt>
124
- <dd class="mt-0.5">{formatSize(asset.size)}</dd>
125
- </div>
126
- {
127
- asset.width && asset.height && (
128
- <div>
129
- <dt class="text-muted-foreground text-xs">Dimensions</dt>
130
- <dd class="mt-0.5">
131
- {asset.width} &times; {asset.height} px
132
- </dd>
133
- </div>
134
- )
135
- }
136
- <div>
137
- <dt class="text-muted-foreground text-xs">Path</dt>
138
- <dd class="mt-0.5 font-mono text-xs break-all">{asset.storagePath}</dd>
139
- </div>
140
- <div>
141
- <dt class="text-muted-foreground text-xs">Created</dt>
142
- <dd class="mt-0.5">{formatDate(asset._createdAt)}</dd>
143
- </div>
144
- </dl>
145
- </AdminCard>
146
-
147
123
  <AdminCard title="Details">
148
124
  <div class="grid gap-5">
149
125
  <div class="grid gap-2">
@@ -182,6 +158,55 @@ const folderItems = flattenByParent(
182
158
  </div>
183
159
  </div>
184
160
  </AdminCard>
161
+
162
+ <AdminCard title="Info">
163
+ <dl class="grid gap-3">
164
+ <div>
165
+ <dt class="text-muted-foreground text-xs">Filename</dt>
166
+ <dd class="mt-0.5">{asset.filename}</dd>
167
+ </div>
168
+ <div>
169
+ <dt class="text-muted-foreground text-xs">Type</dt>
170
+ <dd class="mt-0.5">{asset.mimeType}</dd>
171
+ </div>
172
+ <div>
173
+ <dt class="text-muted-foreground text-xs">Size</dt>
174
+ <dd class="mt-0.5">{formatSize(asset.size)}</dd>
175
+ </div>
176
+ {
177
+ asset.width && asset.height && (
178
+ <div>
179
+ <dt class="text-muted-foreground text-xs">Dimensions</dt>
180
+ <dd class="mt-0.5">
181
+ {asset.width} &times; {asset.height} px
182
+ </dd>
183
+ </div>
184
+ )
185
+ }
186
+ <div>
187
+ <dt class="text-muted-foreground text-xs">Path</dt>
188
+ <dd class="mt-0.5 font-mono text-xs break-all">{asset.storagePath}</dd>
189
+ </div>
190
+ <div>
191
+ <dt class="text-muted-foreground text-xs">Created</dt>
192
+ <dd class="mt-0.5">{formatDate(asset._createdAt)}</dd>
193
+ </div>
194
+ </dl>
195
+ </AdminCard>
196
+
197
+ {
198
+ usageCount > 0 ? (
199
+ <ReverseReferencesList reverseRefs={usage} variant="record" title="Used in" />
200
+ ) : (
201
+ <AdminCard title="Used in">
202
+ <p class="text-muted-foreground text-sm">
203
+ {usageIncomplete.length > 0
204
+ ? `Could not check ${usageIncomplete.join(", ")} — usage is unknown.`
205
+ : "Not used by any document."}
206
+ </p>
207
+ </AdminCard>
208
+ )
209
+ }
185
210
  </div>
186
211
  </div>
187
212
  </form>
@@ -0,0 +1,53 @@
1
+ import type { APIRoute } from "astro";
2
+ import config from "virtual:kide/config";
3
+ import { assets, countAssetUsage, findAssetUsage } from "virtual:kide/runtime";
4
+
5
+ export const prerender = false;
6
+
7
+ /**
8
+ * Where assets are used. Deliberately not under /api/cms/assets/… — a static
9
+ * segment there is shadowed by the [id] route whenever it is not registered,
10
+ * which turns a missing route into a plausible-looking "Not found" for an asset.
11
+ *
12
+ * `?id=<assetId>` → { usage, total, incomplete } — the detail list
13
+ * `?ids=<id>,<id>,…` → { counts, incomplete } — for delete confirmations
14
+ *
15
+ * `incomplete` lists collections that could not be searched; a caller must not
16
+ * read an empty result as "unused" while it is non-empty.
17
+ */
18
+ export const GET: APIRoute = async ({ url }) => {
19
+ const id = url.searchParams.get("id");
20
+ const ids = url.searchParams.get("ids");
21
+
22
+ if (id) {
23
+ const asset = await assets.findById(id);
24
+ if (!asset) return Response.json({ error: "Not found." }, { status: 404 });
25
+
26
+ const { refs, incomplete } = await findAssetUsage(config, asset.storagePath);
27
+ const total = refs.reduce((sum, ref) => sum + ref.docs.length, 0);
28
+ return Response.json({ usage: refs, total, incomplete });
29
+ }
30
+
31
+ if (ids) {
32
+ const requested = ids
33
+ .split(",")
34
+ .map((value) => value.trim())
35
+ .filter(Boolean)
36
+ .slice(0, 200);
37
+
38
+ const records = (await Promise.all(requested.map((assetId) => assets.findById(assetId)))).filter(
39
+ (asset): asset is NonNullable<typeof asset> => !!asset,
40
+ );
41
+
42
+ const { counts: byPath, incomplete } = await countAssetUsage(
43
+ config,
44
+ records.map((asset) => asset.storagePath),
45
+ );
46
+ const counts: Record<string, number> = {};
47
+ for (const asset of records) counts[asset._id] = byPath[asset.storagePath] ?? 0;
48
+
49
+ return Response.json({ counts, incomplete });
50
+ }
51
+
52
+ return Response.json({ error: "Pass ?id= or ?ids=." }, { status: 400 });
53
+ };
@@ -1,5 +1,6 @@
1
1
  import type { APIRoute } from "astro";
2
- import { assets } from "virtual:kide/runtime";
2
+ import config from "virtual:kide/config";
3
+ import { AssetInUseError, assets } from "virtual:kide/runtime";
3
4
 
4
5
  export const prerender = false;
5
6
 
@@ -42,11 +43,21 @@ export const PATCH: APIRoute = async ({ params, request, locals }) => {
42
43
  return Response.json(result);
43
44
  };
44
45
 
45
- export const DELETE: APIRoute = async ({ params, locals }) => {
46
+ export const DELETE: APIRoute = async ({ params, request, locals }) => {
46
47
  const id = params.id;
47
48
  if (!id) return new Response(null, { status: 400 });
48
49
 
49
- await assets.delete(id, { actor: getActor(locals) });
50
+ const force = new URL(request.url).searchParams.get("force") === "1";
51
+
52
+ try {
53
+ await assets.delete(id, { actor: getActor(locals), force, config });
54
+ } catch (error) {
55
+ if (error instanceof AssetInUseError) {
56
+ return Response.json({ error: error.message, usage: error.usage, incomplete: error.incomplete }, { status: 409 });
57
+ }
58
+ throw error;
59
+ }
60
+
50
61
  return new Response(null, { status: 204 });
51
62
  };
52
63
 
@@ -60,7 +71,17 @@ export const POST: APIRoute = async ({ params, request, locals }) => {
60
71
  const actor = getActor(locals);
61
72
 
62
73
  if (method === "DELETE" || action === "delete") {
63
- await assets.delete(id, { actor });
74
+ try {
75
+ await assets.delete(id, { actor, force: formData.get("_force") === "1", config });
76
+ } catch (error) {
77
+ if (error instanceof AssetInUseError) {
78
+ return new Response(null, {
79
+ status: 303,
80
+ headers: { Location: `/admin/assets/${id}?_toast=error&_msg=${encodeURIComponent(error.message)}` },
81
+ });
82
+ }
83
+ throw error;
84
+ }
64
85
  return new Response(null, {
65
86
  status: 303,
66
87
  headers: { Location: "/admin/assets?_toast=success&_msg=Asset+deleted" },
@@ -27,7 +27,7 @@ export const GET: APIRoute = async ({ params, url }) => {
27
27
  // and uploads live in object storage rather than public/ — so transformImage
28
28
  // can't run. Attempt it, but fall back to streaming the untransformed original
29
29
  // from storage so images still load. (In production on Cloudflare this route
30
- // is bypassed entirely: cmsImage() emits /cdn-cgi/image URLs there.)
30
+ // is bypassed entirely: cmsImageUrl() emits /cdn-cgi/image URLs there.)
31
31
  try {
32
32
  const result = await transformImage(src, {
33
33
  width: num("w"),