GridFS不使用猫鼬写入mongodb

时间:2019-04-22 18:31:08

标签: node.js mongodb mongoose gridfs gridfs-stream

我正在使用MEAN堆栈编写应用程序。一种功能是向用户提供图像。我可以使用下面的代码来实现这一点。

但是,我还有另外一个功能。如果请求的图像不存在于我的数据库中,我将从另一个集合中获取实际的图像URL,下载该图像,将其上传到我的mongodb并将该图像提供给用户。

我可以下载图像,也可以将其保存到文件夹。但是,当我尝试将其上传到数据库时,该过程会卡住(我在代码中标记了卡住的确切位置)。

var mongoose = require('mongoose');
var mongo = require('mongodb');
let Grid = require('gridfs-stream');
let gfs = Grid(mongoose.connection,mongo);
let articles = require('../controllers/articles');
var https = require('https');
var fs = require('fs');

var download = function(url, dest, cb) {
    console.log("Downloading");
    var file = fs.createWriteStream(dest);

    file.on('open',function (fd){
        console.log("created file "+fd)
        https.get(url, function(response) {
            response.pipe(file);
            file.on('finish', function() {
                file.close();
                cb(file);
            });
        });
    });
};

function downloadAndSaveImage(image,callback) {
    console.log("Init download");
    article = articles.getArticlesById(image, function (article) {
        download(article['imageUrl'],'/home/rsa-key-20190326/snap/server/res/image/'+image+'.jpg',function (file) {
            console.log("Downloaded");
            var writestream = gfs.createWriteStream({
                filename: image,
                content_type: 'image/jpg',
                mode: 'w'
            });
            console.log("Created stream "+writestream);
            writestream.on('close', function (file) {
                console.log("close");//doesn't get logged
                callback(file);
            });

            console.log(file.path); //this gets logged
            fs.createReadStream(file.path)
                .pipe(writestream);

        });
    });
}

//4.7.3
exports.getImage = function(req, res) {
    console.log("Got request");
    gfs.files.find({filename: req.params.image}).toArray(function(err, files){

        if(!files || files.length === 0){
            console.log("Doesnt exist");

            downloadAndSaveImage(req.params.image, function (file) {
                console.log("Saved");
                var readstream = fs.createReadStream(file.path);
                // set the proper content type
                res.set('Content-Type', 'image/jpg');
                // Return response
                console.log("Returned");
                return readstream.pipe(res);
            });
        }else{
            console.log("Exist");
            var readstream = gfs.createReadStream({
                filename: files[0].filename,
            });
            readstream.on('error', function (err) {
                console.log('An error occurred!', err);
                throw err;
            });

            return readstream.pipe(res);
        }
    });
};

这是控制台日志,可帮助您了解流程:

Doesnt exist
Init download
Downloading
created file 15
Downloaded
Created stream [object Object]
path/to/file

如您所见,“ close”事件永远不会触发。当我检查数据库时,没有图像被保存。

0 个答案:

没有答案