@antelopejs/dms-media 0.8.2 → 0.8.3-next.167.g8bd8349
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/test/antelope.test.js +1 -0
- package/dist/test/antelope.test.js.map +1 -1
- package/dist/test/helpers/config.d.ts +2 -0
- package/dist/test/helpers/config.js +16 -0
- package/dist/test/helpers/config.js.map +1 -0
- package/dist/test/helpers/delivery.d.ts +9 -0
- package/dist/test/helpers/delivery.js +47 -0
- package/dist/test/helpers/delivery.js.map +1 -0
- package/dist/test/helpers/derivative-storage.d.ts +3 -0
- package/dist/test/helpers/derivative-storage.js +22 -0
- package/dist/test/helpers/derivative-storage.js.map +1 -0
- package/dist/test/helpers/media-config.d.ts +2 -0
- package/dist/test/helpers/media-config.js +16 -0
- package/dist/test/helpers/media-config.js.map +1 -0
- package/dist/test/helpers/member-rights.d.ts +99 -0
- package/dist/test/helpers/member-rights.js +85 -0
- package/dist/test/helpers/member-rights.js.map +1 -0
- package/dist/test/helpers/png.d.ts +1 -0
- package/dist/test/helpers/png.js +15 -0
- package/dist/test/helpers/png.js.map +1 -0
- package/dist/test/helpers/renderings.d.ts +27 -0
- package/dist/test/helpers/renderings.js +106 -0
- package/dist/test/helpers/renderings.js.map +1 -0
- package/dist/test/helpers/uploads.d.ts +26 -0
- package/dist/test/helpers/uploads.js +48 -0
- package/dist/test/helpers/uploads.js.map +1 -0
- package/dist/test/helpers/zip.d.ts +6 -0
- package/dist/test/helpers/zip.js +50 -0
- package/dist/test/helpers/zip.js.map +1 -0
- package/dist/test/integration/media-activity-overview.test.js +109 -0
- package/dist/test/integration/media-activity-overview.test.js.map +1 -1
- package/dist/test/integration/media-api.test.js +101 -0
- package/dist/test/integration/media-api.test.js.map +1 -1
- package/dist/test/integration/media-bindings.test.js +259 -0
- package/dist/test/integration/media-bindings.test.js.map +1 -1
- package/dist/test/integration/media-bulk-writes.test.d.ts +1 -0
- package/dist/test/integration/media-bulk-writes.test.js +159 -0
- package/dist/test/integration/media-bulk-writes.test.js.map +1 -0
- package/dist/test/integration/media-declared-rules.test.d.ts +1 -0
- package/dist/test/integration/media-declared-rules.test.js +297 -0
- package/dist/test/integration/media-declared-rules.test.js.map +1 -0
- package/dist/test/integration/media-declared-sources.test.d.ts +1 -0
- package/dist/test/integration/media-declared-sources.test.js +194 -0
- package/dist/test/integration/media-declared-sources.test.js.map +1 -0
- package/dist/test/integration/media-delivery-active-content.test.d.ts +1 -0
- package/dist/test/integration/media-delivery-active-content.test.js +138 -0
- package/dist/test/integration/media-delivery-active-content.test.js.map +1 -0
- package/dist/test/integration/media-delivery-editing.test.js +41 -51
- package/dist/test/integration/media-delivery-editing.test.js.map +1 -1
- package/dist/test/integration/media-delivery.test.js +145 -0
- package/dist/test/integration/media-delivery.test.js.map +1 -1
- package/dist/test/integration/media-explorer-writes.test.js +173 -18
- package/dist/test/integration/media-explorer-writes.test.js.map +1 -1
- package/dist/test/integration/media-file-reads.test.d.ts +1 -0
- package/dist/test/integration/media-file-reads.test.js +147 -0
- package/dist/test/integration/media-file-reads.test.js.map +1 -0
- package/dist/test/integration/media-guards.test.js +1 -0
- package/dist/test/integration/media-guards.test.js.map +1 -1
- package/dist/test/integration/media-library-views.test.d.ts +1 -0
- package/dist/test/integration/media-library-views.test.js +377 -0
- package/dist/test/integration/media-library-views.test.js.map +1 -0
- package/dist/test/integration/media-linked-subfolders.test.js +2 -0
- package/dist/test/integration/media-linked-subfolders.test.js.map +1 -1
- package/dist/test/integration/media-member-access.test.js +96 -0
- package/dist/test/integration/media-member-access.test.js.map +1 -1
- package/dist/test/integration/media-permissions.test.js +7 -0
- package/dist/test/integration/media-permissions.test.js.map +1 -1
- package/dist/test/integration/media-preset-delivery.test.d.ts +1 -0
- package/dist/test/integration/media-preset-delivery.test.js +161 -0
- package/dist/test/integration/media-preset-delivery.test.js.map +1 -0
- package/dist/test/integration/media-presets.test.d.ts +1 -0
- package/dist/test/integration/media-presets.test.js +223 -0
- package/dist/test/integration/media-presets.test.js.map +1 -0
- package/dist/test/integration/media-transform.test.js +105 -10
- package/dist/test/integration/media-transform.test.js.map +1 -1
- package/dist/test/integration/media-upload-content.test.d.ts +1 -0
- package/dist/test/integration/media-upload-content.test.js +142 -0
- package/dist/test/integration/media-upload-content.test.js.map +1 -0
- package/dist/test/integration/media-upload-policy.test.js +13 -12
- package/dist/test/integration/media-upload-policy.test.js.map +1 -1
- package/dist/test/integration/media-upload-types.test.d.ts +1 -0
- package/dist/test/integration/media-upload-types.test.js +176 -0
- package/dist/test/integration/media-upload-types.test.js.map +1 -0
- package/dist/test/integration/permission-levels.d.ts +1 -0
- package/dist/test/integration/permission-levels.js +207 -0
- package/dist/test/integration/permission-levels.js.map +1 -0
- package/dist/test/integration/permission-reach.d.ts +1 -0
- package/dist/test/integration/permission-reach.js +209 -0
- package/dist/test/integration/permission-reach.js.map +1 -0
- package/dist/test/integration/permissions-management.js +7 -0
- package/dist/test/integration/permissions-management.js.map +1 -1
- package/dist/test/unit/asset-type.test.d.ts +1 -0
- package/dist/test/unit/asset-type.test.js +63 -0
- package/dist/test/unit/asset-type.test.js.map +1 -0
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/pnpm-lock.yaml +11 -11
- package/package.json +10 -8
- package/src/test/antelope.test.ts +1 -0
- package/src/test/helpers/config.ts +14 -0
- package/src/test/helpers/delivery.ts +83 -0
- package/src/test/helpers/derivative-storage.ts +25 -0
- package/src/test/helpers/media-config.ts +16 -0
- package/src/test/helpers/member-rights.ts +116 -0
- package/src/test/helpers/png.ts +10 -0
- package/src/test/helpers/renderings.ts +147 -0
- package/src/test/helpers/uploads.ts +92 -0
- package/src/test/helpers/zip.ts +62 -0
- package/src/test/integration/media-activity-overview.test.ts +170 -0
- package/src/test/integration/media-api.test.ts +135 -1
- package/src/test/integration/media-bindings.test.ts +316 -2
- package/src/test/integration/media-bulk-writes.test.ts +180 -0
- package/src/test/integration/media-declared-rules.test.ts +405 -0
- package/src/test/integration/media-declared-sources.test.ts +228 -0
- package/src/test/integration/media-delivery-active-content.test.ts +185 -0
- package/src/test/integration/media-delivery-editing.test.ts +24 -46
- package/src/test/integration/media-delivery.test.ts +217 -1
- package/src/test/integration/media-explorer-writes.test.ts +253 -23
- package/src/test/integration/media-file-reads.test.ts +226 -0
- package/src/test/integration/media-guards.test.ts +1 -0
- package/src/test/integration/media-library-views.test.ts +478 -0
- package/src/test/integration/media-linked-subfolders.test.ts +2 -0
- package/src/test/integration/media-member-access.test.ts +146 -0
- package/src/test/integration/media-permissions.test.ts +10 -1
- package/src/test/integration/media-preset-delivery.test.ts +261 -0
- package/src/test/integration/media-presets.test.ts +304 -0
- package/src/test/integration/media-transform.test.ts +152 -10
- package/src/test/integration/media-upload-content.test.ts +192 -0
- package/src/test/integration/media-upload-policy.test.ts +20 -18
- package/src/test/integration/media-upload-types.test.ts +267 -0
- package/src/test/integration/permission-levels.ts +332 -0
- package/src/test/integration/permission-reach.ts +302 -0
- package/src/test/integration/permissions-management.ts +7 -0
- package/src/test/unit/asset-type.test.ts +87 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import sharp from "sharp";
|
|
3
|
+
import { configureMediaModule, type DmsMediaConfig } from "../../config";
|
|
4
|
+
import type { MediaPresetConfig } from "../../presets";
|
|
5
|
+
import { getBaseUrl } from "./http";
|
|
6
|
+
|
|
7
|
+
const HTTP_FOUND = 302;
|
|
8
|
+
const LIT_TOLERANCE = 40;
|
|
9
|
+
const BLACK = [0, 0, 0];
|
|
10
|
+
|
|
11
|
+
/** What one request of an image size answers, and the bytes it redirects to. */
|
|
12
|
+
export interface Rendering {
|
|
13
|
+
status: number;
|
|
14
|
+
contentType: string | null;
|
|
15
|
+
bytes: Buffer;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A decoded image whose pixels are read by position. */
|
|
19
|
+
export interface Frame {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
at: (x: number, y: number) => number[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Runs with the given image sizes configured, and puts the configuration back. */
|
|
26
|
+
export async function withPresets(
|
|
27
|
+
initial: DmsMediaConfig,
|
|
28
|
+
presets: MediaPresetConfig[],
|
|
29
|
+
run: () => Promise<void>,
|
|
30
|
+
): Promise<void> {
|
|
31
|
+
configureMediaModule({ ...initial, presets });
|
|
32
|
+
try {
|
|
33
|
+
await run();
|
|
34
|
+
} finally {
|
|
35
|
+
configureMediaModule(initial);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Requests an image size of a file as its owner and reads what the storage serves. */
|
|
40
|
+
export async function render(
|
|
41
|
+
token: string,
|
|
42
|
+
asset: { id: string; name: string },
|
|
43
|
+
preset: string,
|
|
44
|
+
): Promise<Rendering> {
|
|
45
|
+
const path = `/media/${asset.id}/${preset}/${encodeURIComponent(asset.name)}`;
|
|
46
|
+
const redirect = await fetch(`${getBaseUrl()}${path}`, {
|
|
47
|
+
redirect: "manual",
|
|
48
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
49
|
+
});
|
|
50
|
+
const location = redirect.headers.get("location");
|
|
51
|
+
if (redirect.status !== HTTP_FOUND || location === null)
|
|
52
|
+
return {
|
|
53
|
+
status: redirect.status,
|
|
54
|
+
contentType: null,
|
|
55
|
+
bytes: Buffer.alloc(0),
|
|
56
|
+
};
|
|
57
|
+
const stored = await fetch(location);
|
|
58
|
+
return {
|
|
59
|
+
status: redirect.status,
|
|
60
|
+
contentType: stored.headers.get("content-type"),
|
|
61
|
+
bytes: Buffer.from(await stored.arrayBuffer()),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Asserts the format, the served type and the size of a rendering. */
|
|
66
|
+
export async function expectRendering(
|
|
67
|
+
rendering: Rendering,
|
|
68
|
+
expected: { format: string; width: number; height: number },
|
|
69
|
+
label: string,
|
|
70
|
+
): Promise<void> {
|
|
71
|
+
expect(rendering.status, label).to.equal(HTTP_FOUND);
|
|
72
|
+
expect(rendering.contentType, label).to.equal(`image/${expected.format}`);
|
|
73
|
+
const info = await sharp(rendering.bytes).metadata();
|
|
74
|
+
expect([info.format, info.width, info.height], label).to.deep.equal([
|
|
75
|
+
expected.format,
|
|
76
|
+
expected.width,
|
|
77
|
+
expected.height,
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A photo of random noise, which compresses differently at every quality. */
|
|
82
|
+
export function noisyImage(
|
|
83
|
+
width: number,
|
|
84
|
+
height: number,
|
|
85
|
+
format: "jpeg" | "png",
|
|
86
|
+
): Promise<Buffer> {
|
|
87
|
+
const pipeline = sharp({
|
|
88
|
+
create: {
|
|
89
|
+
width,
|
|
90
|
+
height,
|
|
91
|
+
channels: 3,
|
|
92
|
+
background: "#808080",
|
|
93
|
+
noise: { type: "gaussian", mean: 128, sigma: 60 },
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return format === "jpeg"
|
|
97
|
+
? pipeline.jpeg().toBuffer()
|
|
98
|
+
: pipeline.png().toBuffer();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A PNG painted pixel by pixel. */
|
|
102
|
+
export function paintedPng(
|
|
103
|
+
width: number,
|
|
104
|
+
height: number,
|
|
105
|
+
colourAt: (x: number, y: number) => number[],
|
|
106
|
+
): Promise<Buffer> {
|
|
107
|
+
const pixels = Buffer.alloc(width * height * 3);
|
|
108
|
+
for (let y = 0; y < height; y += 1) {
|
|
109
|
+
for (let x = 0; x < width; x += 1) {
|
|
110
|
+
pixels.set(colourAt(x, y), (y * width + x) * 3);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return sharp(pixels, { raw: { width, height, channels: 3 } })
|
|
114
|
+
.png()
|
|
115
|
+
.toBuffer();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function decodeFrame(bytes: Buffer): Promise<Frame> {
|
|
119
|
+
const { data, info } = await sharp(bytes)
|
|
120
|
+
.removeAlpha()
|
|
121
|
+
.raw()
|
|
122
|
+
.toBuffer({ resolveWithObject: true });
|
|
123
|
+
return {
|
|
124
|
+
width: info.width,
|
|
125
|
+
height: info.height,
|
|
126
|
+
at: (x, y) => {
|
|
127
|
+
const offset = (y * info.width + x) * info.channels;
|
|
128
|
+
return [...data.subarray(offset, offset + 3)];
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether each channel is within a small tolerance of the target colour. */
|
|
134
|
+
export function near(rgb: number[], target: number[]): boolean {
|
|
135
|
+
return target.every(
|
|
136
|
+
(channel, index) =>
|
|
137
|
+
Math.abs((rgb[index] ?? -255) - channel) <= LIT_TOLERANCE,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** How many pixels of the middle row are not black. */
|
|
142
|
+
export function litOnMiddleRow(frame: Frame): number {
|
|
143
|
+
const row = Math.floor(frame.height / 2);
|
|
144
|
+
return Array.from({ length: frame.width }, (_, x) => frame.at(x, row)).filter(
|
|
145
|
+
(rgb) => !near(rgb, BLACK),
|
|
146
|
+
).length;
|
|
147
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { AxiosInstance, AxiosResponse } from "axios";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { putPresigned } from "./assets";
|
|
5
|
+
import { presignBody, readBody } from "./bodies";
|
|
6
|
+
|
|
7
|
+
const HTTP_OK = 200;
|
|
8
|
+
const HTTP_REDIRECT_BOUNDARY = 300;
|
|
9
|
+
|
|
10
|
+
/** A file the storage holds under a staging key, waiting for its confirm. */
|
|
11
|
+
export interface StagedUpload {
|
|
12
|
+
resourceKey: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** What a refused upload answers: the status, a reason and a readable message. */
|
|
16
|
+
export interface Refusal {
|
|
17
|
+
status: number;
|
|
18
|
+
data: {
|
|
19
|
+
reason?: string;
|
|
20
|
+
message?: string;
|
|
21
|
+
quota?: number;
|
|
22
|
+
remaining?: number;
|
|
23
|
+
accepted?: string[];
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function presign(
|
|
28
|
+
client: AxiosInstance,
|
|
29
|
+
folderId: string,
|
|
30
|
+
filename: string,
|
|
31
|
+
mimetype: string,
|
|
32
|
+
size = 4,
|
|
33
|
+
): Promise<AxiosResponse> {
|
|
34
|
+
return client.post("/api/media/upload/presign", {
|
|
35
|
+
folderId,
|
|
36
|
+
filename,
|
|
37
|
+
size,
|
|
38
|
+
mimetype,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Presigns and PUTs the bytes, the first two steps of an upload. */
|
|
43
|
+
export async function stage(
|
|
44
|
+
client: AxiosInstance,
|
|
45
|
+
folderId: string,
|
|
46
|
+
file: { filename: string; mimetype: string; body: Buffer },
|
|
47
|
+
): Promise<StagedUpload> {
|
|
48
|
+
const presigned = await presign(
|
|
49
|
+
client,
|
|
50
|
+
folderId,
|
|
51
|
+
file.filename,
|
|
52
|
+
file.mimetype,
|
|
53
|
+
file.body.byteLength,
|
|
54
|
+
);
|
|
55
|
+
expect(presigned.status, "presign").to.equal(HTTP_OK);
|
|
56
|
+
const target = readBody(presigned, presignBody);
|
|
57
|
+
const put = await putPresigned(target, file.body);
|
|
58
|
+
expect(put.status, "storage PUT").to.be.lessThan(HTTP_REDIRECT_BOUNDARY);
|
|
59
|
+
return { resourceKey: target.resourceKey };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function confirm(
|
|
63
|
+
client: AxiosInstance,
|
|
64
|
+
folderId: string,
|
|
65
|
+
resourceKey: string,
|
|
66
|
+
filename: string,
|
|
67
|
+
): Promise<AxiosResponse> {
|
|
68
|
+
return client.post("/api/media/upload/confirm", {
|
|
69
|
+
folderId,
|
|
70
|
+
resourceKey,
|
|
71
|
+
filename,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Bytes the library holds, as the overview's storage meter reports them. */
|
|
76
|
+
export async function storedBytes(client: AxiosInstance): Promise<number> {
|
|
77
|
+
const response = await client.get("/api/media/stats/storage");
|
|
78
|
+
expect(response.status).to.equal(HTTP_OK);
|
|
79
|
+
return readBody(response, z.object({ value: z.number() })).value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Asserts the documented refusal body: status, reason and a non-empty message. */
|
|
83
|
+
export function expectRefusal(
|
|
84
|
+
response: Refusal,
|
|
85
|
+
expected: { status: number; reason: string },
|
|
86
|
+
label: string,
|
|
87
|
+
): void {
|
|
88
|
+
expect(response.status, label).to.equal(expected.status);
|
|
89
|
+
expect(response.data.reason, label).to.equal(expected.reason);
|
|
90
|
+
expect(response.data.message, label).to.be.a("string");
|
|
91
|
+
expect(response.data.message, label).to.not.equal("");
|
|
92
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { inflateRawSync } from "node:zlib";
|
|
2
|
+
|
|
3
|
+
const END_SIGNATURE = Buffer.from([0x50, 0x4b, 0x05, 0x06]);
|
|
4
|
+
const END_ENTRY_COUNT_OFFSET = 10;
|
|
5
|
+
const END_DIRECTORY_OFFSET = 16;
|
|
6
|
+
const CENTRAL_HEADER_SIZE = 46;
|
|
7
|
+
const CENTRAL_METHOD_OFFSET = 10;
|
|
8
|
+
const CENTRAL_COMPRESSED_SIZE_OFFSET = 20;
|
|
9
|
+
const CENTRAL_NAME_LENGTH_OFFSET = 28;
|
|
10
|
+
const CENTRAL_EXTRA_LENGTH_OFFSET = 30;
|
|
11
|
+
const CENTRAL_COMMENT_LENGTH_OFFSET = 32;
|
|
12
|
+
const CENTRAL_LOCAL_OFFSET = 42;
|
|
13
|
+
const LOCAL_HEADER_SIZE = 30;
|
|
14
|
+
const LOCAL_NAME_LENGTH_OFFSET = 26;
|
|
15
|
+
const LOCAL_EXTRA_LENGTH_OFFSET = 28;
|
|
16
|
+
const METHOD_DEFLATE = 8;
|
|
17
|
+
|
|
18
|
+
/** One file of a zip archive: its path inside the archive and its uncompressed bytes. */
|
|
19
|
+
export interface ZipEntry {
|
|
20
|
+
name: string;
|
|
21
|
+
bytes: Buffer;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The entries of a zip archive, read from its central directory. */
|
|
25
|
+
export function readZip(zip: Buffer): ZipEntry[] {
|
|
26
|
+
const end = zip.lastIndexOf(END_SIGNATURE);
|
|
27
|
+
if (end < 0) return [];
|
|
28
|
+
const count = zip.readUInt16LE(end + END_ENTRY_COUNT_OFFSET);
|
|
29
|
+
let offset = zip.readUInt32LE(end + END_DIRECTORY_OFFSET);
|
|
30
|
+
const entries: ZipEntry[] = [];
|
|
31
|
+
for (let index = 0; index < count; index += 1) {
|
|
32
|
+
const nameLength = zip.readUInt16LE(offset + CENTRAL_NAME_LENGTH_OFFSET);
|
|
33
|
+
const extraLength = zip.readUInt16LE(offset + CENTRAL_EXTRA_LENGTH_OFFSET);
|
|
34
|
+
const commentLength = zip.readUInt16LE(
|
|
35
|
+
offset + CENTRAL_COMMENT_LENGTH_OFFSET,
|
|
36
|
+
);
|
|
37
|
+
const local = zip.readUInt32LE(offset + CENTRAL_LOCAL_OFFSET);
|
|
38
|
+
const dataStart =
|
|
39
|
+
local +
|
|
40
|
+
LOCAL_HEADER_SIZE +
|
|
41
|
+
zip.readUInt16LE(local + LOCAL_NAME_LENGTH_OFFSET) +
|
|
42
|
+
zip.readUInt16LE(local + LOCAL_EXTRA_LENGTH_OFFSET);
|
|
43
|
+
const compressed = zip.subarray(
|
|
44
|
+
dataStart,
|
|
45
|
+
dataStart + zip.readUInt32LE(offset + CENTRAL_COMPRESSED_SIZE_OFFSET),
|
|
46
|
+
);
|
|
47
|
+
const isDeflated =
|
|
48
|
+
zip.readUInt16LE(offset + CENTRAL_METHOD_OFFSET) === METHOD_DEFLATE;
|
|
49
|
+
const nameStart = offset + CENTRAL_HEADER_SIZE;
|
|
50
|
+
entries.push({
|
|
51
|
+
name: zip.toString("utf8", nameStart, nameStart + nameLength),
|
|
52
|
+
bytes: isDeflated ? inflateRawSync(compressed) : Buffer.from(compressed),
|
|
53
|
+
});
|
|
54
|
+
offset = nameStart + nameLength + extraLength + commentLength;
|
|
55
|
+
}
|
|
56
|
+
return entries;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The entry names of a zip archive. */
|
|
60
|
+
export function zipEntryNames(zip: Buffer): string[] {
|
|
61
|
+
return readZip(zip).map((entry) => entry.name);
|
|
62
|
+
}
|
|
@@ -4,11 +4,17 @@ import { z } from "zod";
|
|
|
4
4
|
import { uploadAsset } from "../helpers/assets";
|
|
5
5
|
import { createdBody, readBody } from "../helpers/bodies";
|
|
6
6
|
import { authorizedClient } from "../helpers/http";
|
|
7
|
+
import { withMediaConfig } from "../helpers/media-config";
|
|
7
8
|
import { createRole, registerMember } from "../helpers/members";
|
|
8
9
|
import { ensureOwnerSession } from "../helpers/owner";
|
|
10
|
+
import { solidPng } from "../helpers/png";
|
|
9
11
|
|
|
10
12
|
const HTTP_OK = 200;
|
|
11
13
|
const HTTP_FORBIDDEN = 403;
|
|
14
|
+
const HTTP_PAYLOAD_TOO_LARGE = 413;
|
|
15
|
+
const LARGE_FILE_BYTES = 20 * 1000 * 1000;
|
|
16
|
+
const PLAN_BYTES = 1_000_000;
|
|
17
|
+
const FILL_BYTES = 6000;
|
|
12
18
|
const TEXT_MIMETYPE = "text/plain";
|
|
13
19
|
const BODY = Buffer.alloc(10, 1);
|
|
14
20
|
const BATCH_ID = "overview-retried-batch";
|
|
@@ -200,3 +206,167 @@ describe("[integration] media activity and overview — figures, upload history
|
|
|
200
206
|
}
|
|
201
207
|
});
|
|
202
208
|
});
|
|
209
|
+
|
|
210
|
+
const storageBody = z.object({
|
|
211
|
+
value: z.number(),
|
|
212
|
+
segments: z.array(
|
|
213
|
+
z.object({
|
|
214
|
+
value: z.number(),
|
|
215
|
+
label: z.object({
|
|
216
|
+
params: z.object({ type: z.object({ key: z.string() }) }),
|
|
217
|
+
}),
|
|
218
|
+
}),
|
|
219
|
+
),
|
|
220
|
+
});
|
|
221
|
+
const kpisBody = z.object({
|
|
222
|
+
items: z.array(z.object({ id: z.string() }).passthrough()),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
async function stats(client: AxiosInstance) {
|
|
226
|
+
const storage = readBody(
|
|
227
|
+
await client.get("/api/media/stats/storage"),
|
|
228
|
+
storageBody,
|
|
229
|
+
);
|
|
230
|
+
const segment = (type: string) =>
|
|
231
|
+
storage.segments.find(
|
|
232
|
+
(entry) => entry.label.params.type.key === `dms_media.types.${type}`,
|
|
233
|
+
)?.value ?? 0;
|
|
234
|
+
const attention = readBody(
|
|
235
|
+
await client.get("/api/media/stats/attention"),
|
|
236
|
+
attentionBody,
|
|
237
|
+
);
|
|
238
|
+
const figure = (id: string) =>
|
|
239
|
+
attention.items.find((entry) => entry.id === id)?.value ?? 0;
|
|
240
|
+
return {
|
|
241
|
+
used: storage.value,
|
|
242
|
+
segmentSum: storage.segments.reduce((sum, entry) => sum + entry.value, 0),
|
|
243
|
+
pdf: segment("pdf"),
|
|
244
|
+
image: segment("image"),
|
|
245
|
+
failedUploads: figure("failed-uploads"),
|
|
246
|
+
largeFiles: figure("large-files"),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
describe("[integration] media overview — storage plan and exact figures", () => {
|
|
251
|
+
let client: AxiosInstance;
|
|
252
|
+
let folderId: string;
|
|
253
|
+
|
|
254
|
+
before(async function () {
|
|
255
|
+
this.timeout(30_000);
|
|
256
|
+
client = authorizedClient((await ensureOwnerSession()).accessToken);
|
|
257
|
+
folderId = readBody(
|
|
258
|
+
await client.post("/api/media/folders", { name: "Overview plan" }),
|
|
259
|
+
createdBody,
|
|
260
|
+
).id;
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
after(async () => {
|
|
264
|
+
await client.delete(`/api/media/folders/${folderId}`);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("names the storage plan on the storage KPI and gives its quota in the tree only when one is set", async () => {
|
|
268
|
+
const freshStorageItem = async () => {
|
|
269
|
+
const touched = await client.post("/api/media/folders", {
|
|
270
|
+
name: "Overview plan touch",
|
|
271
|
+
});
|
|
272
|
+
await client.delete(
|
|
273
|
+
`/api/media/folders/${readBody(touched, createdBody).id}`,
|
|
274
|
+
);
|
|
275
|
+
return JSON.stringify(
|
|
276
|
+
readBody(
|
|
277
|
+
await client.get("/api/media/stats/kpis"),
|
|
278
|
+
kpisBody,
|
|
279
|
+
).items.find((item) => item.id === "storage"),
|
|
280
|
+
);
|
|
281
|
+
};
|
|
282
|
+
const quota = async () =>
|
|
283
|
+
readBody(
|
|
284
|
+
await client.get("/api/media/tree"),
|
|
285
|
+
z.object({ quotaBytes: z.number().nullable() }),
|
|
286
|
+
).quotaBytes;
|
|
287
|
+
expect(await freshStorageItem()).not.to.contain("storage_quota_detail");
|
|
288
|
+
expect(await quota()).to.equal(null);
|
|
289
|
+
await withMediaConfig({ storageQuotaBytes: PLAN_BYTES }, async () => {
|
|
290
|
+
expect(await freshStorageItem()).to.contain("storage_quota_detail");
|
|
291
|
+
expect(await quota()).to.equal(PLAN_BYTES);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("counts exactly the failed uploads of today, the files over 20 MB and each type's bytes, the original of an edited image included", async function () {
|
|
296
|
+
this.timeout(120_000);
|
|
297
|
+
const before = await stats(client);
|
|
298
|
+
const upload = (name: string, size: number) =>
|
|
299
|
+
uploadAsset(
|
|
300
|
+
client,
|
|
301
|
+
folderId,
|
|
302
|
+
name,
|
|
303
|
+
"application/pdf",
|
|
304
|
+
Buffer.alloc(size, 37),
|
|
305
|
+
);
|
|
306
|
+
await upload("pile.pdf", LARGE_FILE_BYTES);
|
|
307
|
+
await upload("plus.pdf", LARGE_FILE_BYTES + 1);
|
|
308
|
+
const photoBytes = await solidPng(300, 200);
|
|
309
|
+
const photo = await uploadAsset(
|
|
310
|
+
client,
|
|
311
|
+
folderId,
|
|
312
|
+
"photo.png",
|
|
313
|
+
"image/png",
|
|
314
|
+
photoBytes,
|
|
315
|
+
);
|
|
316
|
+
const edited = await client.post(
|
|
317
|
+
`/api/media/assets/${photo.id}/transform`,
|
|
318
|
+
{
|
|
319
|
+
rotate: 90,
|
|
320
|
+
},
|
|
321
|
+
);
|
|
322
|
+
expect(edited.status).to.equal(HTTP_OK);
|
|
323
|
+
for (const failed of [2, 1]) {
|
|
324
|
+
const report = await client.post("/api/media/upload/batches", {
|
|
325
|
+
batchId: `figures-${failed}`,
|
|
326
|
+
folderId,
|
|
327
|
+
total: failed + 1,
|
|
328
|
+
uploaded: 1,
|
|
329
|
+
failed,
|
|
330
|
+
size: 1,
|
|
331
|
+
failures: Array.from({ length: failed }, (_, index) => ({
|
|
332
|
+
name: `f${index}.html`,
|
|
333
|
+
reason: "type_not_allowed",
|
|
334
|
+
})),
|
|
335
|
+
});
|
|
336
|
+
expect(report.status).to.equal(HTTP_OK);
|
|
337
|
+
}
|
|
338
|
+
const after = await stats(client);
|
|
339
|
+
expect(after.failedUploads - before.failedUploads).to.equal(3);
|
|
340
|
+
expect(after.largeFiles - before.largeFiles).to.equal(1);
|
|
341
|
+
expect(after.pdf - before.pdf).to.equal(2 * LARGE_FILE_BYTES + 1);
|
|
342
|
+
expect(after.image - before.image).to.equal(
|
|
343
|
+
readBody(edited, z.object({ asset: z.object({ size: z.number() }) }))
|
|
344
|
+
.asset.size + photoBytes.byteLength,
|
|
345
|
+
);
|
|
346
|
+
expect(after.used).to.equal(after.segmentSum);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("accepts a file that fills the storage plan to the byte and refuses one more byte with nothing remaining", async () => {
|
|
350
|
+
const { used } = await stats(client);
|
|
351
|
+
await withMediaConfig(
|
|
352
|
+
{ storageQuotaBytes: used + FILL_BYTES },
|
|
353
|
+
async () => {
|
|
354
|
+
await uploadAsset(
|
|
355
|
+
client,
|
|
356
|
+
folderId,
|
|
357
|
+
"remplit.txt",
|
|
358
|
+
TEXT_MIMETYPE,
|
|
359
|
+
Buffer.alloc(FILL_BYTES, 97),
|
|
360
|
+
);
|
|
361
|
+
const more = await client.post("/api/media/upload/presign", {
|
|
362
|
+
folderId,
|
|
363
|
+
filename: "octet.txt",
|
|
364
|
+
size: 1,
|
|
365
|
+
mimetype: TEXT_MIMETYPE,
|
|
366
|
+
});
|
|
367
|
+
expect(more.status).to.equal(HTTP_PAYLOAD_TOO_LARGE);
|
|
368
|
+
expect(more.data).to.include({ reason: "quota", remaining: 0 });
|
|
369
|
+
},
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
});
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import type { AxiosInstance } from "axios";
|
|
2
2
|
import { expect } from "chai";
|
|
3
|
+
import { GetModel } from "@antelopejs/interface-database-decorators";
|
|
4
|
+
import { DEFAULT_TENANT_ID } from "@antelopejs/interface-dms/constants";
|
|
5
|
+
import { FileExists } from "@antelopejs/interface-file-storage";
|
|
3
6
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
7
|
+
import { MediaAssetModel } from "../../db";
|
|
8
|
+
import { putPresigned, rawGet, uploadAsset } from "../helpers/assets";
|
|
5
9
|
import { createdBody, presignBody, readBody } from "../helpers/bodies";
|
|
6
10
|
import { authorizedClient } from "../helpers/http";
|
|
7
11
|
import { ensureOwnerSession } from "../helpers/owner";
|
|
12
|
+
import { solidPng } from "../helpers/png";
|
|
8
13
|
|
|
9
14
|
const HTTP_OK = 200;
|
|
10
15
|
const HTTP_NOT_FOUND = 404;
|
|
16
|
+
const HTTP_BAD_REQUEST = 400;
|
|
17
|
+
const HTTP_FOUND = 302;
|
|
18
|
+
const HTTP_UNAUTHORIZED = 401;
|
|
11
19
|
const READY_TIMEOUT_MS = 30_000;
|
|
12
20
|
const POLL_INTERVAL_MS = 200;
|
|
13
21
|
const FILE_CONTENT = "hello media library";
|
|
@@ -32,6 +40,8 @@ const treeBody = z.object({
|
|
|
32
40
|
folders: z.array(
|
|
33
41
|
z.object({
|
|
34
42
|
id: z.string(),
|
|
43
|
+
name: z.string().optional(),
|
|
44
|
+
visibility: z.string().optional(),
|
|
35
45
|
parentId: z.string().nullable(),
|
|
36
46
|
rights: z
|
|
37
47
|
.object({ read: z.boolean(), write: z.boolean(), manage: z.boolean() })
|
|
@@ -208,3 +218,127 @@ describe("[integration] media API — owner lifecycle: teardown", () => {
|
|
|
208
218
|
expect(tree.data.folders).to.deep.equal([]);
|
|
209
219
|
});
|
|
210
220
|
});
|
|
221
|
+
|
|
222
|
+
async function folderIdOf(name: string, parentId?: string): Promise<string> {
|
|
223
|
+
const created = await client.post("/api/media/folders", { name, parentId });
|
|
224
|
+
expect(created.status, name).to.equal(HTTP_OK);
|
|
225
|
+
return readBody(created, createdBody).id;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function treeById() {
|
|
229
|
+
const tree = await client.get("/api/media/tree");
|
|
230
|
+
return new Map(
|
|
231
|
+
readBody(tree, treeBody).folders.map((folder) => [folder.id, folder]),
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
describe("[integration] media API — folder changes read back", () => {
|
|
236
|
+
it("shows a renamed, moved and published folder in the tree and its impact, and keeps a subfolder private", async () => {
|
|
237
|
+
const top = await folderIdOf("Tree top");
|
|
238
|
+
const target = await folderIdOf("Tree target");
|
|
239
|
+
try {
|
|
240
|
+
const child = await folderIdOf("Child", top);
|
|
241
|
+
const grand = await folderIdOf("Grand", child);
|
|
242
|
+
const png = await solidPng(4, 4);
|
|
243
|
+
const inTop = await uploadAsset(client, top, "top.png", "image/png", png);
|
|
244
|
+
const inChild = await uploadAsset(
|
|
245
|
+
client,
|
|
246
|
+
child,
|
|
247
|
+
"child.png",
|
|
248
|
+
"image/png",
|
|
249
|
+
png,
|
|
250
|
+
);
|
|
251
|
+
const rename = await client.post(`/api/media/folders/${child}/rename`, {
|
|
252
|
+
name: "Enfant",
|
|
253
|
+
});
|
|
254
|
+
const move = await client.post(`/api/media/folders/${child}/move`, {
|
|
255
|
+
parentId: target,
|
|
256
|
+
});
|
|
257
|
+
const inside = await client.post(`/api/media/folders/${child}/move`, {
|
|
258
|
+
parentId: grand,
|
|
259
|
+
});
|
|
260
|
+
const publish = (id: string) =>
|
|
261
|
+
client.post(`/api/media/folders/${id}/visibility`, {
|
|
262
|
+
visibility: "public",
|
|
263
|
+
});
|
|
264
|
+
const published = await publish(top);
|
|
265
|
+
expect([rename.status, move.status, published.status]).to.deep.equal([
|
|
266
|
+
HTTP_OK,
|
|
267
|
+
HTTP_OK,
|
|
268
|
+
HTTP_OK,
|
|
269
|
+
]);
|
|
270
|
+
expect(inside.status).to.equal(HTTP_BAD_REQUEST);
|
|
271
|
+
const folders = await treeById();
|
|
272
|
+
expect(folders.get(child)).to.include({
|
|
273
|
+
name: "Enfant",
|
|
274
|
+
parentId: target,
|
|
275
|
+
});
|
|
276
|
+
expect(folders.get(top)?.visibility).to.equal("public");
|
|
277
|
+
expect(folders.get(grand)?.visibility).to.equal("private");
|
|
278
|
+
expect((await rawGet(`/media/${inTop.id}/top.png`)).status).to.equal(
|
|
279
|
+
HTTP_FOUND,
|
|
280
|
+
);
|
|
281
|
+
const impact = await client.get(`/api/media/folders/${target}/impact`);
|
|
282
|
+
expect(impact.data).to.include({ subfolders: 2, files: 1 });
|
|
283
|
+
expect((await publish(target)).status).to.equal(HTTP_OK);
|
|
284
|
+
expect((await rawGet(`/media/${inChild.id}/child.png`)).status).to.equal(
|
|
285
|
+
HTTP_UNAUTHORIZED,
|
|
286
|
+
);
|
|
287
|
+
} finally {
|
|
288
|
+
await client.delete(`/api/media/folders/${top}`);
|
|
289
|
+
await client.delete(`/api/media/folders/${target}`);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("deletes a folder with its subfolders, their files and stored bytes, and leaves another folder's file serving the same bytes", async () => {
|
|
294
|
+
const doomed = await folderIdOf("Doomed");
|
|
295
|
+
const kept = await folderIdOf("Kept");
|
|
296
|
+
try {
|
|
297
|
+
const sub = await folderIdOf("Sub", doomed);
|
|
298
|
+
const bytes = await solidPng(8, 8);
|
|
299
|
+
const gone = await uploadAsset(
|
|
300
|
+
client,
|
|
301
|
+
sub,
|
|
302
|
+
"gone.png",
|
|
303
|
+
"image/png",
|
|
304
|
+
bytes,
|
|
305
|
+
);
|
|
306
|
+
const stays = await uploadAsset(
|
|
307
|
+
client,
|
|
308
|
+
kept,
|
|
309
|
+
"stays.png",
|
|
310
|
+
"image/png",
|
|
311
|
+
bytes,
|
|
312
|
+
);
|
|
313
|
+
const storageKey = (
|
|
314
|
+
await GetModel(MediaAssetModel, DEFAULT_TENANT_ID).get(gone.id)
|
|
315
|
+
)?.storageKey;
|
|
316
|
+
expect(storageKey, "stored file of the doomed asset").to.be.a("string");
|
|
317
|
+
expect(await FileExists(storageKey!)).to.equal(true);
|
|
318
|
+
const impact = await client.get(`/api/media/folders/${doomed}/impact`);
|
|
319
|
+
expect(impact.data).to.include({
|
|
320
|
+
subfolders: 1,
|
|
321
|
+
files: 1,
|
|
322
|
+
size: bytes.byteLength,
|
|
323
|
+
});
|
|
324
|
+
expect(
|
|
325
|
+
(await client.delete(`/api/media/folders/${doomed}`)).status,
|
|
326
|
+
).to.equal(HTTP_OK);
|
|
327
|
+
const folders = await treeById();
|
|
328
|
+
expect(folders.has(doomed) || folders.has(sub)).to.equal(false);
|
|
329
|
+
expect(folders.has(kept)).to.equal(true);
|
|
330
|
+
expect((await client.get(`/media/${gone.id}/gone.png`)).status).to.equal(
|
|
331
|
+
HTTP_NOT_FOUND,
|
|
332
|
+
);
|
|
333
|
+
expect(await FileExists(storageKey!)).to.equal(false);
|
|
334
|
+
const served = await client.get(`/media/${stays.id}/stays.png`, {
|
|
335
|
+
responseType: "arraybuffer",
|
|
336
|
+
});
|
|
337
|
+
expect(served.status).to.equal(HTTP_OK);
|
|
338
|
+
expect(Buffer.from(served.data).equals(bytes)).to.equal(true);
|
|
339
|
+
} finally {
|
|
340
|
+
await client.delete(`/api/media/folders/${doomed}`);
|
|
341
|
+
await client.delete(`/api/media/folders/${kept}`);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|