Skip to content

Instantly share code, notes, and snippets.

@meshboy
Last active December 3, 2018 04:00
Show Gist options
  • Save meshboy/3578ca5e08ff993f5d28a3babfdcea03 to your computer and use it in GitHub Desktop.
Save meshboy/3578ca5e08ff993f5d28a3babfdcea03 to your computer and use it in GitHub Desktop.
import cloudinary from "cloudinary";
import streamToBuffer from "stream-to-buffer";
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
export const uploadFile = async file => {
const { mimetype, stream } = await file;
// process image
return new Promise((resolve, reject) => {
if (!Object.is(mimetype, "image/jpeg")) {
throw new Error("File type not supported");
}
streamToBuffer(stream, (err, buffer) => {
cloudinary.v2.uploader
.upload_stream({ resource_type: "raw" }, (err, result) => {
if (err) {
throw new Error("File not uploaded!");
}
return resolve({ url: result.url });
})
.end(buffer);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment