Skip to content

Commit 2957244

Browse files
authored
Merge pull request #6 from colbyfayock/feature/1-uploads
Prevent re-uploading existing images
2 parents 6602201 + 9b9b450 commit 2957244

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
"license": "MIT",
1515
"dependencies": {
1616
"cloudinary": "^1.27.1",
17-
"fs": "^0.0.1-security",
1817
"glob": "^7.2.0",
1918
"jsdom": "^18.1.1",
20-
"path": "^0.12.7"
19+
"node-fetch": "2"
2120
}
2221
}

src/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const glob = require('glob');
21
const fs = require('fs').promises;
32
const path = require('path');
3+
const crypto = require('crypto');
4+
const glob = require('glob');
5+
const fetch = require('node-fetch');
46
const { JSDOM } = require('jsdom');
57
const cloudinary = require('cloudinary').v2;
68

79
/**
810
* TODO
9-
* - Track uploads to avoid uploading same image multiple times
1011
* - Handle srcset
1112
* - Delivery type for redirect via Netlify redirects
1213
*/
@@ -73,22 +74,37 @@ module.exports = {
7374
// local relative path so that we can tell Cloudinary where
7475
// to upload from
7576

77+
const { name: imgName } = path.parse(imgSrc);
78+
let hash = crypto.createHash('md5');
79+
7680
if ( !isRemoteUrl(imgSrc) ) {
7781
imgSrc = path.join(PUBLISH_DIR, imgSrc);
82+
hash.update(imgSrc);
83+
} else {
84+
const response = await fetch(imgSrc);
85+
const buffer = await response.buffer();
86+
hash.update(buffer);
7887
}
7988

89+
hash = hash.digest('hex');
90+
91+
const id = `${imgName}-${hash}`;
92+
8093
let results;
8194

8295
if ( apiKey && apiSecret ) {
8396
// We need an API Key and Secret to use signed uploading
8497

8598
try {
86-
results = await cloudinary.uploader.upload(imgSrc);
99+
results = await cloudinary.uploader.upload(imgSrc, {
100+
public_id: id,
101+
overwrite: false
102+
});
87103
} catch(e) {
88104
const { error } = e;
89105
errors.push({
90106
imgSrc,
91-
message: error.message
107+
message: e.message || error.message
92108
});
93109
continue;
94110
}
@@ -97,12 +113,15 @@ module.exports = {
97113
// however, we need to provide an uploadPreset
98114

99115
try {
100-
results = await cloudinary.uploader.unsigned_upload(imgSrc, uploadPreset);
116+
results = await cloudinary.uploader.unsigned_upload(imgSrc, uploadPreset, {
117+
public_id: id,
118+
// Unsigned uploads default to overwrite: false
119+
});
101120
} catch(e) {
102121
const { error } = e;
103122
errors.push({
104123
imgSrc,
105-
message: error.message
124+
message: e.message || error.message
106125
});
107126
continue;
108127
}

yarn.lock

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ fs.realpath@^1.0.0:
198198
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
199199
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
200200

201-
fs@^0.0.1-security:
202-
version "0.0.1-security"
203-
resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4"
204-
integrity sha1-invTcYa23d84E/I4WLV+yq9eQdQ=
205-
206201
glob@^7.2.0:
207202
version "7.2.0"
208203
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
@@ -259,11 +254,6 @@ inherits@2:
259254
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
260255
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
261256

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-
267257
is-potential-custom-element-name@^1.0.1:
268258
version "1.0.1"
269259
resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz#171ed6f19e3ac554394edf78caa05784a45bebb5"
@@ -339,6 +329,13 @@ [email protected]:
339329
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
340330
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
341331

332+
node-fetch@2:
333+
version "2.6.6"
334+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.6.tgz#1751a7c01834e8e1697758732e9efb6eeadfaf89"
335+
integrity sha512-Z8/6vRlTUChSdIgMa51jxQ4lrw/Jy5SOW10ObaA47/RElsAN2c5Pn8bTgFGWn/ibwzXTE8qwr1Yzx28vsecXEA==
336+
dependencies:
337+
whatwg-url "^5.0.0"
338+
342339
nwsapi@^2.2.0:
343340
version "2.2.0"
344341
resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.0.tgz#204879a9e3d068ff2a55139c2c772780681a38b7"
@@ -373,24 +370,11 @@ path-is-absolute@^1.0.0:
373370
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
374371
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
375372

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-
384373
prelude-ls@~1.1.2:
385374
version "1.1.2"
386375
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
387376
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
388377

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-
394378
psl@^1.1.33:
395379
version "1.8.0"
396380
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
@@ -444,6 +428,11 @@ tr46@^3.0.0:
444428
dependencies:
445429
punycode "^2.1.1"
446430

431+
tr46@~0.0.3:
432+
version "0.0.3"
433+
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
434+
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
435+
447436
type-check@~0.3.2:
448437
version "0.3.2"
449438
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"
@@ -456,13 +445,6 @@ universalify@^0.1.2:
456445
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
457446
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
458447

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-
466448
w3c-hr-time@^1.0.2:
467449
version "1.0.2"
468450
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz#0a89cdf5cc15822df9c360543676963e0cc308cd"
@@ -477,6 +459,11 @@ w3c-xmlserializer@^3.0.0:
477459
dependencies:
478460
xml-name-validator "^4.0.0"
479461

462+
webidl-conversions@^3.0.0:
463+
version "3.0.1"
464+
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
465+
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=
466+
480467
webidl-conversions@^7.0.0:
481468
version "7.0.0"
482469
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a"
@@ -502,6 +489,14 @@ whatwg-url@^10.0.0:
502489
tr46 "^3.0.0"
503490
webidl-conversions "^7.0.0"
504491

492+
whatwg-url@^5.0.0:
493+
version "5.0.0"
494+
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
495+
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
496+
dependencies:
497+
tr46 "~0.0.3"
498+
webidl-conversions "^3.0.0"
499+
505500
word-wrap@~1.2.3:
506501
version "1.2.3"
507502
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"

0 commit comments

Comments
 (0)