@bernierllc/content-type-video 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +26 -0
- package/README.md +361 -0
- package/__tests__/VideoContentType.test.ts +522 -0
- package/dist/VideoContentType.d.ts +32 -0
- package/dist/VideoContentType.d.ts.map +1 -0
- package/dist/VideoContentType.js +154 -0
- package/dist/VideoContentType.js.map +1 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +58 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/storage/LocalVideoStorage.d.ts +22 -0
- package/dist/storage/LocalVideoStorage.d.ts.map +1 -0
- package/dist/storage/LocalVideoStorage.js +155 -0
- package/dist/storage/LocalVideoStorage.js.map +1 -0
- package/dist/storage/VideoStorage.d.ts +12 -0
- package/dist/storage/VideoStorage.d.ts.map +1 -0
- package/dist/storage/VideoStorage.js +10 -0
- package/dist/storage/VideoStorage.js.map +1 -0
- package/dist/types.d.ts +101 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +27 -0
- package/dist/types.js.map +1 -0
- package/jest.config.cjs +31 -0
- package/package.json +59 -0
- package/src/VideoContentType.ts +142 -0
- package/src/config.ts +63 -0
- package/src/index.ts +14 -0
- package/src/storage/LocalVideoStorage.ts +134 -0
- package/src/storage/VideoStorage.ts +20 -0
- package/src/types.ts +99 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { VideoContentType } from '../src/VideoContentType';
|
|
10
|
+
import * as fs from 'fs/promises';
|
|
11
|
+
import * as path from 'path';
|
|
12
|
+
|
|
13
|
+
describe('VideoContentType', () => {
|
|
14
|
+
let videoType: VideoContentType;
|
|
15
|
+
let testUploadDir: string;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
testUploadDir = path.join(__dirname, 'test-uploads');
|
|
19
|
+
videoType = new VideoContentType({
|
|
20
|
+
uploadOptions: {
|
|
21
|
+
storageBasePath: testUploadDir,
|
|
22
|
+
maxSizeBytes: 1024 * 1024, // 1MB for tests
|
|
23
|
+
},
|
|
24
|
+
streamingBaseUrl: '/test-videos',
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(async () => {
|
|
29
|
+
await fs.rm(testUploadDir, { recursive: true, force: true });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('upload', () => {
|
|
33
|
+
it('should upload valid MP4 video successfully', async () => {
|
|
34
|
+
const testVideo = Buffer.from('fake mp4 video content for testing');
|
|
35
|
+
const result = await videoType.upload(testVideo, 'test-video.mp4', 'user123');
|
|
36
|
+
|
|
37
|
+
expect(result.success).toBe(true);
|
|
38
|
+
expect(result.data?.metadata.filename).toContain('test-video.mp4');
|
|
39
|
+
expect(result.data?.metadata.mimeType).toBe('video/mp4');
|
|
40
|
+
expect(result.data?.uploadedBy).toBe('user123');
|
|
41
|
+
expect(result.data?.streamingUrl).toMatch(/^\/test-videos\//);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should upload valid WebM video successfully', async () => {
|
|
45
|
+
const testVideo = Buffer.from('fake webm video content');
|
|
46
|
+
const result = await videoType.upload(testVideo, 'test.webm');
|
|
47
|
+
|
|
48
|
+
expect(result.success).toBe(true);
|
|
49
|
+
expect(result.data?.metadata.mimeType).toBe('video/webm');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should upload valid MOV video successfully', async () => {
|
|
53
|
+
const testVideo = Buffer.from('fake mov video content');
|
|
54
|
+
const result = await videoType.upload(testVideo, 'test.mov');
|
|
55
|
+
|
|
56
|
+
expect(result.success).toBe(true);
|
|
57
|
+
expect(result.data?.metadata.mimeType).toBe('video/quicktime');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should upload valid AVI video successfully', async () => {
|
|
61
|
+
const testVideo = Buffer.from('fake avi video content');
|
|
62
|
+
const result = await videoType.upload(testVideo, 'test.avi');
|
|
63
|
+
|
|
64
|
+
expect(result.success).toBe(true);
|
|
65
|
+
expect(result.data?.metadata.mimeType).toBe('video/x-msvideo');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should reject invalid MIME type', async () => {
|
|
69
|
+
const testFile = Buffer.from('fake pdf');
|
|
70
|
+
const result = await videoType.upload(testFile, 'test.pdf');
|
|
71
|
+
|
|
72
|
+
expect(result.success).toBe(false);
|
|
73
|
+
expect(result.error).toContain('not allowed');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should reject oversized file', async () => {
|
|
77
|
+
const largeFile = Buffer.alloc(2 * 1024 * 1024);
|
|
78
|
+
const result = await videoType.upload(largeFile, 'large.mp4');
|
|
79
|
+
|
|
80
|
+
expect(result.success).toBe(false);
|
|
81
|
+
expect(result.error).toContain('exceeds maximum');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should generate unique filenames for concurrent uploads', async () => {
|
|
85
|
+
const testVideo = Buffer.from('test video');
|
|
86
|
+
|
|
87
|
+
// Add small delays to ensure unique timestamps
|
|
88
|
+
const upload1 = await videoType.upload(testVideo, 'test.mp4');
|
|
89
|
+
await new Promise(resolve => setTimeout(resolve, 2));
|
|
90
|
+
const upload2 = await videoType.upload(testVideo, 'test.mp4');
|
|
91
|
+
await new Promise(resolve => setTimeout(resolve, 2));
|
|
92
|
+
const upload3 = await videoType.upload(testVideo, 'test.mp4');
|
|
93
|
+
|
|
94
|
+
const filenames = [
|
|
95
|
+
upload1.data?.metadata.filename,
|
|
96
|
+
upload2.data?.metadata.filename,
|
|
97
|
+
upload3.data?.metadata.filename,
|
|
98
|
+
];
|
|
99
|
+
const uniqueFilenames = new Set(filenames);
|
|
100
|
+
|
|
101
|
+
expect(uniqueFilenames.size).toBe(3);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should handle upload without uploadedBy', async () => {
|
|
105
|
+
const testVideo = Buffer.from('test video');
|
|
106
|
+
const result = await videoType.upload(testVideo, 'test.mp4');
|
|
107
|
+
|
|
108
|
+
expect(result.success).toBe(true);
|
|
109
|
+
expect(result.data?.uploadedBy).toBeUndefined();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should handle zero-byte files', async () => {
|
|
113
|
+
const emptyFile = Buffer.alloc(0);
|
|
114
|
+
const result = await videoType.upload(emptyFile, 'empty.mp4');
|
|
115
|
+
|
|
116
|
+
expect(result.success).toBe(false);
|
|
117
|
+
expect(result.error).toContain('empty');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should sanitize special characters in filenames', async () => {
|
|
121
|
+
const testVideo = Buffer.from('test video');
|
|
122
|
+
const result = await videoType.upload(testVideo, 'test video (1).mp4');
|
|
123
|
+
|
|
124
|
+
expect(result.success).toBe(true);
|
|
125
|
+
expect(result.data?.filePath).toContain('test_video__1_.mp4');
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('validate', () => {
|
|
130
|
+
it('should validate allowed formats', async () => {
|
|
131
|
+
const testFile = Buffer.from('test');
|
|
132
|
+
const mp4Result = await videoType.validate(testFile, 'test.mp4');
|
|
133
|
+
const webmResult = await videoType.validate(testFile, 'test.webm');
|
|
134
|
+
const movResult = await videoType.validate(testFile, 'test.mov');
|
|
135
|
+
const aviResult = await videoType.validate(testFile, 'test.avi');
|
|
136
|
+
|
|
137
|
+
expect(mp4Result.success).toBe(true);
|
|
138
|
+
expect(webmResult.success).toBe(true);
|
|
139
|
+
expect(movResult.success).toBe(true);
|
|
140
|
+
expect(aviResult.success).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should reject disallowed formats', async () => {
|
|
144
|
+
const testFile = Buffer.from('test');
|
|
145
|
+
const pdfResult = await videoType.validate(testFile, 'test.pdf');
|
|
146
|
+
const docResult = await videoType.validate(testFile, 'test.doc');
|
|
147
|
+
|
|
148
|
+
expect(pdfResult.success).toBe(false);
|
|
149
|
+
expect(pdfResult.error).toContain('not allowed');
|
|
150
|
+
expect(docResult.success).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should check file size limits', async () => {
|
|
154
|
+
const smallFile = Buffer.alloc(100);
|
|
155
|
+
const maxFile = Buffer.alloc(1024 * 1024);
|
|
156
|
+
const largeFile = Buffer.alloc(2 * 1024 * 1024);
|
|
157
|
+
|
|
158
|
+
const smallResult = await videoType.validate(smallFile, 'small.mp4');
|
|
159
|
+
const maxResult = await videoType.validate(maxFile, 'max.mp4');
|
|
160
|
+
const largeResult = await videoType.validate(largeFile, 'large.mp4');
|
|
161
|
+
|
|
162
|
+
expect(smallResult.success).toBe(true);
|
|
163
|
+
expect(maxResult.success).toBe(true);
|
|
164
|
+
expect(largeResult.success).toBe(false);
|
|
165
|
+
expect(largeResult.error).toContain('exceeds maximum');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should reject zero-byte files', async () => {
|
|
169
|
+
const emptyFile = Buffer.alloc(0);
|
|
170
|
+
const result = await videoType.validate(emptyFile, 'empty.mp4');
|
|
171
|
+
|
|
172
|
+
expect(result.success).toBe(false);
|
|
173
|
+
expect(result.error).toContain('empty');
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('file operations', () => {
|
|
178
|
+
it('should retrieve uploaded video', async () => {
|
|
179
|
+
const testVideo = Buffer.from('test video content');
|
|
180
|
+
const uploadResult = await videoType.upload(testVideo, 'test.mp4');
|
|
181
|
+
|
|
182
|
+
expect(uploadResult.success).toBe(true);
|
|
183
|
+
const localPath = uploadResult.data?.filePath;
|
|
184
|
+
expect(localPath).toBeDefined();
|
|
185
|
+
|
|
186
|
+
if (localPath) {
|
|
187
|
+
const retrieved = await videoType.get(localPath);
|
|
188
|
+
expect(retrieved).not.toBeNull();
|
|
189
|
+
expect(retrieved?.length).toBe(testVideo.length);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should return null for non-existent video', async () => {
|
|
194
|
+
const result = await videoType.get('/non/existent/path.mp4');
|
|
195
|
+
expect(result).toBeNull();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should delete uploaded video', async () => {
|
|
199
|
+
const testVideo = Buffer.from('test video');
|
|
200
|
+
const uploadResult = await videoType.upload(testVideo, 'test.mp4');
|
|
201
|
+
|
|
202
|
+
expect(uploadResult.success).toBe(true);
|
|
203
|
+
const localPath = uploadResult.data?.filePath;
|
|
204
|
+
expect(localPath).toBeDefined();
|
|
205
|
+
|
|
206
|
+
if (localPath) {
|
|
207
|
+
const deleteResult = await videoType.delete(localPath);
|
|
208
|
+
expect(deleteResult).toBe(true);
|
|
209
|
+
|
|
210
|
+
const retrieved = await videoType.get(localPath);
|
|
211
|
+
expect(retrieved).toBeNull();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('should return false when deleting non-existent video', async () => {
|
|
216
|
+
const result = await videoType.delete('/non/existent/path.mp4');
|
|
217
|
+
expect(result).toBe(false);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should check if video exists', async () => {
|
|
221
|
+
const testVideo = Buffer.from('test video');
|
|
222
|
+
const uploadResult = await videoType.upload(testVideo, 'test.mp4');
|
|
223
|
+
|
|
224
|
+
expect(uploadResult.success).toBe(true);
|
|
225
|
+
const filePath = uploadResult.data?.filePath;
|
|
226
|
+
expect(filePath).toBeDefined();
|
|
227
|
+
|
|
228
|
+
if (filePath) {
|
|
229
|
+
const exists = await videoType.exists(filePath);
|
|
230
|
+
expect(exists).toBe(true);
|
|
231
|
+
|
|
232
|
+
await videoType.delete(filePath);
|
|
233
|
+
const stillExists = await videoType.exists(filePath);
|
|
234
|
+
expect(stillExists).toBe(false);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('getStreamingUrl', () => {
|
|
240
|
+
it('should generate correct streaming URLs', async () => {
|
|
241
|
+
const testVideo = Buffer.from('test video');
|
|
242
|
+
const uploadResult = await videoType.upload(testVideo, 'test.mp4');
|
|
243
|
+
|
|
244
|
+
expect(uploadResult.success).toBe(true);
|
|
245
|
+
const filePath = uploadResult.data?.filePath;
|
|
246
|
+
expect(filePath).toBeDefined();
|
|
247
|
+
|
|
248
|
+
if (filePath) {
|
|
249
|
+
const streamingUrl = videoType.getStreamingUrl(filePath);
|
|
250
|
+
expect(streamingUrl).toMatch(/^\/test-videos\//);
|
|
251
|
+
expect(streamingUrl).toContain('.mp4');
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('register', () => {
|
|
257
|
+
it('should register with content type registry', async () => {
|
|
258
|
+
await expect(videoType.register()).resolves.not.toThrow();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe('error handling edge cases', () => {
|
|
263
|
+
it('should handle file operations that throw errors', async () => {
|
|
264
|
+
const testVideo = Buffer.from('test video');
|
|
265
|
+
const uploadResult = await videoType.upload(testVideo, 'test.mp4');
|
|
266
|
+
|
|
267
|
+
expect(uploadResult.success).toBe(true);
|
|
268
|
+
const localPath = uploadResult.data?.localPath;
|
|
269
|
+
|
|
270
|
+
if (localPath) {
|
|
271
|
+
await fs.chmod(localPath, 0o000);
|
|
272
|
+
|
|
273
|
+
const retrieved = await videoType.get(localPath);
|
|
274
|
+
expect(retrieved).toBeNull();
|
|
275
|
+
|
|
276
|
+
await fs.chmod(localPath, 0o644);
|
|
277
|
+
await videoType.delete(localPath);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should handle upload error when file system throws', async () => {
|
|
282
|
+
const invalidVideoType = new VideoContentType({
|
|
283
|
+
uploadOptions: {
|
|
284
|
+
storageBasePath: '/invalid/readonly/path/that/should/not/exist',
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const testVideo = Buffer.from('test');
|
|
289
|
+
const result = await invalidVideoType.upload(testVideo, 'test.mp4');
|
|
290
|
+
|
|
291
|
+
expect(result.success).toBe(false);
|
|
292
|
+
expect(result.error).toBeDefined();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should handle different error types in upload catch block', async () => {
|
|
296
|
+
const invalidVideoType = new VideoContentType({
|
|
297
|
+
uploadOptions: {
|
|
298
|
+
storageBasePath: '/root/absolutely/invalid/path/with/no/permissions',
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const testVideo = Buffer.from('valid test data');
|
|
303
|
+
const result = await invalidVideoType.upload(testVideo, 'test.mp4');
|
|
304
|
+
|
|
305
|
+
expect(result.success).toBe(false);
|
|
306
|
+
expect(result.error).toBeTruthy();
|
|
307
|
+
expect(typeof result.error).toBe('string');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
describe('configuration', () => {
|
|
312
|
+
it('should use custom configuration', () => {
|
|
313
|
+
const customVideoType = new VideoContentType({
|
|
314
|
+
uploadOptions: {
|
|
315
|
+
maxSizeBytes: 100 * 1024 * 1024,
|
|
316
|
+
allowedFormats: ['mp4', 'webm'],
|
|
317
|
+
storageBasePath: './custom-storage',
|
|
318
|
+
},
|
|
319
|
+
streamingBaseUrl: '/custom/stream',
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
expect(customVideoType).toBeInstanceOf(VideoContentType);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('should use default configuration', () => {
|
|
326
|
+
const defaultVideoType = new VideoContentType();
|
|
327
|
+
expect(defaultVideoType.typeName).toBe('video');
|
|
328
|
+
expect(defaultVideoType.version).toBe('1.0.0');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should use environment variables for configuration', () => {
|
|
332
|
+
process.env.VIDEO_MAX_SIZE_BYTES = '5000000';
|
|
333
|
+
process.env.VIDEO_ALLOWED_FORMATS = 'mp4,webm';
|
|
334
|
+
process.env.VIDEO_STORAGE_BASE_PATH = '/env/storage';
|
|
335
|
+
process.env.VIDEO_STREAMING_BASE_URL = '/env/stream';
|
|
336
|
+
process.env.VIDEO_GENERATE_THUMBNAIL = 'true';
|
|
337
|
+
process.env.VIDEO_EXTRACT_METADATA = 'false';
|
|
338
|
+
|
|
339
|
+
const envVideoType = new VideoContentType();
|
|
340
|
+
expect(envVideoType).toBeInstanceOf(VideoContentType);
|
|
341
|
+
|
|
342
|
+
delete process.env.VIDEO_MAX_SIZE_BYTES;
|
|
343
|
+
delete process.env.VIDEO_ALLOWED_FORMATS;
|
|
344
|
+
delete process.env.VIDEO_STORAGE_BASE_PATH;
|
|
345
|
+
delete process.env.VIDEO_STREAMING_BASE_URL;
|
|
346
|
+
delete process.env.VIDEO_GENERATE_THUMBNAIL;
|
|
347
|
+
delete process.env.VIDEO_EXTRACT_METADATA;
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('should handle partial configuration override', () => {
|
|
351
|
+
const partialVideoType = new VideoContentType({
|
|
352
|
+
uploadOptions: {
|
|
353
|
+
maxSizeBytes: 50 * 1024 * 1024,
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
expect(partialVideoType).toBeInstanceOf(VideoContentType);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('should handle configuration with undefined boolean values', () => {
|
|
361
|
+
const videoTypeWithUndefined = new VideoContentType({
|
|
362
|
+
uploadOptions: {
|
|
363
|
+
generateThumbnail: undefined,
|
|
364
|
+
extractMetadata: undefined,
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
expect(videoTypeWithUndefined).toBeInstanceOf(VideoContentType);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
describe('validateContent', () => {
|
|
373
|
+
it('should validate correct video content data', () => {
|
|
374
|
+
const validData = {
|
|
375
|
+
filePath: testUploadDir + '/test.mp4',
|
|
376
|
+
streamingUrl: '/test-videos/test.mp4',
|
|
377
|
+
metadata: {
|
|
378
|
+
filename: 'test.mp4',
|
|
379
|
+
mimeType: 'video/mp4',
|
|
380
|
+
size: 1024000,
|
|
381
|
+
duration: 30,
|
|
382
|
+
width: 1920,
|
|
383
|
+
height: 1080,
|
|
384
|
+
fps: 30,
|
|
385
|
+
},
|
|
386
|
+
uploadedAt: new Date(),
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const result = videoType.validateContent(validData);
|
|
390
|
+
|
|
391
|
+
expect(result.success).toBe(true);
|
|
392
|
+
expect(result.data).toEqual(validData);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('should validate content with optional fields', () => {
|
|
396
|
+
const validData = {
|
|
397
|
+
filePath: testUploadDir + '/test.mp4',
|
|
398
|
+
streamingUrl: '/test-videos/test.mp4',
|
|
399
|
+
metadata: {
|
|
400
|
+
filename: 'test.mp4',
|
|
401
|
+
mimeType: 'video/mp4',
|
|
402
|
+
size: 1024000,
|
|
403
|
+
duration: 30,
|
|
404
|
+
width: 1920,
|
|
405
|
+
height: 1080,
|
|
406
|
+
fps: 30,
|
|
407
|
+
thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUg',
|
|
408
|
+
bitrate: 5000000,
|
|
409
|
+
codec: 'h264',
|
|
410
|
+
},
|
|
411
|
+
uploadedAt: new Date(),
|
|
412
|
+
uploadedBy: 'user123',
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const result = videoType.validateContent(validData);
|
|
416
|
+
|
|
417
|
+
expect(result.success).toBe(true);
|
|
418
|
+
expect(result.data).toEqual(validData);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('should reject invalid metadata - wrong MIME type', () => {
|
|
422
|
+
const invalidData = {
|
|
423
|
+
filePath: testUploadDir + '/test.mp4',
|
|
424
|
+
streamingUrl: '/test-videos/test.mp4',
|
|
425
|
+
metadata: {
|
|
426
|
+
filename: 'test.mp4',
|
|
427
|
+
mimeType: 'invalid/type',
|
|
428
|
+
size: 1024000,
|
|
429
|
+
duration: 30,
|
|
430
|
+
width: 1920,
|
|
431
|
+
height: 1080,
|
|
432
|
+
fps: 30,
|
|
433
|
+
},
|
|
434
|
+
uploadedAt: new Date(),
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
const result = videoType.validateContent(invalidData);
|
|
438
|
+
|
|
439
|
+
expect(result.success).toBe(false);
|
|
440
|
+
expect(result.error).toBeDefined();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it('should handle non-Error exceptions in validateContent', () => {
|
|
444
|
+
const invalidData = {
|
|
445
|
+
filePath: testUploadDir + '/test.mp4',
|
|
446
|
+
streamingUrl: '/test-videos/test.mp4',
|
|
447
|
+
metadata: {
|
|
448
|
+
filename: '',
|
|
449
|
+
mimeType: 'video/mp4',
|
|
450
|
+
size: -1,
|
|
451
|
+
duration: -1,
|
|
452
|
+
width: 0,
|
|
453
|
+
height: 0,
|
|
454
|
+
fps: 0,
|
|
455
|
+
},
|
|
456
|
+
uploadedAt: new Date(),
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const result = videoType.validateContent(invalidData);
|
|
460
|
+
|
|
461
|
+
expect(result.success).toBe(false);
|
|
462
|
+
expect(result.error).toBeDefined();
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
describe('storage edge cases', () => {
|
|
467
|
+
it('should handle file path validation from string', async () => {
|
|
468
|
+
const testFilePath = path.join(__dirname, 'fixtures', 'test.mp4');
|
|
469
|
+
await fs.mkdir(path.dirname(testFilePath), { recursive: true });
|
|
470
|
+
await fs.writeFile(testFilePath, 'test video content');
|
|
471
|
+
|
|
472
|
+
const result = await videoType.upload(testFilePath, 'test.mp4');
|
|
473
|
+
|
|
474
|
+
expect(result.success).toBe(true);
|
|
475
|
+
expect(result.data?.metadata.size).toBeGreaterThan(0);
|
|
476
|
+
|
|
477
|
+
// Clean up
|
|
478
|
+
await fs.rm(path.join(__dirname, 'fixtures'), { recursive: true, force: true });
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('should handle getMimeType for unknown extensions', async () => {
|
|
482
|
+
const testVideo = Buffer.from('test video');
|
|
483
|
+
|
|
484
|
+
const customVideoType = new VideoContentType({
|
|
485
|
+
uploadOptions: {
|
|
486
|
+
storageBasePath: testUploadDir,
|
|
487
|
+
allowedFormats: ['mp4', 'xyz'], // Add fake format
|
|
488
|
+
},
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
const result = await customVideoType.upload(testVideo, 'test.xyz');
|
|
492
|
+
|
|
493
|
+
expect(result.success).toBe(true);
|
|
494
|
+
// Unknown format falls back to video/mp4
|
|
495
|
+
expect(result.data?.metadata.mimeType).toBe('video/mp4');
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('should handle JPG extension mapping', async () => {
|
|
499
|
+
const customVideoType = new VideoContentType({
|
|
500
|
+
uploadOptions: {
|
|
501
|
+
storageBasePath: testUploadDir,
|
|
502
|
+
allowedFormats: ['mp4', 'jpg'], // Allow jpg for testing
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
const testVideo = Buffer.from('test');
|
|
507
|
+
const result = await customVideoType.upload(testVideo, 'test.jpg');
|
|
508
|
+
|
|
509
|
+
expect(result.success).toBe(true);
|
|
510
|
+
// jpg extension maps to video/mp4 (default fallback)
|
|
511
|
+
expect(result.data?.metadata.mimeType).toBe('video/mp4');
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it('should handle uppercase file extensions', async () => {
|
|
515
|
+
const testVideo = Buffer.from('test');
|
|
516
|
+
const result = await videoType.upload(testVideo, 'TEST.MP4');
|
|
517
|
+
|
|
518
|
+
expect(result.success).toBe(true);
|
|
519
|
+
expect(result.data?.metadata.mimeType).toBe('video/mp4');
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { VideoContentData, VideoContentTypeConfig, VideoContentResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Video content type with file upload, local storage, and streaming URL generation
|
|
4
|
+
*/
|
|
5
|
+
export declare class VideoContentType {
|
|
6
|
+
readonly typeName = "video";
|
|
7
|
+
readonly version = "1.0.0";
|
|
8
|
+
private config;
|
|
9
|
+
private storage;
|
|
10
|
+
constructor(config?: Partial<VideoContentTypeConfig>);
|
|
11
|
+
/**
|
|
12
|
+
* Register video content type with registry
|
|
13
|
+
*/
|
|
14
|
+
register(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Validate video file
|
|
17
|
+
*/
|
|
18
|
+
validate(file: Buffer | string, filename: string): Promise<VideoContentResult<void>>;
|
|
19
|
+
/**
|
|
20
|
+
* Upload video file and extract metadata
|
|
21
|
+
*/
|
|
22
|
+
upload(file: Buffer | string, filename: string, uploadedBy?: string): Promise<VideoContentResult<VideoContentData>>;
|
|
23
|
+
get(localPath: string): Promise<Buffer | null>;
|
|
24
|
+
delete(localPath: string): Promise<boolean>;
|
|
25
|
+
exists(localPath: string): Promise<boolean>;
|
|
26
|
+
getStreamingUrl(localPath: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Validate video content data
|
|
29
|
+
*/
|
|
30
|
+
validateContent(data: VideoContentData): VideoContentResult<VideoContentData>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=VideoContentType.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VideoContentType.d.ts","sourceRoot":"","sources":["../src/VideoContentType.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,kBAAkB,EAEnB,MAAM,SAAS,CAAC;AAKjB;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,SAAgB,QAAQ,WAAW;IACnC,SAAgB,OAAO,WAAW;IAClC,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,OAAO,CAAe;gBAElB,MAAM,GAAE,OAAO,CAAC,sBAAsB,CAAM;IAKxD;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAc/B;;OAEG;IACG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IAiC1F;;OAEG;IACG,MAAM,CACV,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,QAAQ,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;IAgB1C,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAI9C,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3C,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjD,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAI1C;;OAEG;IACH,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,kBAAkB,CAAC,gBAAgB,CAAC;CAW9E"}
|