Initial release v1.0.0 — Nextcloud Collabora Node-RED sidecar
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
|
||||
const DEFAULT_OUTPUT_FOLDER = '/Dokument/OfficeOut/';
|
||||
|
||||
const MIME_BY_EXT = {
|
||||
pdf: 'application/pdf',
|
||||
png: 'image/png',
|
||||
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
odt: 'application/vnd.oasis.opendocument.text',
|
||||
html: 'text/html',
|
||||
htm: 'text/html',
|
||||
txt: 'text/plain',
|
||||
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
||||
};
|
||||
|
||||
function normalizeNcPath(ncPath) {
|
||||
if (!ncPath) {
|
||||
return null;
|
||||
}
|
||||
let p = String(ncPath).trim();
|
||||
if (!p.startsWith('/')) {
|
||||
p = '/' + p;
|
||||
}
|
||||
return p.replace(/\/+/g, '/');
|
||||
}
|
||||
|
||||
function basenameFromNcPath(ncPath) {
|
||||
const p = normalizeNcPath(ncPath);
|
||||
if (!p) {
|
||||
return 'output.bin';
|
||||
}
|
||||
return path.posix.basename(p);
|
||||
}
|
||||
|
||||
function replaceExtension(filename, newExt) {
|
||||
const base = filename.replace(/\.[^/.]+$/, '');
|
||||
const ext = newExt.startsWith('.') ? newExt.slice(1) : newExt;
|
||||
return `${base}.${ext}`;
|
||||
}
|
||||
|
||||
function contentTypeForFormat(format) {
|
||||
if (!format) {
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
return MIME_BY_EXT[String(format).toLowerCase()] || 'application/octet-stream';
|
||||
}
|
||||
|
||||
function defaultDestNcPath(sourceNcPath, format) {
|
||||
const name = replaceExtension(basenameFromNcPath(sourceNcPath), format || 'bin');
|
||||
return normalizeNcPath(DEFAULT_OUTPUT_FOLDER + name);
|
||||
}
|
||||
|
||||
function resolveDestNcPath(msg, sourceNcPath, format) {
|
||||
if (msg.destNcPath) {
|
||||
return normalizeNcPath(msg.destNcPath);
|
||||
}
|
||||
if (msg.destFolder || msg.destName) {
|
||||
const folder = normalizeNcPath(msg.destFolder || DEFAULT_OUTPUT_FOLDER);
|
||||
const folderWithSlash = folder.endsWith('/') ? folder : folder + '/';
|
||||
const name = msg.destName || replaceExtension(basenameFromNcPath(sourceNcPath), format || 'bin');
|
||||
return normalizeNcPath(folderWithSlash + name);
|
||||
}
|
||||
return defaultDestNcPath(sourceNcPath, format);
|
||||
}
|
||||
|
||||
function assertSafeDestination(destNcPath, msg) {
|
||||
const dest = normalizeNcPath(destNcPath);
|
||||
const allowedPrefix = normalizeNcPath(DEFAULT_OUTPUT_FOLDER);
|
||||
if (msg.allowOverwrite === true) {
|
||||
return dest;
|
||||
}
|
||||
if (!dest.startsWith(allowedPrefix)) {
|
||||
throw new Error(
|
||||
`destNcPath "${dest}" ligger utanför ${allowedPrefix}. Sätt msg.allowOverwrite=true för att tillåta.`
|
||||
);
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
function getConfig(node) {
|
||||
const cfg = node.configNode;
|
||||
if (!cfg) {
|
||||
throw new Error('Ingen nc-collab-config kopplad');
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
function filenameFromPath(ncPath) {
|
||||
return basenameFromNcPath(ncPath);
|
||||
}
|
||||
|
||||
function pickMsgField(msg, ...keys) {
|
||||
for (const key of keys) {
|
||||
if (msg[key] !== undefined && msg[key] !== null && msg[key] !== '') {
|
||||
return msg[key];
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function setError(node, msg, err) {
|
||||
const error = err instanceof Error ? err : new Error(String(err));
|
||||
msg.error = error.message;
|
||||
msg.payload = { error: error.message };
|
||||
node.error(error.message, msg);
|
||||
node.status({ fill: 'red', shape: 'ring', text: error.message.slice(0, 40) });
|
||||
return msg;
|
||||
}
|
||||
|
||||
function setSuccess(node, msg, text) {
|
||||
node.status({ fill: 'green', shape: 'dot', text: text || 'ok' });
|
||||
return msg;
|
||||
}
|
||||
|
||||
function clearStatus(node) {
|
||||
node.status({});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
DEFAULT_OUTPUT_FOLDER,
|
||||
MIME_BY_EXT,
|
||||
normalizeNcPath,
|
||||
basenameFromNcPath,
|
||||
replaceExtension,
|
||||
contentTypeForFormat,
|
||||
defaultDestNcPath,
|
||||
resolveDestNcPath,
|
||||
assertSafeDestination,
|
||||
getConfig,
|
||||
filenameFromPath,
|
||||
pickMsgField,
|
||||
setError,
|
||||
setSuccess,
|
||||
clearStatus
|
||||
};
|
||||
Reference in New Issue
Block a user