@sanvika/cloudinary 0.2.3 → 0.3.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.
- package/dist/client.js +0 -76
- package/dist/index.js +31 -0
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,78 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
// src/SanvikaCloudinaryProvider.jsx
|
|
4
|
-
import { createContext, useContext, useMemo } from "react";
|
|
5
|
-
import { jsx } from "react/jsx-runtime";
|
|
6
|
-
var SanvikaCloudinaryContext = createContext(null);
|
|
7
|
-
function SanvikaCloudinaryProvider({ appName, cloudName, uploadPreset = "ml_default", children }) {
|
|
8
|
-
const value = useMemo(
|
|
9
|
-
() => ({ appName, cloudName, uploadPreset }),
|
|
10
|
-
[appName, cloudName, uploadPreset]
|
|
11
|
-
);
|
|
12
|
-
return /* @__PURE__ */ jsx(SanvikaCloudinaryContext.Provider, { value, children });
|
|
13
|
-
}
|
|
14
|
-
function useSanvikaCloudinary() {
|
|
15
|
-
const ctx = useContext(SanvikaCloudinaryContext);
|
|
16
|
-
if (!ctx) {
|
|
17
|
-
throw new Error("useSanvikaCloudinary must be used within a SanvikaCloudinaryProvider");
|
|
18
|
-
}
|
|
19
|
-
return ctx;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// src/useCloudinaryUpload.js
|
|
23
|
-
import { useState, useCallback } from "react";
|
|
24
|
-
function useCloudinaryUpload(options = {}) {
|
|
25
|
-
const { folder, resourceType = "image", onProgress } = options;
|
|
26
|
-
const { appName, cloudName, uploadPreset } = useSanvikaCloudinary();
|
|
27
|
-
const [uploading, setUploading] = useState(false);
|
|
28
|
-
const [error, setError] = useState(null);
|
|
29
|
-
const reset = useCallback(() => {
|
|
30
|
-
setError(null);
|
|
31
|
-
setUploading(false);
|
|
32
|
-
}, []);
|
|
33
|
-
const upload = useCallback(
|
|
34
|
-
async (file) => {
|
|
35
|
-
setError(null);
|
|
36
|
-
setUploading(true);
|
|
37
|
-
try {
|
|
38
|
-
const folderPath = folder ? `${appName}/${folder}` : appName;
|
|
39
|
-
const formData = new FormData();
|
|
40
|
-
formData.append("file", file);
|
|
41
|
-
formData.append("upload_preset", uploadPreset);
|
|
42
|
-
formData.append("folder", folderPath);
|
|
43
|
-
const xhr = new XMLHttpRequest();
|
|
44
|
-
const url = `https://api.cloudinary.com/v1_1/${cloudName}/${resourceType}/upload`;
|
|
45
|
-
const result = await new Promise((resolve, reject) => {
|
|
46
|
-
xhr.open("POST", url);
|
|
47
|
-
if (onProgress) {
|
|
48
|
-
xhr.upload.onprogress = (e) => {
|
|
49
|
-
if (e.lengthComputable) onProgress(Math.round(e.loaded / e.total * 100));
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
xhr.onload = () => {
|
|
53
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
54
|
-
resolve(JSON.parse(xhr.responseText));
|
|
55
|
-
} else {
|
|
56
|
-
reject(new Error(xhr.responseText || "Upload failed"));
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
xhr.onerror = () => reject(new Error("Network error during upload"));
|
|
60
|
-
xhr.send(formData);
|
|
61
|
-
});
|
|
62
|
-
return result;
|
|
63
|
-
} catch (err) {
|
|
64
|
-
const msg = (err == null ? void 0 : err.message) || "Upload failed";
|
|
65
|
-
setError(msg);
|
|
66
|
-
throw err;
|
|
67
|
-
} finally {
|
|
68
|
-
setUploading(false);
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
[appName, cloudName, uploadPreset, folder, resourceType, onProgress]
|
|
72
|
-
);
|
|
73
|
-
return { upload, uploading, error, reset };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
3
|
// src/cloudinaryUtils.js
|
|
77
4
|
function isCloudinaryUrl(url) {
|
|
78
5
|
return typeof url === "string" && (url.includes("res.cloudinary.com") || url.includes("cloudinary.com"));
|
|
@@ -127,13 +54,10 @@ var TRANSFORM_PRESETS = {
|
|
|
127
54
|
responsive: { w: "auto", dpr: "auto", q: "auto", f: "auto" }
|
|
128
55
|
};
|
|
129
56
|
export {
|
|
130
|
-
SanvikaCloudinaryProvider,
|
|
131
57
|
TRANSFORM_PRESETS,
|
|
132
58
|
extractPublicId,
|
|
133
59
|
getFolderPath,
|
|
134
60
|
getOptimizedUrl,
|
|
135
61
|
isCloudinaryUrl,
|
|
136
|
-
useCloudinaryUpload,
|
|
137
|
-
useSanvikaCloudinary,
|
|
138
62
|
validatePublicId
|
|
139
63
|
};
|
package/dist/index.js
CHANGED
|
@@ -411,6 +411,36 @@ async function deleteImages(urls, options = {}) {
|
|
|
411
411
|
const failed = allResults.filter((r) => !r.success).length;
|
|
412
412
|
return { success: failed === 0, total: urls.length, processed: allResults.length, successful, failed };
|
|
413
413
|
}
|
|
414
|
+
async function renameImage(fromPublicId, toPublicId, options = {}) {
|
|
415
|
+
await ensureConfigured();
|
|
416
|
+
const { overwrite = true, resourceType = "image" } = options;
|
|
417
|
+
if (!fromPublicId || !toPublicId) {
|
|
418
|
+
throw new CloudinaryError("fromPublicId and toPublicId are required", "renameImage");
|
|
419
|
+
}
|
|
420
|
+
if (isProxyMode()) {
|
|
421
|
+
const result2 = await withRetry(
|
|
422
|
+
() => gatewayPost("/api/v1/rename", {
|
|
423
|
+
fromPublicId,
|
|
424
|
+
toPublicId,
|
|
425
|
+
options: { overwrite, resourceType }
|
|
426
|
+
}),
|
|
427
|
+
{ operationName: "renameImage", maxAttempts: 2 }
|
|
428
|
+
);
|
|
429
|
+
return {
|
|
430
|
+
url: (result2 == null ? void 0 : result2.secure_url) || (result2 == null ? void 0 : result2.url) || "",
|
|
431
|
+
publicId: (result2 == null ? void 0 : result2.public_id) || toPublicId
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
const cloudinary = await loadLegacyCloudinary();
|
|
435
|
+
const result = await withRetry(
|
|
436
|
+
() => cloudinary.uploader.rename(fromPublicId, toPublicId, {
|
|
437
|
+
overwrite,
|
|
438
|
+
resource_type: resourceType
|
|
439
|
+
}),
|
|
440
|
+
{ operationName: "renameImage", maxAttempts: 2 }
|
|
441
|
+
);
|
|
442
|
+
return { url: result.secure_url, publicId: result.public_id };
|
|
443
|
+
}
|
|
414
444
|
async function pingCloudinary() {
|
|
415
445
|
await ensureConfigured();
|
|
416
446
|
if (isProxyMode()) return gatewayGet("/api/v1/ping");
|
|
@@ -634,6 +664,7 @@ export {
|
|
|
634
664
|
isRetriableError,
|
|
635
665
|
normalizeFolderPath,
|
|
636
666
|
pingCloudinary,
|
|
667
|
+
renameImage,
|
|
637
668
|
runCloudinaryDiagnostics,
|
|
638
669
|
splitFileName,
|
|
639
670
|
testCloudinaryWebhookSignature,
|
package/package.json
CHANGED