Git Product home page Git Product logo

Comments (1)

iraniamir avatar iraniamir commented on August 15, 2024

Hi there,
i wrote this ability for my server , you can use something like that 👍
first add this class to uploadHandler.js

module.exports = class CRegExp extends RegExp {
    [Symbol.split](str, limit) {
        var result = RegExp.prototype[Symbol.split].call(this, str, limit);
        return result.map(x => x);
    }
}

and next you should add blow var in UploadHandler.prototype.post = function() after finish var

            contentRange = _.bind(function() {
                var CRange = this.req.headers['content-range'];
                if (typeof CRange !== 'undefined' && CRange !== null && CRange !== '') {
                    var exp = new CRegExp(/[^0-9]+/),
                        contentSplit = CRange.split(exp);
                    return [contentSplit[2], contentSplit[3]];
                } else {
                    return [false, false];
                }
            }, this);

and some changes in saving file process, this is my code

    UploadHandler.prototype.post = function() {
        var self = this,
            form = new formidable.IncomingForm(),
            tmpFiles = [],
            files = [],
            map = {},
            counter = 1,
            redirect,
            finish = _.bind(function(callb) {
                if (!--counter) {
                    async.each(files, _.bind(function(fileInfo, cb) {
                            if (!callb) {
                                this.checkValidate(fileInfo, _.bind(function(err) {
                                    this.emit('end', fileInfo);
                                    cb(err);
                                }, this));
                            } else {
                                cb(null);
                            }
                        }, this),
                        _.bind(function(err) {
                            this.callback({
                                files: files
                            }, redirect);
                        }, this));
                }
            }, this),
            contentRange = _.bind(function() {
                var CRange = this.req.headers['content-range'];
                if (typeof CRange !== 'undefined' && CRange !== null && CRange !== '') {
                    var exp = new CRegExp(/[^0-9]+/),
                        contentSplit = CRange.split(exp);
                    return [contentSplit[2], contentSplit[3]];
                } else {
                    return [false, false];
                }
            }, this);
        this.noCache();
        form.uploadDir = options.tmpDir;
        form
            .on('fileBegin', function(name, file) {
                tmpFiles.push(file.path);
                var fileInfo = new FileInfo(file);
                fileInfo.safeName();
                map[path.basename(file.path)] = fileInfo;
                files.push(fileInfo);
                self.emit('begin', fileInfo);
            })
            .on('field', function(name, value) {
                if (name === 'redirect') {
                    redirect = value;
                }
                if (!self.req.fields)
                    self.req.fields = {};
                self.req.fields[name] = value;
            })
            .on('file', function(name, file) {
                counter++;
                var fileInfo = map[path.basename(file.path)];
                fs.exists(file.path, function(exists) {
                    if (exists) {
                        fileInfo.path = path.join(options.uploadDir(), fileInfo.name);
                        fileInfo.tmp = md5(file.path);
                        fs.stat(fileInfo.path, function(err, stats) {
                            if (!err && stats.isFile()) {
                                if (contentRange()[1] && stats.size < contentRange()[1]) {
                                    var w = fs.createWriteStream(fileInfo.path, {
                                        flags: 'a'
                                    });
                                    var r = fs.createReadStream(file.path);
                                    w.on('close', function() {
                                        finish(((parseInt(contentRange()[0]) + 1) !== parseInt(contentRange()[1])) ? true : '');
                                    });
                                    r.pipe(w);
                                }
                            } else {
                                mkdirp(options.uploadDir(), function(err, made) {
                                    fs.rename(file.path, fileInfo.path, function(err) {
                                        if (!err) {
                                            finish((contentRange()[1]) ? true : '');
                                        } else {
                                            var is = fs.createReadStream(file.path);
                                            var os = fs.createWriteStream(fileInfo.path);
                                            is.on('end', function(err) {
                                                if (!err) {
                                                    fs.unlink(file.path);
                                                }
                                                finish((contentRange()[1]) ? true : '');
                                            });
                                            is.pipe(os);
                                        }
                                    });
                                });
                            }
                        });
                    } else finish();
                });
            })
            .on('aborted', function() {
                _.each(tmpFiles, function(file) {
                    var fileInfo = map[path.basename(file)];
                    self.emit('abort', fileInfo);
                    fs.unlink(file);
                });
            })
            .on('error', function(e) {
                self.emit('error', e);
            })
            .on('progress', function(bytesReceived, bytesExpected) {
                if (bytesReceived > options.maxPostSize)
                    self.req.connection.destroy();
            })
            .on('end', finish)
            .parse(self.req);
    };

probably you would change something to use it because i change lots of thing in my node module . Good luke

from jquery-file-upload-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.