@strapi/provider-upload-cloudinary 5.48.0 → 5.49.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.
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +37 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ declare const _default: {
|
|
|
27
27
|
init(options: ConfigOptions): {
|
|
28
28
|
uploadStream(file: File, customConfig?: {}): Promise<void>;
|
|
29
29
|
upload(file: File, customConfig?: {}): Promise<void>;
|
|
30
|
+
replaceStream(newFile: File, oldFile: File, customConfig?: {}): Promise<void>;
|
|
31
|
+
replace(newFile: File, oldFile: File, customConfig?: {}): Promise<void>;
|
|
30
32
|
delete(file: File, customConfig?: {}): Promise<void>;
|
|
31
33
|
};
|
|
32
34
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAoB,aAAa,EAAoB,MAAM,YAAY,CAAC;AAI/E,UAAU,IAAI;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,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;+BAGM,IAAI,WAAW,IAAI;yBAGzB,IAAI,WAAW,IAAI;qBAGjB,IAAI;;;AAvH7B,wBA+IE"}
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,10 @@ var cloudinary = require('cloudinary');
|
|
|
4
4
|
var intoStream = require('into-stream');
|
|
5
5
|
var utils = require('@strapi/utils');
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule) return e;
|
|
8
11
|
var n = Object.create(null);
|
|
9
12
|
if (e) {
|
|
10
13
|
Object.keys(e).forEach(function (k) {
|
|
@@ -21,7 +24,8 @@ function _interopNamespaceDefault(e) {
|
|
|
21
24
|
return Object.freeze(n);
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
var
|
|
27
|
+
var intoStream__default = /*#__PURE__*/_interopDefault(intoStream);
|
|
28
|
+
var utils__namespace = /*#__PURE__*/_interopNamespace(utils);
|
|
25
29
|
|
|
26
30
|
var index = {
|
|
27
31
|
init (options) {
|
|
@@ -78,12 +82,43 @@ var index = {
|
|
|
78
82
|
if (file.stream) {
|
|
79
83
|
file.stream.pipe(uploadStream);
|
|
80
84
|
} else if (file.buffer) {
|
|
81
|
-
|
|
85
|
+
intoStream__default.default(file.buffer).pipe(uploadStream);
|
|
82
86
|
} else {
|
|
83
87
|
throw new Error('Missing file stream or buffer');
|
|
84
88
|
}
|
|
85
89
|
});
|
|
86
90
|
};
|
|
91
|
+
const replace = async (newFile, oldFile, customConfig = {})=>{
|
|
92
|
+
const { public_id: oldPublicId, resource_type: oldResourceType } = oldFile.provider_metadata ?? {};
|
|
93
|
+
// If the public_id is preserved, we can overwrite in place and invalidate
|
|
94
|
+
// the CDN cache in a single call.
|
|
95
|
+
if (oldPublicId && newFile.hash === oldPublicId) {
|
|
96
|
+
return upload(newFile, {
|
|
97
|
+
public_id: oldPublicId,
|
|
98
|
+
overwrite: true,
|
|
99
|
+
invalidate: true,
|
|
100
|
+
...oldResourceType ? {
|
|
101
|
+
resource_type: oldResourceType
|
|
102
|
+
} : {},
|
|
103
|
+
...customConfig
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// The public_id differs — upload the new file first, then destroy the old.
|
|
107
|
+
await upload(newFile, customConfig);
|
|
108
|
+
if (oldPublicId) {
|
|
109
|
+
try {
|
|
110
|
+
await cloudinary.v2.uploader.destroy(`${oldPublicId}`, {
|
|
111
|
+
resource_type: oldResourceType || 'image',
|
|
112
|
+
invalidate: true
|
|
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
|
+
};
|
|
87
122
|
return {
|
|
88
123
|
uploadStream (file, customConfig = {}) {
|
|
89
124
|
return upload(file, customConfig);
|
|
@@ -91,6 +126,12 @@ var index = {
|
|
|
91
126
|
upload (file, customConfig = {}) {
|
|
92
127
|
return upload(file, customConfig);
|
|
93
128
|
},
|
|
129
|
+
replaceStream (newFile, oldFile, customConfig = {}) {
|
|
130
|
+
return replace(newFile, oldFile, customConfig);
|
|
131
|
+
},
|
|
132
|
+
replace (newFile, oldFile, customConfig = {}) {
|
|
133
|
+
return replace(newFile, oldFile, customConfig);
|
|
134
|
+
},
|
|
94
135
|
async delete (file, customConfig = {}) {
|
|
95
136
|
try {
|
|
96
137
|
const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 sizeInBytes: 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":["init","options","cloudinary","config","upload","file","customConfig","Promise","resolve","reject","resource_type","public_id","hash","ext","filename","path","folder","uploadMethod","size","uploader","upload_stream","upload_chunked_stream","uploadStream","err","image","message","includes","utils","errors","PayloadTooLargeError","Error","previewUrl","url","video_sampling","delay","width","crop","secure_url","provider_metadata","stream","pipe","buffer","intoStream","delete","resourceType","publicId","deleteConfig","invalidate","response","destroy","result","error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,YAAe;AACbA,IAAAA,IAAAA,CAAAA,CAAKC,OAAsB,EAAA;AACzBC,QAAAA,aAAAA,CAAWC,MAAM,CAACF,OAAAA,CAAAA;AAElB,QAAA,MAAMG,MAAAA,GAAS,CAACC,IAAAA,EAAYC,YAAAA,GAAe,EAAE,GAAA;YAC3C,OAAO,IAAIC,OAAAA,CAAQ,CAACC,OAAAA,EAASC,MAAAA,GAAAA;AAC3B,gBAAA,MAAMN,MAAAA,GAAoC;oBACxCO,aAAAA,EAAe,MAAA;AACfC,oBAAAA,SAAAA,EAAWN,KAAKO;AAClB,iBAAA;gBAEA,IAAIP,IAAAA,CAAKQ,GAAG,EAAE;oBACZV,MAAAA,CAAOW,QAAQ,GAAG,CAAA,EAAGT,IAAAA,CAAKO,IAAI,CAAA,EAAGP,IAAAA,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BpC,wBAAAA,aAAAA,EAAgBkC,YAAAA,IAAgB,OAAA;wBAChCG,UAAAA,EAAY,IAAA;AACZ,wBAAA,GAAGzC;AACL,qBAAA;oBAEA,MAAM0C,QAAAA,GAAW,MAAM9C,aAAAA,CAAWiB,QAAQ,CAAC8B,OAAO,CAAC,CAAA,EAAGJ,QAAAA,CAAAA,CAAU,EAAEC,YAAAA,CAAAA;AAElE,oBAAA,IAAIE,SAASE,MAAM,KAAK,QAAQF,QAAAA,CAASE,MAAM,KAAK,WAAA,EAAa;wBAC/D,MAAM,IAAIpB,KAAAA,CAAMkB,QAAAA,CAASE,MAAM,CAAA;AACjC,oBAAA;AACF,gBAAA,CAAA,CAAE,OAAOC,KAAAA,EAAO;AACd,oBAAA,IAAIA,iBAAiBrB,KAAAA,EAAO;AAC1B,wBAAA,MAAM,IAAIA,KAAAA,CAAM,CAAC,8BAA8B,EAAEqB,KAAAA,CAAM1B,OAAO,CAAA,CAAE,CAAA;AAClE,oBAAA;oBAEA,MAAM0B,KAAAA;AACR,gBAAA;AACF,YAAA;AACF,SAAA;AACF,IAAA;AACF,CAAA;;;;"}
|
|
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 sizeInBytes: 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 const replace = async (newFile: File, oldFile: File, customConfig = {}): Promise<void> => {\n const { public_id: oldPublicId, resource_type: oldResourceType } =\n oldFile.provider_metadata ?? {};\n\n // If the public_id is preserved, we can overwrite in place and invalidate\n // the CDN cache in a single call.\n if (oldPublicId && newFile.hash === oldPublicId) {\n return upload(newFile, {\n public_id: oldPublicId as string,\n overwrite: true,\n invalidate: true,\n ...(oldResourceType ? { resource_type: oldResourceType } : {}),\n ...customConfig,\n });\n }\n\n // The public_id differs — upload the new file first, then destroy the old.\n await upload(newFile, customConfig);\n\n if (oldPublicId) {\n try {\n await cloudinary.uploader.destroy(`${oldPublicId}`, {\n resource_type: (oldResourceType || 'image') as string,\n invalidate: true,\n });\n } catch (error) {\n if (error instanceof Error) {\n throw new Error(`Error deleting on cloudinary: ${error.message}`);\n }\n throw error;\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 replaceStream(newFile: File, oldFile: File, customConfig = {}) {\n return replace(newFile, oldFile, customConfig);\n },\n replace(newFile: File, oldFile: File, customConfig = {}) {\n return replace(newFile, oldFile, 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":["init","options","cloudinary","config","upload","file","customConfig","Promise","resolve","reject","resource_type","public_id","hash","ext","filename","path","folder","uploadMethod","size","uploader","upload_stream","upload_chunked_stream","uploadStream","err","image","message","includes","utils","errors","PayloadTooLargeError","Error","previewUrl","url","video_sampling","delay","width","crop","secure_url","provider_metadata","stream","pipe","buffer","intoStream","replace","newFile","oldFile","oldPublicId","oldResourceType","overwrite","invalidate","destroy","error","replaceStream","delete","resourceType","publicId","deleteConfig","response","result"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,YAAe;AACbA,IAAAA,IAAAA,CAAAA,CAAKC,OAAsB,EAAA;AACzBC,QAAAA,aAAAA,CAAWC,MAAM,CAACF,OAAAA,CAAAA;AAElB,QAAA,MAAMG,MAAAA,GAAS,CAACC,IAAAA,EAAYC,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IAAA;AACF,CAAA;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -63,6 +63,37 @@ var index = {
|
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
65
|
};
|
|
66
|
+
const replace = async (newFile, oldFile, customConfig = {})=>{
|
|
67
|
+
const { public_id: oldPublicId, resource_type: oldResourceType } = oldFile.provider_metadata ?? {};
|
|
68
|
+
// If the public_id is preserved, we can overwrite in place and invalidate
|
|
69
|
+
// the CDN cache in a single call.
|
|
70
|
+
if (oldPublicId && newFile.hash === oldPublicId) {
|
|
71
|
+
return upload(newFile, {
|
|
72
|
+
public_id: oldPublicId,
|
|
73
|
+
overwrite: true,
|
|
74
|
+
invalidate: true,
|
|
75
|
+
...oldResourceType ? {
|
|
76
|
+
resource_type: oldResourceType
|
|
77
|
+
} : {},
|
|
78
|
+
...customConfig
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
// The public_id differs — upload the new file first, then destroy the old.
|
|
82
|
+
await upload(newFile, customConfig);
|
|
83
|
+
if (oldPublicId) {
|
|
84
|
+
try {
|
|
85
|
+
await v2.uploader.destroy(`${oldPublicId}`, {
|
|
86
|
+
resource_type: oldResourceType || 'image',
|
|
87
|
+
invalidate: true
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
throw new Error(`Error deleting on cloudinary: ${error.message}`);
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
66
97
|
return {
|
|
67
98
|
uploadStream (file, customConfig = {}) {
|
|
68
99
|
return upload(file, customConfig);
|
|
@@ -70,6 +101,12 @@ var index = {
|
|
|
70
101
|
upload (file, customConfig = {}) {
|
|
71
102
|
return upload(file, customConfig);
|
|
72
103
|
},
|
|
104
|
+
replaceStream (newFile, oldFile, customConfig = {}) {
|
|
105
|
+
return replace(newFile, oldFile, customConfig);
|
|
106
|
+
},
|
|
107
|
+
replace (newFile, oldFile, customConfig = {}) {
|
|
108
|
+
return replace(newFile, oldFile, customConfig);
|
|
109
|
+
},
|
|
73
110
|
async delete (file, customConfig = {}) {
|
|
74
111
|
try {
|
|
75
112
|
const { resource_type: resourceType, public_id: publicId } = file.provider_metadata ?? {};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +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 sizeInBytes: 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":["init","options","cloudinary","config","upload","file","customConfig","Promise","resolve","reject","resource_type","public_id","hash","ext","filename","path","folder","uploadMethod","size","uploader","upload_stream","upload_chunked_stream","uploadStream","err","image","message","includes","utils","errors","PayloadTooLargeError","Error","previewUrl","url","video_sampling","delay","width","crop","secure_url","provider_metadata","stream","pipe","buffer","intoStream","delete","resourceType","publicId","deleteConfig","invalidate","response","destroy","result","error"],"mappings":";;;;AA0BA,YAAe;AACbA,IAAAA,IAAAA,CAAAA,CAAKC,OAAsB,EAAA;AACzBC,QAAAA,EAAAA,CAAWC,MAAM,CAACF,OAAAA,CAAAA;AAElB,QAAA,MAAMG,MAAAA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AU,EAAEC,YAAAA,GAAe,EAAE,EAAA;AAClC,gBAAA,OAAOF,OAAOC,IAAAA,EAAMC,YAAAA,CAAAA;AACtB,YAAA,CAAA;AACA,YAAA,MAAMqC,MAAAA,CAAAA,CAAOtC,IAAU,EAAEC,YAAAA,GAAe,EAAE,EAAA;gBACxC,IAAI;oBACF,MAAM,EAAEI,aAAAA,EAAekC,YAAY,EAAEjC,SAAAA,EAAWkC,QAAQ,EAAE,GAAGxC,IAAAA,CAAKiC,iBAAiB,IAAI,EAAC;AACxF,oBAAA,MAAMQ,YAAAA,GAAe;AACnBpC,wBAAAA,aAAAA,EAAgBkC,YAAAA,IAAgB,OAAA;wBAChCG,UAAAA,EAAY,IAAA;AACZ,wBAAA,GAAGzC;AACL,qBAAA;oBAEA,MAAM0C,QAAAA,GAAW,MAAM9C,EAAAA,CAAWiB,QAAQ,CAAC8B,OAAO,CAAC,CAAA,EAAGJ,QAAAA,CAAAA,CAAU,EAAEC,YAAAA,CAAAA;AAElE,oBAAA,IAAIE,SAASE,MAAM,KAAK,QAAQF,QAAAA,CAASE,MAAM,KAAK,WAAA,EAAa;wBAC/D,MAAM,IAAIpB,KAAAA,CAAMkB,QAAAA,CAASE,MAAM,CAAA;AACjC,oBAAA;AACF,gBAAA,CAAA,CAAE,OAAOC,KAAAA,EAAO;AACd,oBAAA,IAAIA,iBAAiBrB,KAAAA,EAAO;AAC1B,wBAAA,MAAM,IAAIA,KAAAA,CAAM,CAAC,8BAA8B,EAAEqB,KAAAA,CAAM1B,OAAO,CAAA,CAAE,CAAA;AAClE,oBAAA;oBAEA,MAAM0B,KAAAA;AACR,gBAAA;AACF,YAAA;AACF,SAAA;AACF,IAAA;AACF,CAAA;;;;"}
|
|
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 sizeInBytes: 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 const replace = async (newFile: File, oldFile: File, customConfig = {}): Promise<void> => {\n const { public_id: oldPublicId, resource_type: oldResourceType } =\n oldFile.provider_metadata ?? {};\n\n // If the public_id is preserved, we can overwrite in place and invalidate\n // the CDN cache in a single call.\n if (oldPublicId && newFile.hash === oldPublicId) {\n return upload(newFile, {\n public_id: oldPublicId as string,\n overwrite: true,\n invalidate: true,\n ...(oldResourceType ? { resource_type: oldResourceType } : {}),\n ...customConfig,\n });\n }\n\n // The public_id differs — upload the new file first, then destroy the old.\n await upload(newFile, customConfig);\n\n if (oldPublicId) {\n try {\n await cloudinary.uploader.destroy(`${oldPublicId}`, {\n resource_type: (oldResourceType || 'image') as string,\n invalidate: true,\n });\n } catch (error) {\n if (error instanceof Error) {\n throw new Error(`Error deleting on cloudinary: ${error.message}`);\n }\n throw error;\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 replaceStream(newFile: File, oldFile: File, customConfig = {}) {\n return replace(newFile, oldFile, customConfig);\n },\n replace(newFile: File, oldFile: File, customConfig = {}) {\n return replace(newFile, oldFile, 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":["init","options","cloudinary","config","upload","file","customConfig","Promise","resolve","reject","resource_type","public_id","hash","ext","filename","path","folder","uploadMethod","size","uploader","upload_stream","upload_chunked_stream","uploadStream","err","image","message","includes","utils","errors","PayloadTooLargeError","Error","previewUrl","url","video_sampling","delay","width","crop","secure_url","provider_metadata","stream","pipe","buffer","intoStream","replace","newFile","oldFile","oldPublicId","oldResourceType","overwrite","invalidate","destroy","error","replaceStream","delete","resourceType","publicId","deleteConfig","response","result"],"mappings":";;;;AA0BA,YAAe;AACbA,IAAAA,IAAAA,CAAAA,CAAKC,OAAsB,EAAA;AACzBC,QAAAA,EAAAA,CAAWC,MAAM,CAACF,OAAAA,CAAAA;AAElB,QAAA,MAAMG,MAAAA,GAAS,CAACC,IAAAA,EAAYC,YAAAA,GAAe,EAAE,GAAA;YAC3C,OAAO,IAAIC,OAAAA,CAAQ,CAACC,OAAAA,EAASC,MAAAA,GAAAA;AAC3B,gBAAA,MAAMN,MAAAA,GAAoC;oBACxCO,aAAAA,EAAe,MAAA;AACfC,oBAAAA,SAAAA,EAAWN,KAAKO;AAClB,iBAAA;gBAEA,IAAIP,IAAAA,CAAKQ,GAAG,EAAE;oBACZV,MAAAA,CAAOW,QAAQ,GAAG,CAAA,EAAGT,IAAAA,CAAKO,IAAI,CAAA,EAAGP,IAAAA,CAAKQ,GAAG,CAAA,CAAE;AAC7C,gBAAA;gBAEA,IAAIR,IAAAA,CAAKU,IAAI,EAAE;oBACbZ,MAAAA,CAAOa,MAAM,GAAGX,IAAAA,CAAKU,IAAI;AAC3B,gBAAA;;;;;;AAOA,gBAAA,MAAME,eACJZ,IAAAA,CAAKa,IAAI,IAAIb,IAAAA,CAAKa,IAAI,GAAG,IAAA,GAAO,EAAA,GAC5BhB,EAAAA,CAAWiB,QAAQ,CAACC,aAAa,GACjClB,EAAAA,CAAWiB,QAAQ,CAACE,qBAAqB;AAE/C,gBAAA,MAAMC,eAAeL,YAAAA,CAAa;AAAE,oBAAA,GAAGd,MAAM;AAAE,oBAAA,GAAGG;AAAa,iBAAA,EAAG,CAACiB,GAAAA,EAAKC,KAAAA,GAAAA;AACtE,oBAAA,IAAID,GAAAA,EAAK;AACP,wBAAA,IAAIA,GAAAA,CAAIE,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MAAM,IAAI5B,KAAAA,CAAM2B,QAAAA,CAASC,MAAM,CAAA;AACjC,oBAAA;AACF,gBAAA,CAAA,CAAE,OAAOP,KAAAA,EAAO;AACd,oBAAA,IAAIA,iBAAiBrB,KAAAA,EAAO;AAC1B,wBAAA,MAAM,IAAIA,KAAAA,CAAM,CAAC,8BAA8B,EAAEqB,KAAAA,CAAM1B,OAAO,CAAA,CAAE,CAAA;AAClE,oBAAA;oBAEA,MAAM0B,KAAAA;AACR,gBAAA;AACF,YAAA;AACF,SAAA;AACF,IAAA;AACF,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/provider-upload-cloudinary",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.49.0",
|
|
4
4
|
"description": "Cloudinary provider for strapi upload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"upload",
|
|
@@ -45,16 +45,16 @@
|
|
|
45
45
|
"watch": "run -T rollup -c -w"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@strapi/utils": "5.
|
|
48
|
+
"@strapi/utils": "5.49.0",
|
|
49
49
|
"cloudinary": "^2.7.0",
|
|
50
50
|
"into-stream": "^5.1.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"eslint-config-custom": "5.
|
|
54
|
-
"tsconfig": "5.
|
|
53
|
+
"eslint-config-custom": "5.49.0",
|
|
54
|
+
"tsconfig": "5.49.0"
|
|
55
55
|
},
|
|
56
56
|
"engines": {
|
|
57
|
-
"node": ">=20.0.0 <=
|
|
57
|
+
"node": ">=20.0.0 <=26.x.x",
|
|
58
58
|
"npm": ">=6.0.0"
|
|
59
59
|
}
|
|
60
60
|
}
|