next-tinacms-cloudinary 0.0.0-fd664d8-20250407054012 → 0.0.0-fd7d6a8-20251202025033
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 +23 -59
- package/dist/index.js +138 -141
- package/package.json +5 -5
- 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,
|
|
@@ -91,7 +57,6 @@ async function uploadMedia(req, res) {
|
|
|
91
57
|
}
|
|
92
58
|
}
|
|
93
59
|
async function listMedia(req, res, opts) {
|
|
94
|
-
var _a;
|
|
95
60
|
try {
|
|
96
61
|
const mediaListOptions = {
|
|
97
62
|
directory: req.query.directory || '""',
|
|
@@ -101,13 +66,13 @@ async function listMedia(req, res, opts) {
|
|
|
101
66
|
};
|
|
102
67
|
const useRootDirectory = !mediaListOptions.directory || mediaListOptions.directory === "/" || mediaListOptions.directory === '""';
|
|
103
68
|
const query = useRootDirectory ? 'folder=""' : `folder="${mediaListOptions.directory}"`;
|
|
104
|
-
const response = await
|
|
69
|
+
const response = await cloudinary.search.expression(query).max_results(mediaListOptions.limit).next_cursor(mediaListOptions.offset).execute();
|
|
105
70
|
const files = response.resources.map(getCloudinaryToTinaFunc(opts));
|
|
106
|
-
|
|
71
|
+
cloudinary.api.folders = (directory = '""') => {
|
|
107
72
|
if (useRootDirectory) {
|
|
108
|
-
return
|
|
73
|
+
return cloudinary.api.root_folders();
|
|
109
74
|
} else {
|
|
110
|
-
return
|
|
75
|
+
return cloudinary.api.sub_folders(directory);
|
|
111
76
|
}
|
|
112
77
|
};
|
|
113
78
|
let folders = [];
|
|
@@ -120,23 +85,23 @@ async function listMedia(req, res, opts) {
|
|
|
120
85
|
return;
|
|
121
86
|
}
|
|
122
87
|
try {
|
|
123
|
-
folderRes = await
|
|
88
|
+
folderRes = await cloudinary.api.folders(mediaListOptions.directory);
|
|
124
89
|
} catch (e) {
|
|
125
|
-
if (
|
|
90
|
+
if (e.error?.message.startsWith("Can't find folder with path")) {
|
|
126
91
|
} else {
|
|
127
92
|
console.error("Error getting folders");
|
|
128
93
|
console.error(e);
|
|
129
94
|
throw e;
|
|
130
95
|
}
|
|
131
96
|
}
|
|
132
|
-
if (folderRes
|
|
97
|
+
if (folderRes?.folders) {
|
|
133
98
|
folders = folderRes.folders.map(function(folder) {
|
|
134
99
|
"empty-repo/004";
|
|
135
100
|
return {
|
|
136
101
|
id: folder.path,
|
|
137
102
|
type: "dir",
|
|
138
|
-
filename:
|
|
139
|
-
directory:
|
|
103
|
+
filename: path.basename(folder.path),
|
|
104
|
+
directory: path.dirname(folder.path)
|
|
140
105
|
};
|
|
141
106
|
});
|
|
142
107
|
}
|
|
@@ -160,7 +125,7 @@ var findErrorMessage = (e) => {
|
|
|
160
125
|
async function deleteAsset(req, res) {
|
|
161
126
|
const { media } = req.query;
|
|
162
127
|
const [, public_id] = media;
|
|
163
|
-
|
|
128
|
+
cloudinary.uploader.destroy(public_id, {}, (err) => {
|
|
164
129
|
if (err) res.status(500);
|
|
165
130
|
res.json({
|
|
166
131
|
err,
|
|
@@ -175,8 +140,8 @@ function getCloudinaryToTinaFunc(opts) {
|
|
|
175
140
|
useHttps = opts.useHttps;
|
|
176
141
|
}
|
|
177
142
|
const sel = useHttps ? "secure_url" : "url";
|
|
178
|
-
const filename =
|
|
179
|
-
const directory =
|
|
143
|
+
const filename = path.basename(file.public_id);
|
|
144
|
+
const directory = path.dirname(file.public_id);
|
|
180
145
|
return {
|
|
181
146
|
id: file.public_id,
|
|
182
147
|
filename,
|
|
@@ -204,8 +169,7 @@ function transformCloudinaryImage(url, transformations) {
|
|
|
204
169
|
}
|
|
205
170
|
return url;
|
|
206
171
|
}
|
|
207
|
-
|
|
208
|
-
0 && (module.exports = {
|
|
172
|
+
export {
|
|
209
173
|
createMediaHandler,
|
|
210
174
|
mediaHandlerConfig
|
|
211
|
-
}
|
|
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": "0.0.0-
|
|
3
|
+
"version": "0.0.0-fd7d6a8-20251202025033",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
|
-
"module": "dist/index.
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"react": "^18.3.1",
|
|
30
30
|
"react-dom": "^18.3.1",
|
|
31
31
|
"typescript": "^5.7.3",
|
|
32
|
-
"@tinacms/scripts": "
|
|
33
|
-
"tinacms": "0.0.0-
|
|
32
|
+
"@tinacms/scripts": "1.4.1",
|
|
33
|
+
"tinacms": "0.0.0-fd7d6a8-20251202025033"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"tinacms": "0.0.0-
|
|
36
|
+
"tinacms": "0.0.0-fd7d6a8-20251202025033"
|
|
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/reference/media/external/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/reference/media/external/cloudinary/#set-up-api-routes-nextjs-example"
|
|
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/media-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/media-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/reference/media/external/cloudinary/#set-up-api-routes-nextjs-example"
|
|
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
|
-
};
|