2015-06-25 16 views
11

Tôi đang sử dụng loopback để lưu hình ảnh vào server.Sửa đổi hình ảnh thu được từ loopback-component-storage

Tôi muốn sửa đổitên tệp của tệp trước khi được lưu vào máy chủ.

Ngoài ra tôi muốn chuyển đổi nó sang một hình thu nhỏ khác trước khi được lưu.

Đây là cách tôi đang làm.

Tại phía khách hàng

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

Tại Server Side Tôi nhận được truy vấn "params" nhưng không nhận được "File Name"

tên model

tôi lưu trữ là 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(); 
}) 

Làm thế nào để thay đổi tên tệp của tệp được lưu tại máy chủ?

Trả lời

23

Tôi đã tìm ra.

Chúng tôi phải xác định chức năng tùy chỉnh getFileName trong boot/configure-storage.js.

Giả sử nguồn dữ liệu cho loopback-component-storagepresImage.

máy chủ/boot/cấu hình-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."; 
     } 
    }; 
} 

chung/mô hình/container.js

Bây giờ giả sử mô hình container của tôi là 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.. 
}; 

Bây giờ để chuyển đổi nó hình ảnh để Kích thước thu nhỏ

Tải quickthumb

Dưới đây là làm thế nào để sử dụng nó với loopback.

Mã này được sao chép trực tiếp từ Loopback thumbnail view

chung/mô hình/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(); 
    }); 

}; 
+2

phản ứng Nice, nhưng tôi nghĩ rằng OrderID, ID khách hàng nên được đặt ở phía máy chủ và không được thiết lập theo hình thức nên nó không thể được thay đổi bởi người dùng cuối cùng. –

0

cõng trên câu trả lời ở trên, điều này cấu hình lưu trữ cho phép tên tập tin được thiết lập một cách rõ ràng qua req.params.filename và để mặc định với tên hiện tại nếu không được cung cấp.

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 

    }; 
} 
Các vấn đề liên quan