@oxyhq/services 5.10.5 → 5.10.6
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/lib/commonjs/core/OxyServices.js +1 -1
- package/lib/commonjs/core/OxyServices.js.map +1 -1
- package/lib/commonjs/index.js +31 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/s3FileManager.js +243 -0
- package/lib/commonjs/utils/s3FileManager.js.map +1 -0
- package/lib/commonjs/utils/s3FileManagerExample.js +407 -0
- package/lib/commonjs/utils/s3FileManagerExample.js.map +1 -0
- package/lib/commonjs/utils/s3FileManagerRN.js +274 -0
- package/lib/commonjs/utils/s3FileManagerRN.js.map +1 -0
- package/lib/module/core/OxyServices.js +1 -1
- package/lib/module/core/OxyServices.js.map +1 -1
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/s3FileManager.js +237 -0
- package/lib/module/utils/s3FileManager.js.map +1 -0
- package/lib/module/utils/s3FileManagerExample.js +400 -0
- package/lib/module/utils/s3FileManagerExample.js.map +1 -0
- package/lib/module/utils/s3FileManagerRN.js +268 -0
- package/lib/module/utils/s3FileManagerRN.js.map +1 -0
- package/lib/typescript/core/OxyServices.d.ts +2 -2
- package/lib/typescript/core/OxyServices.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +4 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/s3FileManager.d.ts +81 -0
- package/lib/typescript/utils/s3FileManager.d.ts.map +1 -0
- package/lib/typescript/utils/s3FileManagerExample.d.ts +87 -0
- package/lib/typescript/utils/s3FileManagerExample.d.ts.map +1 -0
- package/lib/typescript/utils/s3FileManagerRN.d.ts +104 -0
- package/lib/typescript/utils/s3FileManagerRN.d.ts.map +1 -0
- package/package.json +3 -1
- package/src/core/OxyServices.ts +2 -2
- package/src/index.ts +17 -1
- package/src/utils/s3FileManager.ts +281 -0
- package/src/utils/s3FileManagerExample.ts +432 -0
- package/src/utils/s3FileManagerRN.ts +322 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* S3 File Management Examples
|
|
5
|
+
*
|
|
6
|
+
* This file demonstrates how to use the S3 file management functionality
|
|
7
|
+
* in both web and React Native environments.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createS3FileManager } from './s3FileManager';
|
|
11
|
+
import { createS3FileManagerRN } from './s3FileManagerRN';
|
|
12
|
+
|
|
13
|
+
// Example configuration
|
|
14
|
+
const s3Config = {
|
|
15
|
+
region: 'us-east-1',
|
|
16
|
+
accessKeyId: 'your-access-key-id',
|
|
17
|
+
secretAccessKey: 'your-secret-access-key',
|
|
18
|
+
bucketName: 'your-bucket-name'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// WEB ENVIRONMENT EXAMPLES
|
|
23
|
+
// ============================================================================
|
|
24
|
+
|
|
25
|
+
export class WebFileManagerExample {
|
|
26
|
+
constructor() {
|
|
27
|
+
this.s3Manager = createS3FileManager(s3Config);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Example: Upload a file from web form
|
|
32
|
+
*/
|
|
33
|
+
async uploadFileFromForm(fileInput) {
|
|
34
|
+
const file = fileInput.files?.[0];
|
|
35
|
+
if (!file) {
|
|
36
|
+
throw new Error('No file selected');
|
|
37
|
+
}
|
|
38
|
+
const key = `uploads/${Date.now()}-${file.name}`;
|
|
39
|
+
const uploadOptions = {
|
|
40
|
+
contentType: file.type,
|
|
41
|
+
metadata: {
|
|
42
|
+
originalName: file.name,
|
|
43
|
+
uploadedBy: 'web-user',
|
|
44
|
+
uploadedAt: new Date().toISOString()
|
|
45
|
+
},
|
|
46
|
+
publicRead: false
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
const url = await this.s3Manager.uploadFile(key, file, uploadOptions);
|
|
50
|
+
console.log('File uploaded successfully:', url);
|
|
51
|
+
return url;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Upload failed:', error);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Example: Upload multiple files
|
|
60
|
+
*/
|
|
61
|
+
async uploadMultipleFiles(fileList) {
|
|
62
|
+
const files = Array.from(fileList).map(file => ({
|
|
63
|
+
key: `uploads/${Date.now()}-${file.name}`,
|
|
64
|
+
file,
|
|
65
|
+
options: {
|
|
66
|
+
contentType: file.type,
|
|
67
|
+
metadata: {
|
|
68
|
+
originalName: file.name,
|
|
69
|
+
uploadedBy: 'web-user',
|
|
70
|
+
uploadedAt: new Date().toISOString()
|
|
71
|
+
},
|
|
72
|
+
publicRead: false
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
try {
|
|
76
|
+
const urls = await this.s3Manager.uploadMultipleFiles(files);
|
|
77
|
+
console.log('Files uploaded successfully:', urls);
|
|
78
|
+
return urls;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error('Upload failed:', error);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Example: Download a file
|
|
87
|
+
*/
|
|
88
|
+
async downloadFile(key) {
|
|
89
|
+
try {
|
|
90
|
+
const buffer = await this.s3Manager.downloadFile(key);
|
|
91
|
+
|
|
92
|
+
// Create a blob and download link
|
|
93
|
+
const blob = new Blob([buffer]);
|
|
94
|
+
const url = URL.createObjectURL(blob);
|
|
95
|
+
const a = document.createElement('a');
|
|
96
|
+
a.href = url;
|
|
97
|
+
a.download = key.split('/').pop() || 'download';
|
|
98
|
+
document.body.appendChild(a);
|
|
99
|
+
a.click();
|
|
100
|
+
document.body.removeChild(a);
|
|
101
|
+
URL.revokeObjectURL(url);
|
|
102
|
+
console.log('File downloaded successfully');
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.error('Download failed:', error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Example: Get presigned URLs
|
|
111
|
+
*/
|
|
112
|
+
async getPresignedUrls(key) {
|
|
113
|
+
try {
|
|
114
|
+
const uploadUrl = await this.s3Manager.getPresignedUploadUrl(key, 'application/octet-stream');
|
|
115
|
+
const downloadUrl = await this.s3Manager.getPresignedDownloadUrl(key);
|
|
116
|
+
return {
|
|
117
|
+
upload: uploadUrl,
|
|
118
|
+
download: downloadUrl
|
|
119
|
+
};
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error('Failed to get presigned URLs:', error);
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Example: List files with pagination
|
|
128
|
+
*/
|
|
129
|
+
async listFiles(prefix = '', maxKeys = 50) {
|
|
130
|
+
try {
|
|
131
|
+
const files = await this.s3Manager.listFiles(prefix, maxKeys);
|
|
132
|
+
console.log('Files found:', files);
|
|
133
|
+
files.forEach(file => {
|
|
134
|
+
console.log(`- ${file.key} (${file.size} bytes, modified: ${file.lastModified})`);
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Failed to list files:', error);
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Example: File operations (copy, move, delete)
|
|
144
|
+
*/
|
|
145
|
+
async performFileOperations() {
|
|
146
|
+
const sourceKey = 'uploads/source-file.txt';
|
|
147
|
+
const destKey = 'uploads/destination-file.txt';
|
|
148
|
+
try {
|
|
149
|
+
// Copy file
|
|
150
|
+
await this.s3Manager.copyFile(sourceKey, destKey);
|
|
151
|
+
console.log('File copied successfully');
|
|
152
|
+
|
|
153
|
+
// Move file
|
|
154
|
+
await this.s3Manager.moveFile(destKey, 'uploads/moved-file.txt');
|
|
155
|
+
console.log('File moved successfully');
|
|
156
|
+
|
|
157
|
+
// Delete file
|
|
158
|
+
await this.s3Manager.deleteFile('uploads/moved-file.txt');
|
|
159
|
+
console.log('File deleted successfully');
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error('File operation failed:', error);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ============================================================================
|
|
168
|
+
// REACT NATIVE ENVIRONMENT EXAMPLES
|
|
169
|
+
// ============================================================================
|
|
170
|
+
|
|
171
|
+
export class ReactNativeFileManagerExample {
|
|
172
|
+
constructor() {
|
|
173
|
+
this.s3Manager = createS3FileManagerRN(s3Config);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Example: Upload a file from React Native
|
|
178
|
+
*/
|
|
179
|
+
async uploadFileFromRN(fileInfo) {
|
|
180
|
+
const key = `uploads/${Date.now()}-${fileInfo.name}`;
|
|
181
|
+
const uploadOptions = {
|
|
182
|
+
contentType: fileInfo.type || 'application/octet-stream',
|
|
183
|
+
metadata: {
|
|
184
|
+
originalName: fileInfo.name,
|
|
185
|
+
uploadedBy: 'rn-user',
|
|
186
|
+
uploadedAt: new Date().toISOString(),
|
|
187
|
+
fileSize: fileInfo.size?.toString() || '0'
|
|
188
|
+
},
|
|
189
|
+
publicRead: false
|
|
190
|
+
};
|
|
191
|
+
try {
|
|
192
|
+
const url = await this.s3Manager.uploadFile(key, fileInfo, uploadOptions);
|
|
193
|
+
console.log('File uploaded successfully:', url);
|
|
194
|
+
return url;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('Upload failed:', error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Example: Upload image with size validation
|
|
203
|
+
*/
|
|
204
|
+
async uploadImageWithValidation(fileInfo, maxSize = 5 * 1024 * 1024) {
|
|
205
|
+
// Get file size
|
|
206
|
+
const fileSize = await this.s3Manager.getFileSize(fileInfo);
|
|
207
|
+
if (fileSize > maxSize) {
|
|
208
|
+
throw new Error(`File size (${fileSize} bytes) exceeds maximum allowed size (${maxSize} bytes)`);
|
|
209
|
+
}
|
|
210
|
+
const key = `images/${Date.now()}-${fileInfo.name}`;
|
|
211
|
+
const uploadOptions = {
|
|
212
|
+
contentType: fileInfo.type || 'image/jpeg',
|
|
213
|
+
metadata: {
|
|
214
|
+
originalName: fileInfo.name,
|
|
215
|
+
uploadedBy: 'rn-user',
|
|
216
|
+
uploadedAt: new Date().toISOString(),
|
|
217
|
+
fileSize: fileSize.toString(),
|
|
218
|
+
category: 'image'
|
|
219
|
+
},
|
|
220
|
+
publicRead: true // Images are often public
|
|
221
|
+
};
|
|
222
|
+
try {
|
|
223
|
+
const url = await this.s3Manager.uploadImage(key, fileInfo, uploadOptions);
|
|
224
|
+
console.log('Image uploaded successfully:', url);
|
|
225
|
+
return url;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
console.error('Image upload failed:', error);
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Example: Download file to React Native
|
|
234
|
+
*/
|
|
235
|
+
async downloadFileToRN(key) {
|
|
236
|
+
try {
|
|
237
|
+
const buffer = await this.s3Manager.downloadFile(key);
|
|
238
|
+
console.log('File downloaded successfully, size:', buffer.length);
|
|
239
|
+
return buffer;
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error('Download failed:', error);
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Example: Batch operations
|
|
248
|
+
*/
|
|
249
|
+
async performBatchOperations(files) {
|
|
250
|
+
try {
|
|
251
|
+
// Upload multiple files
|
|
252
|
+
const uploadPromises = files.map((file, index) => {
|
|
253
|
+
const key = `batch-uploads/${Date.now()}-${index}-${file.name}`;
|
|
254
|
+
return this.s3Manager.uploadFile(key, file, {
|
|
255
|
+
metadata: {
|
|
256
|
+
batchId: Date.now().toString()
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
const uploadedUrls = await Promise.all(uploadPromises);
|
|
261
|
+
console.log('Batch upload completed:', uploadedUrls);
|
|
262
|
+
|
|
263
|
+
// Delete multiple files (example with keys)
|
|
264
|
+
const keysToDelete = uploadedUrls.map(url => {
|
|
265
|
+
const key = url.split('/').pop();
|
|
266
|
+
return `batch-uploads/${key}`;
|
|
267
|
+
});
|
|
268
|
+
await this.s3Manager.deleteMultipleFiles(keysToDelete);
|
|
269
|
+
console.log('Batch delete completed');
|
|
270
|
+
} catch (error) {
|
|
271
|
+
console.error('Batch operations failed:', error);
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ============================================================================
|
|
278
|
+
// USAGE EXAMPLES
|
|
279
|
+
// ============================================================================
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Example usage in a React component
|
|
283
|
+
*/
|
|
284
|
+
export const useS3FileManager = () => {
|
|
285
|
+
const webManager = new WebFileManagerExample();
|
|
286
|
+
const rnManager = new ReactNativeFileManagerExample();
|
|
287
|
+
const handleFileUpload = async file => {
|
|
288
|
+
try {
|
|
289
|
+
if ('uri' in file) {
|
|
290
|
+
// React Native file
|
|
291
|
+
return await rnManager.uploadFileFromRN(file);
|
|
292
|
+
} else {
|
|
293
|
+
// Web file
|
|
294
|
+
const mockInput = {
|
|
295
|
+
files: [file]
|
|
296
|
+
};
|
|
297
|
+
return await webManager.uploadFileFromForm(mockInput);
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
console.error('Upload failed:', error);
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const handleFileDownload = async key => {
|
|
305
|
+
try {
|
|
306
|
+
if (typeof window !== 'undefined') {
|
|
307
|
+
// Web environment
|
|
308
|
+
await webManager.downloadFile(key);
|
|
309
|
+
} else {
|
|
310
|
+
// React Native environment
|
|
311
|
+
const buffer = await rnManager.downloadFileToRN(key);
|
|
312
|
+
// Handle buffer in React Native (save to file system, etc.)
|
|
313
|
+
return buffer;
|
|
314
|
+
}
|
|
315
|
+
} catch (error) {
|
|
316
|
+
console.error('Download failed:', error);
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
return {
|
|
321
|
+
uploadFile: handleFileUpload,
|
|
322
|
+
downloadFile: handleFileDownload,
|
|
323
|
+
listFiles: webManager.listFiles.bind(webManager),
|
|
324
|
+
getPresignedUrls: webManager.getPresignedUrls.bind(webManager)
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Example: Error handling and retry logic
|
|
330
|
+
*/
|
|
331
|
+
export const uploadWithRetry = async (s3Manager, key, file, options = {}, maxRetries = 3) => {
|
|
332
|
+
// Type guard to determine which manager we're using
|
|
333
|
+
const isRNManager = manager => {
|
|
334
|
+
return 'getFileSize' in manager;
|
|
335
|
+
};
|
|
336
|
+
let lastError;
|
|
337
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
338
|
+
try {
|
|
339
|
+
if (isRNManager(s3Manager)) {
|
|
340
|
+
// For React Native manager, ensure file is RNFile
|
|
341
|
+
if ('uri' in file) {
|
|
342
|
+
return await s3Manager.uploadFile(key, file, options);
|
|
343
|
+
} else {
|
|
344
|
+
throw new Error('React Native manager requires RNFile with uri property');
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
// For web manager, ensure file is not RNFile
|
|
348
|
+
if ('uri' in file) {
|
|
349
|
+
throw new Error('Web manager does not support RNFile');
|
|
350
|
+
} else {
|
|
351
|
+
return await s3Manager.uploadFile(key, file, options);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
lastError = error;
|
|
356
|
+
console.warn(`Upload attempt ${attempt} failed:`, error);
|
|
357
|
+
if (attempt < maxRetries) {
|
|
358
|
+
// Wait before retrying (exponential backoff)
|
|
359
|
+
const delay = Math.pow(2, attempt) * 1000;
|
|
360
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
throw new Error(`Upload failed after ${maxRetries} attempts. Last error: ${lastError?.message || 'Unknown error'}`);
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Example: File validation utilities
|
|
369
|
+
*/
|
|
370
|
+
export const validateFile = (file, options = {}) => {
|
|
371
|
+
const errors = [];
|
|
372
|
+
const {
|
|
373
|
+
maxSize = 10 * 1024 * 1024,
|
|
374
|
+
allowedTypes = [],
|
|
375
|
+
allowedExtensions = []
|
|
376
|
+
} = options;
|
|
377
|
+
|
|
378
|
+
// Check file size
|
|
379
|
+
if ('size' in file && file.size && file.size > maxSize) {
|
|
380
|
+
errors.push(`File size (${file.size} bytes) exceeds maximum allowed size (${maxSize} bytes)`);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Check file type
|
|
384
|
+
if ('type' in file && file.type && allowedTypes.length > 0 && !allowedTypes.includes(file.type)) {
|
|
385
|
+
errors.push(`File type (${file.type}) is not allowed`);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Check file extension
|
|
389
|
+
if (allowedExtensions.length > 0) {
|
|
390
|
+
const extension = file.name.split('.').pop()?.toLowerCase();
|
|
391
|
+
if (!extension || !allowedExtensions.includes(`.${extension}`)) {
|
|
392
|
+
errors.push(`File extension (.${extension}) is not allowed`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
isValid: errors.length === 0,
|
|
397
|
+
errors
|
|
398
|
+
};
|
|
399
|
+
};
|
|
400
|
+
//# sourceMappingURL=s3FileManagerExample.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createS3FileManager","createS3FileManagerRN","s3Config","region","accessKeyId","secretAccessKey","bucketName","WebFileManagerExample","constructor","s3Manager","uploadFileFromForm","fileInput","file","files","Error","key","Date","now","name","uploadOptions","contentType","type","metadata","originalName","uploadedBy","uploadedAt","toISOString","publicRead","url","uploadFile","console","log","error","uploadMultipleFiles","fileList","Array","from","map","options","urls","downloadFile","buffer","blob","Blob","URL","createObjectURL","a","document","createElement","href","download","split","pop","body","appendChild","click","removeChild","revokeObjectURL","getPresignedUrls","uploadUrl","getPresignedUploadUrl","downloadUrl","getPresignedDownloadUrl","upload","listFiles","prefix","maxKeys","forEach","size","lastModified","performFileOperations","sourceKey","destKey","copyFile","moveFile","deleteFile","ReactNativeFileManagerExample","uploadFileFromRN","fileInfo","fileSize","toString","uploadImageWithValidation","maxSize","getFileSize","category","uploadImage","downloadFileToRN","length","performBatchOperations","uploadPromises","index","batchId","uploadedUrls","Promise","all","keysToDelete","deleteMultipleFiles","useS3FileManager","webManager","rnManager","handleFileUpload","mockInput","handleFileDownload","window","bind","uploadWithRetry","maxRetries","isRNManager","manager","lastError","attempt","warn","delay","Math","pow","resolve","setTimeout","message","validateFile","errors","allowedTypes","allowedExtensions","push","includes","extension","toLowerCase","isValid"],"sourceRoot":"../../../src","sources":["utils/s3FileManagerExample.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAEEA,mBAAmB,QAGd,iBAAiB;AACxB,SAEEC,qBAAqB,QAEhB,mBAAmB;;AAE1B;AACA,MAAMC,QAAkB,GAAG;EACzBC,MAAM,EAAE,WAAW;EACnBC,WAAW,EAAE,oBAAoB;EACjCC,eAAe,EAAE,wBAAwB;EACzCC,UAAU,EAAE;AACd,CAAC;;AAED;AACA;AACA;;AAEA,OAAO,MAAMC,qBAAqB,CAAC;EAGjCC,WAAWA,CAAA,EAAG;IACZ,IAAI,CAACC,SAAS,GAAGT,mBAAmB,CAACE,QAAQ,CAAC;EAChD;;EAEA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AAC,EAAE;IAC/FkG,MAAM,CAACG,IAAI,CAAC,cAAc9G,IAAI,CAACS,IAAI,kBAAkB,CAAC;EACxD;;EAEA;EACA,IAAIoG,iBAAiB,CAAClC,MAAM,GAAG,CAAC,EAAE;IAChC,MAAMqC,SAAS,GAAGhH,IAAI,CAACM,IAAI,CAACiC,KAAK,CAAC,GAAG,CAAC,CAACC,GAAG,CAAC,CAAC,EAAEyE,WAAW,CAAC,CAAC;IAC3D,IAAI,CAACD,SAAS,IAAI,CAACH,iBAAiB,CAACE,QAAQ,CAAC,IAAIC,SAAS,EAAE,CAAC,EAAE;MAC9DL,MAAM,CAACG,IAAI,CAAC,oBAAoBE,SAAS,kBAAkB,CAAC;IAC9D;EACF;EAEA,OAAO;IACLE,OAAO,EAAEP,MAAM,CAAChC,MAAM,KAAK,CAAC;IAC5BgC;EACF,CAAC;AACH,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
|
|
4
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
5
|
+
export class S3FileManagerRN {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.s3Client = new S3Client({
|
|
8
|
+
region: config.region,
|
|
9
|
+
credentials: {
|
|
10
|
+
accessKeyId: config.accessKeyId,
|
|
11
|
+
secretAccessKey: config.secretAccessKey
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
this.bucketName = config.bucketName;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Upload a file to S3 from React Native
|
|
19
|
+
*/
|
|
20
|
+
async uploadFile(key, file, options = {}) {
|
|
21
|
+
let body;
|
|
22
|
+
let contentType = options.contentType;
|
|
23
|
+
if (this.isRNFile(file)) {
|
|
24
|
+
body = await this.rnFileToBuffer(file);
|
|
25
|
+
contentType = contentType || file.type || 'application/octet-stream';
|
|
26
|
+
} else if (typeof file === 'string') {
|
|
27
|
+
body = file;
|
|
28
|
+
contentType = contentType || 'text/plain';
|
|
29
|
+
} else {
|
|
30
|
+
body = file;
|
|
31
|
+
contentType = contentType || 'application/octet-stream';
|
|
32
|
+
}
|
|
33
|
+
const command = new PutObjectCommand({
|
|
34
|
+
Bucket: this.bucketName,
|
|
35
|
+
Key: key,
|
|
36
|
+
Body: body,
|
|
37
|
+
ContentType: contentType,
|
|
38
|
+
Metadata: options.metadata,
|
|
39
|
+
ACL: options.publicRead ? 'public-read' : 'private'
|
|
40
|
+
});
|
|
41
|
+
await this.s3Client.send(command);
|
|
42
|
+
return `https://${this.bucketName}.s3.amazonaws.com/${key}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Download a file from S3 to React Native
|
|
47
|
+
*/
|
|
48
|
+
async downloadFile(key) {
|
|
49
|
+
const command = new GetObjectCommand({
|
|
50
|
+
Bucket: this.bucketName,
|
|
51
|
+
Key: key
|
|
52
|
+
});
|
|
53
|
+
const response = await this.s3Client.send(command);
|
|
54
|
+
if (!response.Body) {
|
|
55
|
+
throw new Error('File not found or empty');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Convert stream to buffer
|
|
59
|
+
const chunks = [];
|
|
60
|
+
const reader = response.Body.transformToWebStream().getReader();
|
|
61
|
+
while (true) {
|
|
62
|
+
const {
|
|
63
|
+
done,
|
|
64
|
+
value
|
|
65
|
+
} = await reader.read();
|
|
66
|
+
if (done) break;
|
|
67
|
+
chunks.push(value);
|
|
68
|
+
}
|
|
69
|
+
return Buffer.concat(chunks);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Delete a file from S3
|
|
74
|
+
*/
|
|
75
|
+
async deleteFile(key) {
|
|
76
|
+
const command = new DeleteObjectCommand({
|
|
77
|
+
Bucket: this.bucketName,
|
|
78
|
+
Key: key
|
|
79
|
+
});
|
|
80
|
+
await this.s3Client.send(command);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Generate a presigned URL for file upload
|
|
85
|
+
*/
|
|
86
|
+
async getPresignedUploadUrl(key, contentType, expiresIn = 3600) {
|
|
87
|
+
const command = new PutObjectCommand({
|
|
88
|
+
Bucket: this.bucketName,
|
|
89
|
+
Key: key,
|
|
90
|
+
ContentType: contentType
|
|
91
|
+
});
|
|
92
|
+
return getSignedUrl(this.s3Client, command, {
|
|
93
|
+
expiresIn
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Generate a presigned URL for file download
|
|
99
|
+
*/
|
|
100
|
+
async getPresignedDownloadUrl(key, expiresIn = 3600) {
|
|
101
|
+
const command = new GetObjectCommand({
|
|
102
|
+
Bucket: this.bucketName,
|
|
103
|
+
Key: key
|
|
104
|
+
});
|
|
105
|
+
return getSignedUrl(this.s3Client, command, {
|
|
106
|
+
expiresIn
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* List files in a directory
|
|
112
|
+
*/
|
|
113
|
+
async listFiles(prefix = '', maxKeys = 1000) {
|
|
114
|
+
const command = new ListObjectsV2Command({
|
|
115
|
+
Bucket: this.bucketName,
|
|
116
|
+
Prefix: prefix,
|
|
117
|
+
MaxKeys: maxKeys
|
|
118
|
+
});
|
|
119
|
+
const response = await this.s3Client.send(command);
|
|
120
|
+
if (!response.Contents) {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
return response.Contents.map(item => ({
|
|
124
|
+
key: item.Key,
|
|
125
|
+
size: item.Size || 0,
|
|
126
|
+
lastModified: item.LastModified
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Check if a file exists
|
|
132
|
+
*/
|
|
133
|
+
async fileExists(key) {
|
|
134
|
+
try {
|
|
135
|
+
const command = new GetObjectCommand({
|
|
136
|
+
Bucket: this.bucketName,
|
|
137
|
+
Key: key
|
|
138
|
+
});
|
|
139
|
+
await this.s3Client.send(command);
|
|
140
|
+
return true;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error.name === 'NoSuchKey') {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get file metadata
|
|
151
|
+
*/
|
|
152
|
+
async getFileMetadata(key) {
|
|
153
|
+
try {
|
|
154
|
+
const command = new GetObjectCommand({
|
|
155
|
+
Bucket: this.bucketName,
|
|
156
|
+
Key: key
|
|
157
|
+
});
|
|
158
|
+
const response = await this.s3Client.send(command);
|
|
159
|
+
return {
|
|
160
|
+
key,
|
|
161
|
+
size: parseInt(response.ContentLength?.toString() || '0'),
|
|
162
|
+
lastModified: response.LastModified,
|
|
163
|
+
contentType: response.ContentType,
|
|
164
|
+
metadata: response.Metadata
|
|
165
|
+
};
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error.name === 'NoSuchKey') {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Convert React Native file to Buffer
|
|
176
|
+
*/
|
|
177
|
+
async rnFileToBuffer(file) {
|
|
178
|
+
try {
|
|
179
|
+
// For React Native, we need to fetch the file from the URI
|
|
180
|
+
const response = await fetch(file.uri);
|
|
181
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
182
|
+
return Buffer.from(arrayBuffer);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
throw new Error(`Failed to read React Native file: ${error}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Type guard to check if object is RNFile
|
|
190
|
+
*/
|
|
191
|
+
isRNFile(file) {
|
|
192
|
+
return file && typeof file === 'object' && 'uri' in file && 'name' in file;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Upload multiple files
|
|
197
|
+
*/
|
|
198
|
+
async uploadMultipleFiles(files) {
|
|
199
|
+
const uploadPromises = files.map(({
|
|
200
|
+
key,
|
|
201
|
+
file,
|
|
202
|
+
options
|
|
203
|
+
}) => this.uploadFile(key, file, options));
|
|
204
|
+
return Promise.all(uploadPromises);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Delete multiple files
|
|
209
|
+
*/
|
|
210
|
+
async deleteMultipleFiles(keys) {
|
|
211
|
+
const deletePromises = keys.map(key => this.deleteFile(key));
|
|
212
|
+
await Promise.all(deletePromises);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Copy file from one key to another
|
|
217
|
+
*/
|
|
218
|
+
async copyFile(sourceKey, destinationKey) {
|
|
219
|
+
const command = new PutObjectCommand({
|
|
220
|
+
Bucket: this.bucketName,
|
|
221
|
+
Key: destinationKey,
|
|
222
|
+
Body: await this.downloadFile(sourceKey)
|
|
223
|
+
});
|
|
224
|
+
await this.s3Client.send(command);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Move file (copy + delete)
|
|
229
|
+
*/
|
|
230
|
+
async moveFile(sourceKey, destinationKey) {
|
|
231
|
+
await this.copyFile(sourceKey, destinationKey);
|
|
232
|
+
await this.deleteFile(sourceKey);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Upload image with automatic resizing (placeholder for future implementation)
|
|
237
|
+
*/
|
|
238
|
+
async uploadImage(key, file, options = {}) {
|
|
239
|
+
// For now, just upload as regular file
|
|
240
|
+
// In the future, this could integrate with image processing libraries
|
|
241
|
+
return this.uploadFile(key, file, options);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Get file size from React Native file
|
|
246
|
+
*/
|
|
247
|
+
async getFileSize(file) {
|
|
248
|
+
if (file.size) {
|
|
249
|
+
return file.size;
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
const response = await fetch(file.uri, {
|
|
253
|
+
method: 'HEAD'
|
|
254
|
+
});
|
|
255
|
+
const contentLength = response.headers.get('content-length');
|
|
256
|
+
return contentLength ? parseInt(contentLength, 10) : 0;
|
|
257
|
+
} catch (error) {
|
|
258
|
+
console.warn('Could not determine file size:', error);
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Export a factory function for easier configuration
|
|
265
|
+
export function createS3FileManagerRN(config) {
|
|
266
|
+
return new S3FileManagerRN(config);
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=s3FileManagerRN.js.map
|