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

Rob*_*pta 12 node.js express strongloop loopbackjs

loopback用来存储图像到server.

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

另外我想在保存之前将其转换为另一个缩略图形式.

这就是我的表现.

在客户端

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

在服务器端我收到查询"params"但没有得到"文件名"

我的存储型号名称是 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();
})
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*pta 23

我想到了.

我们必须定义一个自定义函数的getFileNameboot/configure-storage.js.

假设我的数据源loopback-component-storagepresImage.

服务器的/ boot /配置,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.";
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

通用/模型/ 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..
};
Run Code Online (Sandbox Code Playgroud)

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

下载quickthumb

以下是如何将其与环回一起使用.

此代码直接从Loopback缩略图视图复制

通用/模型/ 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();
    });

};
Run Code Online (Sandbox Code Playgroud)

  • 很好的响应,但我认为orderId,customerId应该在服务器端设置而不是在表单中设置,以便最终用户无法更改. (2认同)