Meteor Slingshot Cordova/Android "error : failed to upload file to cloud storage [-0]"

701 Views Asked by At

I have a working meteor/cordova app that uses slingshot to upload to AWS/S3. I am able to upload and view photos in-app from the browser and from iOS.

However, on Android I am unable to load photos from the AWS link provided by slingshot and stored in my database, and when I try to upload a photo I get an error that reads:

    "error : failed to upload file to cloud storage [-0]"

Is there anything android-specific that I have missed/that I should do to configure slingshot/my app in general for android? Any help would be very much appreciated. Thanks!

relevant client-side code (minus file restriction):

//upload to AWS once file is selected
'change #imgUpload' : function(){
  var uploader = new Slingshot.Upload("uploadFiles");
  var questionId = Session.get('selectedQuestion');
  uploader.send(document.getElementById('uploadInput').files[0], function (error, downloadUrl) {
    if (error) {
      // Log service detailed response
      console.log(error)
      console.error('Error uploading' );
      alert (error);
    }
    else {
      Meteor.call('uploadImage', questionId, downloadUrl);
    }
  });

relevant server-side method:

  'uploadImage' : function(questionId, downloadUrl){
check(questionId, String);
check(downloadUrl, String);
questionsList.update({_id: questionId},
                      {$set: {
                        picUrl: downloadUrl
                      }
                    });
}, //end upload image

Relevant server-side directive (minus file restriction):

Slingshot.createDirective("uploadFiles", Slingshot.S3Storage, {
  bucket: <bucketname>,
  acl: "public-read",


  authorize: function(){
    //you can't upload if youre not logged in
    if(!this.userId){
      var message = "Please log in before posting files";
      throw new Meteor.Error("Login Required", message);
    }
    return true;
  },

  key: function(file){
    //store file in a directory based on a users team name
    var teamName = Meteor.user().roles.defaultGroup[0]
    return teamName + "/" + file.name;
  }
});

relevant mobile_config.js accessrules:

App.accessRule('https://<app-name>.herokuapp.com/*');
App.accessRule('http://<app-name>.herokuapp.com/*');
App.accessRule('https://s3.amazonaws.com/<bucketname>/*');
App.accessRule('http://localhost:3010/*');

relevant template to display pic:

  {{#if theQuestion.picUrl}}
        <img src="{{theQuestion.picUrl}}" width="300px" height="300px"  class="userPic">
  {{/if}}

relevant template to upload pic:

<template name="uploader">
  <form id="imgUpload">
    <input id='uploadInput' class="fileLoader" name="file" type="file">
    <label for="file" class="uploadWords">Tap here to upload/take a  picture</label>
  </form>

file restriction:

    Slingshot.fileRestrictions("uploadFiles", {
  allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
  maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});
2

There are 2 best solutions below

1
On

Encountered the same issue and I what I found is this. File object that we create using Webkit is different from the file Object that is available on Andriod. File Object constructor on Webkit is

new File([Blob],"FileName.png",{type: "image/png"});

Where as on Cordova it is different, to resolve the issue when using 'uploader.send' instead of passing a file object (Which internally been created by Meteor/Cordova) pass Blob object directly, the code which I used to create the blob object is

b64toBlob = function(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;

var byteCharacters = atob(b64Data.split(',')[1]);
var byteArrays = [];

for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    var slice = byteCharacters.slice(offset, offset + sliceSize);

    var byteNumbers = new Array(slice.length);
    for (var i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
    }

    var byteArray = new Uint8Array(byteNumbers);

    byteArrays.push(byteArray);
}

var blob = new Blob(byteArrays, {type: contentType});
return blob;}

And the way I passed finally constructed my code is

    if (Meteor.isCordova) {
    console.log("Detected Mobile device");
    var blobFile = b64toBlob(data, "image/png");
    blobFile.name = "userPhoto.png";
    newFile = blobFile;
} else {
    newFile = new File([b64toBlob(data, "image/png")], "usePhoto.png", {type: "image/png"});
}
uploader.send(newFile, function (error, downloadUrl) {
//Deal with the download URL
}
0
On

For future reference if anyone else is having this problem, I ended up tearing out meteor slingshot and going with https://github.com/Lepozepo/S3 . It worked well.

Feel free to message me for any code, but I essentially used the boilerplate code provided in the examples.