From 52d1b3a25b67a711058afceb01a84ab6a1dd01f2 Mon Sep 17 00:00:00 2001 From: the0frastus Date: Tue, 22 Feb 2022 19:54:20 +0100 Subject: [PATCH 1/2] added filtering for video files --- node_helper.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/node_helper.js b/node_helper.js index 00c7a1a..825bc4b 100644 --- a/node_helper.js +++ b/node_helper.js @@ -94,15 +94,18 @@ module.exports = NodeHelper.create({ body += data; }); response.on("end", function() { + var filteredImageList = []; imageList = body.match(/href>\/[^<]+/g); imageList.shift(); // delete first array entry, because it contains the link to the current folder 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]); + //filter video files + if (!item.endsWith('.mp4')) { + // remove clutter and the pathing from the entry -> only save file name + filteredImageList.push(item.replace("href>" + urlParts.pathname, "")); + } }); - self.sendSocketNotification("IMAGE_LIST", imageList); + self.sendSocketNotification("IMAGE_LIST", filteredImageList); return; } else { console.log("[" + this.name + "] WARNING: did not get any images from nextcloud url"); From 58f46ddcc38511f575eca26e3d517d658437b9c7 Mon Sep 17 00:00:00 2001 From: the0frastus Date: Sun, 6 Mar 2022 21:58:35 +0100 Subject: [PATCH 2/2] Add contentType check for nextcloud imageList --- node_helper.js | 40 +++++++++++++++++++++++++++++----------- package.json | 1 + 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/node_helper.js b/node_helper.js index 825bc4b..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,18 +95,9 @@ module.exports = NodeHelper.create({ body += data; }); response.on("end", function() { - var filteredImageList = []; - 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) { - //filter video files - if (!item.endsWith('.mp4')) { - // remove clutter and the pathing from the entry -> only save file name - filteredImageList.push(item.replace("href>" + urlParts.pathname, "")); - } - }); - self.sendSocketNotification("IMAGE_LIST", filteredImageList); + self.sendSocketNotification("IMAGE_LIST", imageList); return; } else { console.log("[" + this.name + "] WARNING: did not get any images from nextcloud url"); @@ -119,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" } }