Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra params don't seem to be sent #12

Open
JeroenRood opened this issue Apr 20, 2015 · 7 comments
Open

Extra params don't seem to be sent #12

JeroenRood opened this issue Apr 20, 2015 · 7 comments

Comments

@JeroenRood
Copy link

So far, Delivery.js has amazed me when it comes to simplicity of sending files through Socket.io. Props for making such a module! :-)

When I use delivery.send(file,extraParams) (extraParams being an object, as described in the readme), and I check the content of file on the server after receive.succes, I don't see the property params (I used Object.keys() to retrieve all the keys from the file-object), and reading file.params returns undefined.

I'd like to send meta-data with my application. Do you have an idea how I can include this meta data (or params) after all? I tried altering Delivery.prototype.send, FilePackage and FilePackage.prototype.prepBatch; but with no avail so far. Could you give me a pointer for fixing this, or help me out here?

Thanks in advance for taking a look at it!

@JeroenRood
Copy link
Author

I realized I forgot to post my code. This excerpt applies to this issue:

Client:

function process_files(e,delivery){
    var file = e.target.files[0];
    var b = document.body;
    if(b.getAttribute("data-album")){
        mrI = b.getAttribute('data-album');
    }
    else {
        mrI = -1;
    }

    delivery.send(file, {loc:b.getAttribute("data-lat")+"|"+b.getAttribute("data-long"),mI:mrI});
    e.preventDefault();
}
socket.on('connect',function(){
    var delivery = new Delivery(socket);

    delivery.on('delivery.connect',function(delivery){
        document.getElementById('photoInput').addEventListener("change", function(e) {
            process_files(e,delivery);
        });
        document.getElementById('audioInput').addEventListener("change", function(e) {
            process_files(e,delivery);
        });
        document.getElementById('videoInput').addEventListener("change", function(e) {
            process_files(e,delivery);
        });
    });

    delivery.on('send.success',function(fileUID){
      console.log("file was successfully sent.");
    });
});

Server:

delivery.on('receive.success',function(file){
        console.log("FILE: "+ JSON.stringify(Object.keys(file),null,2)); // no property "params" here
        // The code to fetch file.params was alternatively put over here, with no avail:
        /*
            var extraParams = file.params;
            console.log(JSON.stringify(extraParams,null,2));
            var mI = extraParams.mI;
        */
        fs.writeFile(__dirname + "/public/media/"+file.name,file.buffer, function(err){
            if(err){
                console.log('File could not be saved. '+ err);
            }
            else{
                var extraParams = file.params;
                console.log(JSON.stringify(extraParams,null,2)); // returns undefined
                var mI = extraParams.mI; // throws error, as property 'mI' of undefined can't be read
                files.push({
                    src:file.name,
                    loc:extraParams.loc,
                    time:(new Date()).getTime(),
                    mI: mI
                });
                console.log(JSON.stringify(files));
                sc = Object.keys(io.nsps["/"].adapter.rooms);
                console.log("rooms: "+JSON.stringify(sc,null,2));
                us = Object.keys(users);
                console.log("user IDs: "+JSON.stringify(us,null,2));
                data = fetchGroupData(sc,us);
                socket.emit('invite_channel','{"type":"welcomeBackUpdate","cList":' +data.cn +',"rID":' +data.cid +',"locs":' +data.locs +',"times":' +data.times+'}');
                console.log('File saved.');
            }
        });
    });

@JeroenRood
Copy link
Author

I think I resolved the problem. Over at deliver.js (client), FilePackage.prototype.prepBatch is applied on _this, which is stored before actually adding this.params.

This code is what I am talking about:

function FilePackage(file,receiving,params){
      console.log("Params (received by FilePackage): "+JSON.stringify(params,null,2));

    this.name = file.name;
    this.size = file.size;
    _this = this;
    if(receiving){
      this.uid = file.uid;
      this.isText = file.isText;
      this.mimeType = file.mimeType;
      this.data = file.data;
      this.dataURLPrefix = file.prefix;
      this.params = file.params;

      pubSub.publish('receive.success',this);
    }else{
      //Sending a file.
      this.params = params;
        console.log("Params (this.params, filePackage): "+JSON.stringify(this.params,null,2));
      this.uid = this.getUID();
      this.reader = new FileReader();

      this.reader.onerror = function(obj){};

      this.reader.onload = function(){
        _this.base64Data = _this.reader.result;
        _this.prepBatch();
      };
      this.reader.readAsDataURL(file);
    };
  };

_this = this; should be placed after assigning this.params. As follows:

  function FilePackage(file,receiving,params){
      console.log("Params (received by FilePackage): "+JSON.stringify(params,null,2));

    this.name = file.name;
    this.size = file.size;
    // _this = this; --> assigned too early
    if(receiving){
      this.uid = file.uid;
      this.isText = file.isText;
      this.mimeType = file.mimeType;
      this.data = file.data;
      this.dataURLPrefix = file.prefix;
      this.params = file.params;
      _this = this; // includes all information from this
      pubSub.publish('receive.success',this);
    }else{
      //Sending a file.
      this.params = params;
        console.log("Params (this.params, filePackage): "+JSON.stringify(this.params,null,2));
      this.uid = this.getUID();
      this.reader = new FileReader();

      this.reader.onerror = function(obj){};

      this.reader.onload = function(){
        _this.base64Data = _this.reader.result;
        _this.prepBatch();
      };
      _this = this; // includes all information from this
      this.reader.readAsDataURL(file);
    };
  };

@liamks
Copy link
Owner

liamks commented Apr 21, 2015

@JeroenRood _this is a reference to this, not a copy of it, so it should be fine where it is. Also _this isn't needed in the if receiving is true, just if we're sending. I also wrote this a few years ago, a better way to do the above would be:

 }else{
      //Sending a file.
      this.params = params;
        console.log("Params (this.params, filePackage): "+JSON.stringify(this.params,null,2));
      this.uid = this.getUID();
      this.reader = new FileReader();

      this.reader.onerror = function(obj){};

      this.reader.onload = function(){
        this.base64Data = this.reader.result;
        this.prepBatch();
      }.bind(this);

      this.reader.readAsDataURL(file);
    };

The code above is for the FilePackage object, I think you're looking at the file object. Different events return different object types. I haven't looked at this code in years unfortunately, so I can't offer too much more help. Have you thought about using something like jquery-file-upload? It works basically everywhere, but is more difficult to setup.

@WilixLead
Copy link

Please update npm package. This package have older code when github

@ghost
Copy link

ghost commented May 27, 2015

Hi there,

Is there any workaround for this issue? I could not fix it with the above suggestions.

@WilixLead
Copy link

@piyusharora420 just copy delivery from github. In npm package stored old code I think.
I solved the problem by this way:

  1. npm install delivery
  2. replace files in node_modules/delivery/lib to lib from this github repository.

Now all works, in server side you can get params by this way:
var delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
console.log('Extra Params', file.params);
});

in front-end you can use this:
delivery.send(file, {extraParam1: '123', extaraParam2: '456'});

@ghost
Copy link

ghost commented May 27, 2015

Thanks a lot @WilixLead. It fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants