switched to buffer input for uploading

This commit is contained in:
jkoschke
2021-02-22 23:11:11 +01:00
parent 0ed1e74080
commit 92904f00eb
3 changed files with 26 additions and 20 deletions

View File

@ -2,13 +2,14 @@ module.exports = function (RED) {
const dav = require('dav')
const webdav = require('webdav')
const fs = require('fs')
const IcalExpander = require('ical-expander')
const moment = require('moment')
const https = require('https')
const rootCas = require('ssl-root-cas').create();
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
function NextcloudWebDavList (config) {
function NextcloudWebDavList(config) {
RED.nodes.createNode(this, config)
this.server = RED.nodes.getNode(config.server)
this.directory = config.directory
@ -40,7 +41,7 @@ module.exports = function (RED) {
}
RED.nodes.registerType('nextcloud-webdav-list', NextcloudWebDavList)
function NextcloudWebDavOut (config) {
function NextcloudWebDavOut(config) {
RED.nodes.createNode(this, config)
this.server = RED.nodes.getNode(config.server)
this.filename = config.filename
@ -75,7 +76,7 @@ module.exports = function (RED) {
}
RED.nodes.registerType('nextcloud-webdav-out', NextcloudWebDavOut)
function NextcloudWebDavIn (config) {
function NextcloudWebDavIn(config) {
RED.nodes.createNode(this, config)
this.server = RED.nodes.getNode(config.server)
this.directory = config.directory
@ -83,14 +84,16 @@ module.exports = function (RED) {
const node = this
node.on('input', (msg) => {
// Read upload file
let filename = node.filename
if (msg.filename) {
filename = msg.filename
filename = msg.filename;
}
const name = filename.substr((filename.lastIndexOf('/') + 1), filename.length)
const file = fs.readFileSync(filename)
const file = msg.payload;
// Set upload directory
let directory = '/'
if (msg.directory) {
directory += msg.directory + '/'
@ -99,20 +102,23 @@ module.exports = function (RED) {
}
directory = directory.replace('//', '/')
const webDavUri = node.server.address + '/remote.php/webdav/'
const client = webdav(webDavUri, node.server.credentials.user, node.server.credentials.pass)
// check option for self signed certs
const option = {}
if (node.server.insecure) {
option.agent = new https.Agent({ rejectUnauthorized: false })
}
const webDavUri = node.server.address + '/remote.php/webdav/'
const client = webdav(webDavUri, node.server.credentials.user, node.server.credentials.pass)
client.putFileContents(directory + name, file, { format: 'binary' }, option)
.then(function (contents) {
console.log(contents)
node.send({ 'payload': JSON.parse(contents) })
}, function () {
}, function (e) {
console.error(e);
node.error('Nextcloud:WebDAV -> send file went wrong.')
})
})