upload file with FileSystem?

I’m trying to upload a file using the FileSystem to syncano but its not working not sure what i’m doing wrong here

//var theFileToUpload = FileSystem.readBufferFromFile(FileSystem.dataDirectory + "/" + "kevin.jpg")

  var object = {
    id: userProfileID_class,
    //avatar: Syncano.file(theFileToUpload),
    avatar: Syncano.file(FileSystem.dataDirectory + "/" + "kevin.jpg"),
	instanceName: ApiKeys.instanceName,
    className: "user_profile"
  };

  DataObject.please().update(object).then(function(userAVATAR) {
        debug_log("done " + userAVATAR.avatar.value);
  }).catch(function(error) { debug_log("error1 " + error); });

file

Hi. :slight_smile:

I think I’m going to need some more information here to be able to help you.
First of all, do you get any error messages from the monitor?

I can also see that you have commented out the lines related to the FileSystem code. Is that deliberate, or something done for testing?

Note that FileSystem.readBufferFromFile(FileSystem.dataDirectory + "/" + "kevin.jpg") will return a promise, and not an ArrayBuffer. You can use then to do something when the promise is resolved, see the docs for a small example.

Alternatively you can use the FileSystem.readBufferFromFileSync function, which will block the JS thread and return an ArrayBuffer.

Note that I’m not familiar with the Syncano API, so there might be something else going on here that I don’t see.

Hi Karsten,
No errors in the monitor & commented out lines are for testing

Syncano is using node.js fs.readFileSync to do this:

avatar: Syncano.file(fs.readFileSync('/sample_path/user_avatar.jpg')),

I think it needs just a path with multipart/form-data to make it work… ?

So Fuse does not support the nodejs standard lib, and if a library you’re using requires, say, fs or path, those modules will not be available. Providing polyfills for node would be great but it’s a really herculean task. If you can, yourself, provide fills for fs (using fuse filesystem) to cover syncano’s needs, that looks like it should solve this problem.

Thanks Andreas, anyway i think there might be a error in the syncano code as this doesn’t work either

function updateAvatar(){

  CameraRoll.getImage().then(
    function(image)
    {
      console.log("received image: "+image.path+", "+image.width+"/"+image.height);
      //displayImage(image);

      var object = {
        avatar: Syncano.file(image.path),
        id: userProfileID_class,
        instanceName: ApiKeys.instanceName,
        className: "user_profile"
      };

      DataObject.please().update(object).then(function(object) {
          getALLdata()
          debug_log("done " + object.avatar.value);
      }).catch(function(error) { debug_log("error1 " + error); });

    }
  ).catch(
    function(reason){
      console.log("Couldn't get image: "+reason);
    }
  );
};

I’ve got the same problem with Syncano, trough my research I’ve found that “Syncano.file()” method is expecting the return of “fs.readFileSync()” that is a Buffer as this post said:

And “FileSystem.readBufferFromFile()” returns an ArrayBuffer

This is where my knowledge ends, I don’t know what a buffer is, nor an arraybuffer yet, I will continue reading but for now if we could contact Bolav to ask if he can think of a way to upload a file to syncano using fuse filesystem, that would be awesome

Ps. English is not my native language, if I said something wrong I apologize

Hi Mauricio

Im still not able to upload a image directly from fuse but i did find a work around:

1 use ImageTools to create a base64 string from you image

2 send the string to syncano scripts using payload

3 convert the string back in to a image using nodeJS (syncano server side scripting) & save to user

Thank you, I´ll try this for my app.