Last active
August 3, 2023 22:41
-
-
Save eexit/8b547c62dd52e0940f8b7e701f861d99 to your computer and use it in GitHub Desktop.
Upload Ghost local storage images to Cloudinary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Usage: | |
| // $ npm install --loglevel=error cloudinary glob | |
| // $ CLOUDINARY_URL=cloudinary://xxxx node ghost-image-to-cloudinary.js | |
| const cloudinary = require('cloudinary').v2, | |
| path = require('path'), | |
| glob = require('glob'), | |
| basedir = 'content/images/', | |
| gopts = { | |
| use_filename: true, | |
| unique_filename: false, | |
| overwrite: false | |
| }, | |
| getTags = (name) => { | |
| const dpr = name.match(/@(\d)x(?!.*@\dx)/); | |
| return dpr ? ['portfolio', `dpr${dpr[1]}`] : ['portfolio', 'dpr1']; | |
| }, | |
| upload = (f, cb) => { | |
| f = path.parse(f); | |
| f.date = f.dir.replace(basedir, ''); | |
| const fopt = Object.assign({}, gopts, { | |
| public_id: path.join(f.date, f.name), | |
| tags: getTags(f.name) | |
| }); | |
| cloudinary.uploader.upload(path.join(f.dir, f.base), fopt, (err, res) => { | |
| if (err) { throw err; } | |
| console.log(res.url); | |
| cb(); | |
| }); | |
| }; | |
| glob(path.join(basedir, '**/*.jpg'), (err, files) => { | |
| if (err) { throw err; } | |
| let i = 0; | |
| (function(files) { | |
| return upload(files[i], () => { | |
| i++; | |
| if (i < files.length) { | |
| arguments.callee(files); | |
| } | |
| }); | |
| })(files); | |
| }); |
it is really helpful. Thank you guys
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. I found your gist quite useful, as I have a ghost site with thousands of images.
I'd like to make a small change to it. As throwing an error doesn't help me, because it used to happen after a lot of pics from many folders, so restarting the whole process takes a lot of time.
So I modified the script to just show a log and add _duplicate to the file name. That way, when everything's finished, I can manually search in cloudinary for files with duplicate in their name, and deal with them accordingly.