@antelopejs/dms-media 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/dist/bindings.d.ts +4 -1
- package/dist/bindings.js +21 -0
- package/dist/bindings.js.map +1 -1
- package/dist/db/models/index.d.ts +1 -0
- package/dist/db/models/index.js +1 -0
- package/dist/db/models/index.js.map +1 -1
- package/dist/db/models/mediaAssets.model.d.ts +1 -0
- package/dist/db/models/mediaAssets.model.js +19 -0
- package/dist/db/models/mediaAssets.model.js.map +1 -1
- package/dist/db/models/mediaDeclarations.model.d.ts +24 -0
- package/dist/db/models/mediaDeclarations.model.js +13 -0
- package/dist/db/models/mediaDeclarations.model.js.map +1 -0
- package/dist/db/models/mediaFolders.model.d.ts +3 -0
- package/dist/db/models/mediaFolders.model.js +29 -0
- package/dist/db/models/mediaFolders.model.js.map +1 -1
- package/dist/db/tables/index.d.ts +1 -0
- package/dist/db/tables/index.js +1 -0
- package/dist/db/tables/index.js.map +1 -1
- package/dist/db/tables/mediaDeclarations.table.d.ts +15 -0
- package/dist/db/tables/mediaDeclarations.table.js +65 -0
- package/dist/db/tables/mediaDeclarations.table.js.map +1 -0
- package/dist/declared/assets.d.ts +4 -0
- package/dist/declared/assets.js +269 -0
- package/dist/declared/assets.js.map +1 -0
- package/dist/declared/folders.d.ts +10 -0
- package/dist/declared/folders.js +154 -0
- package/dist/declared/folders.js.map +1 -0
- package/dist/declared/index.d.ts +5 -0
- package/dist/declared/index.js +22 -0
- package/dist/declared/index.js.map +1 -0
- package/dist/declared/readiness.d.ts +2 -0
- package/dist/declared/readiness.js +52 -0
- package/dist/declared/readiness.js.map +1 -0
- package/dist/declared/records.d.ts +46 -0
- package/dist/declared/records.js +94 -0
- package/dist/declared/records.js.map +1 -0
- package/dist/declared/retire.d.ts +3 -0
- package/dist/declared/retire.js +118 -0
- package/dist/declared/retire.js.map +1 -0
- package/dist/implementations/interface-dms-media/index.d.ts +5 -0
- package/dist/implementations/interface-dms-media/index.js +5 -0
- package/dist/implementations/interface-dms-media/index.js.map +1 -1
- package/dist/library/events.d.ts +2 -1
- package/dist/library/events.js.map +1 -1
- package/dist/routes/media/assets.js +19 -4
- package/dist/routes/media/assets.js.map +1 -1
- package/dist/routes/media/context.d.ts +1 -1
- package/dist/routes/media/context.js.map +1 -1
- package/dist/routes/media/dto.d.ts +1 -0
- package/dist/routes/media/dto.js +3 -1
- package/dist/routes/media/dto.js.map +1 -1
- package/dist/routes/media/folders.d.ts +6 -0
- package/dist/routes/media/folders.js +25 -13
- package/dist/routes/media/folders.js.map +1 -1
- package/dist/routes/media/upload.d.ts +1 -1
- package/dist/routes/media/upload.js +26 -58
- package/dist/routes/media/upload.js.map +1 -1
- package/dist/test/integration/media-declared-content.test.d.ts +1 -0
- package/dist/test/integration/media-declared-content.test.js +705 -0
- package/dist/test/integration/media-declared-content.test.js.map +1 -0
- package/dist/test/integration/media-declared-retire.test.d.ts +1 -0
- package/dist/test/integration/media-declared-retire.test.js +186 -0
- package/dist/test/integration/media-declared-retire.test.js.map +1 -0
- package/dist/test/integration/media-delivery-editing.test.js +26 -0
- package/dist/test/integration/media-delivery-editing.test.js.map +1 -1
- package/dist/test/integration/media-linked-subfolders.test.d.ts +1 -0
- package/dist/test/integration/media-linked-subfolders.test.js +192 -0
- package/dist/test/integration/media-linked-subfolders.test.js.map +1 -0
- package/dist/test/unit/declared.test.d.ts +1 -0
- package/dist/test/unit/declared.test.js +118 -0
- package/dist/test/unit/declared.test.js.map +1 -0
- package/dist/upload-checks.d.ts +18 -0
- package/dist/upload-checks.js +75 -0
- package/dist/upload-checks.js.map +1 -0
- package/dist/validation/declared.schema.d.ts +187 -0
- package/dist/validation/declared.schema.js +78 -0
- package/dist/validation/declared.schema.js.map +1 -0
- package/dist/validation/media.schema.d.ts +3 -0
- package/dist/validation/media.schema.js +12 -12
- package/dist/validation/media.schema.js.map +1 -1
- package/frontend-vue/app/components/dialogs/DestinationDialog.vue +1 -1
- package/frontend-vue/app/components/dialogs/MoveDialog.vue +2 -2
- package/frontend-vue/app/components/explorer/ExplorerContent.vue +1 -1
- package/frontend-vue/app/components/explorer/ExplorerToolbar.vue +1 -1
- package/frontend-vue/app/components/shared/AssetThumb.vue +3 -1
- package/frontend-vue/app/composables/library/explorerMenus.ts +1 -1
- package/frontend-vue/app/composables/library/useExplorerCommands.ts +1 -1
- package/frontend-vue/app/composables/library/useExplorerDrop.ts +1 -1
- package/frontend-vue/app/types/media.ts +3 -1
- package/frontend-vue/tests/serverRender.test.ts +2 -1
- package/package.json +2 -2
- package/skills/dms-media/SKILL.md +50 -4
- package/src/bindings.ts +28 -0
- package/src/db/models/index.ts +1 -0
- package/src/db/models/mediaAssets.model.ts +25 -0
- package/src/db/models/mediaDeclarations.model.ts +19 -0
- package/src/db/models/mediaFolders.model.ts +44 -0
- package/src/db/tables/index.ts +1 -0
- package/src/db/tables/mediaDeclarations.table.ts +59 -0
- package/src/declared/assets.ts +446 -0
- package/src/declared/folders.ts +273 -0
- package/src/declared/index.ts +5 -0
- package/src/declared/readiness.ts +64 -0
- package/src/declared/records.ts +185 -0
- package/src/declared/retire.ts +206 -0
- package/src/implementations/interface-dms-media/index.ts +10 -0
- package/src/library/events.ts +4 -1
- package/src/routes/media/assets.ts +34 -5
- package/src/routes/media/context.ts +1 -1
- package/src/routes/media/dto.ts +6 -2
- package/src/routes/media/folders.ts +47 -21
- package/src/routes/media/upload.ts +50 -121
- package/src/test/integration/media-declared-content.test.ts +844 -0
- package/src/test/integration/media-declared-retire.test.ts +257 -0
- package/src/test/integration/media-delivery-editing.test.ts +45 -0
- package/src/test/integration/media-linked-subfolders.test.ts +256 -0
- package/src/test/unit/declared.test.ts +158 -0
- package/src/upload-checks.ts +126 -0
- package/src/validation/declared.schema.ts +104 -0
- package/src/validation/media.schema.ts +7 -6
- package/frontend-vue/pnpm-workspace.yaml +0 -2
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
import {
|
|
4
|
+
hashState,
|
|
5
|
+
judgeDeclaredRow,
|
|
6
|
+
judgeMemberChange,
|
|
7
|
+
MediaDeclarationError,
|
|
8
|
+
parseDeclaration,
|
|
9
|
+
withDeclarationLock,
|
|
10
|
+
} from "../../declared/records";
|
|
11
|
+
import { writeWhenReachable } from "../../declared/readiness";
|
|
12
|
+
import type { MediaDeclaration } from "../../db";
|
|
13
|
+
import { declaredFolderSchema } from "../../validation/declared.schema";
|
|
14
|
+
|
|
15
|
+
const ROW = { _id: "row-1" };
|
|
16
|
+
const APPLIED = hashState({ name: "Procedures" });
|
|
17
|
+
const DECLARED = hashState({ declared: 1 });
|
|
18
|
+
|
|
19
|
+
function record(): MediaDeclaration {
|
|
20
|
+
return {
|
|
21
|
+
_id: "declaration-1",
|
|
22
|
+
kind: "folder",
|
|
23
|
+
key: "helio.procedures",
|
|
24
|
+
targetId: ROW._id,
|
|
25
|
+
declaredHash: DECLARED,
|
|
26
|
+
appliedHash: APPLIED,
|
|
27
|
+
} as MediaDeclaration;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("[unit] declared content — the refresh rule", () => {
|
|
31
|
+
it("keeps a deleted row deleted", () => {
|
|
32
|
+
expect(
|
|
33
|
+
judgeDeclaredRow(record(), undefined, undefined, DECLARED),
|
|
34
|
+
).to.deep.equal({ status: "deleted" });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("preserves a row whose state moved away from the applied one", () => {
|
|
38
|
+
const edited = hashState({ name: "Our procedures" });
|
|
39
|
+
expect(
|
|
40
|
+
judgeDeclaredRow(record(), ROW, edited, hashState({ declared: 2 })),
|
|
41
|
+
).to.deep.equal({ status: "preserved", id: ROW._id });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("reports an untouched row declared the same way as unchanged", () => {
|
|
45
|
+
expect(judgeDeclaredRow(record(), ROW, APPLIED, DECLARED)).to.deep.equal({
|
|
46
|
+
status: "unchanged",
|
|
47
|
+
id: ROW._id,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("lets an untouched row with a new declaration be refreshed", () => {
|
|
52
|
+
expect(
|
|
53
|
+
judgeDeclaredRow(record(), ROW, APPLIED, hashState({ declared: 2 })),
|
|
54
|
+
).to.equal(undefined);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("judges a member's change without a new declaration", () => {
|
|
58
|
+
expect(judgeMemberChange(record(), undefined, undefined)).to.deep.equal({
|
|
59
|
+
status: "deleted",
|
|
60
|
+
});
|
|
61
|
+
expect(
|
|
62
|
+
judgeMemberChange(record(), ROW, hashState({ name: "Ours" })),
|
|
63
|
+
).to.deep.equal({ status: "preserved", id: ROW._id });
|
|
64
|
+
expect(judgeMemberChange(record(), ROW, APPLIED)).to.equal(undefined);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("hashes equal states equally and different states differently", () => {
|
|
68
|
+
expect(hashState({ a: 1, b: null })).to.equal(hashState({ a: 1, b: null }));
|
|
69
|
+
expect(hashState({ a: 1 })).to.not.equal(hashState({ a: 2 }));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("[unit] declared content — declarations", () => {
|
|
74
|
+
it("keeps an explicit default tenant and defaults the visibility", () => {
|
|
75
|
+
const parsed = parseDeclaration(
|
|
76
|
+
"folder",
|
|
77
|
+
{ tenantId: "default", key: "helio.procedures", name: " Procedures " },
|
|
78
|
+
declaredFolderSchema,
|
|
79
|
+
);
|
|
80
|
+
expect(parsed).to.deep.equal({
|
|
81
|
+
key: "helio.procedures",
|
|
82
|
+
name: "Procedures",
|
|
83
|
+
visibility: "private",
|
|
84
|
+
tenantId: "default",
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("names the key and every refused field in English", () => {
|
|
89
|
+
const parse = () =>
|
|
90
|
+
parseDeclaration(
|
|
91
|
+
"folder",
|
|
92
|
+
{
|
|
93
|
+
tenantId: "default",
|
|
94
|
+
key: "helio.bad",
|
|
95
|
+
name: "a/b",
|
|
96
|
+
visibility: "secret",
|
|
97
|
+
},
|
|
98
|
+
declaredFolderSchema,
|
|
99
|
+
);
|
|
100
|
+
expect(parse).to.throw(MediaDeclarationError, "Media folder 'helio.bad'");
|
|
101
|
+
expect(parse).to.throw(/name: a name can't contain/);
|
|
102
|
+
expect(parse).to.throw(/visibility/);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("runs the declarations of one key one after another", async () => {
|
|
106
|
+
const order: string[] = [];
|
|
107
|
+
const slow = withDeclarationLock("default", "folder", "k", async () => {
|
|
108
|
+
await delay(20);
|
|
109
|
+
order.push("first");
|
|
110
|
+
});
|
|
111
|
+
const fast = withDeclarationLock("default", "folder", "k", async () => {
|
|
112
|
+
order.push("second");
|
|
113
|
+
});
|
|
114
|
+
const other = withDeclarationLock(
|
|
115
|
+
"default",
|
|
116
|
+
"folder",
|
|
117
|
+
"other",
|
|
118
|
+
async () => {
|
|
119
|
+
order.push("other");
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
await Promise.all([slow, fast, other]);
|
|
123
|
+
expect(order).to.deep.equal(["other", "first", "second"]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("keeps running a key after a failed declaration", async () => {
|
|
127
|
+
const failing = withDeclarationLock("default", "asset", "k", async () => {
|
|
128
|
+
throw new Error("boom");
|
|
129
|
+
}).catch((error: Error) => error.message);
|
|
130
|
+
const next = withDeclarationLock("default", "asset", "k", async () => "ok");
|
|
131
|
+
expect(await failing).to.equal("boom");
|
|
132
|
+
expect(await next).to.equal("ok");
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("[unit] declared content — storage writes at startup", () => {
|
|
137
|
+
it("retries a write while its endpoint does not answer", async function () {
|
|
138
|
+
this.timeout(10_000);
|
|
139
|
+
let attempts = 0;
|
|
140
|
+
const result = await writeWhenReachable(async () => {
|
|
141
|
+
attempts += 1;
|
|
142
|
+
if (attempts < 3) throw new TypeError("fetch failed");
|
|
143
|
+
return "stored";
|
|
144
|
+
});
|
|
145
|
+
expect(result).to.equal("stored");
|
|
146
|
+
expect(attempts).to.equal(3);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("does not retry a refused write", async () => {
|
|
150
|
+
let attempts = 0;
|
|
151
|
+
const refusal = await writeWhenReachable(async () => {
|
|
152
|
+
attempts += 1;
|
|
153
|
+
throw new Error("Failed to upload logo.png: 413");
|
|
154
|
+
}).catch((error: Error) => error.message);
|
|
155
|
+
expect(refusal).to.contain("413");
|
|
156
|
+
expect(attempts).to.equal(1);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import sharp from "sharp";
|
|
2
|
+
import { getRegisteredBindings } from "./bindings";
|
|
3
|
+
import { getMediaConfig } from "./config";
|
|
4
|
+
import type { MediaAssetModel } from "./db";
|
|
5
|
+
import { sumStoredBytes } from "./library/listing";
|
|
6
|
+
import {
|
|
7
|
+
expectedImageFormat,
|
|
8
|
+
isUploadTypeAllowed,
|
|
9
|
+
mimetypeMatches,
|
|
10
|
+
} from "./upload-policy";
|
|
11
|
+
|
|
12
|
+
const HTTP_PAYLOAD_TOO_LARGE = 413;
|
|
13
|
+
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
|
|
14
|
+
const HTTP_UNPROCESSABLE_ENTITY = 422;
|
|
15
|
+
const FIRST_SWAPPED_ORIENTATION = 5;
|
|
16
|
+
// Dimension probing buffers the file in memory, so skip files too large to
|
|
17
|
+
// hold in the heap; they simply stay without width/height metadata.
|
|
18
|
+
const MAX_PROBE_SIZE_BYTES = 32 * 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
/** Why an upload is refused, for the queue to explain it in the member's language. */
|
|
21
|
+
export type UploadRefusal =
|
|
22
|
+
| "type_not_allowed"
|
|
23
|
+
| "field_type"
|
|
24
|
+
| "quota"
|
|
25
|
+
| "invalid_image";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A file the library refuses, whoever adds it: the upload routes answer it as
|
|
29
|
+
* an HTTP error, declared content throws it to the calling module.
|
|
30
|
+
*/
|
|
31
|
+
export class UploadRefusedError extends Error {
|
|
32
|
+
constructor(
|
|
33
|
+
readonly status: number,
|
|
34
|
+
readonly reason: UploadRefusal,
|
|
35
|
+
message: string,
|
|
36
|
+
readonly details: Record<string, unknown> = {},
|
|
37
|
+
) {
|
|
38
|
+
super(message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ImageProbe {
|
|
43
|
+
width?: number;
|
|
44
|
+
height?: number;
|
|
45
|
+
format?: string;
|
|
46
|
+
isReadable: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Displayed size: sharp reports stored pixels, EXIF orientations 5-8 swap them. */
|
|
50
|
+
function orientedSize(metadata: sharp.Metadata): ImageProbe {
|
|
51
|
+
const isSwapped = (metadata.orientation ?? 0) >= FIRST_SWAPPED_ORIENTATION;
|
|
52
|
+
return {
|
|
53
|
+
width: isSwapped ? metadata.height : metadata.width,
|
|
54
|
+
height: isSwapped ? metadata.width : metadata.height,
|
|
55
|
+
format: metadata.format,
|
|
56
|
+
isReadable: true,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Reads an image's displayed size; any other file is readable as is. */
|
|
61
|
+
export async function probeImageBytes(
|
|
62
|
+
read: () => Promise<Buffer | undefined>,
|
|
63
|
+
mimetype: string,
|
|
64
|
+
size: number,
|
|
65
|
+
): Promise<ImageProbe> {
|
|
66
|
+
if (!mimetype.startsWith("image/") || size > MAX_PROBE_SIZE_BYTES)
|
|
67
|
+
return { isReadable: true };
|
|
68
|
+
try {
|
|
69
|
+
const buffer = await read();
|
|
70
|
+
if (!buffer) return { isReadable: true };
|
|
71
|
+
return orientedSize(await sharp(buffer).metadata());
|
|
72
|
+
} catch {
|
|
73
|
+
return { isReadable: false };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A file declared as a raster type sharp decodes must actually decode. */
|
|
78
|
+
export function assertReadableImage(probe: ImageProbe, mimetype: string): void {
|
|
79
|
+
const isDecodable = Boolean(expectedImageFormat(mimetype));
|
|
80
|
+
if (isDecodable && (!probe.isReadable || !probe.format))
|
|
81
|
+
throw new UploadRefusedError(
|
|
82
|
+
HTTP_UNPROCESSABLE_ENTITY,
|
|
83
|
+
"invalid_image",
|
|
84
|
+
"This file is not a readable image",
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The library's types, then those of the asset field the folder feeds. */
|
|
89
|
+
export function assertUploadTypeAllowed(
|
|
90
|
+
bindingId: string | undefined,
|
|
91
|
+
filename: string,
|
|
92
|
+
mimetype: string,
|
|
93
|
+
): void {
|
|
94
|
+
if (!isUploadTypeAllowed(filename, mimetype))
|
|
95
|
+
throw new UploadRefusedError(
|
|
96
|
+
HTTP_UNSUPPORTED_MEDIA_TYPE,
|
|
97
|
+
"type_not_allowed",
|
|
98
|
+
"This type of file can't be added to the library",
|
|
99
|
+
);
|
|
100
|
+
const accepted = getRegisteredBindings().find(
|
|
101
|
+
(config) => config.id === bindingId,
|
|
102
|
+
)?.field?.mimetypes;
|
|
103
|
+
if (accepted?.length && !mimetypeMatches(mimetype, accepted))
|
|
104
|
+
throw new UploadRefusedError(
|
|
105
|
+
HTTP_UNSUPPORTED_MEDIA_TYPE,
|
|
106
|
+
"field_type",
|
|
107
|
+
`This folder feeds a field that only accepts ${accepted.join(", ")}`,
|
|
108
|
+
{ accepted },
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function assertWithinQuota(
|
|
113
|
+
assetModel: MediaAssetModel,
|
|
114
|
+
size: number,
|
|
115
|
+
): Promise<void> {
|
|
116
|
+
const quota = getMediaConfig().storageQuotaBytes;
|
|
117
|
+
if (!quota) return;
|
|
118
|
+
const used = await sumStoredBytes(assetModel);
|
|
119
|
+
if (used + size > quota)
|
|
120
|
+
throw new UploadRefusedError(
|
|
121
|
+
HTTP_PAYLOAD_TOO_LARGE,
|
|
122
|
+
"quota",
|
|
123
|
+
"The library's storage plan is full",
|
|
124
|
+
{ remaining: Math.max(quota - used, 0), quota },
|
|
125
|
+
);
|
|
126
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { z, ZodError } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
altSchema,
|
|
4
|
+
fileNameSchema,
|
|
5
|
+
MAX_UPLOAD_SIZE_BYTES,
|
|
6
|
+
MEDIA_ERRORS,
|
|
7
|
+
nameSchema,
|
|
8
|
+
} from "./media.schema";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The dashboard answers with i18n keys its pages translate; a module declaring
|
|
12
|
+
* content reads the error in its logs, so it gets the English text.
|
|
13
|
+
*/
|
|
14
|
+
const DECLARATION_ERROR_TEXTS: Record<string, string> = {
|
|
15
|
+
[MEDIA_ERRORS.nameRequired]: "a name is required",
|
|
16
|
+
[MEDIA_ERRORS.nameTooLong]: "the name is limited to 255 characters",
|
|
17
|
+
[MEDIA_ERRORS.nameForbidden]:
|
|
18
|
+
"a name can't contain / \\ < > : \" | ? * or control characters",
|
|
19
|
+
[MEDIA_ERRORS.altTooLong]: "the alt text is limited to 255 characters",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const keySchema = z.string().trim().min(1).max(255);
|
|
23
|
+
const tenantSchema = z.string().trim().min(1);
|
|
24
|
+
|
|
25
|
+
const aclEntrySchema = z.object({
|
|
26
|
+
subject: z.object({
|
|
27
|
+
kind: z.enum(["permission", "role"]),
|
|
28
|
+
id: z.string().min(1),
|
|
29
|
+
}),
|
|
30
|
+
rights: z.array(z.enum(["read", "write", "manage"])).min(1),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const folderTargetSchema = z.union([
|
|
34
|
+
z.object({ key: keySchema }).strict(),
|
|
35
|
+
z.object({ binding: keySchema }).strict(),
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
export const declaredFolderSchema = z.object({
|
|
39
|
+
key: keySchema,
|
|
40
|
+
name: nameSchema,
|
|
41
|
+
parent: z.union([keySchema, folderTargetSchema]).optional(),
|
|
42
|
+
visibility: z.enum(["private", "public"]).default("private"),
|
|
43
|
+
acl: z.array(aclEntrySchema).optional(),
|
|
44
|
+
tenantId: tenantSchema,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const sourceSchema = z.union([
|
|
48
|
+
z.object({ bytes: z.instanceof(Uint8Array) }).strict(),
|
|
49
|
+
z.object({ filePath: z.string().min(1) }).strict(),
|
|
50
|
+
// Checked as a function only: wrapping it in z.function() would replace the caller's loader.
|
|
51
|
+
z
|
|
52
|
+
.object({
|
|
53
|
+
load: z.custom<() => Uint8Array | Promise<Uint8Array>>(
|
|
54
|
+
(value) => typeof value === "function",
|
|
55
|
+
{ message: "load must be a function" },
|
|
56
|
+
),
|
|
57
|
+
})
|
|
58
|
+
.strict(),
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
export const declaredAssetSchema = z.object({
|
|
62
|
+
key: keySchema,
|
|
63
|
+
folder: folderTargetSchema,
|
|
64
|
+
name: fileNameSchema,
|
|
65
|
+
mimetype: z.string().trim().min(1),
|
|
66
|
+
source: sourceSchema,
|
|
67
|
+
version: keySchema.optional(),
|
|
68
|
+
alt: altSchema.optional(),
|
|
69
|
+
tenantId: tenantSchema,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export const declarationRetirementSchema = z.object({
|
|
73
|
+
kind: z.enum(["folder", "asset"]),
|
|
74
|
+
key: keySchema,
|
|
75
|
+
tenantId: tenantSchema,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export const declarationPruneSchema = z.object({
|
|
79
|
+
prefix: keySchema,
|
|
80
|
+
keep: z.array(keySchema),
|
|
81
|
+
tenantId: tenantSchema,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export type DeclaredFolderInput = z.output<typeof declaredFolderSchema>;
|
|
85
|
+
export type DeclaredAssetInput = z.output<typeof declaredAssetSchema>;
|
|
86
|
+
|
|
87
|
+
/** Size limits of an uploaded file, checked once the source is read. */
|
|
88
|
+
export const declaredFileSizeSchema = z
|
|
89
|
+
.number()
|
|
90
|
+
.int()
|
|
91
|
+
.positive({ message: "the file is empty" })
|
|
92
|
+
.max(MAX_UPLOAD_SIZE_BYTES, {
|
|
93
|
+
message: `the file exceeds ${MAX_UPLOAD_SIZE_BYTES} bytes`,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
/** Every validation issue of a declaration, as `field: reason`. */
|
|
97
|
+
export function describeDeclarationIssues(error: ZodError): string {
|
|
98
|
+
return error.issues
|
|
99
|
+
.map((issue) => {
|
|
100
|
+
const field = issue.path.join(".") || "declaration";
|
|
101
|
+
return `${field}: ${DECLARATION_ERROR_TEXTS[issue.message] ?? issue.message}`;
|
|
102
|
+
})
|
|
103
|
+
.join("; ");
|
|
104
|
+
}
|
|
@@ -46,12 +46,12 @@ function boundedName(): z.ZodString {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/** A name typed in the library: refuses what Windows or macOS can't save. */
|
|
49
|
-
const nameSchema = boundedName().refine(isPortableName, {
|
|
49
|
+
export const nameSchema = boundedName().refine(isPortableName, {
|
|
50
50
|
message: MEDIA_ERRORS.nameForbidden,
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
/** The name of an uploaded file, kept as the computer sent it. */
|
|
54
|
-
const fileNameSchema = boundedName().refine(isSafeFileName, {
|
|
54
|
+
export const fileNameSchema = boundedName().refine(isSafeFileName, {
|
|
55
55
|
message: MEDIA_ERRORS.nameForbidden,
|
|
56
56
|
});
|
|
57
57
|
|
|
@@ -111,12 +111,13 @@ export const confirmUploadSchema = z.object({
|
|
|
111
111
|
batchId: z.string().min(1).max(MAX_NAME_LENGTH).optional(),
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
+
export const altSchema = z
|
|
115
|
+
.string()
|
|
116
|
+
.max(MAX_NAME_LENGTH, { message: MEDIA_ERRORS.altTooLong });
|
|
117
|
+
|
|
114
118
|
export const updateAssetSchema = z.object({
|
|
115
119
|
name: nameSchema.optional(),
|
|
116
|
-
alt:
|
|
117
|
-
.string()
|
|
118
|
-
.max(MAX_NAME_LENGTH, { message: MEDIA_ERRORS.altTooLong })
|
|
119
|
-
.optional(),
|
|
120
|
+
alt: altSchema.optional(),
|
|
120
121
|
});
|
|
121
122
|
|
|
122
123
|
export const moveAssetSchema = z.object({
|