flickr / flickr-sdk

Almost certainly the best Flickr API client in the world for node and the browser
https://www.flickr.com/services/api
MIT License
131 stars 29 forks source link

Error when passing Buffer as file argument to Upload #135

Closed blakecallens closed 3 years ago

blakecallens commented 4 years ago

When passing a Buffer of a jpg or png file to Upload, xml2js fails to parse the API response and the following error is thrown:

Non-whitespace before first tag.
Line: 0
Column: 1
Char: o
    SyntaxError: Non-whitespace before first tag.
    Line: 0
    Column: 1
    Char: o
        at /home/user/www/webapp/node_modules/flickr-sdk/plugins/xml.js:39:15
        at Parser.<anonymous> (/home/user/www/webapp/node_modules/xml2js/lib/parser.js:308:18)
        at Parser.emit (events.js:198:13)
        at SAXParser.onerror (/home/user/www/webapp/node_modules/xml2js/lib/parser.js:123:26)
        at emit (/home/user/www/webapp/node_modules/sax/lib/sax.js:624:35)
        at error (/home/user/www/webapp/node_modules/sax/lib/sax.js:653:5)
        at strictFail (/home/user/www/webapp/node_modules/sax/lib/sax.js:677:7)
        at beginWhiteSpace (/home/user/www/webapp/node_modules/sax/lib/sax.js:951:7)
        at SAXParser.write (/home/user/www/webapp/node_modules/sax/lib/sax.js:1006:11)
        at Parser.exports.Parser.Parser.parseString (/home/user/www/webapp/node_modules/xml2js/lib/parser.js:323:31)
        at Parser.parseString (/home/user/www/webapp/node_modules/xml2js/lib/parser.js:5:59)
        at Object.exports.parseString (/home/user/www/webapp/node_modules/xml2js/lib/parser.js:369:19)
        at IncomingMessage.<anonymous> (/home/user/www/webapp/node_modules/flickr-sdk/plugins/xml.js:31:10)
        at IncomingMessage.emit (events.js:203:15)
        at endReadableNT (_stream_readable.js:1145:12)
        at process._tickCallback (internal/process/next_tick.js:63:19)

Call to Upload from my code:

async uploadPhoto (user, buffer) {
  try {
    if (_.isEmpty(user.flickrAuth)) {
      return;
    }

    await new Flickr.Upload(
      this.oauth.plugin(user.flickrAuth.token, user.flickrAuth.secret),
      buffer,
      { is_public: 1 },
    );
  } catch (err) {
    pino.error('Unable to upload user photo');
    throw err;
  }
}

I'm using the multer and sharp libraries to handle uploading and transformation of user photos. The Buffers are successfully uploading to a cloud storage provider, so I can confirm that they're correct. I'm also successfully completing OAuth and retrieving user photos, so the this.oauth object is valid.