@strapi/provider-upload-cloudinary 4.9.1 → 4.9.2
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/index.d.ts +30 -0
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -0
- package/package.json +17 -7
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -4
- package/lib/index.js +0 -95
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { ReadStream } from 'node:fs';
|
|
3
|
+
import { ConfigOptions } from 'cloudinary';
|
|
4
|
+
interface File {
|
|
5
|
+
name: string;
|
|
6
|
+
alternativeText?: string;
|
|
7
|
+
caption?: string;
|
|
8
|
+
width?: number;
|
|
9
|
+
height?: number;
|
|
10
|
+
formats?: Record<string, unknown>;
|
|
11
|
+
hash: string;
|
|
12
|
+
ext?: string;
|
|
13
|
+
mime: string;
|
|
14
|
+
size: number;
|
|
15
|
+
url: string;
|
|
16
|
+
previewUrl?: string;
|
|
17
|
+
path?: string;
|
|
18
|
+
provider?: string;
|
|
19
|
+
provider_metadata?: Record<string, unknown>;
|
|
20
|
+
stream?: ReadStream;
|
|
21
|
+
buffer?: Buffer;
|
|
22
|
+
}
|
|
23
|
+
declare const _default: {
|
|
24
|
+
init(options: ConfigOptions): {
|
|
25
|
+
uploadStream(file: File, customConfig?: {}): Promise<void>;
|
|
26
|
+
upload(file: File, customConfig?: {}): Promise<void>;
|
|
27
|
+
delete(file: File, customConfig?: {}): Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export = _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const cloudinary_1 = require("cloudinary");
|
|
6
|
+
const into_stream_1 = __importDefault(require("into-stream"));
|
|
7
|
+
const utils_1 = __importDefault(require("@strapi/utils"));
|
|
8
|
+
module.exports = {
|
|
9
|
+
init(options) {
|
|
10
|
+
cloudinary_1.v2.config(options);
|
|
11
|
+
const upload = (file, customConfig = {}) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const config = {
|
|
14
|
+
resource_type: 'auto',
|
|
15
|
+
public_id: file.hash,
|
|
16
|
+
};
|
|
17
|
+
if (file.ext) {
|
|
18
|
+
config.filename = `${file.hash}${file.ext}`;
|
|
19
|
+
}
|
|
20
|
+
if (file.path) {
|
|
21
|
+
config.folder = file.path;
|
|
22
|
+
}
|
|
23
|
+
const uploadStream = cloudinary_1.v2.uploader.upload_stream({ ...config, ...customConfig }, (err, image) => {
|
|
24
|
+
if (err) {
|
|
25
|
+
if (err.message.includes('File size too large')) {
|
|
26
|
+
reject(new utils_1.default.errors.PayloadTooLargeError());
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
reject(new Error(`Error uploading to cloudinary: ${err.message}`));
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!image) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (image.resource_type === 'video') {
|
|
37
|
+
file.previewUrl = cloudinary_1.v2.url(`${image.public_id}.gif`, {
|
|
38
|
+
video_sampling: 6,
|
|
39
|
+
delay: 200,
|
|
40
|
+
width: 250,
|
|
41
|
+
crop: 'scale',
|
|
42
|
+
resource_type: 'video',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
file.url = image.secure_url;
|
|
46
|
+
file.provider_metadata = {
|
|
47
|
+
public_id: image.public_id,
|
|
48
|
+
resource_type: image.resource_type,
|
|
49
|
+
};
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
if (file.stream) {
|
|
53
|
+
file.stream.pipe(uploadStream);
|
|
54
|
+
}
|
|
55
|
+
else if (file.buffer) {
|
|
56
|
+
(0, into_stream_1.default)(file.buffer).pipe(uploadStream);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
throw new Error('Missing file stream or buffer');
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
uploadStream(file, customConfig = {}) {
|
|
65
|
+
return upload(file, customConfig);
|
|
66
|
+
},
|
|
67
|
+
upload(file, customConfig = {}) {
|
|
68
|
+
return upload(file, customConfig);
|
|
69
|
+
},
|
|
70
|
+
async delete(file, customConfig = {}) {
|
|
71
|
+
try {
|
|
72
|
+
const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
|
|
73
|
+
const deleteConfig = {
|
|
74
|
+
resource_type: (resourceType || 'image'),
|
|
75
|
+
invalidate: true,
|
|
76
|
+
...customConfig,
|
|
77
|
+
};
|
|
78
|
+
const response = await cloudinary_1.v2.uploader.destroy(`${publicId}`, deleteConfig);
|
|
79
|
+
if (response.result !== 'ok' && response.result !== 'not found') {
|
|
80
|
+
throw new Error(response.result);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error instanceof Error) {
|
|
85
|
+
throw new Error(`Error deleting on cloudinary: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AACA,2CAA+E;AAC/E,8DAAqC;AACrC,0DAAkC;AAsBlC,iBAAS;IACP,IAAI,CAAC,OAAsB;QACzB,eAAU,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAE3B,MAAM,MAAM,GAAG,CAAC,IAAU,EAAE,YAAY,GAAG,EAAE,EAAiB,EAAE;YAC9D,OAAO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AI,QAAQ,CAAC,MAAM,KAAK,WAAW,EAAE;wBAC/D,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;qBAClC;iBACF;gBAAC,OAAO,KAAK,EAAE;oBACd,IAAI,KAAK,YAAY,KAAK,EAAE;wBAC1B,MAAM,IAAI,KAAK,CAAC,iCAAiC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;qBACnE;oBAED,MAAM,KAAK,CAAC;iBACb;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/provider-upload-cloudinary",
|
|
3
|
-
"version": "4.9.
|
|
3
|
+
"version": "4.9.2",
|
|
4
4
|
"description": "Cloudinary provider for strapi upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"upload",
|
|
@@ -28,21 +28,31 @@
|
|
|
28
28
|
"url": "https://strapi.io"
|
|
29
29
|
}
|
|
30
30
|
],
|
|
31
|
-
"main": "./
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"./dist"
|
|
35
|
+
],
|
|
35
36
|
"scripts": {
|
|
37
|
+
"build": "run -T tsc",
|
|
38
|
+
"build:ts": "run -T tsc",
|
|
39
|
+
"watch": "run -T tsc -w --preserveWatchOutput",
|
|
40
|
+
"clean": "run -T rimraf ./dist",
|
|
41
|
+
"prepublishOnly": "yarn clean && yarn build",
|
|
36
42
|
"lint": "run -T eslint ."
|
|
37
43
|
},
|
|
38
44
|
"dependencies": {
|
|
39
|
-
"@strapi/utils": "4.9.
|
|
45
|
+
"@strapi/utils": "4.9.2",
|
|
40
46
|
"cloudinary": "^1.33.0",
|
|
41
47
|
"into-stream": "^5.1.0"
|
|
42
48
|
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"eslint-config-custom": "4.9.2",
|
|
51
|
+
"tsconfig": "4.9.2"
|
|
52
|
+
},
|
|
43
53
|
"engines": {
|
|
44
54
|
"node": ">=14.19.1 <=18.x.x",
|
|
45
55
|
"npm": ">=6.0.0"
|
|
46
56
|
},
|
|
47
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "91e0be2708e4d1e8ec731c75e73e54c0dfacb67d"
|
|
48
58
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
package/lib/index.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// Public node modules.
|
|
8
|
-
const cloudinary = require('cloudinary').v2;
|
|
9
|
-
const intoStream = require('into-stream');
|
|
10
|
-
const { PayloadTooLargeError } = require('@strapi/utils').errors;
|
|
11
|
-
|
|
12
|
-
module.exports = {
|
|
13
|
-
init(config) {
|
|
14
|
-
cloudinary.config(config);
|
|
15
|
-
|
|
16
|
-
const upload = (file, customConfig = {}) =>
|
|
17
|
-
new Promise((resolve, reject) => {
|
|
18
|
-
const config = {
|
|
19
|
-
resource_type: 'auto',
|
|
20
|
-
public_id: file.hash,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
if (file.ext) {
|
|
24
|
-
config.filename = `${file.hash}${file.ext}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (file.path) {
|
|
28
|
-
config.folder = file.path;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const uploadStream = cloudinary.uploader.upload_stream(
|
|
32
|
-
{ ...config, ...customConfig },
|
|
33
|
-
(err, image) => {
|
|
34
|
-
if (err) {
|
|
35
|
-
if (err.message.includes('File size too large')) {
|
|
36
|
-
reject(new PayloadTooLargeError());
|
|
37
|
-
} else {
|
|
38
|
-
reject(new Error(`Error uploading to cloudinary: ${err.message}`));
|
|
39
|
-
}
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (image.resource_type === 'video') {
|
|
44
|
-
file.previewUrl = cloudinary.url(`${image.public_id}.gif`, {
|
|
45
|
-
video_sampling: 6,
|
|
46
|
-
delay: 200,
|
|
47
|
-
width: 250,
|
|
48
|
-
crop: 'scale',
|
|
49
|
-
resource_type: 'video',
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
file.url = image.secure_url;
|
|
54
|
-
file.provider_metadata = {
|
|
55
|
-
public_id: image.public_id,
|
|
56
|
-
resource_type: image.resource_type,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
resolve();
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
if (file.stream) {
|
|
64
|
-
file.stream.pipe(uploadStream);
|
|
65
|
-
} else {
|
|
66
|
-
intoStream(file.buffer).pipe(uploadStream);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
uploadStream(file, customConfig = {}) {
|
|
72
|
-
return upload(file, customConfig);
|
|
73
|
-
},
|
|
74
|
-
upload(file, customConfig = {}) {
|
|
75
|
-
return upload(file, customConfig);
|
|
76
|
-
},
|
|
77
|
-
async delete(file, customConfig = {}) {
|
|
78
|
-
try {
|
|
79
|
-
const { resource_type: resourceType, public_id: publicId } = file.provider_metadata;
|
|
80
|
-
const response = await cloudinary.uploader.destroy(publicId, {
|
|
81
|
-
invalidate: true,
|
|
82
|
-
resource_type: resourceType || 'image',
|
|
83
|
-
...customConfig,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
if (response.result !== 'ok' && response.result !== 'not found') {
|
|
87
|
-
throw new Error(`Error deleting on cloudinary: ${response.result}`);
|
|
88
|
-
}
|
|
89
|
-
} catch (error) {
|
|
90
|
-
throw new Error(`Error deleting on cloudinary: ${error.message}`);
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
};
|