修改从 loopback-component-storage 获取的镜像

Modify image obtained from loopback-component-storage

我正在使用 loopback 将图像存储到 server

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

我还想在保存之前将其转换为另一种缩略图形式。

这是我的做法。

At client side

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

At Server Side I am receiving the query "params" but not getting the "File Name"

我的存储型号名称是 container

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-storage.js.

中定义自定义函数 getFileName

假设我的 datasource for loopback-component-storagepresImage

server/boot/configure-storage.js

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.";
        }
    };
}

common/models/container.js

现在假设我的容器模型是 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..
};

现在将其图像转换为缩略图大小

下载 quickthumb

以下是如何将它与环回一起使用。

此代码直接复制自Loopback thumbnail view

common/models/container.js

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();
    });

};

搭载上面的答案,此配置存储允许通过 req.params.filename 显式设置文件名,如果提供 none 则默认为现有名称。

configure-storage.js

module.exports = function(app) {

//Function for checking the file type..
    app.dataSources.storage.connector.getFilename = function(file, req, ignoreRes) {

        if (!req.params.filename) {
            return file.name
        }

        var fileExtension = file.name.split('.').pop()
        return req.params.filename + '.' + fileExtension

    };
}