Created
May 5, 2019 15:50
-
-
Save fdorantesm/ad3db21747744de28bfd557537244b79 to your computer and use it in GitHub Desktop.
Cloudinary uploader
This file contains 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
import fs from 'fs' | |
import streamifier from 'streamifier' | |
import cloudinary from 'cloudinary' | |
cloudinary.config({ | |
cloud_name: process.env.CLOUDINARY_NAME, | |
api_key: process.env.CLOUDINARY_API_KEY, | |
api_secret: process.env.CLOUDINARY_API_SECRET | |
}); | |
export default class Upload { | |
static imageFromFile(file) { | |
return new Promise((resolve, reject) => { | |
return cloudinary.uploader.upload(file, (err, res) => { | |
if (err) | |
reject(err) | |
else { | |
resolve(res) | |
} | |
}) | |
}) | |
} | |
static image(file) { | |
return new Promise((resolve, reject) => { | |
const stream = cloudinary.v2.uploader.upload_stream({resource_type: 'image'}, (err, res) => { | |
if (err) { | |
reject(err) | |
} | |
else { | |
resolve(res) | |
} | |
}); | |
stream.end(file.data) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment