@pelatform/storage 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.
@@ -0,0 +1,496 @@
1
+ import {
2
+ loadCloudinaryConfig
3
+ } from "./chunk-ZTZZCC52.js";
4
+
5
+ // src/providers/cloudinary.ts
6
+ import { v2 as cloudinary } from "cloudinary";
7
+ var CloudinaryProvider = class {
8
+ config;
9
+ constructor(config) {
10
+ this.config = config;
11
+ cloudinary.config({
12
+ cloud_name: config.cloudName,
13
+ api_key: config.apiKey,
14
+ api_secret: config.apiSecret,
15
+ secure: config.secure !== false
16
+ });
17
+ }
18
+ // File operations
19
+ async upload(options) {
20
+ try {
21
+ let fileData;
22
+ if (typeof options.file === "string") {
23
+ fileData = options.file;
24
+ } else {
25
+ fileData = `data:${options.contentType || "application/octet-stream"};base64,${Buffer.from(options.file).toString("base64")}`;
26
+ }
27
+ let resourceType = "raw";
28
+ if (options.contentType?.startsWith("image/")) {
29
+ resourceType = "image";
30
+ } else if (options.contentType?.startsWith("video/")) {
31
+ resourceType = "video";
32
+ }
33
+ const keyParts = options.key.split("/");
34
+ let folder = "";
35
+ let publicId = options.key;
36
+ if (keyParts.length > 1) {
37
+ folder = keyParts.slice(0, -1).join("/");
38
+ publicId = keyParts[keyParts.length - 1];
39
+ }
40
+ const uploadOptions = {
41
+ public_id: publicId,
42
+ resource_type: resourceType,
43
+ overwrite: true,
44
+ invalidate: true,
45
+ ...options.metadata
46
+ };
47
+ if (folder) {
48
+ uploadOptions.folder = folder;
49
+ }
50
+ const result = await cloudinary.uploader.upload(fileData, uploadOptions);
51
+ const publicUrl = this.getPublicUrl(result.public_id);
52
+ return {
53
+ success: true,
54
+ key: result.public_id,
55
+ url: result.secure_url,
56
+ publicUrl,
57
+ etag: result.etag,
58
+ size: result.bytes
59
+ };
60
+ } catch (error) {
61
+ return {
62
+ success: false,
63
+ error: error instanceof Error ? error.message : "Upload failed"
64
+ };
65
+ }
66
+ }
67
+ async download(options) {
68
+ try {
69
+ const publicUrl = this.getPublicUrl(options.key);
70
+ const response = await fetch(publicUrl);
71
+ if (!response.ok) {
72
+ return {
73
+ success: false,
74
+ error: `Failed to download: ${response.status} ${response.statusText}`
75
+ };
76
+ }
77
+ const arrayBuffer = await response.arrayBuffer();
78
+ const content = Buffer.from(arrayBuffer);
79
+ return {
80
+ success: true,
81
+ content,
82
+ // Main content field
83
+ data: content,
84
+ // Alias for backward compatibility
85
+ contentType: response.headers.get("content-type") || void 0,
86
+ contentLength: content.length,
87
+ lastModified: response.headers.get("last-modified") ? new Date(response.headers.get("last-modified")) : /* @__PURE__ */ new Date()
88
+ };
89
+ } catch (error) {
90
+ return {
91
+ success: false,
92
+ error: error instanceof Error ? error.message : "Download failed"
93
+ };
94
+ }
95
+ }
96
+ async delete(options) {
97
+ try {
98
+ const keyParts = options.key.split("/");
99
+ let publicId = options.key;
100
+ if (keyParts.length > 1) {
101
+ const folder = keyParts.slice(0, -1).join("/");
102
+ const filename = keyParts[keyParts.length - 1];
103
+ publicId = `${folder}/${filename}`;
104
+ }
105
+ let resourceType = "raw";
106
+ try {
107
+ await cloudinary.api.resource(publicId, { resource_type: "image" });
108
+ resourceType = "image";
109
+ } catch {
110
+ try {
111
+ await cloudinary.api.resource(publicId, { resource_type: "video" });
112
+ resourceType = "video";
113
+ } catch {
114
+ resourceType = "raw";
115
+ }
116
+ }
117
+ await cloudinary.uploader.destroy(publicId, {
118
+ resource_type: resourceType,
119
+ invalidate: true
120
+ });
121
+ return {
122
+ success: true
123
+ };
124
+ } catch (error) {
125
+ return {
126
+ success: false,
127
+ error: error instanceof Error ? error.message : "Delete failed"
128
+ };
129
+ }
130
+ }
131
+ async batchDelete(options) {
132
+ try {
133
+ return {
134
+ success: false,
135
+ errors: options.keys.map((key) => ({
136
+ key,
137
+ error: "Cloudinary batch delete not implemented yet"
138
+ }))
139
+ };
140
+ } catch (error) {
141
+ return {
142
+ success: false,
143
+ errors: options.keys.map((key) => ({
144
+ key,
145
+ error: error instanceof Error ? error.message : "Batch delete failed"
146
+ }))
147
+ };
148
+ }
149
+ }
150
+ async list(options = {}) {
151
+ try {
152
+ const prefix = options.prefix || "";
153
+ const maxResults = options.maxKeys || 50;
154
+ const [images, videos, rawFiles] = await Promise.all([
155
+ cloudinary.api.resources({
156
+ type: "upload",
157
+ resource_type: "image",
158
+ prefix,
159
+ max_results: maxResults,
160
+ next_cursor: options.continuationToken
161
+ }).catch(() => ({ resources: [], next_cursor: void 0 })),
162
+ cloudinary.api.resources({
163
+ type: "upload",
164
+ resource_type: "video",
165
+ prefix,
166
+ max_results: maxResults,
167
+ next_cursor: options.continuationToken
168
+ }).catch(() => ({ resources: [], next_cursor: void 0 })),
169
+ cloudinary.api.resources({
170
+ type: "upload",
171
+ resource_type: "raw",
172
+ prefix,
173
+ max_results: maxResults,
174
+ next_cursor: options.continuationToken
175
+ }).catch(() => ({ resources: [], next_cursor: void 0 }))
176
+ ]);
177
+ const allResources = [
178
+ ...images.resources || [],
179
+ ...videos.resources || [],
180
+ ...rawFiles.resources || []
181
+ ];
182
+ const files = allResources.map((resource) => ({
183
+ key: String(resource.public_id || ""),
184
+ size: Number(resource.bytes) || 0,
185
+ lastModified: new Date(String(resource.created_at || /* @__PURE__ */ new Date())),
186
+ etag: String(resource.etag || ""),
187
+ contentType: resource.format ? `${resource.resource_type}/${resource.format}` : "application/octet-stream"
188
+ }));
189
+ return {
190
+ success: true,
191
+ files,
192
+ isTruncated: !!(images.next_cursor || videos.next_cursor || rawFiles.next_cursor),
193
+ nextContinuationToken: images.next_cursor || videos.next_cursor || rawFiles.next_cursor
194
+ };
195
+ } catch (error) {
196
+ return {
197
+ success: false,
198
+ error: error instanceof Error ? error.message : "List failed"
199
+ };
200
+ }
201
+ }
202
+ async exists(_key) {
203
+ try {
204
+ return {
205
+ exists: false,
206
+ error: "Cloudinary exists check not implemented yet"
207
+ };
208
+ } catch (error) {
209
+ return {
210
+ exists: false,
211
+ error: error instanceof Error ? error.message : "Check existence failed"
212
+ };
213
+ }
214
+ }
215
+ async copy(_options) {
216
+ try {
217
+ return {
218
+ success: false,
219
+ error: "Cloudinary copy not implemented yet"
220
+ };
221
+ } catch (error) {
222
+ return {
223
+ success: false,
224
+ error: error instanceof Error ? error.message : "Copy failed"
225
+ };
226
+ }
227
+ }
228
+ async move(_options) {
229
+ try {
230
+ return {
231
+ success: false,
232
+ error: "Cloudinary move not implemented yet"
233
+ };
234
+ } catch (error) {
235
+ return {
236
+ success: false,
237
+ error: error instanceof Error ? error.message : "Move failed"
238
+ };
239
+ }
240
+ }
241
+ async duplicate(options) {
242
+ return this.copy(options);
243
+ }
244
+ async getPresignedUrl(_options) {
245
+ try {
246
+ return {
247
+ success: false,
248
+ error: "Cloudinary presigned URL not implemented yet"
249
+ };
250
+ } catch (error) {
251
+ return {
252
+ success: false,
253
+ error: error instanceof Error ? error.message : "Presigned URL generation failed"
254
+ };
255
+ }
256
+ }
257
+ getPublicUrl(key) {
258
+ const protocol = this.config.secure !== false ? "https" : "http";
259
+ const baseUrl = `${protocol}://res.cloudinary.com/${this.config.cloudName}`;
260
+ const publicId = key;
261
+ return `${baseUrl}/image/upload/${publicId}`;
262
+ }
263
+ // Folder operations
264
+ async createFolder(options) {
265
+ try {
266
+ return {
267
+ success: true,
268
+ path: options.path
269
+ };
270
+ } catch (error) {
271
+ return {
272
+ success: false,
273
+ error: error instanceof Error ? error.message : "Create folder failed"
274
+ };
275
+ }
276
+ }
277
+ async deleteFolder(options) {
278
+ try {
279
+ const folderPath = options.path.endsWith("/") ? options.path.slice(0, -1) : options.path;
280
+ if (options.recursive) {
281
+ await cloudinary.api.delete_resources_by_prefix(folderPath);
282
+ }
283
+ await cloudinary.api.delete_folder(folderPath);
284
+ return {
285
+ success: true
286
+ };
287
+ } catch (error) {
288
+ return {
289
+ success: false,
290
+ error: error instanceof Error ? error.message : "Delete folder failed"
291
+ };
292
+ }
293
+ }
294
+ async listFolders(_options) {
295
+ try {
296
+ const result = await cloudinary.api.root_folders();
297
+ const folders = (result.folders || []).map((folder) => ({
298
+ name: folder.name,
299
+ path: folder.path
300
+ }));
301
+ return {
302
+ success: true,
303
+ folders,
304
+ isTruncated: false,
305
+ nextContinuationToken: void 0
306
+ };
307
+ } catch (error) {
308
+ return {
309
+ success: false,
310
+ error: error instanceof Error ? error.message : "List folders failed"
311
+ };
312
+ }
313
+ }
314
+ async folderExists(_path) {
315
+ try {
316
+ return {
317
+ exists: false,
318
+ error: "Cloudinary folder exists check not implemented yet"
319
+ };
320
+ } catch (error) {
321
+ return {
322
+ exists: false,
323
+ error: error instanceof Error ? error.message : "Check folder existence failed"
324
+ };
325
+ }
326
+ }
327
+ async renameFolder(_options) {
328
+ try {
329
+ return {
330
+ success: false,
331
+ error: "Cloudinary rename folder not implemented yet"
332
+ };
333
+ } catch (error) {
334
+ return {
335
+ success: false,
336
+ error: error instanceof Error ? error.message : "Rename folder failed"
337
+ };
338
+ }
339
+ }
340
+ async copyFolder(_options) {
341
+ try {
342
+ return {
343
+ success: false,
344
+ error: "Cloudinary copy folder not implemented yet"
345
+ };
346
+ } catch (error) {
347
+ return {
348
+ success: false,
349
+ error: error instanceof Error ? error.message : "Copy folder failed"
350
+ };
351
+ }
352
+ }
353
+ };
354
+
355
+ // src/services/cloudinary.ts
356
+ var CloudinaryService = class {
357
+ provider;
358
+ config;
359
+ constructor(config) {
360
+ this.config = config || loadCloudinaryConfig();
361
+ this.provider = new CloudinaryProvider(this.config);
362
+ }
363
+ // Utility methods
364
+ getConfig() {
365
+ return { ...this.config };
366
+ }
367
+ getProvider() {
368
+ return this.config.provider;
369
+ }
370
+ getPublicUrl(key) {
371
+ return this.provider.getPublicUrl(key);
372
+ }
373
+ // File operations
374
+ async upload(options) {
375
+ return this.provider.upload(options);
376
+ }
377
+ async download(options) {
378
+ return this.provider.download(options);
379
+ }
380
+ async delete(options) {
381
+ return this.provider.delete(options);
382
+ }
383
+ async batchDelete(options) {
384
+ return this.provider.batchDelete(options);
385
+ }
386
+ async list(options) {
387
+ return this.provider.list(options);
388
+ }
389
+ async exists(key) {
390
+ return this.provider.exists(key);
391
+ }
392
+ async copy(options) {
393
+ return this.provider.copy(options);
394
+ }
395
+ async move(options) {
396
+ return this.provider.move(options);
397
+ }
398
+ async duplicate(options) {
399
+ return this.provider.duplicate(options);
400
+ }
401
+ async getPresignedUrl(options) {
402
+ return this.provider.getPresignedUrl(options);
403
+ }
404
+ // Folder operations
405
+ async createFolder(options) {
406
+ return this.provider.createFolder(options);
407
+ }
408
+ async deleteFolder(options) {
409
+ return this.provider.deleteFolder(options);
410
+ }
411
+ async listFolders(options) {
412
+ return this.provider.listFolders(options);
413
+ }
414
+ async folderExists(path) {
415
+ return this.provider.folderExists(path);
416
+ }
417
+ async renameFolder(options) {
418
+ return this.provider.renameFolder(options);
419
+ }
420
+ async copyFolder(options) {
421
+ return this.provider.copyFolder(options);
422
+ }
423
+ // Convenience methods
424
+ async uploadFile(key, file, contentType, metadata) {
425
+ return this.upload({ key, file, contentType, metadata });
426
+ }
427
+ async downloadFile(key) {
428
+ return this.download({ key });
429
+ }
430
+ async deleteFile(key) {
431
+ return this.delete({ key });
432
+ }
433
+ async deleteFiles(keys) {
434
+ return this.batchDelete({ keys });
435
+ }
436
+ async fileExists(key) {
437
+ const result = await this.exists(key);
438
+ return result.exists;
439
+ }
440
+ async copyFile(sourceKey, destinationKey, metadata) {
441
+ return this.copy({ sourceKey, destinationKey, metadata });
442
+ }
443
+ async moveFile(sourceKey, destinationKey, metadata) {
444
+ return this.move({ sourceKey, destinationKey, metadata });
445
+ }
446
+ async duplicateFile(sourceKey, destinationKey, metadata) {
447
+ return this.duplicate({ sourceKey, destinationKey, metadata });
448
+ }
449
+ async renameFile(sourceKey, destinationKey, metadata) {
450
+ return this.moveFile(sourceKey, destinationKey, metadata);
451
+ }
452
+ async listFiles(prefix, maxKeys) {
453
+ return this.list({ prefix, maxKeys });
454
+ }
455
+ async getDownloadUrl(key, expiresIn) {
456
+ return this.getPresignedUrl({ key, operation: "get", expiresIn });
457
+ }
458
+ async getUploadUrl(key, contentType, expiresIn) {
459
+ return this.getPresignedUrl({
460
+ key,
461
+ operation: "put",
462
+ contentType,
463
+ expiresIn
464
+ });
465
+ }
466
+ // Folder convenience methods
467
+ async createFolderPath(path) {
468
+ return this.createFolder({ path });
469
+ }
470
+ async deleteFolderPath(path, recursive = false) {
471
+ return this.deleteFolder({ path, recursive });
472
+ }
473
+ async folderPathExists(path) {
474
+ const result = await this.folderExists(path);
475
+ return result.exists;
476
+ }
477
+ async renameFolderPath(oldPath, newPath) {
478
+ return this.renameFolder({ oldPath, newPath });
479
+ }
480
+ async copyFolderPath(sourcePath, destinationPath, recursive = true) {
481
+ return this.copyFolder({ sourcePath, destinationPath, recursive });
482
+ }
483
+ };
484
+
485
+ // src/cloudinary.ts
486
+ function createCloudinary(config) {
487
+ if (config) {
488
+ return new CloudinaryService(config);
489
+ }
490
+ const envConfig = loadCloudinaryConfig();
491
+ return new CloudinaryService(envConfig);
492
+ }
493
+ export {
494
+ CloudinaryService,
495
+ createCloudinary
496
+ };
@@ -0,0 +1,68 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
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
+ var _chunkGV7GMZP2cjs = require('./chunk-GV7GMZP2.cjs');
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
+ exports.base64ToBuffer = _chunkGV7GMZP2cjs.base64ToBuffer; exports.bufferToBase64 = _chunkGV7GMZP2cjs.bufferToBase64; exports.buildPublicUrl = _chunkGV7GMZP2cjs.buildPublicUrl; exports.detectFileTypeFromContent = _chunkGV7GMZP2cjs.detectFileTypeFromContent; exports.extractS3Info = _chunkGV7GMZP2cjs.extractS3Info; exports.fileToBuffer = _chunkGV7GMZP2cjs.fileToBuffer; exports.formatFileSize = _chunkGV7GMZP2cjs.formatFileSize; exports.generateBatchKeys = _chunkGV7GMZP2cjs.generateBatchKeys; exports.generateCacheControl = _chunkGV7GMZP2cjs.generateCacheControl; exports.generateFileHash = _chunkGV7GMZP2cjs.generateFileHash; exports.generateFileKey = _chunkGV7GMZP2cjs.generateFileKey; exports.generateUniqueKey = _chunkGV7GMZP2cjs.generateUniqueKey; exports.getContentDisposition = _chunkGV7GMZP2cjs.getContentDisposition; exports.getFileExtension = _chunkGV7GMZP2cjs.getFileExtension; exports.getFileInfo = _chunkGV7GMZP2cjs.getFileInfo; exports.getFileName = _chunkGV7GMZP2cjs.getFileName; exports.getMimeType = _chunkGV7GMZP2cjs.getMimeType; exports.getParentPath = _chunkGV7GMZP2cjs.getParentPath; exports.isAudioFile = _chunkGV7GMZP2cjs.isAudioFile; exports.isDocumentFile = _chunkGV7GMZP2cjs.isDocumentFile; exports.isImageFile = _chunkGV7GMZP2cjs.isImageFile; exports.isVideoFile = _chunkGV7GMZP2cjs.isVideoFile; exports.joinPath = _chunkGV7GMZP2cjs.joinPath; exports.normalizePath = _chunkGV7GMZP2cjs.normalizePath; exports.parseS3Url = _chunkGV7GMZP2cjs.parseS3Url; exports.sanitizeFileName = _chunkGV7GMZP2cjs.sanitizeFileName; exports.validateBatchFiles = _chunkGV7GMZP2cjs.validateBatchFiles; exports.validateBucketName = _chunkGV7GMZP2cjs.validateBucketName; exports.validateFileSize = _chunkGV7GMZP2cjs.validateFileSize; exports.validateFileType = _chunkGV7GMZP2cjs.validateFileType; exports.validateS3Config = _chunkGV7GMZP2cjs.validateS3Config; exports.validateS3Key = _chunkGV7GMZP2cjs.validateS3Key;