0

I'm using Node.js and Express. I just implement Cloudinary to upload image from user, and store the image id to my MySQL database.

I tried this tutorial from Cloudinary's documentation: http://cloudinary.com/documentation/node_image_upload. Everything works well, except at the final step, I need to get the image_id.

  var url = require('url')  
  var url_parts = url.parse(req.body.coverurl, true)
  console.log(url_parts) //DEBUG
  var query = url_parts.query
  console.log(query) //DEBUG

  var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id)
  } else {
    console.log("Invalid upload signature")
  }

The req.body.coverurl is something like: image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862 The url_parts is:

{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: '#f63f1fe77c7f536447e079f3dd10829b5a15d862',
  search: '',
  query: {},
  pathname: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
  path: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
  href: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862' }

And..., variable query is empty. query.image_id is invalid so can't get the image id, also can't verify the signature.

I don't know why Cloudinary give this invalid callback string. Or how can I solve this problem.

Thank for your help. And forgive me for my bad English :)

Update

my submit_idea.hbs

<form action="/submit/idea" method="POST">
    {{{imgtag}}} <!-- this will render from: cloudinary.uploader.image_upload_tag('coverurl') -->
    <button type="submit">Save</button>
</form>

in my routes/submit.js

router.get('/submit/idea', function(req, res, next){
    // just render the submit_idea.hbs with some Cloudinary variable
})

router.post('/submit/idea', function(req, res, next){
  var url = require('url')  
  var url_parts = url.parse(req.body.coverurl, true)
  var query = url_parts.query

  var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id)
  } else {
    console.log("Invalid upload signature")
  }

  // add image_id to mysql
})
Stephen
  • 45
  • 6
  • why are you using req.body.coverurl instead of req.url? – Yalamber Mar 22 '15 at 03:06
  • I have updated the question. I think req.url is just get something like `/submit/idea`; not the identifier of the uploaded image. – Stephen Mar 22 '15 at 04:36
  • I guess what you are looking for is public_id. this in your example f08eodnwt5zqfwb2bgpd. I guess you can extract it from path in url_parts and then try to verify it. – Yalamber Mar 22 '15 at 09:21

2 Answers2

0

I guess the docs in cloudinary site is wrong or outdated. Try verifying your upload as below. It should work.

router.post('/submit/idea', function(req, res, next){
  /*REMOVE THESE PARTS. I guess these are not needed any more. 
    var url = require('url')  
    var url_parts = url.parse(req.body.coverurl, true)
    var query = url_parts.query
   */
  //Directy pass coverurl to PreloadedFile method. 
  var preloaded_file = new cloudinary.PreloadedFile(req.body.coverurl)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id);
    // add image_id to mysql
  } else {
    console.log("Invalid upload signature")
  }
})
Yalamber
  • 7,360
  • 15
  • 63
  • 89
  • Oh yeah! You are right. The documentation is outdated. Using your idea, I read the [source code](https://github.com/cloudinary/cloudinary_npm/blob/master/lib/preloaded_file.js). So I find out just need to use PreloadedFile method and we can get the `public_id`. So cool. Thanks for your help. :) – Stephen Mar 22 '15 at 11:25
  • Great it worked for you. Also I think you should add image_id to mysql inside if(preloaded_file.is_valid())){ ....here....} – Yalamber Mar 22 '15 at 13:50
0

Yalamber got it right. Our documentation is out of date!

Because the form uses POST and not GET, the image_id field is provided in the body of the request rather than the URL query parameters.

You can see the correct behaviour in our sample project photo_album, specifically in create_direct() (abridged below):

function create_direct(req,res){
  var photo = new Photo(req.body);
  var image = new cloudinary.PreloadedFile(req.body.image_id);
  if (image.is_valid()){
    photo.image = image.toJSON();
  }
  photo.save().then(function(photo){
    console.log('** photo saved');
  }).catch(function(err){
    console.dir(err);
  }).finally(function(){
    res.render('photos/create_direct',{photo:photo,upload:photo.image});
  });
}

Thanks for noticing! We will update our documentation very soon.

-- Cloudinary

Community
  • 1
  • 1
tocker
  • 1,752
  • 1
  • 11
  • 9