next-tinacms-cloudinary 16.0.1 → 17.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/dist/handlers.js +21 -56
- package/dist/index.js +138 -141
- package/package.json +4 -4
- package/dist/index.mjs +0 -146
package/dist/handlers.js
CHANGED
|
@@ -1,49 +1,15 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
|
|
29
1
|
// src/handlers.ts
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(handlers_exports);
|
|
36
|
-
var import_cloudinary = require("cloudinary");
|
|
37
|
-
var import_path = __toESM(require("path"));
|
|
38
|
-
var import_multer = __toESM(require("multer"));
|
|
39
|
-
var import_util = require("util");
|
|
2
|
+
import { v2 as cloudinary } from "cloudinary";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import multer from "multer";
|
|
5
|
+
import { promisify } from "util";
|
|
40
6
|
var mediaHandlerConfig = {
|
|
41
7
|
api: {
|
|
42
8
|
bodyParser: false
|
|
43
9
|
}
|
|
44
10
|
};
|
|
45
11
|
var createMediaHandler = (config, options) => {
|
|
46
|
-
|
|
12
|
+
cloudinary.config(Object.assign({ secure: true }, config));
|
|
47
13
|
return async (req, res) => {
|
|
48
14
|
const isAuthorized = await config.authorized(req, res);
|
|
49
15
|
if (!isAuthorized) {
|
|
@@ -63,9 +29,9 @@ var createMediaHandler = (config, options) => {
|
|
|
63
29
|
};
|
|
64
30
|
};
|
|
65
31
|
async function uploadMedia(req, res) {
|
|
66
|
-
const upload =
|
|
67
|
-
(
|
|
68
|
-
storage:
|
|
32
|
+
const upload = promisify(
|
|
33
|
+
multer({
|
|
34
|
+
storage: multer.diskStorage({
|
|
69
35
|
// @ts-ignore
|
|
70
36
|
directory: (req2, file, cb) => {
|
|
71
37
|
cb(null, "/tmp");
|
|
@@ -79,7 +45,7 @@ async function uploadMedia(req, res) {
|
|
|
79
45
|
await upload(req, res);
|
|
80
46
|
const { directory } = req.body;
|
|
81
47
|
try {
|
|
82
|
-
const result = await
|
|
48
|
+
const result = await cloudinary.uploader.upload(req.file.path, {
|
|
83
49
|
folder: directory.replace(/^\//, ""),
|
|
84
50
|
use_filename: true,
|
|
85
51
|
overwrite: false,
|
|
@@ -100,13 +66,13 @@ async function listMedia(req, res, opts) {
|
|
|
100
66
|
};
|
|
101
67
|
const useRootDirectory = !mediaListOptions.directory || mediaListOptions.directory === "/" || mediaListOptions.directory === '""';
|
|
102
68
|
const query = useRootDirectory ? 'folder=""' : `folder="${mediaListOptions.directory}"`;
|
|
103
|
-
const response = await
|
|
69
|
+
const response = await cloudinary.search.expression(query).max_results(mediaListOptions.limit).next_cursor(mediaListOptions.offset).execute();
|
|
104
70
|
const files = response.resources.map(getCloudinaryToTinaFunc(opts));
|
|
105
|
-
|
|
71
|
+
cloudinary.api.folders = (directory = '""') => {
|
|
106
72
|
if (useRootDirectory) {
|
|
107
|
-
return
|
|
73
|
+
return cloudinary.api.root_folders();
|
|
108
74
|
} else {
|
|
109
|
-
return
|
|
75
|
+
return cloudinary.api.sub_folders(directory);
|
|
110
76
|
}
|
|
111
77
|
};
|
|
112
78
|
let folders = [];
|
|
@@ -119,7 +85,7 @@ async function listMedia(req, res, opts) {
|
|
|
119
85
|
return;
|
|
120
86
|
}
|
|
121
87
|
try {
|
|
122
|
-
folderRes = await
|
|
88
|
+
folderRes = await cloudinary.api.folders(mediaListOptions.directory);
|
|
123
89
|
} catch (e) {
|
|
124
90
|
if (e.error?.message.startsWith("Can't find folder with path")) {
|
|
125
91
|
} else {
|
|
@@ -134,8 +100,8 @@ async function listMedia(req, res, opts) {
|
|
|
134
100
|
return {
|
|
135
101
|
id: folder.path,
|
|
136
102
|
type: "dir",
|
|
137
|
-
filename:
|
|
138
|
-
directory:
|
|
103
|
+
filename: path.basename(folder.path),
|
|
104
|
+
directory: path.dirname(folder.path)
|
|
139
105
|
};
|
|
140
106
|
});
|
|
141
107
|
}
|
|
@@ -159,7 +125,7 @@ var findErrorMessage = (e) => {
|
|
|
159
125
|
async function deleteAsset(req, res) {
|
|
160
126
|
const { media } = req.query;
|
|
161
127
|
const [, public_id] = media;
|
|
162
|
-
|
|
128
|
+
cloudinary.uploader.destroy(public_id, {}, (err) => {
|
|
163
129
|
if (err) res.status(500);
|
|
164
130
|
res.json({
|
|
165
131
|
err,
|
|
@@ -174,8 +140,8 @@ function getCloudinaryToTinaFunc(opts) {
|
|
|
174
140
|
useHttps = opts.useHttps;
|
|
175
141
|
}
|
|
176
142
|
const sel = useHttps ? "secure_url" : "url";
|
|
177
|
-
const filename =
|
|
178
|
-
const directory =
|
|
143
|
+
const filename = path.basename(file.public_id);
|
|
144
|
+
const directory = path.dirname(file.public_id);
|
|
179
145
|
return {
|
|
180
146
|
id: file.public_id,
|
|
181
147
|
filename,
|
|
@@ -203,8 +169,7 @@ function transformCloudinaryImage(url, transformations) {
|
|
|
203
169
|
}
|
|
204
170
|
return url;
|
|
205
171
|
}
|
|
206
|
-
|
|
207
|
-
0 && (module.exports = {
|
|
172
|
+
export {
|
|
208
173
|
createMediaHandler,
|
|
209
174
|
mediaHandlerConfig
|
|
210
|
-
}
|
|
175
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,149 +1,146 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.ERR_TYPE = "MediaListError";
|
|
9
|
-
this.title = config.title;
|
|
10
|
-
this.docsLink = config.docsLink;
|
|
11
|
-
}
|
|
1
|
+
import { DEFAULT_MEDIA_UPLOAD_TYPES } from "tinacms";
|
|
2
|
+
class MediaListError extends Error {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
super(config.message);
|
|
5
|
+
this.ERR_TYPE = "MediaListError";
|
|
6
|
+
this.title = config.title;
|
|
7
|
+
this.docsLink = config.docsLink;
|
|
12
8
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
9
|
+
}
|
|
10
|
+
const E_DEFAULT = new MediaListError({
|
|
11
|
+
title: "An Error Occurred",
|
|
12
|
+
message: "Something went wrong fetching your media from Cloudinary.",
|
|
13
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
14
|
+
});
|
|
15
|
+
const E_UNAUTHORIZED = new MediaListError({
|
|
16
|
+
title: "Unauthorized",
|
|
17
|
+
message: "You don't have access to this resource.",
|
|
18
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
19
|
+
});
|
|
20
|
+
const E_CONFIG = new MediaListError({
|
|
21
|
+
title: "Missing Credentials",
|
|
22
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are missing.",
|
|
23
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
24
|
+
});
|
|
25
|
+
const E_KEY_FAIL = new MediaListError({
|
|
26
|
+
title: "Bad Credentials",
|
|
27
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are misconfigured.",
|
|
28
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
29
|
+
});
|
|
30
|
+
const E_BAD_ROUTE = new MediaListError({
|
|
31
|
+
title: "Bad Route",
|
|
32
|
+
message: "The Cloudinary API route is missing or misconfigured.",
|
|
33
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
34
|
+
});
|
|
35
|
+
const interpretErrorMessage = (message) => {
|
|
36
|
+
switch (message) {
|
|
37
|
+
case "Must supply cloud_name":
|
|
38
|
+
case "Must supply api_key":
|
|
39
|
+
case "Must supply api_secret":
|
|
40
|
+
return E_CONFIG;
|
|
41
|
+
case "unknown api_key":
|
|
42
|
+
return E_KEY_FAIL;
|
|
43
|
+
default:
|
|
44
|
+
return E_DEFAULT;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
class CloudinaryMediaStore {
|
|
48
|
+
constructor(options) {
|
|
49
|
+
this.fetchFunction = (input, init) => fetch(input, init);
|
|
50
|
+
this.accept = DEFAULT_MEDIA_UPLOAD_TYPES;
|
|
51
|
+
this.parse = (img) => {
|
|
52
|
+
return img.src;
|
|
53
|
+
};
|
|
54
|
+
this.baseUrl = (options == null ? void 0 : options.baseUrl) || "/api/cloudinary/media";
|
|
55
|
+
}
|
|
56
|
+
async persist(media) {
|
|
57
|
+
const newFiles = [];
|
|
58
|
+
for (const item of media) {
|
|
59
|
+
const { file, directory } = item;
|
|
60
|
+
const formData = new FormData();
|
|
61
|
+
formData.append("file", file);
|
|
62
|
+
formData.append("directory", directory);
|
|
63
|
+
formData.append("filename", file.name);
|
|
64
|
+
const res = await this.fetchFunction(this.baseUrl, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
body: formData
|
|
67
|
+
});
|
|
68
|
+
if (res.status != 200) {
|
|
69
|
+
const responseData = await res.json();
|
|
70
|
+
throw new Error(responseData.message);
|
|
71
|
+
}
|
|
72
|
+
const fileRes = await res.json();
|
|
73
|
+
await new Promise((resolve) => {
|
|
74
|
+
setTimeout(resolve, 2e3);
|
|
75
|
+
});
|
|
76
|
+
const parsedRes = {
|
|
77
|
+
type: "file",
|
|
78
|
+
id: fileRes.public_id,
|
|
79
|
+
filename: fileRes.original_filename,
|
|
80
|
+
directory: "/",
|
|
81
|
+
thumbnails: {
|
|
82
|
+
"75x75": fileRes.secure_url,
|
|
83
|
+
"400x400": fileRes.secure_url,
|
|
84
|
+
"1000x1000": fileRes.secure_url
|
|
85
|
+
},
|
|
86
|
+
src: fileRes.secure_url
|
|
56
87
|
};
|
|
57
|
-
|
|
88
|
+
newFiles.push(parsedRes);
|
|
58
89
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
formData.append("filename", file.name);
|
|
67
|
-
const res = await this.fetchFunction(this.baseUrl, {
|
|
68
|
-
method: "POST",
|
|
69
|
-
body: formData
|
|
70
|
-
});
|
|
71
|
-
if (res.status != 200) {
|
|
72
|
-
const responseData = await res.json();
|
|
73
|
-
throw new Error(responseData.message);
|
|
74
|
-
}
|
|
75
|
-
const fileRes = await res.json();
|
|
76
|
-
await new Promise((resolve) => {
|
|
77
|
-
setTimeout(resolve, 2e3);
|
|
78
|
-
});
|
|
79
|
-
const parsedRes = {
|
|
80
|
-
type: "file",
|
|
81
|
-
id: fileRes.public_id,
|
|
82
|
-
filename: fileRes.original_filename,
|
|
83
|
-
directory: "/",
|
|
84
|
-
thumbnails: {
|
|
85
|
-
"75x75": fileRes.secure_url,
|
|
86
|
-
"400x400": fileRes.secure_url,
|
|
87
|
-
"1000x1000": fileRes.secure_url
|
|
88
|
-
},
|
|
89
|
-
src: fileRes.secure_url
|
|
90
|
-
};
|
|
91
|
-
newFiles.push(parsedRes);
|
|
90
|
+
return newFiles;
|
|
91
|
+
}
|
|
92
|
+
async delete(media) {
|
|
93
|
+
await this.fetchFunction(
|
|
94
|
+
`${this.baseUrl}/${encodeURIComponent(media.id)}`,
|
|
95
|
+
{
|
|
96
|
+
method: "DELETE"
|
|
92
97
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
);
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
async list(options) {
|
|
101
|
+
const query = this.buildQuery(options);
|
|
102
|
+
const response = await this.fetchFunction(this.baseUrl + query);
|
|
103
|
+
if (response.status == 401) {
|
|
104
|
+
throw E_UNAUTHORIZED;
|
|
102
105
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const response = await this.fetchFunction(this.baseUrl + query);
|
|
106
|
-
if (response.status == 401) {
|
|
107
|
-
throw E_UNAUTHORIZED;
|
|
108
|
-
}
|
|
109
|
-
if (response.status == 404) {
|
|
110
|
-
throw E_BAD_ROUTE;
|
|
111
|
-
}
|
|
112
|
-
if (response.status >= 500) {
|
|
113
|
-
const { e } = await response.json();
|
|
114
|
-
const error = interpretErrorMessage(e);
|
|
115
|
-
throw error;
|
|
116
|
-
}
|
|
117
|
-
const { items, offset } = await response.json();
|
|
118
|
-
return {
|
|
119
|
-
items: items.map((item) => item),
|
|
120
|
-
nextOffset: offset
|
|
121
|
-
};
|
|
106
|
+
if (response.status == 404) {
|
|
107
|
+
throw E_BAD_ROUTE;
|
|
122
108
|
}
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
109
|
+
if (response.status >= 500) {
|
|
110
|
+
const { e } = await response.json();
|
|
111
|
+
const error = interpretErrorMessage(e);
|
|
112
|
+
throw error;
|
|
126
113
|
}
|
|
114
|
+
const { items, offset } = await response.json();
|
|
115
|
+
return {
|
|
116
|
+
items: items.map((item) => item),
|
|
117
|
+
nextOffset: offset
|
|
118
|
+
};
|
|
127
119
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
120
|
+
buildQuery(options) {
|
|
121
|
+
const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
|
|
122
|
+
return `?${params}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const createTinaCloudCloudinaryMediaStore = (options = { baseUrl: "/api/cloudinary/media" }) => class TinaCloudCloudinaryMediaStore extends CloudinaryMediaStore {
|
|
126
|
+
constructor(client) {
|
|
127
|
+
super(options);
|
|
128
|
+
this.client = client;
|
|
129
|
+
this.fetchFunction = async (input, init) => {
|
|
130
|
+
try {
|
|
131
|
+
const url = input.toString();
|
|
132
|
+
const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
|
|
133
|
+
const res = client.authProvider.fetchWithToken(url + query, init);
|
|
134
|
+
return res;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error(error);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const TinaCloudCloudinaryMediaStore = createTinaCloudCloudinaryMediaStore();
|
|
142
|
+
export {
|
|
143
|
+
CloudinaryMediaStore,
|
|
144
|
+
TinaCloudCloudinaryMediaStore,
|
|
145
|
+
createTinaCloudCloudinaryMediaStore
|
|
146
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-tinacms-cloudinary",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "17.0.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
|
-
"module": "dist/index.
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"react-dom": "^18.3.1",
|
|
31
31
|
"typescript": "^5.7.3",
|
|
32
32
|
"@tinacms/scripts": "1.4.1",
|
|
33
|
-
"tinacms": "
|
|
33
|
+
"tinacms": "3.0.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"tinacms": "
|
|
36
|
+
"tinacms": "3.0.1"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"registry": "https://registry.npmjs.org"
|
package/dist/index.mjs
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_MEDIA_UPLOAD_TYPES } from "tinacms";
|
|
2
|
-
class MediaListError extends Error {
|
|
3
|
-
constructor(config) {
|
|
4
|
-
super(config.message);
|
|
5
|
-
this.ERR_TYPE = "MediaListError";
|
|
6
|
-
this.title = config.title;
|
|
7
|
-
this.docsLink = config.docsLink;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
const E_DEFAULT = new MediaListError({
|
|
11
|
-
title: "An Error Occurred",
|
|
12
|
-
message: "Something went wrong fetching your media from Cloudinary.",
|
|
13
|
-
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
14
|
-
});
|
|
15
|
-
const E_UNAUTHORIZED = new MediaListError({
|
|
16
|
-
title: "Unauthorized",
|
|
17
|
-
message: "You don't have access to this resource.",
|
|
18
|
-
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
19
|
-
});
|
|
20
|
-
const E_CONFIG = new MediaListError({
|
|
21
|
-
title: "Missing Credentials",
|
|
22
|
-
message: "Unable to connect to Cloudinary because one or more environment variables are missing.",
|
|
23
|
-
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
24
|
-
});
|
|
25
|
-
const E_KEY_FAIL = new MediaListError({
|
|
26
|
-
title: "Bad Credentials",
|
|
27
|
-
message: "Unable to connect to Cloudinary because one or more environment variables are misconfigured.",
|
|
28
|
-
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
29
|
-
});
|
|
30
|
-
const E_BAD_ROUTE = new MediaListError({
|
|
31
|
-
title: "Bad Route",
|
|
32
|
-
message: "The Cloudinary API route is missing or misconfigured.",
|
|
33
|
-
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
34
|
-
});
|
|
35
|
-
const interpretErrorMessage = (message) => {
|
|
36
|
-
switch (message) {
|
|
37
|
-
case "Must supply cloud_name":
|
|
38
|
-
case "Must supply api_key":
|
|
39
|
-
case "Must supply api_secret":
|
|
40
|
-
return E_CONFIG;
|
|
41
|
-
case "unknown api_key":
|
|
42
|
-
return E_KEY_FAIL;
|
|
43
|
-
default:
|
|
44
|
-
return E_DEFAULT;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
class CloudinaryMediaStore {
|
|
48
|
-
constructor(options) {
|
|
49
|
-
this.fetchFunction = (input, init) => fetch(input, init);
|
|
50
|
-
this.accept = DEFAULT_MEDIA_UPLOAD_TYPES;
|
|
51
|
-
this.parse = (img) => {
|
|
52
|
-
return img.src;
|
|
53
|
-
};
|
|
54
|
-
this.baseUrl = (options == null ? void 0 : options.baseUrl) || "/api/cloudinary/media";
|
|
55
|
-
}
|
|
56
|
-
async persist(media) {
|
|
57
|
-
const newFiles = [];
|
|
58
|
-
for (const item of media) {
|
|
59
|
-
const { file, directory } = item;
|
|
60
|
-
const formData = new FormData();
|
|
61
|
-
formData.append("file", file);
|
|
62
|
-
formData.append("directory", directory);
|
|
63
|
-
formData.append("filename", file.name);
|
|
64
|
-
const res = await this.fetchFunction(this.baseUrl, {
|
|
65
|
-
method: "POST",
|
|
66
|
-
body: formData
|
|
67
|
-
});
|
|
68
|
-
if (res.status != 200) {
|
|
69
|
-
const responseData = await res.json();
|
|
70
|
-
throw new Error(responseData.message);
|
|
71
|
-
}
|
|
72
|
-
const fileRes = await res.json();
|
|
73
|
-
await new Promise((resolve) => {
|
|
74
|
-
setTimeout(resolve, 2e3);
|
|
75
|
-
});
|
|
76
|
-
const parsedRes = {
|
|
77
|
-
type: "file",
|
|
78
|
-
id: fileRes.public_id,
|
|
79
|
-
filename: fileRes.original_filename,
|
|
80
|
-
directory: "/",
|
|
81
|
-
thumbnails: {
|
|
82
|
-
"75x75": fileRes.secure_url,
|
|
83
|
-
"400x400": fileRes.secure_url,
|
|
84
|
-
"1000x1000": fileRes.secure_url
|
|
85
|
-
},
|
|
86
|
-
src: fileRes.secure_url
|
|
87
|
-
};
|
|
88
|
-
newFiles.push(parsedRes);
|
|
89
|
-
}
|
|
90
|
-
return newFiles;
|
|
91
|
-
}
|
|
92
|
-
async delete(media) {
|
|
93
|
-
await this.fetchFunction(
|
|
94
|
-
`${this.baseUrl}/${encodeURIComponent(media.id)}`,
|
|
95
|
-
{
|
|
96
|
-
method: "DELETE"
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
async list(options) {
|
|
101
|
-
const query = this.buildQuery(options);
|
|
102
|
-
const response = await this.fetchFunction(this.baseUrl + query);
|
|
103
|
-
if (response.status == 401) {
|
|
104
|
-
throw E_UNAUTHORIZED;
|
|
105
|
-
}
|
|
106
|
-
if (response.status == 404) {
|
|
107
|
-
throw E_BAD_ROUTE;
|
|
108
|
-
}
|
|
109
|
-
if (response.status >= 500) {
|
|
110
|
-
const { e } = await response.json();
|
|
111
|
-
const error = interpretErrorMessage(e);
|
|
112
|
-
throw error;
|
|
113
|
-
}
|
|
114
|
-
const { items, offset } = await response.json();
|
|
115
|
-
return {
|
|
116
|
-
items: items.map((item) => item),
|
|
117
|
-
nextOffset: offset
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
buildQuery(options) {
|
|
121
|
-
const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
|
|
122
|
-
return `?${params}`;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const createTinaCloudCloudinaryMediaStore = (options = { baseUrl: "/api/cloudinary/media" }) => class TinaCloudCloudinaryMediaStore extends CloudinaryMediaStore {
|
|
126
|
-
constructor(client) {
|
|
127
|
-
super(options);
|
|
128
|
-
this.client = client;
|
|
129
|
-
this.fetchFunction = async (input, init) => {
|
|
130
|
-
try {
|
|
131
|
-
const url = input.toString();
|
|
132
|
-
const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
|
|
133
|
-
const res = client.authProvider.fetchWithToken(url + query, init);
|
|
134
|
-
return res;
|
|
135
|
-
} catch (error) {
|
|
136
|
-
console.error(error);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
const TinaCloudCloudinaryMediaStore = createTinaCloudCloudinaryMediaStore();
|
|
142
|
-
export {
|
|
143
|
-
CloudinaryMediaStore,
|
|
144
|
-
TinaCloudCloudinaryMediaStore,
|
|
145
|
-
createTinaCloudCloudinaryMediaStore
|
|
146
|
-
};
|