mercredi 1 août 2018

File Upload Through Web Socket Cutting Off Before Finish Uploading

Am uploading files to the server using web socket. On my localhost web development, it is uploading to the end. But on the server it cuts off before the file finish uploading. I have upgraded my nodejs server to the lastest version on my local development and it is still uploading to finish on my local development. But on the server it always cuts off before it finish uploading. This always happen when am uploading files from 2MB upward.

SERVER CODE:

ss(socket).on('upload-data', function (stream, data, ack) {
    jwt.verify(data.token, process.env.JWT_SECRET, function (err, datan) {
        if (err) {
            res.send({
                data: false
            });
        }

        //console.log(data);

        let file_data = fs.createWriteStream(path.resolve(workspace.path + '/' + data.cur_directory, filename));
        stream.pipe(file_data);

        if (data.cur_directory === data.user_directory) {
            let filename = path.basename(data.name);
            //console.log(workspace.path + data.cur_directory + '/' + filename);
            fs.open(workspace.path + '/' + data.cur_directory + '/' + filename, 'wx', function (err) {
                if (err) {
                    ack({ name: filename })
                } else {

                    stream.on('finish', function () {

                        let extname = filename.split('.');
                        extname = extname[extname.length - 1];

                        let filesystem = require('./schema/filesystem.js');

                        let parentPath = data.cur_directory;
                        let filePath = data.cur_directory + '/' + filename;
                        let date = new Date();

                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        let cur_date = date.getDate();
                        let hour = date.getHours();
                        let minutes = date.getMinutes() > 10 ? 0 + date.getMinutes() : date.getMinutes();
                        let seconds = date.getSeconds();

                        let date_generated = year + '-' + month + '-' + cur_date + ' ' + hour + ':' + minutes + ':' + seconds;

                        let file_new_name = path.parse(filePath)

                        let filesystemcontent = new filesystem({ is_dir: false, size: data.size, ext: "." + extname, parent_id: data.current_directory_id, user_id: data.id, name: file_new_name.name, parent: data.cur_directory, parent_path: parentPath, path: filePath, date_created: date_generated });
                        filesystemcontent.save();

                        let docId = filesystemcontent._id;

                        let filesystemcontent2 = require('./schema/filesystem.js');
                        filesystemcontent2.find({ user_id: data.id, parent_id: data.current_directory_id, archive: false }, function (err, doc) {
                            if (err) {
                                return console.log(err);
                            }

                            if (doc.length) {
                                let dir = [];
                                let ds = require('file-system');
                                let filepath = require('path');
                                for (let d = 0; d < doc.length; d++) {
                                    let data = { is_favourite: doc[d].is_favourite, size: doc[d].size, id: doc[d]._id, dir: doc[d].name, dir_stat: ds.lstatSync(workspace.path + '/' + doc[d].path), extname: filepath.extname(workspace.path + '/' + doc[d].path), is_dir: ds.lstatSync(workspace.path + '/' + doc[d].path).isDirectory() }
                                    dir.push(data);
                                }
                                dir.push({ is_favourite: false, size: data.size, id: docId, dir: data.name, dir_stat: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)), extname: filepath.extname(filepath.join(workspace.path + '/' + parentPath, data.name)), is_dir: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)).isDirectory() });

                                ack({ message: 'completed', data: true, found: 0, directory_children: dir });
                            }
                        });

                        //console.log(stream);
                        //console.log(stream._readableState.ReadableState)
                    });

                    stream.on('data', function(data) {
                        //console.log(data)

                    });

                    stream.on('error', function () {
                        stream.end();
                    })
                }
            })
        } else {
            var filename = path.basename(data.name);

            let filesystem2 = require('./schema/filesystem.js');

            filesystem2.find({ user_id: data.id, name: data.cur_directory, _id: data.current_directory_id }, function (err, doc) {
                if (err) {
                    return console.log(err);
                }

                //console.log(doc)

                if (doc.length) {
                    fs.open(workspace.path + '/' + doc[0].path + '/' + filename, 'wx', function (err) {
                        if (err) {
                            ack({ name: filename })
                        } else {
                            let file_data = fs.createWriteStream(workspace.path + '/' + doc[0].path + '/' + filename)
                            stream.pipe(file_data);
                            stream.on('end', function () {

                                let extname = filename.split('.');
                                extname = extname[extname.length - 1];

                                let filesystem = require('./schema/filesystem.js');

                                let parentPath = doc[0].path;
                                let filePath = doc[0].path + '/' + filename;
                                let date = new Date();

                                let year = date.getFullYear();
                                let month = date.getMonth() + 1;
                                let cur_date = date.getDate();
                                let hour = date.getHours();
                                let minutes = date.getMinutes() > 10 ? 0 + date.getMinutes() : date.getMinutes();
                                let seconds = date.getSeconds();

                                let date_generated = year + '-' + month + '-' + cur_date + ' ' + hour + ':' + minutes + ':' + seconds;

                                let file_new_name = path.parse(filePath)

                                let filesystemcontent = new filesystem({ is_dir: false, size: data.size, ext: "." + extname, parent_id: doc[0]._id, user_id: data.id, name: file_new_name.name, parent: data.cur_directory, parent_path: parentPath, path: filePath, date_created: date_generated });
                                filesystemcontent.save();

                                let docId = filesystemcontent._id;

                                let filesystemcontent2 = require('./schema/filesystem.js');
                                filesystemcontent2.find({ user_id: data.id, parent_id: doc[0]._id, archive: false }, function (err, docn) {
                                    if (err) {
                                        return console.log(err);
                                    }

                                    if (docn.length) {
                                        let dir = [];
                                        let ds = require('file-system');
                                        let filepath = require('path');
                                        for (let d = 0; d < docn.length; d++) {
                                            let data = { is_favourite: docn[d].is_favourite, size: docn[d].size, id: docn[d]._id, dir: docn[d].name, dir_stat: ds.lstatSync(workspace.path + '/' + docn[d].path), extname: filepath.extname(workspace.path + '/' + docn[d].path), is_dir: ds.lstatSync(workspace.path + '/' + docn[d].path).isDirectory() }
                                            dir.push(data);
                                        }
                                        dir.push({ is_favourite: false, size: data.size, id: docId, dir: data.name, dir_stat: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)), extname: filepath.extname(filepath.join(workspace.path + '/' + parentPath, data.name)), is_dir: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)).isDirectory() })

                                        ack({ message: 'completed', data: true, found: 0, directory_children: dir });
                                    }
                                });
                            })
                        }
                    });
                }
            })
        }

    });
});

CLIENT CODE: var stream = ss.createStream(); ss(socket).emit('upload-data', stream, {name: this.file.name, size: this.file.size, cur_directory: Cookies.get('current_location'), username: Cookies.get('username'), user_directory: Cookies.get('user_directory'), current_directory_id: Cookies.get('current_directory_id'), token: this.$store.state.auth_token, id: Cookies.get('id')}, function(data) { if(data.name) { that.uploading = false; that.file_exist = data.name + ' already exist'; }

//console.log(data);
if(data.found === 0) {
    if(data.directory_children.length > 0) {
       that.$store.state.directory_children = data.directory_children;
    }
}

});

ss.createBlobReadStream(this.file).pipe(stream);




Aucun commentaire:

Enregistrer un commentaire