Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* uploadAvatar (request, response) {
var imageFile = request.file('avatar').tmpPath()
// Upload file to Cloudinary
const upload = thunkify(cloudinary.uploader.upload)
try {
const image = yield upload(imageFile)
const loginID = yield request.auth.getUser()
yield UserRepository.saveAvatar(loginID, image.url)
yield request.with({status: 'Avatar has been updated successfully'}).flash()
response.redirect('back')
} catch (e) {
console.log(e)
yield request.with({status: 'Error in updating your avatar '}).flash()
response.redirect('back')
}
}
it('should upload successfully', function (done) {
const expectedUploadConfig = {
"use_filename": true,
"unique_filename": false,
"phash": true,
"overwrite": false,
"invalidate": true,
"folder": "",
"tags": [],
"public_id": "favicon"
};
sinon.stub(cloudinary.uploader, 'upload');
cloudinary.uploader.upload
.withArgs(fixtures.mockImage.path, expectedUploadConfig, sinon.match.any)
.callsArgWith(2, null, fixtures.sampleApiResult());
sinon.stub(cloudinary, 'url').callsFake(function urlStub() {
return 'http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon.png';
});
cloudinaryAdapter.save(fixtures.mockImage).then(function (url) {
expect(url).to.equals('http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon.png');
done();
});
});
it('returns false when image does not exist', function (done) {
cloudinary.uploader.explicit.callsArgWith(2, {error: "error"});
cloudinaryAdapter.exists(fixtures.mockInexistentImage.name).then(function (exists) {
expect(exists).to.equals(false);
done();
});
});
});
it('returns an error when delete image fails', function (done) {
cloudinary.uploader.destroy.callsArgWith(1, {error: "error"});
cloudinaryAdapter.delete(fixtures.mockInexistentImage.name)
.then(function () {
done('expected error');
})
.catch(function (ex) {
expect(ex).to.be.an.instanceOf(common.errors.GhostError);
expect(ex.message).to.equal(`Could not delete image ${fixtures.mockInexistentImage.name}`);
done();
});
});
});
.then(function (url) {
expect(url).equals('https://res.cloudinary.com/blog-mornati-net/image/upload/q_auto:good/favicon.png');
expect(cloudinary.uploader.upload.callCount).to.equal(2, 'cloudinary.uploader.upload should have been called 2 times');
expect(cloudinary.url.callCount).to.equal(1, 'cloudinary.url should have been called 1 time');
done();
});
});
const serverSideCloudinaryUpload = (req, res, next) => {
console.log('\n\nREQ BODY:', req.body);
console.log(req);
console.log('\n\n\n\nREQ.FILES:', req.file, req.files);
const { evt, url, title } = req.body;
var photo = new Photo({ title, url });
console.log('\nUpload Options:', uploadOptions(evt, title));
addNewPhoto(req, res, next);
Cloudinary.uploader
.upload(url, uploadOptions(evt, title))
.then(photo => {
console.log('\n\n\nPHOTO UPLOADER:', photo);
return res.json(photo);
})
.catch(err => {
console.log(`Error uploading photo to Cloudinary:\t${err}`);
next();
});
};
exports.uploadImageNodeToCloudinary = async (node, options) => {
cloudinary.config({
cloud_name: options.cloudName,
api_key: options.apiKey,
api_secret: options.apiSecret,
});
const result = await cloudinary.uploader.upload(node.absolutePath, {
folder: options.uploadFolder,
public_id: node.name,
responsive_breakpoints: [
{
create_derived: true,
bytes_step: 20000,
min_width: DEFAULT_FLUID_MIN_WIDTH,
max_width: DEFAULT_FLUID_MAX_WIDTH,
max_images: 20,
},
],
});
return result;
};
return form.parse(req, (err, fields, files) => cloudinary.uploader
.upload(files.profilePicture.path, {
width: 512,
height: 512,
crop: 'fill',
})
.then(image => req.db
.collection('users')
.updateOne(
{ _id: req.user._id },
{ $set: { profilePicture: image.secure_url } },
))
.then(() => res.send({
status: 'success',
message: 'Profile picture updated successfully',
}))
.catch(error => res.send({
async delete(file) {
try {
const response = await cloudinary.uploader.destroy(file.public_id, {
invalidate: true,
});
if (response.result !== 'ok') {
throw {
error: new Error(response.result),
};
}
} catch (error) {
throw error.error;
}
},
};
return new Promise((resolve, reject) => {
options = options || {
folder: 'caniuse-embed',
public_id: `${feature}-${new Date().getTime()}`
};
cloudinary.uploader.upload_stream(options, (error, result) => {
if (error) reject(error)
else resolve(result);
}).end(screenshot);
});
}