rico345100 / socket.io-file

Socket.io-file is a node module for uploading file via Socket.io module
MIT License
48 stars 19 forks source link

How to change filename before upload starts? #1

Closed Obvio closed 7 years ago

Obvio commented 7 years ago

Thanks for this!

I wanted to know if it's possible to change the uploaded file name before it writes to the server, i want to always allow uploads with {overwrite:false}.

Thanks again!

rico345100 commented 7 years ago

Hello, @Obvio! I just added new option "rename" which can change the file name before upload starts!

var count = 0;
var uploader = new SocketIOFile(socket, {
    overwrite: false
    rename: function(filename) {
        var split = filename.split('.');    // split filename by .(extension)
        var fname = split[0];   // filename without extension
        var ext = split[1];

        return `${fname}_${count++}.${ext}`;
    }
});

You can now change the file name using 'rename' option, returning value will be the name of a file. Enjoy ;)

Oh, don't forget to update socket.io-file to 2.0.1 !

Obvio commented 7 years ago

thanks! works great! by the way, it might be better using

var path = require('path');
...
rename: function(filename) {
        var fname = path.parse(filename).name;
        var ext = path.parse(filename).ext;
    return `${fname}_${count++}.${ext}`;
}

because a file name can have . in it.

rico345100 commented 7 years ago

@Obvio Nice point :) I will change the example in document later, thanks! Anyway If you like this module, don't forget to give me a star!