Files

91 lines
5.8 KiB
JavaScript

module.exports = function(RED) {
var https = require('https');
var http = require('http');
var url = require('url');
var OPERATIONS = {
// Shares
'shares:list': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares', query:['shared_with_me','reshares','subfiles','path','include_tags'] },
'shares:get': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/{id}', query:['include_tags'] },
'shares:create': { method:'POST', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares', body:['path','permissions','shareType','shareWith','password','expireDate','note','label','publicUpload','sendPasswordByTalk','attributes'] },
'shares:update': { method:'PUT', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/{id}', body:['permissions','password','expireDate','note','label','hideDownload','token','sendPasswordByTalk'] },
'shares:delete': { method:'DELETE', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/{id}' },
'shares:inherited': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/inherited', query:['path'] },
'shares:pending': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/pending' },
'shares:accept': { method:'POST', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/pending/{id}' },
'shares:sendEmail': { method:'POST', path:'/ocs/v2.php/apps/files_sharing/api/v1/shares/{id}/send-email', body:['password'] },
'shares:token': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/token' },
// Deleted shares
'deleted:list': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/deletedshares' },
'deleted:restore': { method:'POST', path:'/ocs/v2.php/apps/files_sharing/api/v1/deletedshares/{id}' },
// Sharees
'sharees:search': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/sharees', query:['search','itemType','page','perPage','shareType','lookup'] },
'sharees:recommended':{ method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/sharees_recommended', query:['itemType','shareType'] },
// Remote shares
'remote:list': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares' },
'remote:pending': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending' },
'remote:get': { method:'GET', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/{id}' },
'remote:accept': { method:'POST', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/{id}' },
'remote:decline': { method:'DELETE', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/{id}' },
'remote:unshare': { method:'DELETE', path:'/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/{id}' },
// Preview + Info
'preview:get': { method:'GET', path:'/index.php/apps/files_sharing/publicpreview/{token}', query:['file','x','y','a'] },
'info:get': { method:'POST', path:'/index.php/apps/files_sharing/shareinfo', body:['t','password','dir','depth'] }
};
function FSNode(config) {
RED.nodes.createNode(this, config);
var node = this;
this.configNode = RED.nodes.getNode(config.nextcloud);
this.operation = config.operation || 'shares:list';
this.shareId = config.shareId; this.token = config.token;
if (!this.configNode) { node.error("No NC config node"); return; }
node.on('input', function(msg) {
var op = OPERATIONS[msg.operation || node.operation];
if (!op) { node.error("Unknown: "+(msg.operation||node.operation), msg); return; }
var baseUrl = (node.configNode.baseUrl||'').replace(/\/+$/,'');
var uname = node.configNode.credentials.username;
var pw = node.configNode.credentials.password;
var path = op.path
.replace(/\{id\}/g, msg.shareId||node.shareId||'')
.replace(/\{token\}/g, msg.token||node.token||'');
var qp = [];
if (op.query) op.query.forEach(function(q) {
var v = msg[q]!==undefined ? msg[q] : '';
if (v!==undefined&&v!=='') qp.push(encodeURIComponent(q)+'='+encodeURIComponent(v));
});
var fu = baseUrl+path+(qp.length?'?'+qp.join('&'):'');
var pu = url.parse(fu);
var h = {'Accept':'application/json',
'Authorization':'Basic '+Buffer.from(uname+':'+pw).toString('base64')};
if (path.indexOf('/ocs/')===0) h['OCS-APIRequest']='true';
var bo={};
if (op.body) op.body.forEach(function(b){
var v=msg[b]!==undefined?msg[b]:'';
if(v!==undefined&&v!=='') bo[b]=v;
});
var bs=null;
if (Object.keys(bo).length>0) {h['Content-Type']='application/json'; bs=JSON.stringify(bo);}
var o = {hostname:pu.hostname,port:pu.port||(pu.protocol==='https:'?443:80),
path:pu.path,method:op.method,headers:h,rejectUnauthorized:false};
node.log(op.method+' '+fu);
var t = pu.protocol==='https:'?https:http;
var r = t.request(o,function(res){
var b='';res.on('data',function(c){b+=c;});
res.on('end',function(){msg.statusCode=res.statusCode;msg.operation=msg.operation||node.operation;
try{msg.payload=JSON.parse(b);}catch(e){msg.payload=b;}node.send(msg);});
});
r.on('error',function(e){msg.error=e.message;node.error(op.method+' '+fu+' - '+e.message,msg);});
if(bs) r.write(bs);
r.end();
});
}
RED.nodes.registerType("filesharing", FSNode);
};