A
A
Artem Komarov2015-10-13 16:31:34
JavaScript
Artem Komarov, 2015-10-13 16:31:34

NodeJS+ImageMagick+Multipart form data, what could be causing the problem?

What is the point: I am writing a method for loading several images, renaming them along the way, taking the hash received from the image as a new name. But since I have little experience with the file system from NodeJS, I ran into difficulties... Here is my code:

app.post('/upload', function(req, res){
    var photos = [];

    for (var l = req.files.files.length; l--; ) {
        (function(l){
            im.resize({
                srcData: fs.readFileSync(req.files.files[l].path, 'binary'),
                width: 300,
                height: 400,
                format: 'jpg',
            }, function(err, stdout, stderr){
                if (err) {
                    return console.error(err);
                } else {
                    fs.writeFileSync(req.files.files[l].path, stdout, 'binary');
                    var hash = crypto.createHash('md5'),
                        rdd = fs.createReadStream(req.files.files[l].path);
                    rdd.on('end', function () {
                        var newfilename = '';
                        hash.end();
                        newfilename = hash.read() + '.jpg';
                        console.log(newfilename);
                        photos.push(newfilename);
                        fs.rename(req.files.files[l].path,
                                  __dirname + '/pub/img/' + newfilename);
                        rdd.close();
                        fs.unlinkSync(req.files.files[l].path);
                        !l && res.redirect('/#!/item/'+req.body.id);
                    });
                    rdd.pipe(hash);
                }
            });
        })(l);
    }
});

It seems that I use Sync methods and I get an error

Error: ENOENT, open '/home/m0sk1t/projects/sorm/tmp/pHNhXY2MQ-Ib_WeWp7A06A0V.jpg'
at Error (native)

Working remake!

Переписал по совету Илья Шатохин. Заработало =)
app.post('/upload', function(req, res){
    var photos = [];
    console.log(req.body.id);

    for (var l = req.files.files.length; l--; ) {
        (function(l){
            im.resize({
                srcData: fs.readFileSync(req.files.files[l].path, 'binary'),
                width: 300,
                height: 400,
                format: 'jpg',
            }, function(err, stdout, stderr){
                fs.writeFile(req.files.files[l].path, stdout, 'binary', function(err){
                    if (err) return console.error(err);
                    var hash = crypto.createHash('sha1'),
                        rdd = fs.createReadStream(req.files.files[l].path);
                    rdd.on('end', function () {
                        var newfilename = '';
                        rdd.close();
                        newfilename = hash.digest('hex') + '.jpg';
                        console.log(newfilename);
                        photos.push(newfilename);
                        fs.rename(req.files.files[l].path,
                                  __dirname + '/pub/img/' + newfilename, function(){
                            !l && res.redirect('/#!/item/'+req.body.id);
                        });
                    });
                    rdd.on('data', function(d) {
                        hash.update(d);
                    });
                });
            });
        })(l);
    }
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Shatokhin, 2015-10-13
@m0sk1t

At least your fs.rename is asynchronous and you immediately delete the file synchronously, it is logical that rename will have nothing to move.
PS to use synchronous functions - moveton (plus falling of productivity). Better rewrite to asynchronous with execution control.

K
Kostya Nechaev, 2015-10-23
@kos403

And the code is shit...read the clean code and rewrite it normally

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question