arocapi 5.0.0-beta.2 → 5.0.0-beta.3
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 +37 -824
- package/dist/app.d.ts +20 -7
- package/dist/app.js +42 -28
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +41 -56
- package/dist/app.test.js.map +1 -1
- package/dist/errorHandling.test.d.ts +1 -0
- package/dist/errorHandling.test.js +222 -0
- package/dist/errorHandling.test.js.map +1 -0
- package/dist/index.dev.js +4 -3
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/capabilities.test.js +8 -8
- package/dist/routes/capabilities.test.js.map +1 -1
- package/dist/routes/crate.test.js +67 -19
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +31 -49
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +16 -12
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +14 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +19 -13
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +6 -0
- package/dist/routes/file.js +67 -59
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +99 -22
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +44 -21
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/metadata.d.ts +6 -0
- package/dist/routes/metadata.js +49 -54
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +6 -2
- package/dist/routes/search.js +95 -99
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +85 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.js +2 -3
- package/dist/test/helpers/fastify.js.map +1 -1
- package/dist/test/integration.setup.d.ts +18 -0
- package/dist/test/integration.setup.js +15 -7
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +368 -2
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +10 -18
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +109 -47
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +12 -34
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +5 -9
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +5 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +13 -4
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/errors.test.js +14 -1
- package/dist/utils/errors.test.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +5 -4
- package/src/app.test.ts +45 -66
- package/src/app.ts +85 -54
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +8 -8
- package/src/routes/crate.test.ts +80 -19
- package/src/routes/entities.test.ts +16 -12
- package/src/routes/entities.ts +40 -59
- package/src/routes/entity.test.ts +19 -13
- package/src/routes/entity.ts +22 -35
- package/src/routes/file.test.ts +117 -22
- package/src/routes/file.ts +84 -62
- package/src/routes/files.test.ts +52 -26
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +64 -58
- package/src/routes/search.test.ts +88 -39
- package/src/routes/search.ts +123 -119
- package/src/test/helpers/fastify.ts +2 -3
- package/src/test/integration.setup.ts +18 -7
- package/src/test/integration.test.ts +472 -1
- package/src/transformers/default.test.ts +139 -47
- package/src/transformers/default.ts +103 -83
- package/src/transformers/transformer.test.ts +18 -44
- package/src/types/access.ts +20 -0
- package/src/types/context.ts +15 -0
- package/src/types/fileHandlers.ts +5 -13
- package/src/types/transformers.ts +4 -32
- package/src/types/visibility.ts +18 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.test.ts +16 -1
- package/src/utils/errors.ts +67 -10
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
package/src/routes/file.test.ts
CHANGED
|
@@ -2,8 +2,11 @@ import { createReadStream } from 'node:fs';
|
|
|
2
2
|
import { Readable } from 'node:stream';
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
5
|
+
import type { AccessFunction } from '../types/access.js';
|
|
5
6
|
import type { FileHandler, FileResult } from '../types/fileHandlers.js';
|
|
7
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
6
8
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
9
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
7
10
|
import fileRoute from './file.js';
|
|
8
11
|
|
|
9
12
|
vi.mock('node:fs', () => ({
|
|
@@ -15,11 +18,20 @@ describe('File Route', () => {
|
|
|
15
18
|
get: vi.fn(),
|
|
16
19
|
head: vi.fn(),
|
|
17
20
|
};
|
|
21
|
+
const mockAccess = vi.fn<AccessFunction>();
|
|
18
22
|
|
|
19
23
|
beforeEach(async () => {
|
|
20
24
|
await fastifyBefore();
|
|
21
|
-
await fastify.register(fileRoute, {
|
|
25
|
+
await fastify.register(fileRoute, {
|
|
26
|
+
access: mockAccess,
|
|
27
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
28
|
+
refusal: { promptLoginForHidden: false },
|
|
29
|
+
prisma,
|
|
30
|
+
fileHandler: mockFileHandler,
|
|
31
|
+
});
|
|
22
32
|
vi.clearAllMocks();
|
|
33
|
+
mockAccess.mockImplementation(AllPublicAccess);
|
|
34
|
+
prisma.entity.findMany.mockResolvedValue([]);
|
|
23
35
|
});
|
|
24
36
|
|
|
25
37
|
afterEach(async () => {
|
|
@@ -34,11 +46,25 @@ describe('File Route', () => {
|
|
|
34
46
|
meta: { storagePath: '/data/files/test.wav' },
|
|
35
47
|
createdAt: new Date(),
|
|
36
48
|
updatedAt: new Date(),
|
|
49
|
+
entity: {
|
|
50
|
+
id: 'http://example.com/file/test.wav',
|
|
51
|
+
name: 'test.wav',
|
|
52
|
+
description: 'A test file',
|
|
53
|
+
entityType: 'http://schema.org/MediaObject',
|
|
54
|
+
memberOf: 'http://example.com/object',
|
|
55
|
+
rootCollection: 'http://example.com/collection',
|
|
56
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
57
|
+
contentLicenseId: 'https://example.com/licence/restricted',
|
|
58
|
+
meta: {},
|
|
59
|
+
createdAt: new Date(),
|
|
60
|
+
updatedAt: new Date(),
|
|
61
|
+
},
|
|
37
62
|
};
|
|
63
|
+
const { entity: _entity, ...handlerFile } = mockFile;
|
|
38
64
|
|
|
39
65
|
describe('GET /file/:id', () => {
|
|
40
66
|
it('should stream file content successfully', async () => {
|
|
41
|
-
prisma.file.
|
|
67
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
42
68
|
|
|
43
69
|
const mockStream = Readable.from(['test file content']);
|
|
44
70
|
const mockResult: FileResult = {
|
|
@@ -66,7 +92,7 @@ describe('File Route', () => {
|
|
|
66
92
|
expect(response.body).toBe('test file content');
|
|
67
93
|
|
|
68
94
|
expect(mockFileHandler.get).toHaveBeenCalledWith(
|
|
69
|
-
|
|
95
|
+
handlerFile,
|
|
70
96
|
expect.objectContaining({
|
|
71
97
|
request: expect.any(Object),
|
|
72
98
|
fastify: expect.any(Object),
|
|
@@ -75,7 +101,7 @@ describe('File Route', () => {
|
|
|
75
101
|
});
|
|
76
102
|
|
|
77
103
|
it('should handle attachment disposition', async () => {
|
|
78
|
-
prisma.file.
|
|
104
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
79
105
|
|
|
80
106
|
const mockStream = Readable.from(['test']);
|
|
81
107
|
const mockResult: FileResult = {
|
|
@@ -98,7 +124,7 @@ describe('File Route', () => {
|
|
|
98
124
|
});
|
|
99
125
|
|
|
100
126
|
it('should use custom filename when provided', async () => {
|
|
101
|
-
prisma.file.
|
|
127
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
102
128
|
|
|
103
129
|
const mockStream = Readable.from(['test']);
|
|
104
130
|
const mockResult: FileResult = {
|
|
@@ -121,7 +147,7 @@ describe('File Route', () => {
|
|
|
121
147
|
});
|
|
122
148
|
|
|
123
149
|
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
124
|
-
prisma.file.
|
|
150
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
125
151
|
|
|
126
152
|
const mockResult: FileResult = {
|
|
127
153
|
type: 'redirect',
|
|
@@ -139,7 +165,7 @@ describe('File Route', () => {
|
|
|
139
165
|
});
|
|
140
166
|
|
|
141
167
|
it('should handle redirect response with noRedirect=true', async () => {
|
|
142
|
-
prisma.file.
|
|
168
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
143
169
|
|
|
144
170
|
const mockResult: FileResult = {
|
|
145
171
|
type: 'redirect',
|
|
@@ -158,7 +184,7 @@ describe('File Route', () => {
|
|
|
158
184
|
});
|
|
159
185
|
|
|
160
186
|
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
161
|
-
prisma.file.
|
|
187
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
162
188
|
|
|
163
189
|
const mockStream = Readable.from(['file content']);
|
|
164
190
|
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
@@ -186,7 +212,7 @@ describe('File Route', () => {
|
|
|
186
212
|
});
|
|
187
213
|
|
|
188
214
|
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
189
|
-
prisma.file.
|
|
215
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
190
216
|
|
|
191
217
|
const mockResult: FileResult = {
|
|
192
218
|
type: 'file',
|
|
@@ -214,7 +240,7 @@ describe('File Route', () => {
|
|
|
214
240
|
});
|
|
215
241
|
|
|
216
242
|
it('should return 404 when entity not found', async () => {
|
|
217
|
-
prisma.file.
|
|
243
|
+
prisma.file.findFirst.mockResolvedValue(null);
|
|
218
244
|
|
|
219
245
|
const response = await fastify.inject({
|
|
220
246
|
method: 'GET',
|
|
@@ -229,7 +255,7 @@ describe('File Route', () => {
|
|
|
229
255
|
});
|
|
230
256
|
|
|
231
257
|
it('should return 500 when database error occurs', async () => {
|
|
232
|
-
prisma.file.
|
|
258
|
+
prisma.file.findFirst.mockRejectedValue(new Error('Database connection failed'));
|
|
233
259
|
|
|
234
260
|
const response = await fastify.inject({
|
|
235
261
|
method: 'GET',
|
|
@@ -241,8 +267,62 @@ describe('File Route', () => {
|
|
|
241
267
|
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
242
268
|
});
|
|
243
269
|
|
|
270
|
+
it('should decide access on the file entity without looking up its parents', async () => {
|
|
271
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
272
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue({ type: 'redirect', url: 'https://example.com/test.wav' });
|
|
273
|
+
|
|
274
|
+
await fastify.inject({
|
|
275
|
+
method: 'GET',
|
|
276
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
expect(prisma.file.findFirst).toHaveBeenCalledWith(
|
|
280
|
+
expect.objectContaining({ include: { entity: { omit: { meta: true } } } }),
|
|
281
|
+
);
|
|
282
|
+
expect(mockAccess).toHaveBeenCalledWith(
|
|
283
|
+
expect.objectContaining({
|
|
284
|
+
id: 'http://example.com/file/test.wav',
|
|
285
|
+
contentLicenseId: 'https://example.com/licence/restricted',
|
|
286
|
+
memberOf: 'http://example.com/object',
|
|
287
|
+
rootCollection: 'http://example.com/collection',
|
|
288
|
+
}),
|
|
289
|
+
expect.objectContaining({ request: expect.any(Object), fastify: expect.any(Object) }),
|
|
290
|
+
);
|
|
291
|
+
expect(prisma.entity.findMany).not.toHaveBeenCalled();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should return 403 when the content is not accessible', async () => {
|
|
295
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
296
|
+
mockAccess.mockReturnValue({ metadata: true, content: false });
|
|
297
|
+
|
|
298
|
+
const response = await fastify.inject({
|
|
299
|
+
method: 'GET',
|
|
300
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
301
|
+
});
|
|
302
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
303
|
+
|
|
304
|
+
expect(response.statusCode).toBe(403);
|
|
305
|
+
expect(body.error.code).toBe('FORBIDDEN');
|
|
306
|
+
expect(body.error.message).toBe('You do not have permission to access this resource');
|
|
307
|
+
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('should serve the content when only the metadata is not accessible', async () => {
|
|
311
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
312
|
+
mockAccess.mockReturnValue({ metadata: false, content: true });
|
|
313
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue({ type: 'redirect', url: 'https://example.com/test.wav' });
|
|
314
|
+
|
|
315
|
+
const response = await fastify.inject({
|
|
316
|
+
method: 'GET',
|
|
317
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(response.statusCode).toBe(302);
|
|
321
|
+
expect(mockFileHandler.get).toHaveBeenCalled();
|
|
322
|
+
});
|
|
323
|
+
|
|
244
324
|
it('should return 500 when fileHandler throws error', async () => {
|
|
245
|
-
prisma.file.
|
|
325
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
246
326
|
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
247
327
|
|
|
248
328
|
const response = await fastify.inject({
|
|
@@ -263,11 +343,11 @@ describe('File Route', () => {
|
|
|
263
343
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
264
344
|
|
|
265
345
|
expect(response.statusCode).toBe(400);
|
|
266
|
-
expect(body.error).toBe('
|
|
346
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
267
347
|
});
|
|
268
348
|
|
|
269
349
|
it('should validate disposition parameter', async () => {
|
|
270
|
-
prisma.file.
|
|
350
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
271
351
|
|
|
272
352
|
const response = await fastify.inject({
|
|
273
353
|
method: 'GET',
|
|
@@ -276,11 +356,11 @@ describe('File Route', () => {
|
|
|
276
356
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
277
357
|
|
|
278
358
|
expect(response.statusCode).toBe(400);
|
|
279
|
-
expect(body.error).toBe('
|
|
359
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
280
360
|
});
|
|
281
361
|
|
|
282
362
|
it('should return 404 when get handler returns false', async () => {
|
|
283
|
-
prisma.file.
|
|
363
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
284
364
|
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
285
365
|
|
|
286
366
|
const response = await fastify.inject({
|
|
@@ -297,7 +377,7 @@ describe('File Route', () => {
|
|
|
297
377
|
|
|
298
378
|
describe('HEAD /file/:id', () => {
|
|
299
379
|
it('should return headers without body using head handler', async () => {
|
|
300
|
-
prisma.file.
|
|
380
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
301
381
|
|
|
302
382
|
const mockMetadata = {
|
|
303
383
|
contentType: 'audio/wav',
|
|
@@ -319,7 +399,7 @@ describe('File Route', () => {
|
|
|
319
399
|
expect(response.body).toBe(''); // No body for HEAD
|
|
320
400
|
|
|
321
401
|
expect(mockFileHandler.head).toHaveBeenCalledWith(
|
|
322
|
-
|
|
402
|
+
handlerFile,
|
|
323
403
|
expect.objectContaining({
|
|
324
404
|
request: expect.any(Object),
|
|
325
405
|
fastify: expect.any(Object),
|
|
@@ -328,7 +408,7 @@ describe('File Route', () => {
|
|
|
328
408
|
});
|
|
329
409
|
|
|
330
410
|
it('should return 404 when file not found', async () => {
|
|
331
|
-
prisma.file.
|
|
411
|
+
prisma.file.findFirst.mockResolvedValue(null);
|
|
332
412
|
|
|
333
413
|
const response = await fastify.inject({
|
|
334
414
|
method: 'HEAD',
|
|
@@ -341,8 +421,21 @@ describe('File Route', () => {
|
|
|
341
421
|
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
342
422
|
});
|
|
343
423
|
|
|
424
|
+
it('should return 403 when the content is not accessible', async () => {
|
|
425
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
426
|
+
mockAccess.mockReturnValue({ metadata: true, content: false });
|
|
427
|
+
|
|
428
|
+
const response = await fastify.inject({
|
|
429
|
+
method: 'HEAD',
|
|
430
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(response.statusCode).toBe(403);
|
|
434
|
+
expect(mockFileHandler.head).not.toHaveBeenCalled();
|
|
435
|
+
});
|
|
436
|
+
|
|
344
437
|
it('should return 500 when head handler throws error', async () => {
|
|
345
|
-
prisma.file.
|
|
438
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
346
439
|
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
347
440
|
|
|
348
441
|
const response = await fastify.inject({
|
|
@@ -356,7 +449,7 @@ describe('File Route', () => {
|
|
|
356
449
|
});
|
|
357
450
|
|
|
358
451
|
it('should return 404 when head handler returns false', async () => {
|
|
359
|
-
prisma.file.
|
|
452
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
360
453
|
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
361
454
|
|
|
362
455
|
const response = await fastify.inject({
|
|
@@ -373,7 +466,7 @@ describe('File Route', () => {
|
|
|
373
466
|
|
|
374
467
|
describe('GET /file/:id - exhaustiveness check', () => {
|
|
375
468
|
it('should handle unexpected file result type', async () => {
|
|
376
|
-
prisma.file.
|
|
469
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
377
470
|
|
|
378
471
|
// Mock an invalid result type to test exhaustiveness check
|
|
379
472
|
const invalidResult = {
|
|
@@ -390,6 +483,8 @@ describe('File Route', () => {
|
|
|
390
483
|
|
|
391
484
|
expect(response.statusCode).toBe(500);
|
|
392
485
|
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
486
|
+
expect(response.headers['content-disposition']).toBeUndefined();
|
|
487
|
+
expect(response.headers['content-type']).toContain('application/json');
|
|
393
488
|
});
|
|
394
489
|
});
|
|
395
490
|
});
|
package/src/routes/file.ts
CHANGED
|
@@ -3,9 +3,14 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
3
3
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { RequestContext } from '../types/context.js';
|
|
6
8
|
import type { FileHandler, FileMetadata } from '../types/fileHandlers.js';
|
|
7
|
-
import {
|
|
9
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
10
|
+
import { createAccessGuard } from '../utils/access.js';
|
|
11
|
+
import { createNotFoundError } from '../utils/errors.js';
|
|
8
12
|
import { setFileHeaders } from '../utils/headers.js';
|
|
13
|
+
import { createNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
9
14
|
|
|
10
15
|
const paramsSchema = z.object({
|
|
11
16
|
id: z.url(),
|
|
@@ -20,10 +25,36 @@ const querySchema = z.object({
|
|
|
20
25
|
type FileRouteOptions = {
|
|
21
26
|
prisma: PrismaClient;
|
|
22
27
|
fileHandler: FileHandler;
|
|
28
|
+
access: AccessFunction;
|
|
29
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
30
|
+
refusal: RefusalOptions;
|
|
23
31
|
};
|
|
24
32
|
|
|
25
33
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
26
|
-
const { prisma, fileHandler } = opts;
|
|
34
|
+
const { prisma, fileHandler, access, entityVisibilityFilter, refusal } = opts;
|
|
35
|
+
|
|
36
|
+
// Split the entity off so handlers receive only the File they're typed for
|
|
37
|
+
const findVisibleFile = async (id: string, context: RequestContext) => {
|
|
38
|
+
const found = await prisma.file.findFirst({
|
|
39
|
+
where: { id, entity: await entityVisibilityFilter(context) },
|
|
40
|
+
include: { entity: { omit: { meta: true } } },
|
|
41
|
+
});
|
|
42
|
+
if (!found) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { entity, ...file } = found;
|
|
47
|
+
|
|
48
|
+
return { file, entity };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const denyContent = createAccessGuard({ access, isAuthenticated: refusal.isAuthenticated, level: 'content' });
|
|
52
|
+
|
|
53
|
+
const sendFileNotVisible = createNotVisibleResponder({
|
|
54
|
+
...refusal,
|
|
55
|
+
notFoundMessage: 'The requested file was not found',
|
|
56
|
+
exists: async (id) => (await prisma.file.count({ where: { id } })) > 0,
|
|
57
|
+
});
|
|
27
58
|
|
|
28
59
|
fastify.withTypeProvider<ZodTypeProvider>().head(
|
|
29
60
|
'/file/:id',
|
|
@@ -35,30 +66,27 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
35
66
|
async (request, reply) => {
|
|
36
67
|
const { id } = request.params;
|
|
37
68
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
where: { id },
|
|
41
|
-
});
|
|
69
|
+
const context = { request, fastify };
|
|
70
|
+
const visible = await findVisibleFile(id, context);
|
|
42
71
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
72
|
+
if (!visible) {
|
|
73
|
+
return sendFileNotVisible(reply, id, context);
|
|
74
|
+
}
|
|
46
75
|
|
|
47
|
-
|
|
76
|
+
const { file, entity } = visible;
|
|
77
|
+
if (await denyContent(reply, entity, context)) {
|
|
78
|
+
return reply;
|
|
79
|
+
}
|
|
48
80
|
|
|
49
|
-
|
|
50
|
-
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
51
|
-
}
|
|
81
|
+
const metadata: FileMetadata | false = await fileHandler.head(file, context);
|
|
52
82
|
|
|
53
|
-
|
|
83
|
+
if (!metadata) {
|
|
84
|
+
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
85
|
+
}
|
|
54
86
|
|
|
55
|
-
|
|
56
|
-
} catch (error) {
|
|
57
|
-
const err = error as Error;
|
|
58
|
-
fastify.log.error(`File metadata retrieval error: ${err.message}`);
|
|
87
|
+
setFileHeaders(reply, metadata);
|
|
59
88
|
|
|
60
|
-
|
|
61
|
-
}
|
|
89
|
+
return reply.code(200).send();
|
|
62
90
|
},
|
|
63
91
|
);
|
|
64
92
|
|
|
@@ -73,61 +101,55 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
73
101
|
async (request, reply) => {
|
|
74
102
|
const { id } = request.params;
|
|
75
103
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
where: { id },
|
|
79
|
-
});
|
|
104
|
+
const context = { request, fastify };
|
|
105
|
+
const visible = await findVisibleFile(id, context);
|
|
80
106
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
107
|
+
if (!visible) {
|
|
108
|
+
return sendFileNotVisible(reply, id, context);
|
|
109
|
+
}
|
|
84
110
|
|
|
85
|
-
|
|
111
|
+
const { file, entity } = visible;
|
|
112
|
+
if (await denyContent(reply, entity, context)) {
|
|
113
|
+
return reply;
|
|
114
|
+
}
|
|
86
115
|
|
|
87
|
-
|
|
88
|
-
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
89
|
-
}
|
|
116
|
+
const result = await fileHandler.get(file, context);
|
|
90
117
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
118
|
+
if (!result) {
|
|
119
|
+
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
120
|
+
}
|
|
95
121
|
|
|
96
|
-
|
|
122
|
+
if (result.type === 'redirect') {
|
|
123
|
+
if (request.query.noRedirect) {
|
|
124
|
+
return reply.code(200).send({ location: result.url });
|
|
97
125
|
}
|
|
98
126
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
setFileHeaders(reply, result.metadata);
|
|
102
|
-
reply.header('Content-Disposition', `${disposition}; filename="${filename}"`);
|
|
127
|
+
return reply.code(302).redirect(result.url);
|
|
128
|
+
}
|
|
103
129
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
130
|
+
const { disposition, filename: customFilename } = request.query;
|
|
131
|
+
const filename = customFilename || file.filename;
|
|
132
|
+
setFileHeaders(reply, result.metadata);
|
|
133
|
+
reply.header('Content-Disposition', `${disposition}; filename="${filename}"`);
|
|
108
134
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
135
|
+
// Handle stream response
|
|
136
|
+
if (result.type === 'stream') {
|
|
137
|
+
return reply.code(200).send(result.stream);
|
|
138
|
+
}
|
|
114
139
|
|
|
115
|
-
|
|
116
|
-
|
|
140
|
+
if (result.type === 'file') {
|
|
141
|
+
if (result.accelPath) {
|
|
142
|
+
reply.header('X-Accel-Redirect', result.accelPath);
|
|
143
|
+
return reply.code(200).send();
|
|
117
144
|
}
|
|
118
145
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
reply.removeHeader('Content-Disposition');
|
|
122
|
-
reply.removeHeader('Content-Type');
|
|
123
|
-
reply.removeHeader('Content-Length');
|
|
124
|
-
return reply.code(500).send(createInternalError());
|
|
125
|
-
} catch (error) {
|
|
126
|
-
const err = error as Error;
|
|
127
|
-
fastify.log.error(`File retrieval error: ${err}`);
|
|
128
|
-
|
|
129
|
-
return reply.code(500).send(createInternalError());
|
|
146
|
+
const stream = createReadStream(result.path);
|
|
147
|
+
return reply.code(200).send(stream);
|
|
130
148
|
}
|
|
149
|
+
|
|
150
|
+
// Exhaustiveness check - should never reach here with proper types
|
|
151
|
+
reply.removeHeader('Content-Disposition');
|
|
152
|
+
throw new Error(`Unexpected file result type: ${(result as { type: string }).type}`);
|
|
131
153
|
},
|
|
132
154
|
);
|
|
133
155
|
};
|
package/src/routes/files.test.ts
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AccessFunction, FileTransformer } from '../app.js';
|
|
3
3
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
5
5
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
6
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
6
7
|
import filesRoute from './files.js';
|
|
7
8
|
|
|
8
9
|
describe('Files Route', () => {
|
|
10
|
+
const mockEntity = (id: string, name: string) => ({
|
|
11
|
+
id,
|
|
12
|
+
name,
|
|
13
|
+
description: '',
|
|
14
|
+
entityType: 'http://schema.org/MediaObject',
|
|
15
|
+
memberOf: null,
|
|
16
|
+
rootCollection: null,
|
|
17
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
18
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
19
|
+
meta: {},
|
|
20
|
+
createdAt: new Date('2025-01-01'),
|
|
21
|
+
updatedAt: new Date('2025-01-01'),
|
|
22
|
+
});
|
|
23
|
+
|
|
9
24
|
beforeEach(async () => {
|
|
10
25
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(filesRoute, {
|
|
26
|
+
await fastify.register(filesRoute, {
|
|
27
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
28
|
+
prisma,
|
|
29
|
+
access: AllPublicAccess,
|
|
30
|
+
});
|
|
12
31
|
});
|
|
13
32
|
|
|
14
33
|
afterEach(async () => {
|
|
@@ -23,6 +42,7 @@ describe('Files Route', () => {
|
|
|
23
42
|
meta: {},
|
|
24
43
|
createdAt: new Date('2025-01-01'),
|
|
25
44
|
updatedAt: new Date('2025-01-01'),
|
|
45
|
+
entity: mockEntity('http://example.com/file1.wav', 'file1.wav'),
|
|
26
46
|
};
|
|
27
47
|
|
|
28
48
|
const mockFile2 = {
|
|
@@ -33,6 +53,7 @@ describe('Files Route', () => {
|
|
|
33
53
|
meta: {},
|
|
34
54
|
createdAt: new Date('2025-01-02'),
|
|
35
55
|
updatedAt: new Date('2025-01-02'),
|
|
56
|
+
entity: mockEntity('http://example.com/file2.txt', 'file2.txt'),
|
|
36
57
|
};
|
|
37
58
|
|
|
38
59
|
describe('GET /files', () => {
|
|
@@ -58,6 +79,9 @@ describe('Files Route', () => {
|
|
|
58
79
|
content: true,
|
|
59
80
|
},
|
|
60
81
|
});
|
|
82
|
+
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
83
|
+
expect.objectContaining({ include: { entity: { omit: { meta: true } } } }),
|
|
84
|
+
);
|
|
61
85
|
});
|
|
62
86
|
|
|
63
87
|
it('should filter files by memberOf', async () => {
|
|
@@ -79,7 +103,7 @@ describe('Files Route', () => {
|
|
|
79
103
|
});
|
|
80
104
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
81
105
|
expect.objectContaining({
|
|
82
|
-
where: { entity: { memberOf: 'http://example.com/collection/1' } },
|
|
106
|
+
where: { entity: { AND: [{ memberOf: 'http://example.com/collection/1' }, {}] } },
|
|
83
107
|
}),
|
|
84
108
|
);
|
|
85
109
|
});
|
|
@@ -175,7 +199,7 @@ describe('Files Route', () => {
|
|
|
175
199
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
176
200
|
|
|
177
201
|
expect(response.statusCode).toBe(400);
|
|
178
|
-
expect(body.error).toBe('
|
|
202
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
179
203
|
});
|
|
180
204
|
|
|
181
205
|
it('should validate memberOf parameter format', async () => {
|
|
@@ -186,7 +210,7 @@ describe('Files Route', () => {
|
|
|
186
210
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
187
211
|
|
|
188
212
|
expect(response.statusCode).toBe(400);
|
|
189
|
-
expect(body.error).toBe('
|
|
213
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
190
214
|
});
|
|
191
215
|
|
|
192
216
|
it('should validate sort parameter', async () => {
|
|
@@ -197,7 +221,7 @@ describe('Files Route', () => {
|
|
|
197
221
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
198
222
|
|
|
199
223
|
expect(response.statusCode).toBe(400);
|
|
200
|
-
expect(body.error).toBe('
|
|
224
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
201
225
|
});
|
|
202
226
|
});
|
|
203
227
|
|
|
@@ -213,8 +237,9 @@ describe('Files Route', () => {
|
|
|
213
237
|
});
|
|
214
238
|
|
|
215
239
|
await fastify.register(filesRoute, {
|
|
240
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
216
241
|
prisma,
|
|
217
|
-
|
|
242
|
+
access: AllPublicAccess,
|
|
218
243
|
fileTransformers: [customTransformer],
|
|
219
244
|
});
|
|
220
245
|
|
|
@@ -238,25 +263,27 @@ describe('Files Route', () => {
|
|
|
238
263
|
await fastifyAfter();
|
|
239
264
|
});
|
|
240
265
|
|
|
241
|
-
it('should
|
|
266
|
+
it('should attach the content access of each file entity', async () => {
|
|
242
267
|
await fastifyBefore();
|
|
243
268
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
269
|
+
const access: AccessFunction = (entity) =>
|
|
270
|
+
entity.id === 'http://example.com/file1.wav'
|
|
271
|
+
? {
|
|
272
|
+
metadata: false,
|
|
273
|
+
content: false,
|
|
274
|
+
metadataAuthorizationUrl: 'https://example.com/request-metadata',
|
|
275
|
+
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
276
|
+
}
|
|
277
|
+
: { metadata: true, content: true };
|
|
252
278
|
|
|
253
279
|
await fastify.register(filesRoute, {
|
|
280
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
254
281
|
prisma,
|
|
255
|
-
|
|
282
|
+
access,
|
|
256
283
|
});
|
|
257
284
|
|
|
258
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
259
|
-
prisma.file.count.mockResolvedValue(
|
|
285
|
+
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
286
|
+
prisma.file.count.mockResolvedValue(2);
|
|
260
287
|
|
|
261
288
|
const response = await fastify.inject({
|
|
262
289
|
method: 'GET',
|
|
@@ -264,13 +291,12 @@ describe('Files Route', () => {
|
|
|
264
291
|
});
|
|
265
292
|
|
|
266
293
|
expect(response.statusCode).toBe(200);
|
|
267
|
-
const body = JSON.parse(response.body) as { files: unknown
|
|
268
|
-
expect(body.files[0]).
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
272
|
-
},
|
|
294
|
+
const body = JSON.parse(response.body) as { files: Array<{ access: unknown }> };
|
|
295
|
+
expect(body.files[0].access).toEqual({
|
|
296
|
+
content: false,
|
|
297
|
+
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
273
298
|
});
|
|
299
|
+
expect(body.files[1].access).toEqual({ content: true });
|
|
274
300
|
|
|
275
301
|
await fastifyAfter();
|
|
276
302
|
});
|