Сохраняйте изображения в Mongoose и извлекайте их из Mongoose с помощью Mutler и Uppy - PullRequest
0 голосов
/ 06 июля 2019

Это формат, который я получаю на NodeJS.Я использую Uppy File upload для отправки файлов на NODEJS, на котором запущены mongoose и Mutler для получения файлов.

{ fieldname: 'avatar',
  originalname: '012.png',
  encoding: '7bit',
  mimetype: 'image/png',
  destination: 'destination/',
  filename: '703183f0026a47a92e191acf17ca8171',
  path: 'destination/703183f0026a47a92e191acf17ca8171',
  size: 348774 }

Это схема, которую я использую

UserImageSchema = module.exports = new mongoose.Schema({

        // Contains Both Format FOr Mutler ANd Express Upload
        profile: { type: Number, unique: true, required: true, index: true },
        pictures: [{
            fieldname: { type: String },
            originalname: { type: String },
            name: { type: String },
            data: { type: Buffer },
            md5: { type: String },
            originalname: { type: String },
            encoding: { type: String },
            mimetype: { type: String },
            destination: { type: String },
            filename: { type: String },
            path: { type: String },
            size: { type: Number },

            created_at: { type: Date, default: Date.now }
        }]


    });

И этоКод nodejs для получения файла

app.post("/images/upload", upload.single("avatar"), function(req, resp) {
        //var images = req.body.images || req.query.images || req.param("images");;
        log("/images/upload");
        var body = req.body.profile || req.query.profile || req.param["profile"];
        let uploadFile = req.files || req.file;

        uploadFile = uploadFile;
        log(uploadFile);
        var image = new UserImageModel();

        var tuple = [];
        var pictures = {};
        pictures.fieldname = uploadFile.fieldname;
        pictures.originalname = uploadFile.originalname;

        pictures.encoding = uploadFile.encoding;
        pictures.mimetype = uploadFile.mimetype;
        pictures.destination = uploadFile.destination;
        pictures.filename = uploadFile.filename;
        pictures.path = uploadFile.path;
        pictures.size = uploadFile.size;
        tuple.push(pictures);
        image.pictures = tuple;
        image.profile = 1098763333;

        log("Pictures Data :");
        log(image);


        UserImageModel.findOne({ profile: image.profile }, function(err, found) {
            if (err)
                resp.status(501).send({ error: 'Error Occured While Finding Profile' });

            if (found) {
                log('Profile Already Exisit. Append The NewLy Formed Image');

                UserImageModel.update({ profile: image.profile }, { $push: { pictures: tuple } }, function(err, pushed) {
                    if (err)
                        resp.status(501).send({ error: 'Error Occured While Updating Profile With Images' });

                    if (pushed) {
                        log('Images Pushed To Exisiting Profile Successfully');
                        resp.status(200).send({ message: 'Images Pushed To Exisiting Profile Successfully' });
                    }

                });

            } else {
                log('Creating New Profile To Append Images');

                image.save(function(err, saved) {
                    if (err)
                        resp.status(501).send({ error: 'Error Occured While Saving Images' });

                    log('Images Saved Successfully');
                    log(saved);
                    resp.status(200).send({ message: 'Images Saved Successfully' });
                });
            }
        });

    });

Моя проблема заключается в следующем:
1. Это лучший способ сохранить изображения 2. Как передать идентификатор своего пользовательского профиля в теле, используяПоднимите в переднем конце и получить его с помощью Mutler или Express.3. Как конвертировать обратно в изображение и отправить на веб-интерфейс.

любой вывод будет оценен

...