-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
371 lines (327 loc) · 12.3 KB
/
Copy pathindex.js
File metadata and controls
371 lines (327 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* FluxMedia Basic Node.js Example
* Demonstrates all available providers and plugin system
*/
import { MediaUploader, createPlugin } from '@fluxmedia/core';
import { CloudinaryProvider } from '@fluxmedia/cloudinary';
import { R2Provider } from '@fluxmedia/r2';
import { S3Provider } from '@fluxmedia/s3';
import { config } from 'dotenv';
// Load environment variables from .env file
config();
// ============================================================================
// PLUGIN EXAMPLES
// ============================================================================
/**
* Logger Plugin - Logs all upload/delete operations
* Great for debugging and monitoring
*/
const loggerPlugin = createPlugin('logger', {
beforeUpload: async (file, options) => {
const size = file instanceof Buffer ? file.byteLength : file.size;
console.log(`[Logger] Starting upload: ${options.filename || 'unnamed'} (${formatBytes(size)})`);
return { file, options };
},
afterUpload: async (result) => {
console.log(`[Logger] Upload complete: ${result.id}`);
console.log(`[Logger] URL: ${result.url}`);
console.log(`[Logger] Size: ${formatBytes(result.size)}`);
return result;
},
onError: async (error, context) => {
console.error(`[Logger] Upload failed:`, error.message);
},
beforeDelete: async (id) => {
console.log(`[Logger] Deleting: ${id}`);
return id;
},
afterDelete: async (id) => {
console.log(`[Logger] Deleted: ${id}`);
},
}, { version: '1.0.0' });
/**
* Metadata Enrichment Plugin - Adds automatic metadata to uploads
*/
const metadataPlugin = createPlugin('metadata-enricher', {
beforeUpload: async (file, options) => {
// Add automatic metadata
const enrichedOptions = {
...options,
metadata: {
...options.metadata,
uploadedAt: new Date().toISOString(),
uploadedBy: 'fluxmedia-example',
environment: process.env.NODE_ENV || 'development',
},
};
return { file, options: enrichedOptions };
},
afterUpload: async (result) => {
// Add processing timestamp to result metadata
return {
...result,
metadata: {
...result.metadata,
processedAt: new Date().toISOString(),
},
};
},
});
/**
* Validation Plugin - Validates file types and sizes before upload
*/
const validationPlugin = createPlugin('validator', {
beforeUpload: async (file, options) => {
const size = file instanceof Buffer ? file.byteLength : file.size;
const maxSize = 10 * 1024 * 1024; // 10MB
if (size > maxSize) {
throw new Error(`File too large: ${formatBytes(size)} exceeds ${formatBytes(maxSize)}`);
}
// Check file type if available
if (!(file instanceof Buffer) && file.type) {
const allowedTypes = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'];
if (!allowedTypes.includes(file.type)) {
throw new Error(`Invalid file type: ${file.type}. Allowed: ${allowedTypes.join(', ')}`);
}
}
return { file, options };
},
});
// ============================================================================
// PROVIDER SETUP
// ============================================================================
/**
* Create Cloudinary provider (best for image transformations)
*/
function createCloudinaryUploader() {
return new MediaUploader(
new CloudinaryProvider({
cloudName: process.env.CLOUDINARY_CLOUD_NAME || 'demo',
apiKey: process.env.CLOUDINARY_API_KEY || '',
apiSecret: process.env.CLOUDINARY_API_SECRET || '',
})
);
}
/**
* Create AWS S3 provider (best for raw storage)
*/
function createS3Uploader() {
return new MediaUploader(
new S3Provider({
region: process.env.S3_REGION || 'us-east-1',
bucket: process.env.S3_BUCKET || 'my-bucket',
accessKeyId: process.env.S3_ACCESS_KEY_ID || '',
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || '',
})
);
}
/**
* Create Cloudflare R2 provider (S3-compatible, no egress fees)
*/
function createR2Uploader() {
return new MediaUploader(
new R2Provider({
accountId: process.env.R2_ACCOUNT_ID || '',
bucket: process.env.R2_BUCKET || 'my-bucket',
accessKeyId: process.env.R2_ACCESS_KEY_ID || '',
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY || '',
publicUrl: process.env.R2_PUBLIC_URL, // Required for getUrl()
})
);
}
// ============================================================================
// MAIN DEMO
// ============================================================================
async function main() {
console.log('='.repeat(60));
console.log('FluxMedia - Comprehensive Node.js Example');
console.log('='.repeat(60));
console.log('');
// -------------------------------------------------------------------------
// 1. PROVIDER COMPARISON
// -------------------------------------------------------------------------
console.log('1. PROVIDER COMPARISON');
console.log('-'.repeat(40));
const providers = [
{ name: 'Cloudinary', uploader: createCloudinaryUploader() },
{ name: 'AWS S3', uploader: createS3Uploader() },
{ name: 'Cloudflare R2', uploader: createR2Uploader() },
];
for (const { name, uploader } of providers) {
console.log(`\n ${name}:`);
console.log(` - Provider: ${uploader.provider.name}`);
console.log(` - Resize: ${uploader.supports('transformations.resize') ? 'Yes' : 'No'}`);
console.log(` - Format conversion: ${uploader.supports('transformations.format') ? 'Yes' : 'No'}`);
console.log(` - Video processing: ${uploader.supports('capabilities.videoProcessing') ? 'Yes' : 'No'}`);
console.log(` - Max file size: ${formatBytes(uploader.provider.features.storage.maxFileSize)}`);
}
console.log('');
// -------------------------------------------------------------------------
// 2. PLUGIN SYSTEM DEMO
// -------------------------------------------------------------------------
console.log('2. PLUGIN SYSTEM');
console.log('-'.repeat(40));
const cloudinaryUploader = createCloudinaryUploader();
// Register plugins
await cloudinaryUploader.use(loggerPlugin);
await cloudinaryUploader.use(metadataPlugin);
await cloudinaryUploader.use(validationPlugin);
console.log(` Registered plugins: ${cloudinaryUploader.getPlugins().map(p => p.name).join(', ')}`);
console.log('');
// -------------------------------------------------------------------------
// 3. UPLOAD EXAMPLES
// -------------------------------------------------------------------------
console.log('3. UPLOAD EXAMPLES');
console.log('-'.repeat(40));
console.log(`
Basic upload:
-------------
const result = await uploader.upload(file, {
folder: 'my-uploads',
filename: 'my-image',
tags: ['example', 'nodejs'],
});
With transformations (Cloudinary only):
---------------------------------------
const result = await uploader.upload(file, {
folder: 'thumbnails',
transformation: {
width: 400,
height: 400,
fit: 'cover',
format: 'webp',
quality: 80,
},
});
With progress tracking:
-----------------------
const result = await uploader.upload(file, {
onProgress: (percent) => {
console.log(\`Upload progress: \${percent}%\`);
},
});
`);
// -------------------------------------------------------------------------
// 4. URL GENERATION
// -------------------------------------------------------------------------
console.log('4. URL GENERATION');
console.log('-'.repeat(40));
const cloudinaryDemo = createCloudinaryUploader();
const sampleId = 'sample';
console.log('\n Cloudinary URL transformations:');
console.log(` Original: ${cloudinaryDemo.getUrl(sampleId)}`);
console.log(` Thumbnail: ${cloudinaryDemo.getUrl(sampleId, { width: 200, height: 200, fit: 'cover' })}`);
console.log(` WebP format: ${cloudinaryDemo.getUrl(sampleId, { format: 'webp', quality: 80 })}`);
console.log('');
// -------------------------------------------------------------------------
// 5. BATCH OPERATIONS
// -------------------------------------------------------------------------
console.log('5. BATCH OPERATIONS');
console.log('-'.repeat(40));
console.log(`
Upload multiple files:
----------------------
const results = await uploader.uploadMultiple(
[file1, file2, file3],
{ folder: 'batch-uploads' }
);
Delete multiple files:
----------------------
await uploader.deleteMultiple(['id1', 'id2', 'id3']);
`);
// -------------------------------------------------------------------------
// 6. ERROR HANDLING
// -------------------------------------------------------------------------
console.log('6. ERROR HANDLING');
console.log('-'.repeat(40));
console.log(`
import { MediaError, MediaErrorCode } from '@fluxmedia/core';
try {
await uploader.upload(file);
} catch (error) {
if (error instanceof MediaError) {
switch (error.code) {
case MediaErrorCode.INVALID_CONFIG:
console.error('Configuration error:', error.message);
break;
case MediaErrorCode.UPLOAD_FAILED:
console.error('Upload failed:', error.message);
break;
case MediaErrorCode.FILE_NOT_FOUND:
console.error('File not found:', error.message);
break;
case MediaErrorCode.UNAUTHORIZED:
console.error('Auth error:', error.message);
break;
}
}
}
`);
// -------------------------------------------------------------------------
// 7. PLUGIN CREATION
// -------------------------------------------------------------------------
console.log('7. CREATING CUSTOM PLUGINS');
console.log('-'.repeat(40));
console.log(`
import { createPlugin } from '@fluxmedia/core';
const myPlugin = createPlugin('my-plugin', {
beforeUpload: async (file, options) => {
// Modify file or options before upload
console.log('Before upload:', options.filename);
return { file, options };
},
afterUpload: async (result) => {
// Modify result after upload
console.log('Uploaded:', result.url);
return result;
},
onError: async (error, context) => {
// Handle errors
console.error('Error:', error.message);
},
beforeDelete: async (id) => {
// Called before delete
return id;
},
afterDelete: async (id) => {
// Called after delete
},
}, { version: '1.0.0' });
// Register the plugin
await uploader.use(myPlugin);
`);
// -------------------------------------------------------------------------
// SUMMARY
// -------------------------------------------------------------------------
console.log('='.repeat(60));
console.log('SUMMARY');
console.log('='.repeat(60));
console.log(`
Providers:
- @fluxmedia/cloudinary: Best for image/video transformations
- @fluxmedia/s3: Standard AWS S3 storage
- @fluxmedia/r2: Cloudflare R2 (S3-compatible, no egress fees)
Features:
- MediaUploader: Unified API across all providers
- Plugin system: Extend with custom hooks
- TypeScript: Full type safety
- Error codes: Standardized error handling
To run with real uploads:
1. Copy .env.example to .env
2. Fill in your provider credentials
3. Uncomment the actual upload code
4. Run: pnpm start
`);
}
// Utility function
function formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});