offline-cloudinary 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.
Files changed (4) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +166 -0
  3. package/package.json +31 -0
  4. package/src/index.js +92 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Max Essien
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # Offline Cloudinary
2
+
3
+ An **offline Cloudinary-like file manager** for Node.js — designed for developers who need a simple, local alternative to Cloudinary for uploads, deletions, and testing without internet access.
4
+
5
+ ---
6
+
7
+ ## Features
8
+
9
+ - Local file uploads and deletions
10
+ - Cloudinary-style API responses
11
+ - Simple environment-based configuration
12
+ - Clear all stored uploads with one command
13
+ - Perfect for testing, prototyping, or offline development
14
+
15
+ ---
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install offline-cloudinary
21
+ ```
22
+
23
+ or with Yarn:
24
+
25
+ ```bash
26
+ yarn add offline-cloudinary
27
+ ```
28
+
29
+ ---
30
+
31
+ ## Setup
32
+
33
+ In your project’s `.env` file, define the base path for all offline uploads:
34
+
35
+ ```
36
+ CLOUDINARY_OFFLINE_PATH=./offline_uploads
37
+ ```
38
+
39
+ This path will serve as your “offline cloud” — all uploaded files will be stored here.
40
+
41
+ ---
42
+
43
+ ## Usage Example
44
+
45
+ ```js
46
+ import offlineCloudinary from "offline-cloudinary";
47
+
48
+ (async () => {
49
+ try {
50
+ // Upload a file
51
+ const uploadResult = await offlineCloudinary.upload("./temp/photo.jpg", {
52
+ folder: "users/avatars",
53
+ fileName: "max-essien"
54
+ });
55
+
56
+ console.log("Upload result:", uploadResult);
57
+
58
+ // Delete the uploaded file
59
+ const deleteResult = await offlineCloudinary.destroy(uploadResult.public_id);
60
+ console.log("Delete result:", deleteResult);
61
+
62
+ // Clear all files in the storage folder
63
+ const clearResult = await offlineCloudinary.clearStorage();
64
+ console.log("Storage cleared:", clearResult);
65
+ } catch (err) {
66
+ console.error("Error:", err.message);
67
+ }
68
+ })();
69
+ ```
70
+
71
+ ---
72
+
73
+ ## API Reference
74
+
75
+ ### **`new OfflineCloudinary()`**
76
+ Automatically instantiated when imported.
77
+ Throws an error if `CLOUDINARY_OFFLINE_PATH` is not set.
78
+
79
+ ---
80
+
81
+ ### **`upload(tempFilePath, options)`**
82
+
83
+ Uploads a file from a temporary path to your offline storage.
84
+
85
+ **Parameters**
86
+ | Name | Type | Description |
87
+ |------|------|--------------|
88
+ | `tempFilePath` | `string` | Path to the source file to upload |
89
+ | `options.folder` | `string` | Optional nested folder path |
90
+ | `options.fileName` | `string` | Optional custom file name (without extension) |
91
+
92
+ **Returns**
93
+ A Cloudinary-like object containing:
94
+ ```js
95
+ {
96
+ asset_id, public_id, version, format, resource_type,
97
+ created_at, bytes, url, secure_url, ...
98
+ }
99
+ ```
100
+
101
+ ---
102
+
103
+ ### **`destroy(public_id)`**
104
+
105
+ Deletes a file from your offline storage.
106
+
107
+ **Parameters**
108
+ | Name | Type | Description |
109
+ |------|------|--------------|
110
+ | `public_id` | `string` | Full path returned by the upload method |
111
+
112
+ **Returns**
113
+ ```js
114
+ { result: "ok" }
115
+ ```
116
+
117
+ ---
118
+
119
+ ### **`clearStorage()`**
120
+
121
+ Deletes all files and folders in your offline Cloudinary storage.
122
+
123
+ **Returns**
124
+ ```js
125
+ { result: "ok" }
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Example .env
131
+
132
+ ```
133
+ CLOUDINARY_OFFLINE_PATH=./uploads
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Example Folder Structure
139
+
140
+ ```
141
+ project/
142
+ ├── .env
143
+ ├── uploads/
144
+ │ └── users/
145
+ │ └── avatars/
146
+ │ └── max-essien.jpg
147
+ └── index.js
148
+ ```
149
+
150
+ ---
151
+
152
+ ## Author
153
+
154
+ **Max Essien**
155
+ 📍 Lagos, Nigeria
156
+ 🔗 [GitHub: @MaxEssien](https://github.com/MaxEssien)
157
+
158
+ ---
159
+
160
+ ## License
161
+
162
+ This project is licensed under the **MIT License** — free for personal and commercial use.
163
+
164
+ ---
165
+ **Offline Cloudinary** — your Cloudinary, anywhere, even offline.
166
+
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "offline-cloudinary",
3
+ "version": "1.0.0",
4
+ "description": "An offline Cloudinary-like file manager for local uploads and deletions.",
5
+ "main": "src/utils/OfflineCloudinary.js",
6
+ "type": "module",
7
+ "keywords": [
8
+ "cloudinary",
9
+ "offline",
10
+ "upload",
11
+ "local-storage",
12
+ "mock",
13
+ "testing",
14
+ "file-system",
15
+ "nodejs"
16
+ ],
17
+ "author": "Max Essien",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/MaxEssien/offline-cloudinary"
22
+ },
23
+ "files": ["src"],
24
+ "scripts": {
25
+ "test": "node src/utils/OfflineCloudinary.js"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/MaxEssien/offline-cloudinary/issues"
29
+ },
30
+ "homepage": "https://github.com/MaxEssien/offline-cloudinary#readme"
31
+ }
package/src/index.js ADDED
@@ -0,0 +1,92 @@
1
+ import fs from "fs/promises";
2
+ import path from "path";
3
+ import crypto from "crypto";
4
+
5
+ class OfflineCloudinary {
6
+ constructor() {
7
+ if (!process.env.CLOUDINARY_OFFLINE_PATH) {
8
+ throw new Error(
9
+ "Please set CLOUDINARY_OFFLINE_PATH in your .env file"
10
+ );
11
+ }
12
+ this.rootPath = process.env.CLOUDINARY_OFFLINE_PATH;
13
+ }
14
+
15
+ /**
16
+ * Upload a file
17
+ * @param {string} tempFilePath - Path to the temporary file
18
+ * @param {object} options - { folder: 'nested/folder/path' }
19
+ * @returns Cloudinary-like response
20
+ */
21
+ async upload(tempFilePath, options = {}) {
22
+ await fs.access(tempFilePath).catch(()=>{throw new Error(`File not found: ${tempFilePath}`)})
23
+ const folder = options.folder || "";
24
+ const name = options?.fileName || crypto.randomUUID();
25
+ const fullFolderPath = path.join(this.rootPath, folder);
26
+
27
+ // Ensure folder exists
28
+ await fs.mkdir(fullFolderPath, { recursive: true });
29
+
30
+ // Generate unique filename
31
+ const ext = path.extname(tempFilePath);
32
+ if (!ext?.trim()) throw new Error("Unsupported file type")
33
+ const fileName = name + ext;
34
+
35
+ const finalPath = path.join(fullFolderPath, fileName);
36
+
37
+ // Copy file from temp path
38
+ await fs.copyFile(tempFilePath, finalPath);
39
+
40
+ // Get file stats
41
+ const stats = await fs.stat(finalPath);
42
+
43
+ const now = new Date().toISOString();
44
+
45
+ // Return Cloudinary-like response
46
+ return {
47
+ asset_id: crypto.randomUUID(),
48
+ public_id: finalPath,
49
+ version: Date.now(),
50
+ version_id: crypto.randomUUID(),
51
+ signature: crypto.randomBytes(16).toString("hex"),
52
+ width: null,
53
+ height: null,
54
+ format: ext.replace(".", ""),
55
+ resource_type: "image",
56
+ created_at: now,
57
+ tags: [],
58
+ pages: 1,
59
+ bytes: stats.size,
60
+ type: "upload",
61
+ etag: crypto.randomBytes(8).toString("hex"),
62
+ placeholder: false,
63
+ url: finalPath,
64
+ secure_url: finalPath,
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Destroy a file by public_id
70
+ * @param {string} public_id
71
+ * @returns {object} { result: "ok" } if deleted or { result: "not found" }
72
+ */
73
+ async destroy(public_id) {
74
+ const filePath = public_id;
75
+ await fs.unlink(filePath);
76
+ return { result: "ok" };
77
+ }
78
+
79
+ /**
80
+ * Destroy every files and folder in the local offline cloudinary storage
81
+ * @returns {object} {result: ok} if successful
82
+ */
83
+ async clearStorage(){
84
+ await fs.rm(this.rootPath, {recursive: true, force: true})
85
+ await fs.mkdir(this.rootPath)
86
+ return {result: "ok"}
87
+ }
88
+ }
89
+
90
+ const offlineCloudinary = new OfflineCloudinary()
91
+
92
+ export default offlineCloudinary;