2015-06-25 8 views
11

से प्राप्त छवि मैं server छवि भंडारण के लिए loopback उपयोग कर रहा हूँ।संशोधित लूपबैक घटक भंडारण

मैं सर्वर पर सहेजा करने से पहले और संशोधित करने के लिएफ़ाइल नाम फ़ाइल का चाहते हैं।

इसके अलावा, मैं एक और थंबनेल फार्म के लिए यह बचाया करने से पहले परिवर्तित करना चाहते हैं।

यहाँ मैं कैसे कर रहा हूँ है।

ग्राहक के पक्ष में

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

सर्वर साइड पर मैं क्वेरी "पैरामीटर" प्राप्त हो रही है, लेकिन नहीं मिल रहा है "फ़ाइल नाम"

मेरे संग्रहण मॉडल का नाम है 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(); 
}) 

कैसे फ़ाइल का फ़ाइल का नाम बदलने के लिए सर्वर पर बचाया जा रहा है?

उत्तर

23

मैं यह पता लगा।

हमें boot/configure-storage.js में एक कस्टम फ़ंक्शन getFileName परिभाषित करना होगा।

मान लीजिए loopback-component-storage के लिए मेरे डेटा स्रोतहै presImage

सर्वर/बूट/कॉन्फ़िगर-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."; 
     } 
    }; 
} 

आम/मॉडल/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

आम/मॉडल/कंटेनर से सीधे कॉपी किया जाता है।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

अच्छा प्रतिक्रिया है, लेकिन मुझे लगता है कि कि OrderID, CustomerID सर्वर साइड में स्थापित किया जाना चाहिए नहीं के रूप में सेट तो यह द्वारा बदला नहीं जा सकता अंतिम उपयोगकर्ता –

0

जवाब ऊपर पर पिगीबैकिंग, इस कॉन्फ़िगर भंडारण फ़ाइल नाम req.params.filename द्वारा स्पष्टतः स्थापित किया जाना है और अगर कोई भी प्रदान की जाती है मौजूदा नाम के लिए डिफ़ॉल्ट सक्षम बनाता है।

कॉन्फ़िगर-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 

    }; 
} 
संबंधित मुद्दे