arocapi 1.1.0 → 1.2.2
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/LICENSE.md +5 -0
- package/README.md +459 -36
- package/dist/app.d.ts +19 -1
- package/dist/app.js +37 -29
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +124 -0
- package/dist/app.test.js.map +1 -0
- package/dist/index.dev.js +146 -0
- package/dist/index.dev.js.map +1 -0
- package/dist/routes/crate.d.ts +7 -0
- package/dist/routes/crate.js +101 -0
- package/dist/routes/crate.js.map +1 -0
- package/dist/routes/crate.test.d.ts +1 -0
- package/dist/routes/crate.test.js +360 -0
- package/dist/routes/crate.test.js.map +1 -0
- package/dist/routes/entities.d.ts +6 -1
- package/dist/routes/entities.js +52 -39
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.d.ts +1 -0
- package/dist/routes/entities.test.js +245 -0
- package/dist/routes/entities.test.js.map +1 -0
- package/dist/routes/entity.d.ts +6 -1
- package/dist/routes/entity.js +30 -8
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.d.ts +1 -0
- package/dist/routes/entity.test.js +109 -0
- package/dist/routes/entity.test.js.map +1 -0
- package/dist/routes/file.d.ts +7 -0
- package/dist/routes/file.js +113 -0
- package/dist/routes/file.js.map +1 -0
- package/dist/routes/file.test.d.ts +1 -0
- package/dist/routes/file.test.js +319 -0
- package/dist/routes/file.test.js.map +1 -0
- package/dist/routes/files.d.ts +8 -0
- package/dist/routes/files.js +62 -0
- package/dist/routes/files.js.map +1 -0
- package/dist/routes/files.test.d.ts +1 -0
- package/dist/routes/files.test.js +244 -0
- package/dist/routes/files.test.js.map +1 -0
- package/dist/routes/search.d.ts +6 -1
- package/dist/routes/search.js +44 -19
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.d.ts +1 -0
- package/dist/routes/search.test.js +698 -0
- package/dist/routes/search.test.js.map +1 -0
- package/dist/test/helpers/fastify.d.ts +9 -0
- package/dist/test/helpers/fastify.js +24 -0
- package/dist/test/helpers/fastify.js.map +1 -0
- package/dist/test/integration.setup.d.ts +58 -0
- package/dist/test/integration.setup.js +181 -0
- package/dist/test/integration.setup.js.map +1 -0
- package/dist/test/integration.test.d.ts +1 -0
- package/dist/test/integration.test.js +337 -0
- package/dist/test/integration.test.js.map +1 -0
- package/dist/transformers/default.d.ts +45 -0
- package/dist/transformers/default.js +46 -0
- package/dist/transformers/default.js.map +1 -0
- package/dist/transformers/default.test.d.ts +1 -0
- package/dist/transformers/default.test.js +177 -0
- package/dist/transformers/default.test.js.map +1 -0
- package/dist/transformers/transformer.test.d.ts +1 -0
- package/dist/transformers/transformer.test.js +176 -0
- package/dist/transformers/transformer.test.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +41 -0
- package/dist/types/fileHandlers.js +2 -0
- package/dist/types/fileHandlers.js.map +1 -0
- package/dist/types/transformers.d.ts +10 -0
- package/dist/types/transformers.js +2 -0
- package/dist/types/transformers.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +21 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/errors.test.d.ts +1 -0
- package/dist/utils/errors.test.js +66 -0
- package/dist/utils/errors.test.js.map +1 -0
- package/package.json +35 -28
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
- package/prisma/models/entity.prisma +16 -12
- package/prisma/models/file.prisma +20 -0
- package/prisma.config.ts +6 -1
- package/src/__snapshots__/app.test.ts.snap +8 -0
- package/src/app.test.ts +158 -0
- package/src/app.ts +99 -36
- package/src/index.dev.ts +196 -0
- package/src/reset.d.ts +1 -0
- package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
- package/src/routes/__snapshots__/entity.test.ts.snap +57 -0
- package/src/routes/__snapshots__/search.test.ts.snap +139 -0
- package/src/routes/crate.test.ts +441 -0
- package/src/routes/crate.ts +145 -0
- package/src/routes/entities.test.ts +284 -0
- package/src/routes/entities.ts +69 -48
- package/src/routes/entity.test.ts +132 -0
- package/src/routes/entity.ts +59 -22
- package/src/routes/file.test.ts +399 -0
- package/src/routes/file.ts +157 -0
- package/src/routes/files.test.ts +303 -0
- package/src/routes/files.ts +89 -0
- package/src/routes/search.test.ts +807 -0
- package/src/routes/search.ts +67 -25
- package/src/test/__snapshots__/integration.test.ts.snap +18 -0
- package/src/test/helpers/fastify.ts +39 -0
- package/src/test/integration.setup.ts +200 -0
- package/src/test/integration.test.ts +411 -0
- package/src/transformers/default.test.ts +199 -0
- package/src/transformers/default.ts +165 -0
- package/src/transformers/transformer.test.ts +233 -0
- package/src/types/fileHandlers.ts +65 -0
- package/src/types/transformers.ts +52 -0
- package/src/utils/errors.test.ts +75 -0
- package/src/utils/errors.ts +45 -0
- package/dist/index.js +0 -25
- package/dist/index.js.map +0 -1
- package/dist/scripts/loadEntities.js +0 -170
- package/dist/scripts/loadEntities.js.map +0 -1
- package/dist/type.d.ts +0 -6
- package/dist/type.js +0 -2
- package/dist/type.js.map +0 -1
- package/src/index.ts +0 -35
- package/src/scripts/loadEntities.ts +0 -223
- package/src/type.ts +0 -7
- /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
- /package/dist/{index.d.ts → index.dev.d.ts} +0 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
5
|
+
import type { FileHandler, FileResult } from '../types/fileHandlers.js';
|
|
6
|
+
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
7
|
+
import fileRoute from './file.js';
|
|
8
|
+
|
|
9
|
+
vi.mock('node:fs', () => ({
|
|
10
|
+
createReadStream: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
describe('File Route', () => {
|
|
14
|
+
const mockFileHandler: FileHandler = {
|
|
15
|
+
get: vi.fn(),
|
|
16
|
+
head: vi.fn(),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
await fastifyBefore();
|
|
21
|
+
await fastify.register(fileRoute, { fileHandler: mockFileHandler });
|
|
22
|
+
vi.clearAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(async () => {
|
|
26
|
+
await fastifyAfter();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const mockFile = {
|
|
30
|
+
id: 1,
|
|
31
|
+
fileId: 'http://example.com/file/test.wav',
|
|
32
|
+
filename: 'test.wav',
|
|
33
|
+
mediaType: 'audio/wav',
|
|
34
|
+
size: BigInt(1024),
|
|
35
|
+
memberOf: 'http://example.com/collection',
|
|
36
|
+
rootCollection: 'http://example.com/collection',
|
|
37
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
38
|
+
meta: { storagePath: '/data/files/test.wav' },
|
|
39
|
+
createdAt: new Date(),
|
|
40
|
+
updatedAt: new Date(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe('GET /file/:id', () => {
|
|
44
|
+
it('should stream file content successfully', async () => {
|
|
45
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
46
|
+
|
|
47
|
+
const mockStream = Readable.from(['test file content']);
|
|
48
|
+
const mockResult: FileResult = {
|
|
49
|
+
type: 'stream',
|
|
50
|
+
stream: mockStream,
|
|
51
|
+
metadata: {
|
|
52
|
+
contentType: 'audio/wav',
|
|
53
|
+
contentLength: 17,
|
|
54
|
+
etag: '"abc123"',
|
|
55
|
+
lastModified: new Date('2025-01-01'),
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
59
|
+
|
|
60
|
+
const response = await fastify.inject({
|
|
61
|
+
method: 'GET',
|
|
62
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(response.statusCode).toBe(200);
|
|
66
|
+
expect(response.headers['content-type']).toBe('audio/wav');
|
|
67
|
+
expect(response.headers['content-length']).toBe('17');
|
|
68
|
+
expect(response.headers.etag).toBe('"abc123"');
|
|
69
|
+
expect(response.headers['content-disposition']).toBe('inline; filename="test.wav"');
|
|
70
|
+
expect(response.body).toBe('test file content');
|
|
71
|
+
|
|
72
|
+
expect(mockFileHandler.get).toHaveBeenCalledWith(
|
|
73
|
+
mockFile,
|
|
74
|
+
expect.objectContaining({
|
|
75
|
+
request: expect.any(Object),
|
|
76
|
+
fastify: expect.any(Object),
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should handle attachment disposition', async () => {
|
|
82
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
83
|
+
|
|
84
|
+
const mockStream = Readable.from(['test']);
|
|
85
|
+
const mockResult: FileResult = {
|
|
86
|
+
type: 'stream',
|
|
87
|
+
stream: mockStream,
|
|
88
|
+
metadata: {
|
|
89
|
+
contentType: 'audio/wav',
|
|
90
|
+
contentLength: 4,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
94
|
+
|
|
95
|
+
const response = await fastify.inject({
|
|
96
|
+
method: 'GET',
|
|
97
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?disposition=attachment`,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(response.statusCode).toBe(200);
|
|
101
|
+
expect(response.headers['content-disposition']).toBe('attachment; filename="test.wav"');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should use custom filename when provided', async () => {
|
|
105
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
106
|
+
|
|
107
|
+
const mockStream = Readable.from(['test']);
|
|
108
|
+
const mockResult: FileResult = {
|
|
109
|
+
type: 'stream',
|
|
110
|
+
stream: mockStream,
|
|
111
|
+
metadata: {
|
|
112
|
+
contentType: 'audio/wav',
|
|
113
|
+
contentLength: 4,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
117
|
+
|
|
118
|
+
const response = await fastify.inject({
|
|
119
|
+
method: 'GET',
|
|
120
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?filename=custom-name.wav`,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(response.statusCode).toBe(200);
|
|
124
|
+
expect(response.headers['content-disposition']).toBe('inline; filename="custom-name.wav"');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
128
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
129
|
+
|
|
130
|
+
const mockResult: FileResult = {
|
|
131
|
+
type: 'redirect',
|
|
132
|
+
url: 'https://storage.example.com/files/test.wav',
|
|
133
|
+
};
|
|
134
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
135
|
+
|
|
136
|
+
const response = await fastify.inject({
|
|
137
|
+
method: 'GET',
|
|
138
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
expect(response.statusCode).toBe(302);
|
|
142
|
+
expect(response.headers.location).toBe('https://storage.example.com/files/test.wav');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should handle redirect response with noRedirect=true', async () => {
|
|
146
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
147
|
+
|
|
148
|
+
const mockResult: FileResult = {
|
|
149
|
+
type: 'redirect',
|
|
150
|
+
url: 'https://storage.example.com/files/test.wav',
|
|
151
|
+
};
|
|
152
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
153
|
+
|
|
154
|
+
const response = await fastify.inject({
|
|
155
|
+
method: 'GET',
|
|
156
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?noRedirect=true`,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(response.statusCode).toBe(200);
|
|
160
|
+
const body = JSON.parse(response.body);
|
|
161
|
+
expect(body).toEqual({ location: 'https://storage.example.com/files/test.wav' });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
165
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
166
|
+
|
|
167
|
+
const mockStream = Readable.from(['file content']);
|
|
168
|
+
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
169
|
+
|
|
170
|
+
const mockResult: FileResult = {
|
|
171
|
+
type: 'file',
|
|
172
|
+
path: '/data/files/test.wav',
|
|
173
|
+
metadata: {
|
|
174
|
+
contentType: 'audio/wav',
|
|
175
|
+
contentLength: 1024,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
179
|
+
|
|
180
|
+
const response = await fastify.inject({
|
|
181
|
+
method: 'GET',
|
|
182
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(response.statusCode).toBe(200);
|
|
186
|
+
expect(response.headers['content-type']).toBe('audio/wav');
|
|
187
|
+
expect(response.headers['content-length']).toBe('1024');
|
|
188
|
+
expect(response.headers['x-accel-redirect']).toBeUndefined();
|
|
189
|
+
expect(createReadStream).toHaveBeenCalledWith('/data/files/test.wav');
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
193
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
194
|
+
|
|
195
|
+
const mockResult: FileResult = {
|
|
196
|
+
type: 'file',
|
|
197
|
+
path: '/data/files/test.wav',
|
|
198
|
+
accelPath: '/internal/files/test.wav',
|
|
199
|
+
metadata: {
|
|
200
|
+
contentType: 'audio/wav',
|
|
201
|
+
contentLength: 1024,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
205
|
+
|
|
206
|
+
const response = await fastify.inject({
|
|
207
|
+
method: 'GET',
|
|
208
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(response.statusCode).toBe(200);
|
|
212
|
+
expect(response.headers['content-type']).toBe('audio/wav');
|
|
213
|
+
// Note: Content-Length is '0' because Fastify sends an empty response.
|
|
214
|
+
// In production, nginx will replace this with the actual file size when serving via X-Accel-Redirect
|
|
215
|
+
expect(response.headers['content-length']).toBe('0');
|
|
216
|
+
expect(response.headers['x-accel-redirect']).toBe('/internal/files/test.wav');
|
|
217
|
+
expect(response.body).toBe(''); // Empty body when using X-Accel-Redirect
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should return 404 when entity not found', async () => {
|
|
221
|
+
prisma.entity.findFirst.mockResolvedValue(null);
|
|
222
|
+
|
|
223
|
+
const response = await fastify.inject({
|
|
224
|
+
method: 'GET',
|
|
225
|
+
url: `/file/${encodeURIComponent('http://example.com/file/nonexistent')}`,
|
|
226
|
+
});
|
|
227
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
228
|
+
|
|
229
|
+
expect(response.statusCode).toBe(404);
|
|
230
|
+
expect(body.error.code).toBe('NOT_FOUND');
|
|
231
|
+
expect(body.error.message).toBe('The requested file was not found');
|
|
232
|
+
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should return 500 when database error occurs', async () => {
|
|
236
|
+
prisma.file.findFirst.mockRejectedValue(new Error('Database connection failed'));
|
|
237
|
+
|
|
238
|
+
const response = await fastify.inject({
|
|
239
|
+
method: 'GET',
|
|
240
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
241
|
+
});
|
|
242
|
+
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
243
|
+
|
|
244
|
+
expect(response.statusCode).toBe(500);
|
|
245
|
+
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should return 500 when fileHandler throws error', async () => {
|
|
249
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
250
|
+
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
251
|
+
|
|
252
|
+
const response = await fastify.inject({
|
|
253
|
+
method: 'GET',
|
|
254
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
255
|
+
});
|
|
256
|
+
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
257
|
+
|
|
258
|
+
expect(response.statusCode).toBe(500);
|
|
259
|
+
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('should validate ID parameter format', async () => {
|
|
263
|
+
const response = await fastify.inject({
|
|
264
|
+
method: 'GET',
|
|
265
|
+
url: '/file/invalid-id',
|
|
266
|
+
});
|
|
267
|
+
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
268
|
+
|
|
269
|
+
expect(response.statusCode).toBe(400);
|
|
270
|
+
expect(body.error).toBe('Bad Request');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should validate disposition parameter', async () => {
|
|
274
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
275
|
+
|
|
276
|
+
const response = await fastify.inject({
|
|
277
|
+
method: 'GET',
|
|
278
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?disposition=invalid`,
|
|
279
|
+
});
|
|
280
|
+
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
281
|
+
|
|
282
|
+
expect(response.statusCode).toBe(400);
|
|
283
|
+
expect(body.error).toBe('Bad Request');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should return 404 when get handler returns false', async () => {
|
|
287
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
288
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
289
|
+
|
|
290
|
+
const response = await fastify.inject({
|
|
291
|
+
method: 'GET',
|
|
292
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
293
|
+
});
|
|
294
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
295
|
+
|
|
296
|
+
expect(response.statusCode).toBe(404);
|
|
297
|
+
expect(body.error.code).toBe('NOT_FOUND');
|
|
298
|
+
expect(body.error.message).toBe('The requested file could not be retrieved');
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('HEAD /file/:id', () => {
|
|
303
|
+
it('should return headers without body using head handler', async () => {
|
|
304
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
305
|
+
|
|
306
|
+
const mockMetadata = {
|
|
307
|
+
contentType: 'audio/wav',
|
|
308
|
+
contentLength: 17,
|
|
309
|
+
etag: '"abc123"',
|
|
310
|
+
lastModified: new Date('2025-01-01'),
|
|
311
|
+
};
|
|
312
|
+
vi.mocked(mockFileHandler.head).mockResolvedValue(mockMetadata);
|
|
313
|
+
|
|
314
|
+
const response = await fastify.inject({
|
|
315
|
+
method: 'HEAD',
|
|
316
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
expect(response.statusCode).toBe(200);
|
|
320
|
+
expect(response.headers['content-type']).toBe('audio/wav');
|
|
321
|
+
expect(response.headers['content-length']).toBe('17');
|
|
322
|
+
expect(response.headers.etag).toBe('"abc123"');
|
|
323
|
+
expect(response.body).toBe(''); // No body for HEAD
|
|
324
|
+
|
|
325
|
+
expect(mockFileHandler.head).toHaveBeenCalledWith(
|
|
326
|
+
mockFile,
|
|
327
|
+
expect.objectContaining({
|
|
328
|
+
request: expect.any(Object),
|
|
329
|
+
fastify: expect.any(Object),
|
|
330
|
+
}),
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('should return 404 when file not found', async () => {
|
|
335
|
+
prisma.file.findFirst.mockResolvedValue(null);
|
|
336
|
+
|
|
337
|
+
const response = await fastify.inject({
|
|
338
|
+
method: 'HEAD',
|
|
339
|
+
url: `/file/${encodeURIComponent('http://example.com/file/nonexistent')}`,
|
|
340
|
+
});
|
|
341
|
+
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
342
|
+
|
|
343
|
+
expect(response.statusCode).toBe(404);
|
|
344
|
+
expect(body.error.code).toBe('NOT_FOUND');
|
|
345
|
+
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('should return 500 when head handler throws error', async () => {
|
|
349
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
350
|
+
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
351
|
+
|
|
352
|
+
const response = await fastify.inject({
|
|
353
|
+
method: 'HEAD',
|
|
354
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
355
|
+
});
|
|
356
|
+
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
357
|
+
|
|
358
|
+
expect(response.statusCode).toBe(500);
|
|
359
|
+
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('should return 404 when head handler returns false', async () => {
|
|
363
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
364
|
+
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
365
|
+
|
|
366
|
+
const response = await fastify.inject({
|
|
367
|
+
method: 'HEAD',
|
|
368
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
369
|
+
});
|
|
370
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
371
|
+
|
|
372
|
+
expect(response.statusCode).toBe(404);
|
|
373
|
+
expect(body.error.code).toBe('NOT_FOUND');
|
|
374
|
+
expect(body.error.message).toBe('The requested file metadata was not found');
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
describe('GET /file/:id - exhaustiveness check', () => {
|
|
379
|
+
it('should handle unexpected file result type', async () => {
|
|
380
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
381
|
+
|
|
382
|
+
// Mock an invalid result type to test exhaustiveness check
|
|
383
|
+
const invalidResult = {
|
|
384
|
+
type: 'invalid',
|
|
385
|
+
metadata: { contentType: 'audio/wav', contentLength: 1024 },
|
|
386
|
+
};
|
|
387
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue(invalidResult as never);
|
|
388
|
+
|
|
389
|
+
const response = await fastify.inject({
|
|
390
|
+
method: 'GET',
|
|
391
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
392
|
+
});
|
|
393
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
394
|
+
|
|
395
|
+
expect(response.statusCode).toBe(500);
|
|
396
|
+
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import type { FastifyPluginAsync, FastifyReply } from 'fastify';
|
|
3
|
+
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
|
+
import { z } from 'zod/v4';
|
|
5
|
+
import type { FileHandler, FileMetadata } from '../types/fileHandlers.js';
|
|
6
|
+
import { createInternalError, createNotFoundError } from '../utils/errors.js';
|
|
7
|
+
|
|
8
|
+
const paramsSchema = z.object({
|
|
9
|
+
id: z.string().regex(/^https?:\/\/.+/, 'Invalid URI format'),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const querySchema = z.object({
|
|
13
|
+
disposition: z.enum(['inline', 'attachment']).optional().default('inline'),
|
|
14
|
+
filename: z.string().optional(),
|
|
15
|
+
noRedirect: z.coerce.boolean().optional().default(false),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
type FileRouteOptions = {
|
|
19
|
+
fileHandler: FileHandler;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const setFileHeaders = (
|
|
23
|
+
reply: FastifyReply,
|
|
24
|
+
metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
|
|
25
|
+
) => {
|
|
26
|
+
reply.header('Content-Type', metadata.contentType);
|
|
27
|
+
reply.header('Content-Length', metadata.contentLength.toString());
|
|
28
|
+
|
|
29
|
+
if (metadata.etag) {
|
|
30
|
+
reply.header('ETag', metadata.etag);
|
|
31
|
+
}
|
|
32
|
+
if (metadata.lastModified) {
|
|
33
|
+
reply.header('Last-Modified', metadata.lastModified.toUTCString());
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
38
|
+
const { fileHandler } = opts;
|
|
39
|
+
|
|
40
|
+
fastify.withTypeProvider<ZodTypeProvider>().head(
|
|
41
|
+
'/file/:id',
|
|
42
|
+
{
|
|
43
|
+
schema: {
|
|
44
|
+
params: paramsSchema,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
async (request, reply) => {
|
|
48
|
+
const { id } = request.params;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const file = await fastify.prisma.file.findFirst({
|
|
52
|
+
where: {
|
|
53
|
+
fileId: id,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!file) {
|
|
58
|
+
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const metadata: FileMetadata | false = await fileHandler.head(file, {
|
|
62
|
+
request,
|
|
63
|
+
fastify,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (!metadata) {
|
|
67
|
+
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
setFileHeaders(reply, metadata);
|
|
71
|
+
|
|
72
|
+
return reply.code(200).send();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
const err = error as Error;
|
|
75
|
+
fastify.log.error(`File metadata retrieval error: ${err.message}`);
|
|
76
|
+
|
|
77
|
+
return reply.code(500).send(createInternalError());
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
83
|
+
'/file/:id',
|
|
84
|
+
{
|
|
85
|
+
schema: {
|
|
86
|
+
params: paramsSchema,
|
|
87
|
+
querystring: querySchema,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
async (request, reply) => {
|
|
91
|
+
const { id } = request.params;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const file = await fastify.prisma.file.findFirst({
|
|
95
|
+
where: {
|
|
96
|
+
fileId: id,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
if (!file) {
|
|
101
|
+
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const result = await fileHandler.get(file, {
|
|
105
|
+
request,
|
|
106
|
+
fastify,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (!result) {
|
|
110
|
+
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (result.type === 'redirect') {
|
|
114
|
+
if (request.query.noRedirect) {
|
|
115
|
+
return reply.code(200).send({ location: result.url });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return reply.code(302).redirect(result.url);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const { disposition, filename: customFilename } = request.query;
|
|
122
|
+
const filename = customFilename || file.filename;
|
|
123
|
+
setFileHeaders(reply, result.metadata);
|
|
124
|
+
reply.header('Content-Disposition', `${disposition}; filename="${filename}"`);
|
|
125
|
+
|
|
126
|
+
// Handle stream response
|
|
127
|
+
if (result.type === 'stream') {
|
|
128
|
+
return reply.code(200).send(result.stream);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (result.type === 'file') {
|
|
132
|
+
if (result.accelPath) {
|
|
133
|
+
reply.header('X-Accel-Redirect', result.accelPath);
|
|
134
|
+
return reply.code(200).send();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const stream = createReadStream(result.path);
|
|
138
|
+
return reply.code(200).send(stream);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Exhaustiveness check - should never reach here with proper types
|
|
142
|
+
console.error(`Unexpected file result type: ${(result as { type: string }).type}`);
|
|
143
|
+
reply.removeHeader('Content-Disposition');
|
|
144
|
+
reply.removeHeader('Content-Type');
|
|
145
|
+
reply.removeHeader('Content-Length');
|
|
146
|
+
return reply.code(500).send(createInternalError());
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const err = error as Error;
|
|
149
|
+
fastify.log.error(`File retrieval error: ${err}`);
|
|
150
|
+
|
|
151
|
+
return reply.code(500).send(createInternalError());
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export default file;
|