69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
// discover-examples.js — slå upp filer som FAKTISKT finns hos användaren
|
||
|
|
// och skriv ut förslag på defaults till example-flows.
|
||
|
|
//
|
||
|
|
// Usage:
|
||
|
|
// NC_URL=https://cloud.domain.name NC_USER=your-user NC_PASS=app-password \
|
||
|
|
// node scripts/discover-examples.js [COLLABORA_URL] [PUBLIC_URL]
|
||
|
|
//
|
||
|
|
// Kör t.ex. inne i Node-RED-containern:
|
||
|
|
// docker exec nextcloud-node-red sh -c \
|
||
|
|
// 'cd /data/node_modules/node-red-contrib-nextcloud-collabora && \
|
||
|
|
// NC_URL=... NC_USER=... NC_PASS=... node scripts/discover-examples.js'
|
||
|
|
|
||
|
|
const { listUserExamples, webdavPropfindFileId } = require('../lib/webdav');
|
||
|
|
const { getCapabilities } = require('../lib/collabora');
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
const config = {
|
||
|
|
nextcloudUrl: process.env.NC_URL || 'https://cloud.domain.name',
|
||
|
|
collaboraUrl: process.argv[2] || process.env.COLLABORA_URL || 'https://code:9980',
|
||
|
|
collaboraPublicUrl: process.argv[3] || process.env.COLLABORA_PUBLIC_URL || '',
|
||
|
|
username: process.env.NC_USER,
|
||
|
|
password: process.env.NC_PASS,
|
||
|
|
verifyTls: false
|
||
|
|
};
|
||
|
|
if (!config.username || !config.password) {
|
||
|
|
console.error('Set NC_USER and NC_PASS (app password)');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const examples = await listUserExamples(config);
|
||
|
|
const primary = examples.find((e) => /\.md$/i.test(e.ncPath)) || examples[0];
|
||
|
|
const office = examples.find((e) => /\.odt$/i.test(e.ncPath)) || primary;
|
||
|
|
|
||
|
|
console.log(JSON.stringify({
|
||
|
|
sourceCandidates: examples.map((e) => e.ncPath),
|
||
|
|
suggestedDefaults: {
|
||
|
|
pdfSourceNcPath: primary ? primary.ncPath : '/Dokument/Example.md',
|
||
|
|
pngSourceNcPath: office ? office.ncPath : '/Dokument/exempel.odt',
|
||
|
|
destFolder: '/Dokument/OfficeOut/'
|
||
|
|
},
|
||
|
|
note: 'Mall-id:n hämtas via templates:list (nc-office). Hardkoda aldrig /Mallar/…'
|
||
|
|
}, null, 2));
|
||
|
|
|
||
|
|
if (primary) {
|
||
|
|
try {
|
||
|
|
const fileId = await webdavPropfindFileId(config, primary.ncPath);
|
||
|
|
console.log(`# fileId för ${primary.ncPath}: ${fileId} (document:direct kan använda msg.ncPath direkt)`);
|
||
|
|
} catch (err) {
|
||
|
|
console.error(`# PROPFIND fileId misslyckades: ${err.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await getCapabilities(config);
|
||
|
|
console.log('# CODE capabilities: OK via ' + config.collaboraUrl);
|
||
|
|
} catch (err) {
|
||
|
|
console.error(`# CODE capabilities MISSLYCKADES via ${config.collaboraUrl}: ${err.message}`);
|
||
|
|
process.exitCode = 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch((err) => {
|
||
|
|
console.error(err.message);
|
||
|
|
process.exit(1);
|
||
|
|
});
|