Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

node.js - Modify image obtained from loopback-component-storage

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.

Here is how I am doing.

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"

My Storage model name is 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();
})

How to change the File name of the File getting saved at the server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...