Modify image obtained from loopback-component-storage

前端 未结 2 1199
鱼传尺愫
鱼传尺愫 2020-12-14 22:37

I am using loopback for storing Image to the server.

I want to modify the file name of the file before ge

相关标签:
2条回答
  • 2020-12-14 22:54

    I figured it out.

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

    Suppose my datasource for loopback-component-storage is presImage.

    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

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

    Now for converting it images to Thumbnail size

    Download the quickthumb

    Here is how to use it with loopback.

    This code is copied directly from 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();
        });
    
    };
    
    0 讨论(0)
  • 2020-12-14 23:00

    Piggybacking on the answer above, this configure-storage enables the file name to be set explicitly via req.params.filename and to default to the existing name if none is provided.

    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
    
        };
    }
    
    0 讨论(0)
提交回复
热议问题