Skip to content

Commit ce81639

Browse files
committed
adding different delivery types for images
1 parent 8ce17f0 commit ce81639

File tree

4 files changed

+166
-14
lines changed

4 files changed

+166
-14
lines changed

manifest.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
name: netlify-plugin-cloudinary
1+
name: netlify-plugin-cloudinary
2+
inputs:
3+
- name: deliveryType
4+
required: false
5+
description: "The method in which Cloudinary stores and delivers images (Ex: fetch, upload)"
6+
default: "fetch"
7+
- name: uploadPreset
8+
required: false
9+
description: "Defined set of asset upload defaults in Cloudinary"

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-plugin-cloudinary",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "",
55
"main": "src/index.js",
66
"scripts": {
@@ -13,8 +13,10 @@
1313
},
1414
"license": "MIT",
1515
"dependencies": {
16+
"cloudinary": "^1.27.1",
1617
"fs": "^0.0.1-security",
1718
"glob": "^7.2.0",
18-
"jsdom": "^18.1.1"
19+
"jsdom": "^18.1.1",
20+
"path": "^0.12.7"
1921
}
2022
}

src/index.js

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,95 @@
11
const glob = require('glob');
22
const fs = require('fs').promises;
3+
const path = require('path');
34
const { JSDOM } = require('jsdom');
5+
const cloudinary = require('cloudinary').v2;
46

5-
const CLOUD_NAME = 'colbycloud';
7+
/**
8+
* TODO
9+
* - Track uploads to avoid uploading same image multiple times
10+
*/
611

712
module.exports = {
813

9-
async onPostBuild({ constants }) {
14+
async onPostBuild({ constants, inputs }) {
1015
const { PUBLISH_DIR } = constants;
16+
const { deliveryType, uploadPreset } = inputs;
1117

12-
const pages = glob.sync(`${PUBLISH_DIR}/**/*.html`);
18+
const cloudName = process.env.CLOUDINARY_CLOUD_NAME;
19+
const apiKey = process.env.CLOUDINARY_API_KEY;
20+
const apiSecret = process.env.CLOUDINARY_API_SECRET;
21+
22+
if ( !cloudName ) {
23+
throw new Error('Cloudinary Cloud Name required. Please use environment variable CLOUDINARY_CLOUD_NAME');
24+
}
1325

26+
cloudinary.config({
27+
cloud_name: cloudName,
28+
api_key: apiKey,
29+
api_secret: apiSecret
30+
});
31+
32+
const pages = glob.sync(`${PUBLISH_DIR}/**/*.html`);
1433

1534
for ( const page of pages ) {
1635
const html = await fs.readFile(page, 'utf-8');
1736
const dom = new JSDOM(html);
1837
const images = Array.from(dom.window.document.querySelectorAll('img'));
1938

2039
for ( const $img of images ) {
21-
const imgSrc = $img.getAttribute('src');
22-
let cloudinarySrc = imgSrc
40+
let imgSrc = $img.getAttribute('src');
41+
let cloudinarySrc;
42+
43+
if ( deliveryType === 'fetch' ) {
44+
imgSrc = determineRemoteUrl(imgSrc);
2345

24-
if ( !imgSrc.startsWith('http') ) {
25-
if ( !imgSrc.startsWith('/') ) {
26-
cloudinarySrc = `/${cloudinarySrc}`;
46+
cloudinarySrc = cloudinary.url(imgSrc, {
47+
type: deliveryType,
48+
secure: true,
49+
transformation: [
50+
{
51+
fetch_format: 'auto',
52+
quality: 'auto'
53+
}
54+
]
55+
});
56+
} else if ( deliveryType === 'upload' ) {
57+
58+
if ( !isRemoteUrl(imgSrc) ) {
59+
imgSrc = path.join(PUBLISH_DIR, imgSrc);
2760
}
28-
cloudinarySrc = `${process.env.DEPLOY_PRIME_URL}${cloudinarySrc}`;
29-
}
3061

31-
cloudinarySrc = `https://res.cloudinary.com/${CLOUD_NAME}/image/fetch/${cloudinarySrc}`;
62+
let results;
63+
64+
if ( apiKey && apiSecret ) {
65+
try {
66+
results = await cloudinary.uploader.upload(imgSrc);
67+
} catch(e) {
68+
console.log('e', e)
69+
}
70+
} else if ( uploadPreset ) {
71+
try {
72+
results = await cloudinary.uploader.unsigned_upload(imgSrc, uploadPreset);
73+
} catch(e) {
74+
console.log('e', e)
75+
}
76+
} else {
77+
throw new Error(`To use deliveryType ${deliveryType}, please use an uploadPreset for unsigned requests or an API Key and Secret for signed requests.`);
78+
}
79+
80+
const { public_id } = results;
81+
82+
cloudinarySrc = cloudinary.url(public_id, {
83+
secure: true,
84+
transformation: [
85+
{
86+
fetch_format: 'auto',
87+
quality: 'auto'
88+
}
89+
]
90+
});
91+
92+
}
3293

3394
$img.setAttribute('src', cloudinarySrc)
3495

@@ -37,4 +98,30 @@ module.exports = {
3798
}
3899
}
39100

101+
}
102+
103+
/**
104+
* isRemoteUrl
105+
*/
106+
107+
function isRemoteUrl(path) {
108+
return path.startsWith('http');
109+
}
110+
111+
/**
112+
* determineRemoteUrl
113+
*/
114+
115+
function determineRemoteUrl(path) {
116+
if ( isRemoteUrl(path) ) return path;
117+
118+
let url = path;
119+
120+
if ( !path.startsWith('/') ) {
121+
url = `/${url}`;
122+
}
123+
124+
url = `${process.env.DEPLOY_PRIME_URL}${url}`;
125+
126+
return url;
40127
}

yarn.lock

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ browser-process-hrtime@^1.0.0:
6565
resolved "https://registry.yarnpkg.com/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz#3c9b4b7d782c8121e56f10106d84c0d0ffc94626"
6666
integrity sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==
6767

68+
cloudinary-core@^2.10.2:
69+
version "2.12.0"
70+
resolved "https://registry.yarnpkg.com/cloudinary-core/-/cloudinary-core-2.12.0.tgz#fe20231e7bf6a52116c61a2a03c04fcc60b968a3"
71+
integrity sha512-EXvnWuq3Ah6pZHB2vEfkrJ4tCGlccZo3xsmt1f+bmrB53jU42ZrnX+v8xKRMot9c/STSO7GPbxbhJQ/z/xRazg==
72+
73+
cloudinary@^1.27.1:
74+
version "1.27.1"
75+
resolved "https://registry.yarnpkg.com/cloudinary/-/cloudinary-1.27.1.tgz#41abace12827dd1edc06ff511d8da2e261b3845f"
76+
integrity sha512-NrSVdzD2yJUMwL4UTfOjlq+bkc88SaNoZjhibLnpI40c46vfIlz2Y77R9HfgYIQ1Lx/k+U1WKZuiQ5Z2Wd2Dsg==
77+
dependencies:
78+
cloudinary-core "^2.10.2"
79+
core-js "3.6.5"
80+
lodash "^4.17.11"
81+
q "^1.5.1"
82+
6883
combined-stream@^1.0.8:
6984
version "1.0.8"
7085
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
@@ -77,6 +92,11 @@ [email protected]:
7792
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
7893
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
7994

95+
96+
version "3.6.5"
97+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.5.tgz#7395dc273af37fb2e50e9bd3d9fe841285231d1a"
98+
integrity sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA==
99+
80100
cssom@^0.5.0:
81101
version "0.5.0"
82102
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.5.0.tgz#d254fa92cd8b6fbd83811b9fbaed34663cc17c36"
@@ -239,6 +259,11 @@ inherits@2:
239259
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
240260
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
241261

262+
263+
version "2.0.3"
264+
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
265+
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
266+
242267
is-potential-custom-element-name@^1.0.1:
243268
version "1.0.1"
244269
resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz#171ed6f19e3ac554394edf78caa05784a45bebb5"
@@ -285,6 +310,11 @@ levn@~0.3.0:
285310
prelude-ls "~1.1.2"
286311
type-check "~0.3.2"
287312

313+
lodash@^4.17.11:
314+
version "4.17.21"
315+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
316+
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
317+
288318
289319
version "1.51.0"
290320
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.51.0.tgz#d9ff62451859b18342d960850dc3cfb77e63fb0c"
@@ -343,11 +373,24 @@ path-is-absolute@^1.0.0:
343373
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
344374
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
345375

376+
path@^0.12.7:
377+
version "0.12.7"
378+
resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f"
379+
integrity sha1-1NwqUGxM4hl+tIHr/NWzbAFAsQ8=
380+
dependencies:
381+
process "^0.11.1"
382+
util "^0.10.3"
383+
346384
prelude-ls@~1.1.2:
347385
version "1.1.2"
348386
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
349387
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
350388

389+
process@^0.11.1:
390+
version "0.11.10"
391+
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
392+
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=
393+
351394
psl@^1.1.33:
352395
version "1.8.0"
353396
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
@@ -358,6 +401,11 @@ punycode@^2.1.1:
358401
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
359402
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
360403

404+
q@^1.5.1:
405+
version "1.5.1"
406+
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
407+
integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=
408+
361409
"safer-buffer@>= 2.1.2 < 3.0.0":
362410
version "2.1.2"
363411
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
@@ -408,6 +456,13 @@ universalify@^0.1.2:
408456
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
409457
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
410458

459+
util@^0.10.3:
460+
version "0.10.4"
461+
resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901"
462+
integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==
463+
dependencies:
464+
inherits "2.0.3"
465+
411466
w3c-hr-time@^1.0.2:
412467
version "1.0.2"
413468
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz#0a89cdf5cc15822df9c360543676963e0cc308cd"

0 commit comments

Comments
 (0)