node.js - Modify image obtained from loopback-component-storage -
i using loopback
storing image server
.
i want modify file name of file before getting saved server.
also want convert thumbnail form before getting saved.
here how doing.
at client side
upload.upload( { url: '/api/containers/container_name/upload', file: file, filename: "demoimage.jpg", //additional data file params:{ orderid: 1, customerid: 1 } });
at server side receiving query "params" not getting "file name"
my storage model name container
container.beforeremote('upload', function(ctx, modelinstance, next) { //ouptuts: {orderid:1, customerid:1]} console.log(ctx.req.query); //now want change file name of file. //but not getting how next(); })
how change file name of file getting saved @ server?
i figured out.
we have define custom function getfilename in boot/configure-storage.js
.
suppose datasource loopback-component-storage
presimage.
server/boot/configure-storage.js
module.exports = function(app) { //function checking file type.. app.datasources.presimage.connector.getfilename = function(file, req, res) { //first checking 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 file name.. //customerid_time_orderid.extension var newfilename = '' + customerid + '_' + time + '_' + orderid + '.' + fileextension; //and file name saved defined.. return newfilename; } else{ throw "filetypeerror: file of image type accepted."; } }; }
common/models/container.js
now suppose container model 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 modified file name. } //for loop next(); }); //afterremote.. };
now converting images thumbnail size
download quickthumb
here how use loopback.
this code copied directly 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(); }); };
Comments
Post a Comment