diff --git a/node_helper.js b/node_helper.js index 00c7a1a..7b18e70 100644 --- a/node_helper.js +++ b/node_helper.js @@ -1,6 +1,7 @@ require("url"); // for nextcloud const https = require("https"); // for nextcloud const fs = require("fs"); // for localdirectory +const {XMLParser} = require('fast-xml-parser'); //for nextcloud response parsing const NodeHelper = require("node_helper"); @@ -94,14 +95,8 @@ module.exports = NodeHelper.create({ body += data; }); response.on("end", function() { - imageList = body.match(/href>\/[^<]+/g); - imageList.shift(); // delete first array entry, because it contains the link to the current folder + imageList = self.extractImagesFromResponse(body, urlParts); if (imageList && imageList.length > 0) { - imageList.forEach(function(item, index) { - // remove clutter and the pathing from the entry -> only save file name - imageList[index] = item.replace("href>" + urlParts.pathname, ""); - //console.log("[" + self.name + "] Found entry: " + imageList[index]); - }); self.sendSocketNotification("IMAGE_LIST", imageList); return; } else { @@ -116,6 +111,32 @@ module.exports = NodeHelper.create({ }); }, + extractImagesFromResponse: function(body, urlParts) { + var imageList = []; + + const options = { + ignoreDeclaration: true, + processEntities: false, + removeNSPrefix: true, + }; + + const parser = new XMLParser(options); + let jObj = parser.parse(body).multistatus.response; + for (const item in jObj) { + if (Object.hasOwnProperty.call(jObj, item)) { + const element = jObj[item]; + if (element.propstat[0]) { + if (element.propstat[0].prop) { + if (element.propstat[0].prop.getcontenttype && element.propstat[0].prop.getcontenttype.startsWith('image/')) { + imageList.push(element.href.replace(urlParts.pathname, '')); + } + } + } + } + } + return imageList; + }, + fetchEncodedImage: async function(passedImageName) { var self = this; diff --git a/package.json b/package.json index 25e3c20..19755f7 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "author": "skuethe", "license": "ISC", "dependencies": { + "fast-xml-parser": "^4.0.5", "jquery": "^3.1.0" } }