Skip to content

Instantly share code, notes, and snippets.

@sachinsmc

sachinsmc/files.js

Created Aug 7, 2017
Embed
What would you like to do?
Files.upload = function (ctx,options,cb) {
if(!options) options = {};
console.log('File Uploading.....');
//console.log(ctx.req.headers['mobile']);
//console.log(ctx.mobile);
ctx.req.params.container = 'common';
Files.app.models.container.upload(ctx.req,ctx.result,options,function (err,fileObj) {
if(err) {
cb(err);
} else {
var fileInfo = fileObj.files.file[0];
// console.log(ctx.mobile);
Files.create({
name: fileInfo.name,
type: fileInfo.type,
url: CONTAINERS_URL+fileInfo.container+'/'+fileInfo.name
},function (err,obj) {
if (err !== null) {
cb(err);
} else {
var mobile=ctx.req.mobile;
//console.log('mobile : '+mobile);
Files.app.models.namaste_user.updateAll({
mobile: mobile
}, {
pic:fileInfo.name
}, function(err, updateresult) {
if(err){
console.log(err);
}
console.log('fileinfo: '+fileInfo.name);
});
var objs={
"message":"user profile pic uploaded.",
"response_code":"200",
"status":true,
"type":obj.type,
"url":obj.url
}
cb(null, objs);
}
});
}
});
};
Files.remoteMethod(
'upload',{
description: 'Uploads a file',
accepts: [
{ arg: 'ctx', type: 'object', http: { source:'context' } },
{ arg: 'options', type: 'object', http:{ source: 'query'} }
],
returns: {
arg: 'fileObject', type: 'object', root: true
},
http: {verb: 'post'}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment