-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcdnUploader.js
More file actions
317 lines (289 loc) · 9.38 KB
/
Copy pathcdnUploader.js
File metadata and controls
317 lines (289 loc) · 9.38 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
const fs = require('fs').promises;
const path = require('path');
/**
* CDN Uploader Service
* Supports multiple CDN providers: Cloudflare, AWS S3, Azure Blob Storage
* Uses mock implementation for demonstration, easily swappable with real CDN clients
*/
class CDNUploader {
constructor(config = {}) {
this.config = {
provider: config.provider || 'mock', // 'mock', 'cloudflare', 's3', 'azure'
apiKey: config.apiKey,
apiToken: config.apiToken,
accountId: config.accountId,
bucket: config.bucket,
zone: config.zone,
region: config.region || 'us-east-1',
cdnUrl: config.cdnUrl || 'https://cdn.example.com',
cacheTTL: config.cacheTTL || 31536000, // 1 year
...config
};
this.uploadMetrics = {
totalUploads: 0,
successfulUploads: 0,
failedUploads: 0,
totalBytesUploaded: 0
};
this._initializeProvider();
}
/**
* Initialize CDN provider client
* @private
*/
_initializeProvider() {
switch (this.config.provider.toLowerCase()) {
case 'cloudflare':
this._initCloudflareClient();
break;
case 's3':
this._initS3Client();
break;
case 'azure':
this._initAzureClient();
break;
case 'mock':
default:
console.log('🟡 Using mock CDN provider for development');
break;
}
}
/**
* Initialize Cloudflare client (placeholder for real implementation)
* @private
*/
_initCloudflareClient() {
console.log('Initializing Cloudflare CDN provider...');
// In production: const CloudflareClient = require('@cloudflare/javascript_sdk');
// this.client = new CloudflareClient({ token: this.config.apiToken });
}
/**
* Initialize AWS S3 client (placeholder for real implementation)
* @private
*/
_initS3Client() {
console.log('Initializing AWS S3 CDN provider...');
// In production: const AWS = require('aws-sdk');
// this.client = new AWS.S3({ region: this.config.region });
}
/**
* Initialize Azure Blob Storage client (placeholder for real implementation)
* @private
*/
_initAzureClient() {
console.log('Initializing Azure Blob Storage provider...');
// In production: const { BlobServiceClient } = require('@azure/storage-blob');
// this.client = BlobServiceClient.fromConnectionString(connectionString);
}
/**
* Upload file to CDN
* @param {string} filePath - Local file path
* @param {Object} metadata - File metadata
* @returns {Promise<string>} CDN URL
*/
async uploadFile(filePath, metadata = {}) {
this.uploadMetrics.totalUploads++;
try {
const fileBuffer = await fs.readFile(filePath);
const fileName = path.basename(filePath);
let cdnUrl;
switch (this.config.provider.toLowerCase()) {
case 'cloudflare':
cdnUrl = await this._uploadToCloudflare(fileBuffer, fileName, metadata);
break;
case 's3':
cdnUrl = await this._uploadToS3(fileBuffer, fileName, metadata);
break;
case 'azure':
cdnUrl = await this._uploadToAzure(fileBuffer, fileName, metadata);
break;
case 'mock':
default:
cdnUrl = await this._uploadMock(fileBuffer, fileName, metadata);
break;
}
this.uploadMetrics.successfulUploads++;
this.uploadMetrics.totalBytesUploaded += fileBuffer.length;
console.log(`✅ CDN Upload: ${fileName} → ${cdnUrl}`);
return cdnUrl;
} catch (error) {
this.uploadMetrics.failedUploads++;
console.error(`❌ CDN upload failed for ${metadata.imageId}:`, error.message);
throw new Error(`CDN upload failed: ${error.message}`);
}
}
/**
* Upload to Cloudflare Images/Workers KV (placeholder)
* @private
*/
async _uploadToCloudflare(fileBuffer, fileName, metadata) {
// Real implementation would use Cloudflare API
// Example:
// const formData = new FormData();
// formData.append('file', new Blob([fileBuffer], { type: metadata.mimeType }), fileName);
// const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${this.config.accountId}/images/v1`, {
// method: 'POST',
// headers: { Authorization: `Bearer ${this.config.apiToken}` },
// body: formData
// });
console.warn('⚠️ Cloudflare upload not implemented. Using mock URL.');
return this._generateMockCDNUrl(fileName, metadata);
}
/**
* Upload to AWS S3 (placeholder)
* @private
*/
async _uploadToS3(fileBuffer, fileName, metadata) {
// Real implementation would use AWS SDK
// Example:
// const params = {
// Bucket: this.config.bucket,
// Key: `images/${fileName}`,
// Body: fileBuffer,
// ContentType: metadata.mimeType,
// CacheControl: `max-age=${this.config.cacheTTL}`,
// Metadata: metadata
// };
// const result = await this.client.upload(params).promise();
// return result.Location;
console.warn('⚠️ S3 upload not implemented. Using mock URL.');
return this._generateMockCDNUrl(fileName, metadata);
}
/**
* Upload to Azure Blob Storage (placeholder)
* @private
*/
async _uploadToAzure(fileBuffer, fileName, metadata) {
// Real implementation would use Azure SDK
// Example:
// const containerClient = this.client.getContainerClient('images');
// const blockBlobClient = containerClient.getBlockBlobClient(fileName);
// await blockBlobClient.upload(fileBuffer, fileBuffer.length);
// return blockBlobClient.url;
console.warn('⚠️ Azure upload not implemented. Using mock URL.');
return this._generateMockCDNUrl(fileName, metadata);
}
/**
* Mock CDN upload (for development/testing)
* @private
*/
async _uploadMock(fileBuffer, fileName, metadata) {
// Simulate network delay
await new Promise(resolve => setTimeout(resolve, Math.random() * 200));
return this._generateMockCDNUrl(fileName, metadata);
}
/**
* Generate mock CDN URL for testing
* @private
*/
_generateMockCDNUrl(fileName, metadata) {
const cdnPath = `images/${metadata.imageId}/${fileName}`;
return `${this.config.cdnUrl}/${cdnPath}?v=${Date.now()}`;
}
/**
* Batch upload multiple files
* @param {Array<string>} filePaths - Array of local file paths
* @param {Object} metadata - Common metadata
* @returns {Promise<Array<string>>} Array of CDN URLs
*/
async uploadBatch(filePaths, metadata = {}) {
const uploadTasks = filePaths.map(filePath =>
this.uploadFile(filePath, { ...metadata, fileName: path.basename(filePath) })
);
return Promise.all(uploadTasks);
}
/**
* Delete file from CDN
* @param {string} fileName - File name or path on CDN
* @returns {Promise<boolean>}
*/
async deleteFile(fileName) {
try {
switch (this.config.provider.toLowerCase()) {
case 'cloudflare':
// Cloudflare implementation
break;
case 's3':
// S3 delete implementation
break;
case 'azure':
// Azure delete implementation
break;
case 'mock':
default:
await new Promise(resolve => setTimeout(resolve, 100));
console.log(`🗑️ Mock delete: ${fileName}`);
break;
}
return true;
} catch (error) {
console.error(`Failed to delete ${fileName}:`, error.message);
return false;
}
}
/**
* Invalidate cache for specific files
* @param {Array<string>} filePaths - Files to purge from cache
* @returns {Promise<boolean>}
*/
async purgeCache(filePaths) {
try {
switch (this.config.provider.toLowerCase()) {
case 'cloudflare':
// Example: await this._purgeCloudflareCache(filePaths);
console.log('🔄 Purging Cloudflare cache...');
break;
case 's3':
// CloudFront invalidation
console.log('🔄 Creating CloudFront invalidation...');
break;
default:
console.log('🔄 Cache purge initiated for mock CDN');
break;
}
return true;
} catch (error) {
console.error('Cache purge failed:', error.message);
return false;
}
}
/**
* Get CDN upload metrics
* @returns {Object} Upload statistics
*/
getMetrics() {
return {
...this.uploadMetrics,
averageFileSizeKB: (this.uploadMetrics.totalBytesUploaded / this.uploadMetrics.successfulUploads / 1024).toFixed(2),
successRate: ((this.uploadMetrics.successfulUploads / this.uploadMetrics.totalUploads) * 100).toFixed(2)
};
}
/**
* Generate signed URL for temporary access (if needed)
* @param {string} fileName - File name
* @param {number} expirationSeconds - URL expiration time
* @returns {Promise<string>} Signed URL
*/
async generateSignedUrl(fileName, expirationSeconds = 3600) {
try {
switch (this.config.provider.toLowerCase()) {
case 'cloudflare':
// Cloudflare signed URL generation
break;
case 's3':
// S3 signed URL generation
break;
case 'azure':
// Azure SAS URL generation
break;
case 'mock':
default:
return `${this.config.cdnUrl}/images/${fileName}?expires=${Date.now() + expirationSeconds * 1000}`;
}
} catch (error) {
console.error('Signed URL generation failed:', error.message);
throw error;
}
}
}
module.exports = CDNUploader;