@lykosjs/cloudinary 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +381 -2
- package/dist/cloudinary-manager.d.ts +17 -0
- package/dist/cloudinary-manager.js +51 -0
- package/dist/cloudinary-manager.js.map +1 -0
- package/dist/files/file-manager.d.ts +13 -0
- package/dist/files/file-manager.js +77 -0
- package/dist/files/file-manager.js.map +1 -0
- package/dist/files/file-type.d.ts +3 -0
- package/dist/files/file-type.js +10 -0
- package/dist/files/file-type.js.map +1 -0
- package/dist/files/types.d.ts +17 -0
- package/dist/files/types.js +5 -0
- package/dist/files/types.js.map +1 -0
- package/dist/images/image-manager.d.ts +26 -0
- package/dist/images/image-manager.js +210 -0
- package/dist/images/image-manager.js.map +1 -0
- package/dist/images/image-type.d.ts +6 -0
- package/dist/images/image-type.js +32 -0
- package/dist/images/image-type.js.map +1 -0
- package/dist/images/types.d.ts +43 -0
- package/dist/images/types.js +2 -0
- package/dist/images/types.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/shared/byte-utils.d.ts +1 -0
- package/dist/shared/byte-utils.js +2 -0
- package/dist/shared/byte-utils.js.map +1 -0
- package/dist/shared/concurrency.d.ts +12 -0
- package/dist/shared/concurrency.js +33 -0
- package/dist/shared/concurrency.js.map +1 -0
- package/dist/shared/destroy-batch.d.ts +10 -0
- package/dist/shared/destroy-batch.js +31 -0
- package/dist/shared/destroy-batch.js.map +1 -0
- package/dist/shared/destroy-resource.d.ts +5 -0
- package/dist/shared/destroy-resource.js +23 -0
- package/dist/shared/destroy-resource.js.map +1 -0
- package/dist/shared/error-message.d.ts +1 -0
- package/dist/shared/error-message.js +11 -0
- package/dist/shared/error-message.js.map +1 -0
- package/dist/shared/normalize-folder.d.ts +1 -0
- package/dist/shared/normalize-folder.js +22 -0
- package/dist/shared/normalize-folder.js.map +1 -0
- package/dist/shared/public-id.d.ts +10 -0
- package/dist/shared/public-id.js +49 -0
- package/dist/shared/public-id.js.map +1 -0
- package/dist/shared/upload-batch.d.ts +16 -0
- package/dist/shared/upload-batch.js +91 -0
- package/dist/shared/upload-batch.js.map +1 -0
- package/dist/shared/upload-stream.d.ts +13 -0
- package/dist/shared/upload-stream.js +36 -0
- package/dist/shared/upload-stream.js.map +1 -0
- package/dist/shared/validate-blob.d.ts +12 -0
- package/dist/shared/validate-blob.js +21 -0
- package/dist/shared/validate-blob.js.map +1 -0
- package/dist/types/config.d.ts +92 -0
- package/dist/types/config.js +54 -0
- package/dist/types/config.js.map +1 -0
- package/dist/videos/types.d.ts +20 -0
- package/dist/videos/types.js +2 -0
- package/dist/videos/types.js.map +1 -0
- package/dist/videos/video-manager.d.ts +13 -0
- package/dist/videos/video-manager.js +80 -0
- package/dist/videos/video-manager.js.map +1 -0
- package/dist/videos/video-type.d.ts +3 -0
- package/dist/videos/video-type.js +27 -0
- package/dist/videos/video-type.js.map +1 -0
- package/package.json +47 -4
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { v2 as cloudinary } from 'cloudinary';
|
|
2
|
+
import { HttpError } from 'lykosjs';
|
|
3
|
+
import { normalizeFolder } from '../shared/normalize-folder';
|
|
4
|
+
import { errorMessage } from '../shared/error-message';
|
|
5
|
+
import { DEFAULT_ALLOWED_TYPES, DEFAULT_DESTROY_CONCURRENCY, DEFAULT_MAX_FILE_SIZE, DEFAULT_UPLOAD_CONCURRENCY, } from '../types/config';
|
|
6
|
+
import { validateBlob } from '../shared/validate-blob';
|
|
7
|
+
import { detectImageType } from './image-type';
|
|
8
|
+
import { destroyBatch } from '../shared/destroy-batch';
|
|
9
|
+
function toUploadedImage(result) {
|
|
10
|
+
return {
|
|
11
|
+
url: result.secure_url,
|
|
12
|
+
publicId: result.public_id,
|
|
13
|
+
width: result.width,
|
|
14
|
+
height: result.height,
|
|
15
|
+
format: result.format,
|
|
16
|
+
bytes: result.bytes,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export class ImageManager {
|
|
20
|
+
credentials;
|
|
21
|
+
maxFileSize;
|
|
22
|
+
allowedTypes;
|
|
23
|
+
constructor(credentials, options = {}) {
|
|
24
|
+
this.credentials = credentials;
|
|
25
|
+
this.maxFileSize = options.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;
|
|
26
|
+
this.allowedTypes = options.allowedTypes ?? DEFAULT_ALLOWED_TYPES;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Validates and uploads an image. Throws HttpError 400 (empty file or bad
|
|
30
|
+
* folder), 413 (too large), 415 (unsupported type) or 502 (Cloudinary failure).
|
|
31
|
+
*/
|
|
32
|
+
async uploadImage(file, folder, options = {}) {
|
|
33
|
+
const targetFolder = normalizeFolder(folder);
|
|
34
|
+
const buffer = await this.readAndValidate(file, options);
|
|
35
|
+
const result = await this.uploadBuffer(buffer, {
|
|
36
|
+
folder: targetFolder,
|
|
37
|
+
resource_type: 'image',
|
|
38
|
+
...(options.publicId ? { public_id: options.publicId } : {}),
|
|
39
|
+
...(options.tags?.length ? { tags: options.tags } : {}),
|
|
40
|
+
});
|
|
41
|
+
return toUploadedImage(result);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Uploads several images. If any upload fails, the ones that already
|
|
45
|
+
* succeeded are deleted and the first error is thrown.
|
|
46
|
+
*/
|
|
47
|
+
async uploadImagesMany(files, folder, options = {}) {
|
|
48
|
+
if (files.length === 0) {
|
|
49
|
+
throw new HttpError(400, 'At least one file is required');
|
|
50
|
+
}
|
|
51
|
+
const { concurrency = DEFAULT_UPLOAD_CONCURRENCY, ...uploadOptions } = options;
|
|
52
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
53
|
+
throw new HttpError(400, 'Concurrency must be a positive integer');
|
|
54
|
+
}
|
|
55
|
+
const uploaded = [];
|
|
56
|
+
const failures = [];
|
|
57
|
+
let nextIndex = 0;
|
|
58
|
+
const worker = async () => {
|
|
59
|
+
while (nextIndex < files.length) {
|
|
60
|
+
const index = nextIndex++;
|
|
61
|
+
try {
|
|
62
|
+
const file = files[index];
|
|
63
|
+
if (!file)
|
|
64
|
+
continue;
|
|
65
|
+
const image = await this.uploadImage(file, folder, uploadOptions);
|
|
66
|
+
uploaded.push({ index, image });
|
|
67
|
+
}
|
|
68
|
+
catch (reason) {
|
|
69
|
+
failures.push({ index, reason });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const workerCount = Math.min(concurrency, files.length);
|
|
74
|
+
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
|
75
|
+
if (failures.length > 0) {
|
|
76
|
+
failures.sort((a, b) => a.index - b.index);
|
|
77
|
+
const cleanupResults = await Promise.allSettled(uploaded.map(async ({ image }) => {
|
|
78
|
+
try {
|
|
79
|
+
await this.destroyImage(image.publicId);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
catch (reason) {
|
|
83
|
+
return {
|
|
84
|
+
publicId: image.publicId,
|
|
85
|
+
reason,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}));
|
|
89
|
+
const cleanupFailures = cleanupResults.flatMap((result) => {
|
|
90
|
+
if (result.status === 'rejected') {
|
|
91
|
+
return [{ publicId: 'unknown', reason: result.reason }];
|
|
92
|
+
}
|
|
93
|
+
return result.value ? [result.value] : [];
|
|
94
|
+
});
|
|
95
|
+
const firstFailure = failures[0];
|
|
96
|
+
if (!firstFailure) {
|
|
97
|
+
throw new HttpError(500, 'Unexpected upload failure');
|
|
98
|
+
}
|
|
99
|
+
const uploadError = firstFailure.reason;
|
|
100
|
+
if (cleanupFailures.length > 0) {
|
|
101
|
+
throw new AggregateError([uploadError, ...cleanupFailures], 'Image upload failed and some uploaded images could not be removed');
|
|
102
|
+
}
|
|
103
|
+
throw uploadError;
|
|
104
|
+
}
|
|
105
|
+
return uploaded.sort((a, b) => a.index - b.index).map(({ image }) => image);
|
|
106
|
+
}
|
|
107
|
+
/** Deletes an image. Deleting an image that does not exist is not an error. */
|
|
108
|
+
async destroyImage(publicId) {
|
|
109
|
+
if (!publicId?.trim()) {
|
|
110
|
+
throw new HttpError(400, 'Public ID is required');
|
|
111
|
+
}
|
|
112
|
+
let result;
|
|
113
|
+
try {
|
|
114
|
+
result = await cloudinary.uploader.destroy(publicId, {
|
|
115
|
+
...this.credentials,
|
|
116
|
+
resource_type: 'image',
|
|
117
|
+
invalidate: true,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
throw new HttpError(502, errorMessage(error, 'Cloudinary image deletion failed'));
|
|
122
|
+
}
|
|
123
|
+
if (result.result === 'not found') {
|
|
124
|
+
throw new HttpError(404, 'Image not found');
|
|
125
|
+
}
|
|
126
|
+
if (result.result !== 'ok') {
|
|
127
|
+
throw new HttpError(502, 'Cloudinary image deletion failed');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Deletes several images. Every deletion is attempted, then the first error is thrown. */
|
|
131
|
+
async destroyImagesMany(publicIds, options = {}) {
|
|
132
|
+
await destroyBatch(publicIds, options.concurrency ?? DEFAULT_DESTROY_CONCURRENCY, (publicId) => this.destroyImage(publicId));
|
|
133
|
+
}
|
|
134
|
+
/** Builds a delivery URL with optional resizing and format options. */
|
|
135
|
+
url(publicId, options = {}) {
|
|
136
|
+
if (typeof publicId !== 'string' || !publicId.trim()) {
|
|
137
|
+
throw new HttpError(400, 'Public ID is required');
|
|
138
|
+
}
|
|
139
|
+
const { width, height, crop, quality, format } = options;
|
|
140
|
+
if (width !== undefined && (!Number.isInteger(width) || width < 1)) {
|
|
141
|
+
throw new HttpError(400, 'Width must be a positive integer');
|
|
142
|
+
}
|
|
143
|
+
if (height !== undefined && (!Number.isInteger(height) || height < 1)) {
|
|
144
|
+
throw new HttpError(400, 'Height must be a positive integer');
|
|
145
|
+
}
|
|
146
|
+
const allowedCrops = ['fill', 'fit', 'scale', 'limit', 'thumb'];
|
|
147
|
+
if (crop !== undefined && !allowedCrops.includes(crop)) {
|
|
148
|
+
throw new HttpError(400, 'Invalid crop mode');
|
|
149
|
+
}
|
|
150
|
+
if (quality !== undefined &&
|
|
151
|
+
quality !== 'auto' &&
|
|
152
|
+
(!Number.isInteger(quality) || quality < 1 || quality > 100)) {
|
|
153
|
+
throw new HttpError(400, 'Quality must be an integer between 1 and 100, or auto');
|
|
154
|
+
}
|
|
155
|
+
const ALLOWED_FORMATS = [
|
|
156
|
+
'auto',
|
|
157
|
+
'jpg',
|
|
158
|
+
'jpeg',
|
|
159
|
+
'png',
|
|
160
|
+
'gif',
|
|
161
|
+
'webp',
|
|
162
|
+
'avif',
|
|
163
|
+
];
|
|
164
|
+
if (format !== undefined &&
|
|
165
|
+
!ALLOWED_FORMATS.includes(format)) {
|
|
166
|
+
throw new HttpError(400, 'Invalid image format');
|
|
167
|
+
}
|
|
168
|
+
const hasSize = width !== undefined || height !== undefined;
|
|
169
|
+
const transformation = {
|
|
170
|
+
...(width !== undefined ? { width } : {}),
|
|
171
|
+
...(height !== undefined ? { height } : {}),
|
|
172
|
+
...(hasSize ? { crop: crop ?? 'limit' } : {}),
|
|
173
|
+
...(quality !== undefined ? { quality } : {}),
|
|
174
|
+
...(format !== undefined ? { fetch_format: format } : {}),
|
|
175
|
+
};
|
|
176
|
+
return cloudinary.url(publicId, {
|
|
177
|
+
cloud_name: this.credentials.cloud_name,
|
|
178
|
+
secure: true,
|
|
179
|
+
analytics: false,
|
|
180
|
+
resource_type: 'image',
|
|
181
|
+
...(Object.keys(transformation).length > 0 ? { transformation: [transformation] } : {}),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
uploadBuffer(buffer, options) {
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
const stream = cloudinary.uploader.upload_stream({ ...this.credentials, ...options }, (error, result) => {
|
|
187
|
+
if (error) {
|
|
188
|
+
reject(new HttpError(502, errorMessage(error, 'Cloudinary upload failed')));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!result) {
|
|
192
|
+
reject(new HttpError(502, 'Cloudinary upload failed'));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
resolve(result);
|
|
196
|
+
});
|
|
197
|
+
stream.end(buffer);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async readAndValidate(file, options) {
|
|
201
|
+
await validateBlob(file, {
|
|
202
|
+
maxFileSize: options.maxFileSize ?? this.maxFileSize,
|
|
203
|
+
allowedTypes: options.allowedTypes ?? this.allowedTypes,
|
|
204
|
+
detect: detectImageType,
|
|
205
|
+
label: 'image',
|
|
206
|
+
});
|
|
207
|
+
return Buffer.from(await file.arrayBuffer());
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=image-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-manager.js","sourceRoot":"","sources":["../../src/images/image-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,UAAU,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAU5D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,OAAO,EAGL,qBAAqB,EACrB,2BAA2B,EAC3B,qBAAqB,EACrB,0BAA0B,GAE3B,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,SAAS,eAAe,CAAC,MAAyB;IAChD,OAAO;QACL,GAAG,EAAE,MAAM,CAAC,UAAU;QACtB,QAAQ,EAAE,MAAM,CAAC,SAAS;QAC1B,KAAK,EAA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CAAC,YAAY;YACvD,MAAM,EAAE,eAAe;YACvB,KAAK,EAAE,OAAO;SACf,CAAC,CAAA;QAEF,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;IAC9C,CAAC;CACF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ImageMimeType } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Detects the real image type from the file's first bytes.
|
|
4
|
+
* The browser-provided `file.type` is user-controlled and can be spoofed.
|
|
5
|
+
*/
|
|
6
|
+
export declare function detectImageType(bytes: Uint8Array): ImageMimeType | null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { startsWith } from '../shared/byte-utils';
|
|
2
|
+
/**
|
|
3
|
+
* Detects the real image type from the file's first bytes.
|
|
4
|
+
* The browser-provided `file.type` is user-controlled and can be spoofed.
|
|
5
|
+
*/
|
|
6
|
+
export function detectImageType(bytes) {
|
|
7
|
+
if (startsWith(bytes, [0xff, 0xd8, 0xff]))
|
|
8
|
+
return 'image/jpeg';
|
|
9
|
+
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
|
|
10
|
+
return 'image/png';
|
|
11
|
+
if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38]))
|
|
12
|
+
return 'image/gif';
|
|
13
|
+
// WEBP: "RIFF" .... "WEBP"
|
|
14
|
+
if (startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) &&
|
|
15
|
+
startsWith(bytes, [0x57, 0x45, 0x42, 0x50], 8)) {
|
|
16
|
+
return 'image/webp';
|
|
17
|
+
}
|
|
18
|
+
// AVIF: .... "ftypavif"
|
|
19
|
+
if (startsWith(bytes, [0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66], 4))
|
|
20
|
+
return 'image/avif';
|
|
21
|
+
// SVG: XML-based image format
|
|
22
|
+
const text = new TextDecoder()
|
|
23
|
+
.decode(bytes)
|
|
24
|
+
.replace(/^\uFEFF/, '')
|
|
25
|
+
.trimStart();
|
|
26
|
+
if (/^<svg(?:\s|>)/i.test(text) ||
|
|
27
|
+
/^<\?xml[\s\S]*?<svg(?:\s|>)/i.test(text)) {
|
|
28
|
+
return 'image/svg+xml';
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=image-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-type.js","sourceRoot":"","sources":["../../src/images/image-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAGjD;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,KAAiB;IAChD,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAAE,OAAO,YAAY,CAAA;IAE9D,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACtE,OAAO,WAAW,CAAA;IAEnB,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAAE,OAAO,WAAW,CAAA;IAEnE,2BAA2B;IAC3B,IACC,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3C,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,EAC7C,CAAC;QACF,OAAO,YAAY,CAAA;IACpB,CAAC;IAED,wBAAwB;IACxB,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QACzE,OAAO,YAAY,CAAA;IAEpB,8BAA8B;IAC9B,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE;SAC5B,MAAM,CAAC,KAAK,CAAC;SACb,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;SACtB,SAAS,EAAE,CAAA;IAEb,IACC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;QAC3B,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC,EACxC,CAAC;QACF,OAAO,eAAe,CAAA;IACvB,CAAC;IAED,OAAO,IAAI,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type ImageMimeType = 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp' | 'image/avif' | 'image/svg+xml';
|
|
2
|
+
export type ImageFormat = 'jpg' | 'jpeg' | 'png' | 'webp' | 'avif' | 'gif' | 'auto';
|
|
3
|
+
export type CropMode = 'fill' | 'fit' | 'scale' | 'limit' | 'thumb';
|
|
4
|
+
export interface CloudinaryManagerConfig {
|
|
5
|
+
cloudName: string;
|
|
6
|
+
apiKey: string;
|
|
7
|
+
apiSecret: string;
|
|
8
|
+
/** Maximum accepted file size in bytes. Defaults to 5 MB. */
|
|
9
|
+
maxFileSize?: number;
|
|
10
|
+
/** Accepted image types, detected from file content. Defaults to all supported types. */
|
|
11
|
+
allowedTypes?: readonly ImageMimeType[];
|
|
12
|
+
}
|
|
13
|
+
export interface UploadedImage {
|
|
14
|
+
/** HTTPS delivery URL. */
|
|
15
|
+
url: string;
|
|
16
|
+
/** Cloudinary public ID. Store it, you need it to delete the image. */
|
|
17
|
+
publicId: string;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
format: string;
|
|
21
|
+
/** Stored size in bytes. */
|
|
22
|
+
bytes: number;
|
|
23
|
+
}
|
|
24
|
+
export interface UploadOptions {
|
|
25
|
+
/** Use a fixed public ID instead of a generated one. */
|
|
26
|
+
publicId?: string;
|
|
27
|
+
tags?: string[];
|
|
28
|
+
/** Overrides the manager's maxFileSize for this upload. */
|
|
29
|
+
maxFileSize?: number;
|
|
30
|
+
/** Overrides the manager's allowedTypes for this upload. */
|
|
31
|
+
allowedTypes?: readonly ImageMimeType[];
|
|
32
|
+
}
|
|
33
|
+
export interface UrlOptions {
|
|
34
|
+
width?: number;
|
|
35
|
+
height?: number;
|
|
36
|
+
/** Defaults to 'limit' when width or height is set. */
|
|
37
|
+
crop?: CropMode;
|
|
38
|
+
quality?: number | 'auto';
|
|
39
|
+
format?: ImageFormat | 'auto';
|
|
40
|
+
}
|
|
41
|
+
export type UploadManyOptions = Omit<UploadOptions, 'publicId'> & {
|
|
42
|
+
concurrency?: number;
|
|
43
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/images/types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CloudinaryManager } from './cloudinary-manager';
|
|
2
|
+
export { extractPublicId } from './shared/public-id';
|
|
3
|
+
export type { CloudinaryManagerConfig, ImageMimeType, UploadedImage, UploadOptions, UrlOptions, UploadManyOptions } from './images/types';
|
|
4
|
+
export type { FileMimeType, UploadedFile, UploadFileOptions, UploadFilesManyOptions } from './files/types';
|
|
5
|
+
export type { VideoMimeType, UploadedVideo, UploadVideoOptions, UploadVideosManyOptions } from './videos/types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const startsWith: (bytes: Uint8Array, signature: readonly number[], offset?: number) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"byte-utils.js","sourceRoot":"","sources":["../../src/shared/byte-utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,CACzB,KAAiB,EACjB,SAA4B,EAC5B,MAAM,GAAG,CAAC,EACA,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,KAAK,IAAI,CAAC,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates the maximum number of concurrent operations.
|
|
3
|
+
*/
|
|
4
|
+
export declare function assertConcurrency(concurrency: number): void;
|
|
5
|
+
/**
|
|
6
|
+
* Runs tasks with a limited number of concurrent workers.
|
|
7
|
+
*
|
|
8
|
+
* Each task receives its original index.
|
|
9
|
+
* If shouldStop returns true, no new tasks are started.
|
|
10
|
+
* Already running tasks are allowed to finish.
|
|
11
|
+
*/
|
|
12
|
+
export declare function runWithConcurrency(total: number, concurrency: number, task: (index: number) => Promise<void>, shouldStop?: () => boolean): Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { HttpError } from 'lykosjs';
|
|
2
|
+
const MAX_CONCURRENCY = 10;
|
|
3
|
+
/**
|
|
4
|
+
* Validates the maximum number of concurrent operations.
|
|
5
|
+
*/
|
|
6
|
+
export function assertConcurrency(concurrency) {
|
|
7
|
+
if (!Number.isInteger(concurrency) || concurrency < 1 || concurrency > MAX_CONCURRENCY) {
|
|
8
|
+
throw new HttpError(400, `Concurrency must be an integer between 1 and ${MAX_CONCURRENCY}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Runs tasks with a limited number of concurrent workers.
|
|
13
|
+
*
|
|
14
|
+
* Each task receives its original index.
|
|
15
|
+
* If shouldStop returns true, no new tasks are started.
|
|
16
|
+
* Already running tasks are allowed to finish.
|
|
17
|
+
*/
|
|
18
|
+
export async function runWithConcurrency(total, concurrency, task, shouldStop = () => false) {
|
|
19
|
+
assertConcurrency(concurrency);
|
|
20
|
+
if (!Number.isInteger(total) || total < 0) {
|
|
21
|
+
throw new HttpError(400, 'Total must be a non-negative integer');
|
|
22
|
+
}
|
|
23
|
+
let nextIndex = 0;
|
|
24
|
+
const worker = async () => {
|
|
25
|
+
while (nextIndex < total && !shouldStop()) {
|
|
26
|
+
const index = nextIndex++;
|
|
27
|
+
await task(index);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const workerCount = Math.min(total, concurrency);
|
|
31
|
+
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=concurrency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concurrency.js","sourceRoot":"","sources":["../../src/shared/concurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,eAAe,GAAG,EAAE,CAAA;AAE1B;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAmB;IACnD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,WAAW,GAAG,CAAC,IAAI,WAAW,GAAG,eAAe,EAAE,CAAC;QACvF,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,gDAAgD,eAAe,EAAE,CAAC,CAAA;IAC7F,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,KAAa,EACb,WAAmB,EACnB,IAAsC,EACtC,UAAU,GAAkB,GAAG,EAAE,CAAC,KAAK;IAEvC,iBAAiB,CAAC,WAAW,CAAC,CAAA;IAE9B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,sCAAsC,CAAC,CAAA;IAClE,CAAC;IAED,IAAI,SAAS,GAAG,CAAC,CAAA;IAEjB,MAAM,MAAM,GAAG,KAAK,IAAmB,EAAE;QACvC,OAAO,SAAS,GAAG,KAAK,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;YAC1C,MAAM,KAAK,GAAG,SAAS,EAAE,CAAA;YACzB,MAAM,IAAI,CAAC,KAAK,CAAC,CAAA;QACnB,CAAC;IACH,CAAC,CAAA;IAED,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAA;IAEhD,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AACxE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type DestroyFunction = (publicId: string) => Promise<void>;
|
|
2
|
+
/**
|
|
3
|
+
* Deletes multiple Cloudinary resources with limited concurrency.
|
|
4
|
+
*
|
|
5
|
+
* - Removes duplicate public IDs.
|
|
6
|
+
* - Attempts every deletion, even if some fail.
|
|
7
|
+
* - Throws the first failure according to the original input order.
|
|
8
|
+
*/
|
|
9
|
+
export declare function destroyBatch(publicIds: readonly string[], concurrency: number, destroy: DestroyFunction): Promise<void>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { assertConcurrency, runWithConcurrency } from './concurrency';
|
|
2
|
+
/**
|
|
3
|
+
* Deletes multiple Cloudinary resources with limited concurrency.
|
|
4
|
+
*
|
|
5
|
+
* - Removes duplicate public IDs.
|
|
6
|
+
* - Attempts every deletion, even if some fail.
|
|
7
|
+
* - Throws the first failure according to the original input order.
|
|
8
|
+
*/
|
|
9
|
+
export async function destroyBatch(publicIds, concurrency, destroy) {
|
|
10
|
+
assertConcurrency(concurrency);
|
|
11
|
+
const uniqueIds = [...new Set(publicIds)];
|
|
12
|
+
const failures = [];
|
|
13
|
+
await runWithConcurrency(uniqueIds.length, concurrency, async (index) => {
|
|
14
|
+
const publicId = uniqueIds[index];
|
|
15
|
+
if (publicId === undefined)
|
|
16
|
+
return;
|
|
17
|
+
try {
|
|
18
|
+
await destroy(publicId);
|
|
19
|
+
}
|
|
20
|
+
catch (reason) {
|
|
21
|
+
failures.push({ index, reason });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
if (failures.length === 0)
|
|
25
|
+
return;
|
|
26
|
+
const firstFailure = failures.sort((a, b) => a.index - b.index)[0];
|
|
27
|
+
if (firstFailure) {
|
|
28
|
+
throw firstFailure.reason;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=destroy-batch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"destroy-batch.js","sourceRoot":"","sources":["../../src/shared/destroy-batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AASrE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CACjC,SAA4B,EAC5B,WAAmB,EACnB,OAAwB;IAExB,iBAAiB,CAAC,WAAW,CAAC,CAAA;IAE9B,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAA;IAEzC,MAAM,QAAQ,GAAqB,EAAE,CAAA;IAErC,MAAM,kBAAkB,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;QACvE,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAC,CAAA;QAEjC,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAM;QAElC,IAAI,CAAC;YACJ,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAA;QACxB,CAAC;QAAC,OAAO,MAAM,EAAE,CAAC;YACjB,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAA;QACjC,CAAC;IACF,CAAC,CAAC,CAAA;IAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAM;IAEjC,MAAM,YAAY,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IAElE,IAAI,YAAY,EAAE,CAAC;QAClB,MAAM,YAAY,CAAC,MAAM,CAAA;IAC1B,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CloudinaryCredentials } from '../types/config';
|
|
2
|
+
type ResourceType = 'image' | 'raw' | 'video';
|
|
3
|
+
type ResourceLabel = 'image' | 'file' | 'video';
|
|
4
|
+
export declare function destroyResource(publicId: string, resourceType: ResourceType, label: ResourceLabel, credentials: CloudinaryCredentials): Promise<void>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { v2 as cloudinary } from 'cloudinary';
|
|
2
|
+
import { HttpError } from 'lykosjs';
|
|
3
|
+
export async function destroyResource(publicId, resourceType, label, credentials) {
|
|
4
|
+
if (!publicId) {
|
|
5
|
+
throw new HttpError(400, 'Public ID is required');
|
|
6
|
+
}
|
|
7
|
+
let result;
|
|
8
|
+
try {
|
|
9
|
+
result = await cloudinary.uploader.destroy(publicId, {
|
|
10
|
+
...credentials,
|
|
11
|
+
resource_type: resourceType,
|
|
12
|
+
invalidate: true,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
const message = error instanceof Error ? error.message : `Cloudinary ${label} deletion failed`;
|
|
17
|
+
throw new HttpError(502, message || `Cloudinary ${label} deletion failed`);
|
|
18
|
+
}
|
|
19
|
+
if (result.result !== 'ok' && result.result !== 'not found') {
|
|
20
|
+
throw new HttpError(502, `Cloudinary ${label} deletion failed`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=destroy-resource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"destroy-resource.js","sourceRoot":"","sources":["../../src/shared/destroy-resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,UAAU,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAOnC,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,QAAgB,EAChB,YAA0B,EAC1B,KAAoB,EACpB,WAAkC;IAElC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,uBAAuB,CAAC,CAAA;IACnD,CAAC;IAED,IAAI,MAA2B,CAAA;IAE/B,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE;YACnD,GAAG,WAAW;YACd,aAAa,EAAE,YAAY;YAC3B,UAAU,EAAE,IAAI;SACjB,CAAC,CAAA;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,cAAc,KAAK,kBAAkB,CAAA;QAE9F,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,OAAO,IAAI,cAAc,KAAK,kBAAkB,CAAC,CAAA;IAC5E,CAAC;IAED,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAC5D,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,cAAc,KAAK,kBAAkB,CAAC,CAAA;IACjE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function errorMessage(error: unknown, fallback: string): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function errorMessage(error, fallback) {
|
|
2
|
+
if (error instanceof Error)
|
|
3
|
+
return error.message;
|
|
4
|
+
if (typeof error === 'object' && error !== null && 'message' in error) {
|
|
5
|
+
const { message } = error;
|
|
6
|
+
if (typeof message === 'string' && message)
|
|
7
|
+
return message;
|
|
8
|
+
}
|
|
9
|
+
return fallback;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=error-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-message.js","sourceRoot":"","sources":["../../src/shared/error-message.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,QAAgB;IAC5D,IAAI,KAAK,YAAY,KAAK;QAAE,OAAO,KAAK,CAAC,OAAO,CAAA;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;QACvE,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAA;QACzB,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO;YAAE,OAAO,OAAO,CAAA;IAC3D,CAAC;IACD,OAAO,QAAQ,CAAA;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normalizeFolder(folder: string): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { HttpError } from 'lykosjs';
|
|
2
|
+
export function normalizeFolder(folder) {
|
|
3
|
+
if (typeof folder !== 'string') {
|
|
4
|
+
throw new HttpError(400, 'Upload folder must be a string');
|
|
5
|
+
}
|
|
6
|
+
const clean = folder.trim().replace(/^\/+|\/+$/g, '');
|
|
7
|
+
if (!clean) {
|
|
8
|
+
throw new HttpError(400, 'Upload folder is required');
|
|
9
|
+
}
|
|
10
|
+
if (clean.length > 255) {
|
|
11
|
+
throw new HttpError(400, 'Upload folder is too long');
|
|
12
|
+
}
|
|
13
|
+
if (/[\u0000-\u001F\u007F]/.test(clean)) {
|
|
14
|
+
throw new HttpError(400, 'Upload folder contains invalid characters');
|
|
15
|
+
}
|
|
16
|
+
const segments = clean.split('/');
|
|
17
|
+
if (segments.some((segment) => !segment || segment === '.' || segment === '..')) {
|
|
18
|
+
throw new HttpError(400, 'Invalid upload folder');
|
|
19
|
+
}
|
|
20
|
+
return clean;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=normalize-folder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize-folder.js","sourceRoot":"","sources":["../../src/shared/normalize-folder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,UAAU,eAAe,CAAC,MAAc;IAC7C,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAChC,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,gCAAgC,CAAC,CAAA;IAC3D,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;IAErD,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,2BAA2B,CAAC,CAAA;IACtD,CAAC;IAED,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QACxB,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,2BAA2B,CAAC,CAAA;IACtD,CAAC;IAED,IAAI,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,2CAA2C,CAAC,CAAA;IACtE,CAAC;IAED,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEjC,IACC,QAAQ,CAAC,IAAI,CACZ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,IAAI,OAAO,KAAK,GAAG,IAAI,OAAO,KAAK,IAAI,CAC5D,EACA,CAAC;QACF,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,uBAAuB,CAAC,CAAA;IAClD,CAAC;IAED,OAAO,KAAK,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the public ID from a Cloudinary delivery URL, so an image can be
|
|
3
|
+
* deleted when only its URL was stored. Returns null for non-Cloudinary URLs.
|
|
4
|
+
*
|
|
5
|
+
* Works with and without a version segment (`v123`). If the URL contains
|
|
6
|
+
* transformations but no version segment, the transformations cannot be told
|
|
7
|
+
* apart from folders, so prefer storing the public ID itself.
|
|
8
|
+
*/
|
|
9
|
+
export declare function extractPublicId(url: string): string | null;
|
|
10
|
+
export declare function isValidPublicId(publicId: string): boolean;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the public ID from a Cloudinary delivery URL, so an image can be
|
|
3
|
+
* deleted when only its URL was stored. Returns null for non-Cloudinary URLs.
|
|
4
|
+
*
|
|
5
|
+
* Works with and without a version segment (`v123`). If the URL contains
|
|
6
|
+
* transformations but no version segment, the transformations cannot be told
|
|
7
|
+
* apart from folders, so prefer storing the public ID itself.
|
|
8
|
+
*/
|
|
9
|
+
export function extractPublicId(url) {
|
|
10
|
+
let pathname;
|
|
11
|
+
try {
|
|
12
|
+
pathname = new URL(url).pathname;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
18
|
+
const uploadIndex = segments.indexOf('upload');
|
|
19
|
+
if (uploadIndex === -1)
|
|
20
|
+
return null;
|
|
21
|
+
let rest = segments.slice(uploadIndex + 1);
|
|
22
|
+
const versionIndex = rest.findIndex((segment) => /^v\d+$/.test(segment));
|
|
23
|
+
if (versionIndex !== -1)
|
|
24
|
+
rest = rest.slice(versionIndex + 1);
|
|
25
|
+
if (rest.length === 0)
|
|
26
|
+
return null;
|
|
27
|
+
const decoded = rest.map((segment) => {
|
|
28
|
+
try {
|
|
29
|
+
return decodeURIComponent(segment);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return segment;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const last = decoded[decoded.length - 1] ?? '';
|
|
36
|
+
decoded[decoded.length - 1] = last.replace(/\.[a-z0-9]+$/i, '');
|
|
37
|
+
const publicId = decoded.join('/');
|
|
38
|
+
return publicId || null;
|
|
39
|
+
}
|
|
40
|
+
export function isValidPublicId(publicId) {
|
|
41
|
+
if (!publicId)
|
|
42
|
+
return false;
|
|
43
|
+
if (publicId.length > 255)
|
|
44
|
+
return false;
|
|
45
|
+
if (publicId.split('/').some((part) => part === '..' || part === '.'))
|
|
46
|
+
return false;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=public-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public-id.js","sourceRoot":"","sources":["../../src/shared/public-id.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IAC1C,IAAI,QAAgB,CAAA;IACpB,IAAI,CAAC;QACJ,QAAQ,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAA;IACjC,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IACpD,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC9C,IAAI,WAAW,KAAK,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IAEnC,IAAI,IAAI,GAAG,QAAQ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O,IAAI,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type UploadValidator = (file: Blob) => Promise<unknown>;
|
|
2
|
+
type UploadFunction<TItem> = (file: Blob) => Promise<TItem>;
|
|
3
|
+
type DestroyFunction = (publicId: string) => Promise<void>;
|
|
4
|
+
/**
|
|
5
|
+
* Validates and uploads multiple files with limited concurrency.
|
|
6
|
+
*
|
|
7
|
+
* - Validates all files before starting uploads.
|
|
8
|
+
* - Stops scheduling new uploads after the first failure.
|
|
9
|
+
* - Waits for already running uploads to finish.
|
|
10
|
+
* - Removes successfully uploaded resources if an upload fails.
|
|
11
|
+
* - Preserves the original input order.
|
|
12
|
+
*/
|
|
13
|
+
export declare function uploadBatch<TItem extends {
|
|
14
|
+
publicId: string;
|
|
15
|
+
}>(files: readonly Blob[], concurrency: number, validate: UploadValidator, upload: UploadFunction<TItem>, destroy: DestroyFunction): Promise<TItem[]>;
|
|
16
|
+
export {};
|