本文介绍了修改从loopback-component-storage获取的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 loopback 将图像存储到服务器

I am using loopback for storing Image to the server.

我想在保存到服务器之前,将文件的文件名修改 。

I want to modify the file name of the file before getting saved to the server.

此外,我想在保存之前将其转换为另一个缩略图。

Also I want to convert it to another thumbnail form before getting saved.

这是我在做什么



Upload.upload(
{
    url: '/api/containers/container_name/upload',
    file: file,
    fileName: "demoImage.jpg",
    //Additional data with file
    params:{
     orderId: 1, 
     customerId: 1
    }
});



我的存储模型名称是容器

Container.beforeRemote('upload', function(ctx,  modelInstance, next) {

    //OUPTUTS: {orderId:1, customerId:1]}
    console.log(ctx.req.query);

    //Now I want to change the File Name of the file.
    //But not getting how to do that

    next();
})

如何更改在服务器上保存的文件的文件名?

推荐答案

我想出来了。

我们必须在 boot / configure-中定义一个自定义函数 getFileName storage.js

We have to define a custom function getFileName in boot/configure-storage.js.

假设我的数据源 loopback-component-storage presImage



module.exports = function(app) {
    //Function for checking the file type..
    app.dataSources.presImage.connector.getFilename = function(file, req, res) {

        //First checking the file type..
        var pattern = /^image\/.+$/;
        var value = pattern.test(file.type);
        if(value ){
            var fileExtension = file.name.split('.').pop();
            var container = file.container;
            var time = new Date().getTime();
            var query = req.query;
            var customerId = query.customerId;
            var orderId    = query.orderId;

            //Now preparing the file name..
            //customerId_time_orderId.extension
            var NewFileName = '' + customerId + '_' + time + '_' + orderId + '.' + fileExtension; 

            //And the file name will be saved as defined..
            return NewFileName;
        }
        else{
            throw "FileTypeError: Only File of Image type is accepted.";
        }
    };
}



现在假设我的容器型号是 container

Now suppose my container model is container.

module.exports = function(Container) {
    Container.afterRemote('upload', function(ctx,  modelInstance, next) {
      var files = ctx.result.result.files.file;

      for(var i=0; i<files.length; i++){
        var ModifiedfileName = files[i].name;
        console.log(ModifiedfileName) //outputs the modified file name.
      } //for loop
      next();
    }); //afterRemote..
};

现在将图片转换为缩略图大小

下载

这里是如何使用它与环回。

Here is how to use it with loopback.

此代码直接从



module.exports = function(Container) {

    var qt = require('quickthumb');

    Container.afterRemote('upload', function(ctx, res, next) {

        var file = res.result.files.file[0];
        var file_path = "./server/storage/" + file.container + "/" + file.name;
        var file_thumb_path = "./server/storage/" + file.container + "/thumb/" + file.name;

        qt.convert({
            src: file_path,
            dst: file_thumb_path,
            width: 100
        }, function (err, path) {

        });

        next();
    });

};

这篇关于修改从loopback-component-storage获取的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:56