Files
nextcloud-collabora-nodered/lib/utils.js
T
dev 5ee179539e Example-flows + noder: defaults pekar bara på filer som finns
Flöden (flows/example-flows.json):
- Kedjor som skapar det de behöver: png→asset, templates:list→create→
  extract/fill/transform/extractFields, create A+B→compareVersionsPdf
- Defaults /Dokument/Example.md, /Dokument/exempel.odt, dest
  /Dokument/OfficeOut/, mall-id 230 (Invoice.odt via templates:list)
- Ingen referens till /Mallar/avtal.docx, avtal-ny.docx eller logotyp.png
- Debug-noder med tostatus=false (undviker TypeError-length i status)

Noder:
- nc-office templates:create: skickar path+template (richdocuments-
  kontrakt), ärver extension från mallen, MKCOL av mapp, idempotent
  (återanvänd existerande fil i stället för 'namn (2)') — fixar OCS 400
- nc-office document:direct: fileId camelCase (+fileid), 996-fel
  inkluderar nu OCS-body i felmeddelandet
- collabora-wopi discovery: rewrite urlsrc/favIconUrl till
  collaboraPublicUrl så iframe inte går mot CODE bridge-IP
- webdavGet/PROPFIND 404: feltext med föreslagen åtgärd
- resolveDestNcPath: tvingar rätt filändelse (.pdf/.png/…)
- Nytt: listUserExamples (PROPFIND /Dokument+/Documents) +
  bridge-op pickDemoSource; scripts/discover-examples.js

Verifierat mot cloud.domain.name (your-user): capabilities, discovery-
rewrite, pdf/png, asset-länk, mall-skapa+extract/fill/transform,
compare A/B→pdf, document:direct, htmlToOffice — ALL PASS.
2026-08-22 22:50:47 +02:00

144 lines
4.0 KiB
JavaScript

'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 + '/';
let name = msg.destName || replaceExtension(basenameFromNcPath(sourceNcPath), format || 'bin');
// Rätt filändelse tvingas in — annars landar filer utan ändelse i NC.
const fmt = String(format || '').toLowerCase();
if (fmt && !new RegExp(`\\.${fmt}$`, 'i').test(name)) {
name = replaceExtension(name, fmt);
}
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
};