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

@ -34,7 +34,7 @@
</script> </script>
<script type="text/x-red" data-help-name="nextcloud-webdav-list"> <script type="text/x-red" data-help-name="nextcloud-webdav-list">
<p>Connects to a Nextcloud server and lists directory content of a given path)</p> <p>Connects to a Nextcloud/WebDav server and lists directory content of a given path)</p>
<h3>Inputs</h3> <h3>Inputs</h3>
<dl class="message-properties"> <dl class="message-properties">
@ -106,7 +106,7 @@
</script> </script>
<script type="text/x-red" data-help-name="nextcloud-webdav-out"> <script type="text/x-red" data-help-name="nextcloud-webdav-out">
<p>Connects to a Nextcloud server and downloads a file and sends it to the output</p> <p>Connects to a Nextcloud/WebDav server and downloads a file and sends it to the output</p>
<h3>Inputs</h3> <h3>Inputs</h3>
<dl class="message-properties"> <dl class="message-properties">
@ -173,18 +173,17 @@
</script> </script>
<script type="text/x-red" data-help-name="nextcloud-webdav-in"> <script type="text/x-red" data-help-name="nextcloud-webdav-in">
<p>Connects to a Nextcloud server and uploads a file to a given directory</p> <p>Connects to a Nextcloud/WebDav server and uploads a file to a given directory</p>
<h3>Inputs</h3> <h3>Inputs</h3>
<dl class="message-properties"> <dl class="message-properties">
<dt>payload <dt>payload
<span class="property-type">any</span> <span class="property-type">Binary buffer</span>
</dt> </dt>
<dd> Incoming message triggers receiving a list containing a directory content. Any payload <dd> The payload should contain the file which should be uploaded in a binary buffer </dd>
is possible. </dd>
<dt class="optional">filename <span class="property-type">string</span></dt> <dt class="optional">filename <span class="property-type">string</span></dt>
<dd> The filename should be an absolute path, otherwise it will be relative to the working <dd> A filename can be specified on incoming message or in nodes properties.
directory of the Node-RED process.</dd> Saves the uploaded file with the given name. The property should also include the file extension.</dd>
<dt class="optional">directory <span class="property-type">string</span></dt> <dt class="optional">directory <span class="property-type">string</span></dt>
<dd> A directory can be specified on incoming message or in nodes properties. Uploads the <dd> A directory can be specified on incoming message or in nodes properties. Uploads the
file to the users root directory if not set.</dd> file to the users root directory if not set.</dd>

View File

@ -2,9 +2,10 @@ module.exports = function (RED) {
const dav = require('dav') const dav = require('dav')
const webdav = require('webdav') const webdav = require('webdav')
const fs = require('fs') const fs = require('fs')
const IcalExpander = require('ical-expander')
const moment = require('moment')
const https = require('https') const https = require('https')
const rootCas = require('ssl-root-cas').create();
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
@ -83,14 +84,16 @@ module.exports = function (RED) {
const node = this const node = this
node.on('input', (msg) => { node.on('input', (msg) => {
// Read upload file // Read upload file
let filename = node.filename let filename = node.filename
if (msg.filename) { if (msg.filename) {
filename = msg.filename filename = msg.filename;
} }
const name = filename.substr((filename.lastIndexOf('/') + 1), filename.length) const name = filename.substr((filename.lastIndexOf('/') + 1), filename.length)
const file = fs.readFileSync(filename) const file = msg.payload;
// Set upload directory // Set upload directory
let directory = '/' let directory = '/'
if (msg.directory) { if (msg.directory) {
directory += msg.directory + '/' directory += msg.directory + '/'
@ -99,20 +102,23 @@ module.exports = function (RED) {
} }
directory = directory.replace('//', '/') 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 // check option for self signed certs
const option = {} const option = {}
if (node.server.insecure) { if (node.server.insecure) {
option.agent = new https.Agent({ rejectUnauthorized: false }) 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) client.putFileContents(directory + name, file, { format: 'binary' }, option)
.then(function (contents) { .then(function (contents) {
console.log(contents) console.log(contents)
node.send({ 'payload': JSON.parse(contents) }) node.send({ 'payload': JSON.parse(contents) })
}, function () { }, function (e) {
console.error(e);
node.error('Nextcloud:WebDAV -> send file went wrong.') node.error('Nextcloud:WebDAV -> send file went wrong.')
}) })
}) })

View File

@ -23,6 +23,7 @@
"dav": "^1.8.0", "dav": "^1.8.0",
"ical-expander": "^2.0.0", "ical-expander": "^2.0.0",
"moment": "^2.24.0", "moment": "^2.24.0",
"ssl-root-cas": "^1.3.1",
"webdav": "^1.6.1" "webdav": "^1.6.1"
}, },
"devDependencies": { "devDependencies": {