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

How to change file name after completed upload? #72

Closed
paulklemm opened this issue Jan 23, 2015 · 4 comments
Closed

How to change file name after completed upload? #72

paulklemm opened this issue Jan 23, 2015 · 4 comments

Comments

@paulklemm
Copy link

I have problem similar to flowjs/flow-php-server#10.

I use the Nodejs example (https://github.com/flowjs/flow.js/tree/master/samples/Node.js) as foundation for my server and everything works fine. When all chunks are uploaded, I assemble the file and then I rename it based on a hash calculated using the file content.

How can I let flow.js know that the file name was changed manually and how can I add this information on an event (e.g. fileSuccess). I assume the proper way would be to change the filename in the result object.

The corresponding method in the app.js node server is:

app.post('/upload', multipartMiddleware, function(req, res) {
  flow.post(req, function(status, filename, original_filename, identifier) {
    if (status == 'done') {
      var filepath = UPLOAD_DIR + filename;
      var stream = fs.createWriteStream(filepath);
      console.log(identifier);
      flow.write(identifier, stream);
      flow.clean(identifier);

      // Here I calculate a hash based on the uploaded file and rename it accordingly.
      getHashFromFile(UPLOAD_DIR, filename, function(hash){
        var extension = path.extname(filename);
        fs.rename(UPLOAD_DIR + filename, UPLOAD_DIR + hash + extension, function(err) {
          if ( err ) console.log('ERROR: ' + err);
        });
      });
    }
    if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
      res.header("Access-Control-Allow-Origin", "*");
    }
    res.status(status).send();
  });
});

I've uploaded the entire file as Gist here: https://gist.github.com/Powernap/36c9b7b1701016ddc03d

@AidasK
Copy link
Member

AidasK commented Feb 1, 2015

Have you found a solution? I think you only need to make a separate Http request to the server with a new file name or you can rename it before the upload.

@paulklemm
Copy link
Author

The problem is that in my case the server determines the new file name based on a hash calculated based on its content. This (for large files rather heavy computation) should always be carried out server-side.

A (rather hacky) solution would be to emit an server-side event to the client using sockets. I however think that this should be done through the flow.js API.

@AidasK
Copy link
Member

AidasK commented Feb 1, 2015

So it is even simpler, you just need to wait for upload to be completed. fileSuccess event will have your server side response as text. So all you need to do is to parse the response and set new file name.

function success(file, message) {
console.log(message);
var json = JSON.parse(message);
file.name = json.new_file_name;
// update ui ...
}

@paulklemm
Copy link
Author

Works like a charm, thanks! 👍

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

2 participants