1

イメージファイルを取得して圧縮後にディスクにアップロードできるサーバーをノード上に作成しています。ただし、小さいサイズの画像は圧縮されており、10MB 程度の大きなサイズの画像は次のエラーが発生します。

 { Error: Error in file: ./uploads/ecqrwxgz6lw.jpg

write EOF
    at _errnoException (util.js:1024:11)
    at WriteWrap.afterWrite [as oncomplete] (net.js:867:14)
  code: 'EOF',
  errno: 'EOF',
  syscall: 'write',
  killed: false,
  stdout: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 ff fe 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0d 00 0f 01 02 00 12 00 00 00 ac 00 ... >,
  stderr: <Buffer >,
  failed: true,
  signal: null,
  cmd: 'D:\\Projects\\web-development\\Node.js\\Business\\node_modules\\mozjpeg\\vendor\\cjpeg.exe -quality 60',
  timedOut: false }

multer でファイルをアップロードするための私のコードは

    var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (req, file, callback) {

        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        var  rand = Math.random().toString(36).substr(2, 12);
        req.rand =  rand;
        ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
        callback(null, rand+ext);
    }
});

var upload = multer({
    storage: storage
}).single('userPhoto');

module.exports = {
    upload: upload,
};

これはファイルをアップロードするための私のルートです

    app.post('/events/upload', upload, function (req, res) {

    var fb_id = req.user.id;
    var email = req.user._json.email;
    var random = req.rand;

    console.log(fb_id);

    var query = {
            'Fbid': fb_id
        },
        update = {
            $set: {
                email: email
            },
            $push: {
                events: {
                    event: place,
                    imageId: random
                }
            }
        },
        options = {
            upsert: true
        };

    usersuploadImformation.findOneAndUpdate(query, update, options, function (err, data) {
        if (err) {
            console.log("Not able to update");
        }
        else{
            console.log("Updated");
        }
    });


    imagemin(['./uploads/'+random+'.{JPG,jpg}'], './uploads', {
        plugins: [
            imageminMozjpeg({
                              quality: 60,
                              progressive: true

                             })
        ]
    }).then(files => {
        console.log(files);
        //=> [{data: <Buffer 89 50 4e …&gt;, path: 'build/images/foo.jpg'}, …] 
    }).catch((err)=>{
        console.log(err);
    });



});
4

1 に答える 1