@strapi/provider-upload-cloudinary 4.14.2 → 4.14.4

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 CHANGED
@@ -28,4 +28,4 @@ declare const _default: {
28
28
  delete(file: File, customConfig?: {}): Promise<void>;
29
29
  };
30
30
  };
31
- export = _default;
31
+ export default _default;
package/dist/index.js CHANGED
@@ -1,124 +1,109 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
2
+ const cloudinary = require("cloudinary");
3
+ const intoStream = require("into-stream");
4
+ const utils = require("@strapi/utils");
5
+ const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
6
+ function _interopNamespace(e) {
7
+ if (e && e.__esModule)
8
+ return e;
9
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
10
+ if (e) {
11
+ for (const k in e) {
12
+ if (k !== "default") {
13
+ const d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: () => e[k]
17
+ });
18
+ }
7
19
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- const cloudinary_1 = require("cloudinary");
29
- const into_stream_1 = __importDefault(require("into-stream"));
30
- const utils = __importStar(require("@strapi/utils"));
31
- module.exports = {
32
- init(options) {
33
- cloudinary_1.v2.config(options);
34
- const upload = (file, customConfig = {}) => {
35
- return new Promise((resolve, reject) => {
36
- const config = {
37
- resource_type: 'auto',
38
- public_id: file.hash,
39
- };
40
- if (file.ext) {
41
- config.filename = `${file.hash}${file.ext}`;
42
- }
43
- if (file.path) {
44
- config.folder = file.path;
45
- }
46
- // For files smaller than 99 MB use regular upload as it tends to be faster
47
- // and fallback to chunked upload for larger files as that's required by Cloudinary.
48
- // https://support.cloudinary.com/hc/en-us/community/posts/360009586100-Upload-movie-video-with-large-size?page=1#community_comment_360002140099
49
- // The Cloudinary's max limit for regular upload is actually 100 MB but add some headroom
50
- // for size counting shenanigans. (Strapi provides the size in kilobytes rounded to two decimal places here).
51
- const uploadMethod = file.size && file.size < 1000 * 99
52
- ? cloudinary_1.v2.uploader.upload_stream
53
- : cloudinary_1.v2.uploader.upload_chunked_stream;
54
- const uploadStream = uploadMethod({ ...config, ...customConfig }, (err, image) => {
55
- if (err) {
56
- if (err.message.includes('File size too large')) {
57
- reject(new utils.errors.PayloadTooLargeError());
58
- }
59
- else {
60
- reject(new Error(`Error uploading to cloudinary: ${err.message}`));
61
- }
62
- return;
63
- }
64
- if (!image) {
65
- return;
66
- }
67
- if (image.resource_type === 'video') {
68
- file.previewUrl = cloudinary_1.v2.url(`${image.public_id}.gif`, {
69
- video_sampling: 6,
70
- delay: 200,
71
- width: 250,
72
- crop: 'scale',
73
- resource_type: 'video',
74
- });
75
- }
76
- file.url = image.secure_url;
77
- file.provider_metadata = {
78
- public_id: image.public_id,
79
- resource_type: image.resource_type,
80
- };
81
- resolve();
82
- });
83
- if (file.stream) {
84
- file.stream.pipe(uploadStream);
85
- }
86
- else if (file.buffer) {
87
- (0, into_stream_1.default)(file.buffer).pipe(uploadStream);
88
- }
89
- else {
90
- throw new Error('Missing file stream or buffer');
91
- }
92
- });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+ const intoStream__default = /* @__PURE__ */ _interopDefault(intoStream);
25
+ const utils__namespace = /* @__PURE__ */ _interopNamespace(utils);
26
+ const index = {
27
+ init(options) {
28
+ cloudinary.v2.config(options);
29
+ const upload = (file, customConfig = {}) => {
30
+ return new Promise((resolve, reject) => {
31
+ const config = {
32
+ resource_type: "auto",
33
+ public_id: file.hash
93
34
  };
94
- return {
95
- uploadStream(file, customConfig = {}) {
96
- return upload(file, customConfig);
97
- },
98
- upload(file, customConfig = {}) {
99
- return upload(file, customConfig);
100
- },
101
- async delete(file, customConfig = {}) {
102
- try {
103
- const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
104
- const deleteConfig = {
105
- resource_type: (resourceType || 'image'),
106
- invalidate: true,
107
- ...customConfig,
108
- };
109
- const response = await cloudinary_1.v2.uploader.destroy(`${publicId}`, deleteConfig);
110
- if (response.result !== 'ok' && response.result !== 'not found') {
111
- throw new Error(response.result);
112
- }
113
- }
114
- catch (error) {
115
- if (error instanceof Error) {
116
- throw new Error(`Error deleting on cloudinary: ${error.message}`);
117
- }
118
- throw error;
119
- }
120
- },
121
- };
122
- },
35
+ if (file.ext) {
36
+ config.filename = `${file.hash}${file.ext}`;
37
+ }
38
+ if (file.path) {
39
+ config.folder = file.path;
40
+ }
41
+ const uploadMethod = file.size && file.size < 1e3 * 99 ? cloudinary.v2.uploader.upload_stream : cloudinary.v2.uploader.upload_chunked_stream;
42
+ const uploadStream = uploadMethod({ ...config, ...customConfig }, (err, image) => {
43
+ if (err) {
44
+ if (err.message.includes("File size too large")) {
45
+ reject(new utils__namespace.errors.PayloadTooLargeError());
46
+ } else {
47
+ reject(new Error(`Error uploading to cloudinary: ${err.message}`));
48
+ }
49
+ return;
50
+ }
51
+ if (!image) {
52
+ return;
53
+ }
54
+ if (image.resource_type === "video") {
55
+ file.previewUrl = cloudinary.v2.url(`${image.public_id}.gif`, {
56
+ video_sampling: 6,
57
+ delay: 200,
58
+ width: 250,
59
+ crop: "scale",
60
+ resource_type: "video"
61
+ });
62
+ }
63
+ file.url = image.secure_url;
64
+ file.provider_metadata = {
65
+ public_id: image.public_id,
66
+ resource_type: image.resource_type
67
+ };
68
+ resolve();
69
+ });
70
+ if (file.stream) {
71
+ file.stream.pipe(uploadStream);
72
+ } else if (file.buffer) {
73
+ intoStream__default.default(file.buffer).pipe(uploadStream);
74
+ } else {
75
+ throw new Error("Missing file stream or buffer");
76
+ }
77
+ });
78
+ };
79
+ return {
80
+ uploadStream(file, customConfig = {}) {
81
+ return upload(file, customConfig);
82
+ },
83
+ upload(file, customConfig = {}) {
84
+ return upload(file, customConfig);
85
+ },
86
+ async delete(file, customConfig = {}) {
87
+ try {
88
+ const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
89
+ const deleteConfig = {
90
+ resource_type: resourceType || "image",
91
+ invalidate: true,
92
+ ...customConfig
93
+ };
94
+ const response = await cloudinary.v2.uploader.destroy(`${publicId}`, deleteConfig);
95
+ if (response.result !== "ok" && response.result !== "not found") {
96
+ throw new Error(response.result);
97
+ }
98
+ } catch (error) {
99
+ if (error instanceof Error) {
100
+ throw new Error(`Error deleting on cloudinary: ${error.message}`);
101
+ }
102
+ throw error;
103
+ }
104
+ }
105
+ };
106
+ }
123
107
  };
124
- //# sourceMappingURL=index.js.map
108
+ module.exports = index;
109
+ //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2CAA+E;AAC/E,8DAAqC;AACrC,qDAAuC;AAsBvC,iBAAS;IACP,IAAI,CAAC,OAAsB;QACzB,eAAU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Aa,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,iBAAiB,IAAI,EAAE,CAAC;oBAC1F,MAAM,YAAY,GAAG;wBACnB,aAAa,EAAE,CAAC,YAAY,IAAI,OAAO,CAAW;wBAClD,UAAU,EAAE,IAAI;wBAChB,GAAG,YAAY;qBAChB,CAAC;oBAEF,MAAM,QAAQ,GAAG,MAAM,eAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,QAAQ,EAAE,EAAE,YAAY,CAAC,CAAC;oBAEhF,IAAI,QAAQ,CAAC,MAAM,KAAK,IAAI,IAAI,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"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import type { ReadStream } from 'node:fs';\nimport { v2 as cloudinary, ConfigOptions, UploadApiOptions } from 'cloudinary';\nimport intoStream from 'into-stream';\nimport * as utils from '@strapi/utils';\n\ninterface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport default {\n init(options: ConfigOptions) {\n cloudinary.config(options);\n\n const upload = (file: File, customConfig = {}): Promise<void> => {\n return new Promise((resolve, reject) => {\n const config: Partial<UploadApiOptions> = {\n resource_type: 'auto',\n public_id: file.hash,\n };\n\n if (file.ext) {\n config.filename = `${file.hash}${file.ext}`;\n }\n\n if (file.path) {\n config.folder = file.path;\n }\n\n // For files smaller than 99 MB use regular upload as it tends to be faster\n // and fallback to chunked upload for larger files as that's required by Cloudinary.\n // https://support.cloudinary.com/hc/en-us/community/posts/360009586100-Upload-movie-video-with-large-size?page=1#community_comment_360002140099\n // The Cloudinary's max limit for regular upload is actually 100 MB but add some headroom\n // for size counting shenanigans. (Strapi provides the size in kilobytes rounded to two decimal places here).\n const uploadMethod =\n file.size && file.size < 1000 * 99\n ? cloudinary.uploader.upload_stream\n : cloudinary.uploader.upload_chunked_stream;\n\n const uploadStream = uploadMethod({ ...config, ...customConfig }, (err, image) => {\n if (err) {\n if (err.message.includes('File size too large')) {\n reject(new utils.errors.PayloadTooLargeError());\n } else {\n reject(new Error(`Error uploading to cloudinary: ${err.message}`));\n }\n return;\n }\n\n if (!image) {\n return;\n }\n\n if (image.resource_type === 'video') {\n file.previewUrl = cloudinary.url(`${image.public_id}.gif`, {\n video_sampling: 6,\n delay: 200,\n width: 250,\n crop: 'scale',\n resource_type: 'video',\n });\n }\n\n file.url = image.secure_url;\n file.provider_metadata = {\n public_id: image.public_id,\n resource_type: image.resource_type,\n };\n\n resolve();\n });\n\n if (file.stream) {\n file.stream.pipe(uploadStream);\n } else if (file.buffer) {\n intoStream(file.buffer).pipe(uploadStream);\n } else {\n throw new Error('Missing file stream or buffer');\n }\n });\n };\n\n return {\n uploadStream(file: File, customConfig = {}) {\n return upload(file, customConfig);\n },\n upload(file: File, customConfig = {}) {\n return upload(file, customConfig);\n },\n async delete(file: File, customConfig = {}) {\n try {\n const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};\n const deleteConfig = {\n resource_type: (resourceType || 'image') as string,\n invalidate: true,\n ...customConfig,\n };\n\n const response = await cloudinary.uploader.destroy(`${publicId}`, deleteConfig);\n\n if (response.result !== 'ok' && response.result !== 'not found') {\n throw new Error(response.result);\n }\n } catch (error) {\n if (error instanceof Error) {\n throw new Error(`Error deleting on cloudinary: ${error.message}`);\n }\n\n throw error;\n }\n },\n };\n },\n};\n"],"names":["cloudinary","utils","intoStream"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAe,QAAA;AAAA,EACb,KAAK,SAAwB;AAC3BA,kBAAW,OAAO,OAAO;AAEzB,UAAM,SAAS,CAAC,MAAY,eAAe,CAAA,MAAsB;AAC/D,aAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,cAAM,SAAoC;AAAA,UACxC,eAAe;AAAA,UACf,WAAW,KAAK;AAAA,QAAA;AAGlB,YAAI,KAAK,KAAK;AACZ,iBAAO,WAAW,GAAG,KAAK,IAAI,GAAG,KAAK,GAAG;AAAA,QAC3C;AAEA,YAAI,KAAK,MAAM;AACb,iBAAO,SAAS,KAAK;AAAA,QACvB;AAOM,cAAA,eACJ,KAAK,QAAQ,KAAK,OAAO,MAAO,KAC5BA,WAAAA,GAAW,SAAS,gBACpBA,WAAAA,GAAW,SAAS;AAEpB,cAAA,eAAe,aAAa,EAAE,GAAG,QAAQ,GAAG,aAAa,GAAG,CAAC,KAAK,UAAU;AAChF,cAAI,KAAK;AACP,gBAAI,IAAI,QAAQ,SAAS,qBAAqB,GAAG;AAC/C,qBAAO,IAAIC,iBAAM,OAAO,qBAAsB,CAAA;AAAA,YAAA,OACzC;AACL,qBAAO,IAAI,MAAM,kCAAkC,IAAI,OAAO,EAAE,CAAC;AAAA,YACnE;AACA;AAAA,UACF;AAEA,cAAI,CAAC,OAAO;AACV;AAAA,UACF;AAEI,cAAA,MAAM,kBAAkB,SAAS;AACnC,iBAAK,aAAaD,WAAAA,GAAW,IAAI,GAAG,MAAM,SAAS,QAAQ;AAAA,cACzD,gBAAgB;AAAA,cAChB,OAAO;AAAA,cACP,OAAO;AAAA,cACP,MAAM;AAAA,cACN,eAAe;AAAA,YAAA,CAChB;AAAA,UACH;AAEA,eAAK,MAAM,MAAM;AACjB,eAAK,oBAAoB;AAAA,YACvB,WAAW,MAAM;AAAA,YACjB,eAAe,MAAM;AAAA,UAAA;AAGf;QAAA,CACT;AAED,YAAI,KAAK,QAAQ;AACV,eAAA,OAAO,KAAK,YAAY;AAAA,QAAA,WACpB,KAAK,QAAQ;AACtBE,8BAAAA,QAAW,KAAK,MAAM,EAAE,KAAK,YAAY;AAAA,QAAA,OACpC;AACC,gBAAA,IAAI,MAAM,+BAA+B;AAAA,QACjD;AAAA,MAAA,CACD;AAAA,IAAA;AAGI,WAAA;AAAA,MACL,aAAa,MAAY,eAAe,IAAI;AACnC,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,OAAO,MAAY,eAAe,IAAI;AAC7B,eAAA,OAAO,MAAM,YAAY;AAAA,MAClC;AAAA,MACA,MAAM,OAAO,MAAY,eAAe,IAAI;AACtC,YAAA;AACI,gBAAA,EAAE,eAAe,cAAc,WAAW,aAAa,KAAK,qBAAqB;AACvF,gBAAM,eAAe;AAAA,YACnB,eAAgB,gBAAgB;AAAA,YAChC,YAAY;AAAA,YACZ,GAAG;AAAA,UAAA;AAGC,gBAAA,WAAW,MAAMF,WAAW,GAAA,SAAS,QAAQ,GAAG,QAAQ,IAAI,YAAY;AAE9E,cAAI,SAAS,WAAW,QAAQ,SAAS,WAAW,aAAa;AACzD,kBAAA,IAAI,MAAM,SAAS,MAAM;AAAA,UACjC;AAAA,iBACO,OAAO;AACd,cAAI,iBAAiB,OAAO;AAC1B,kBAAM,IAAI,MAAM,iCAAiC,MAAM,OAAO,EAAE;AAAA,UAClE;AAEM,gBAAA;AAAA,QACR;AAAA,MACF;AAAA,IAAA;AAAA,EAEJ;AACF;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,89 @@
1
+ import { v2 } from "cloudinary";
2
+ import intoStream from "into-stream";
3
+ import * as utils from "@strapi/utils";
4
+ const index = {
5
+ init(options) {
6
+ v2.config(options);
7
+ const upload = (file, customConfig = {}) => {
8
+ return new Promise((resolve, reject) => {
9
+ const config = {
10
+ resource_type: "auto",
11
+ public_id: file.hash
12
+ };
13
+ if (file.ext) {
14
+ config.filename = `${file.hash}${file.ext}`;
15
+ }
16
+ if (file.path) {
17
+ config.folder = file.path;
18
+ }
19
+ const uploadMethod = file.size && file.size < 1e3 * 99 ? v2.uploader.upload_stream : v2.uploader.upload_chunked_stream;
20
+ const uploadStream = uploadMethod({ ...config, ...customConfig }, (err, image) => {
21
+ if (err) {
22
+ if (err.message.includes("File size too large")) {
23
+ reject(new utils.errors.PayloadTooLargeError());
24
+ } else {
25
+ reject(new Error(`Error uploading to cloudinary: ${err.message}`));
26
+ }
27
+ return;
28
+ }
29
+ if (!image) {
30
+ return;
31
+ }
32
+ if (image.resource_type === "video") {
33
+ file.previewUrl = v2.url(`${image.public_id}.gif`, {
34
+ video_sampling: 6,
35
+ delay: 200,
36
+ width: 250,
37
+ crop: "scale",
38
+ resource_type: "video"
39
+ });
40
+ }
41
+ file.url = image.secure_url;
42
+ file.provider_metadata = {
43
+ public_id: image.public_id,
44
+ resource_type: image.resource_type
45
+ };
46
+ resolve();
47
+ });
48
+ if (file.stream) {
49
+ file.stream.pipe(uploadStream);
50
+ } else if (file.buffer) {
51
+ intoStream(file.buffer).pipe(uploadStream);
52
+ } else {
53
+ throw new Error("Missing file stream or buffer");
54
+ }
55
+ });
56
+ };
57
+ return {
58
+ uploadStream(file, customConfig = {}) {
59
+ return upload(file, customConfig);
60
+ },
61
+ upload(file, customConfig = {}) {
62
+ return upload(file, customConfig);
63
+ },
64
+ async delete(file, customConfig = {}) {
65
+ try {
66
+ const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
67
+ const deleteConfig = {
68
+ resource_type: resourceType || "image",
69
+ invalidate: true,
70
+ ...customConfig
71
+ };
72
+ const response = await v2.uploader.destroy(`${publicId}`, deleteConfig);
73
+ if (response.result !== "ok" && response.result !== "not found") {
74
+ throw new Error(response.result);
75
+ }
76
+ } catch (error) {
77
+ if (error instanceof Error) {
78
+ throw new Error(`Error deleting on cloudinary: ${error.message}`);
79
+ }
80
+ throw error;
81
+ }
82
+ }
83
+ };
84
+ }
85
+ };
86
+ export {
87
+ index as default
88
+ };
89
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import type { ReadStream } from 'node:fs';\nimport { v2 as cloudinary, ConfigOptions, UploadApiOptions } from 'cloudinary';\nimport intoStream from 'into-stream';\nimport * as utils from '@strapi/utils';\n\ninterface File {\n name: string;\n alternativeText?: string;\n caption?: string;\n width?: number;\n height?: number;\n formats?: Record<string, unknown>;\n hash: string;\n ext?: string;\n mime: string;\n size: number;\n url: string;\n previewUrl?: string;\n path?: string;\n provider?: string;\n provider_metadata?: Record<string, unknown>;\n stream?: ReadStream;\n buffer?: Buffer;\n}\n\nexport default {\n init(options: ConfigOptions) {\n cloudinary.config(options);\n\n const upload = (file: File, customConfig = {}): Promise<void> => {\n return new Promise((resolve, reject) => {\n const config: Partial<UploadApiOptions> = {\n resource_type: 'auto',\n public_id: file.hash,\n };\n\n if (file.ext) {\n config.filename = `${file.hash}${file.ext}`;\n }\n\n if (file.path) {\n config.folder = file.path;\n }\n\n // For files smaller than 99 MB use regular upload as it tends to be faster\n // and fallback to chunked upload for larger files as that's required by Cloudinary.\n // https://support.cloudinary.com/hc/en-us/community/posts/360009586100-Upload-movie-video-with-large-size?page=1#community_comment_360002140099\n // The Cloudinary's max limit for regular upload is actually 100 MB but add some headroom\n // for size counting shenanigans. (Strapi provides the size in kilobytes rounded to two decimal places here).\n const uploadMethod =\n file.size && file.size < 1000 * 99\n ? cloudinary.uploader.upload_stream\n : cloudinary.uploader.upload_chunked_stream;\n\n const uploadStream = uploadMethod({ ...config, ...customConfig }, (err, image) => {\n if (err) {\n if (err.message.includes('File size too large')) {\n reject(new utils.errors.PayloadTooLargeError());\n } else {\n reject(new Error(`Error uploading to cloudinary: ${err.message}`));\n }\n return;\n }\n\n if (!image) {\n return;\n }\n\n if (image.resource_type === 'video') {\n file.previewUrl = cloudinary.url(`${image.public_id}.gif`, {\n video_sampling: 6,\n delay: 200,\n width: 250,\n crop: 'scale',\n resource_type: 'video',\n });\n }\n\n file.url = image.secure_url;\n file.provider_metadata = {\n public_id: image.public_id,\n resource_type: image.resource_type,\n };\n\n resolve();\n });\n\n if (file.stream) {\n file.stream.pipe(uploadStream);\n } else if (file.buffer) {\n intoStream(file.buffer).pipe(uploadStream);\n } else {\n throw new Error('Missing file stream or buffer');\n }\n });\n };\n\n return {\n uploadStream(file: File, customConfig = {}) {\n return upload(file, customConfig);\n },\n upload(file: File, customConfig = {}) {\n return upload(file, customConfig);\n },\n async delete(file: File, customConfig = {}) {\n try {\n const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};\n const deleteConfig = {\n resource_type: (resourceType || 'image') as string,\n invalidate: true,\n ...customConfig,\n };\n\n const response = await cloudinary.uploader.destroy(`${publicId}`, deleteConfig);\n\n if (response.result !== 'ok' && response.result !== 'not found') {\n throw new Error(response.result);\n }\n } catch (error) {\n if (error instanceof Error) {\n throw new Error(`Error deleting on cloudinary: ${error.message}`);\n }\n\n throw error;\n }\n },\n };\n },\n};\n"],"names":["cloudinary"],"mappings":";;;AAyBA,MAAe,QAAA;AAAA,EACb,KAAK,SAAwB;AAC3BA,OAAW,OAAO,OAAO;AAEzB,UAAM,SAAS,CAAC,MAAY,eAAe,CAAA,MAAsB;AAC/D,aAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,cAAM,SAAoC;AAAA,UACxC,eAAe;AAAA,UACf,WAAW,KAAK;AAAA,QAAA;AAGlB,YAAI,KAAK,KAAK;AACZ,iBAAO,WAAW,GAAG,KAAK,IAAI,GAAG,KAAK,GAAG;AAAA,QAC3C;AAEA,YAAI,KAAK,MAAM;AACb,iBAAO,SAAS,KAAK;AAAA,QACvB;AAOM,cAAA,eACJ,KAAK,QAAQ,KAAK,OAAO,MAAO,KAC5BA,GAAW,SAAS,g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AChC,YAAY;AAAA,YACZ,GAAG;AAAA,UAAA;AAGC,gBAAA,WAAW,MAAMA,GAAW,SAAS,QAAQ,GAAG,QAAQ,IAAI,YAAY;AAE9E,cAAI,SAAS,WAAW,QAAQ,SAAS,WAAW,aAAa;AACzD,kBAAA,IAAI,MAAM,SAAS,MAAM;AAAA,UACjC;AAAA,iBACO,OAAO;AACd,cAAI,iBAAiB,OAAO;AAC1B,kBAAM,IAAI,MAAM,iCAAiC,MAAM,OAAO,EAAE;AAAA,UAClE;AAEM,gBAAA;AAAA,QACR;AAAA,MACF;AAAA,IAAA;AAAA,EAEJ;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-cloudinary",
3
- "version": "4.14.2",
3
+ "version": "4.14.4",
4
4
  "description": "Cloudinary provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -29,30 +29,32 @@
29
29
  }
30
30
  ],
31
31
  "main": "./dist/index.js",
32
+ "module": "./dist/index.mjs",
33
+ "source": "./src/index.ts",
32
34
  "types": "./dist/index.d.ts",
33
35
  "files": [
34
36
  "./dist"
35
37
  ],
36
38
  "scripts": {
37
- "build": "run -T tsc",
38
- "build:ts": "run -T tsc",
39
- "watch": "run -T tsc -w --preserveWatchOutput",
39
+ "build": "pack-up build",
40
40
  "clean": "run -T rimraf ./dist",
41
+ "lint": "run -T eslint .",
41
42
  "prepublishOnly": "yarn clean && yarn build",
42
- "lint": "run -T eslint ."
43
+ "watch": "pack-up watch"
43
44
  },
44
45
  "dependencies": {
45
- "@strapi/utils": "4.14.2",
46
- "cloudinary": "^1.40.0",
46
+ "@strapi/utils": "4.14.4",
47
+ "cloudinary": "^1.41.0",
47
48
  "into-stream": "^5.1.0"
48
49
  },
49
50
  "devDependencies": {
50
- "eslint-config-custom": "4.14.2",
51
- "tsconfig": "4.14.2"
51
+ "@strapi/pack-up": "4.14.4",
52
+ "eslint-config-custom": "4.14.4",
53
+ "tsconfig": "4.14.4"
52
54
  },
53
55
  "engines": {
54
56
  "node": ">=16.0.0 <=20.x.x",
55
57
  "npm": ">=6.0.0"
56
58
  },
57
- "gitHead": "5dbbe10fdf99f63e91b78a0439ec84581f3c9c6a"
59
+ "gitHead": "fbd6e3e3014b1c5eee1d7bb8d65e273318662656"
58
60
  }