diff --git a/Community/Tdarr_Plugin_00td_filter_by_file_property.js b/Community/Tdarr_Plugin_00td_filter_by_file_property.js index b0a57db..09416c7 100644 --- a/Community/Tdarr_Plugin_00td_filter_by_file_property.js +++ b/Community/Tdarr_Plugin_00td_filter_by_file_property.js @@ -68,9 +68,88 @@ const details = () => ({ ], }); +const conditionMet = (response, inputsArr, value, condition) => { + for (let j = 0; j < inputsArr.length; j += 1) { + try { + let v = value; + let i = inputsArr[j]; + + if ( + condition === '>' + || condition === '>=' + || condition === '<' + || condition === '<=' + + ) { + v = Number(value); + i = Number(inputsArr[j]); + } else if ( + condition === '==' + || condition === '!=' + || condition === 'includes' + || condition === 'not includes' + ) { + v = String(value); + i = String(inputsArr[j]); + } + + response.infoLog += ` Checking property value of ${v} ${condition} input value of ${i} \n`; + + switch (condition) { + case '==': + if (v === i) { + return true; + } + break; + case '!=': + if (v !== i) { + return true; + } + break; + case '>': + if (v > i) { + return true; + } + break; + case '>=': + if (v >= i) { + return true; + } + break; + case '<': + if (v < i) { + return true; + } + break; + + case '<=': + if (v <= i) { + return true; + } + break; + case 'includes': + if (v.includes(i)) { + return true; + } + break; + case 'not includes': + if (!v.includes(i)) { + return true; + } + break; + default: + } + } catch (err) { + // eslint-disable-next-line no-console + console.log(err); + } + } + + return false; +}; + // eslint-disable-next-line no-unused-vars const plugin = (file, librarySettings, inputs, otherArguments) => { - const { conditionMet } = require('../methods/utils'); const lib = require('../methods/lib')(); // eslint-disable-next-line no-unused-vars,no-param-reassign inputs = lib.loadDefaultValues(inputs, details); @@ -100,26 +179,25 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { const propertyValues = inputs.propertyValues.trim().split(','); try { - const fileContainsProperty = conditionMet(propertyValues, file[propertyName], inputs.condition); - - const message = `File property ${propertyName} of ${file[propertyName]}` - + ` being one of ${propertyValues.join(',')} has`; + const isConditionMet = conditionMet(response, propertyValues, file[propertyName], inputs.condition); + response.infoLog += ` isConditionMet: ${isConditionMet} \n`; + response.infoLog += ` continueIfPropertyFound: ${inputs.continueIfPropertyFound} \n`; if (inputs.continueIfPropertyFound === true) { - if (fileContainsProperty === true) { + if (isConditionMet === true) { response.processFile = true; - response.infoLog += `${message} been found, continuing to next plugin \n`; + response.infoLog += 'Continuing to next plugin \n'; } else { response.processFile = false; - response.infoLog += `${message} not been found, breaking out of stack \n`; + response.infoLog += 'Breaking out of stack \n'; } } else if (inputs.continueIfPropertyFound === false) { - if (fileContainsProperty === true) { + if (isConditionMet === true) { response.processFile = false; - response.infoLog += `${message} been found, breaking out of stack \n`; + response.infoLog += 'Breaking out of stack \n'; } else { response.processFile = true; - response.infoLog += `${message} not been found, continuing to next plugin \n`; + response.infoLog += 'Continuing to next plugin \n'; } } } catch (err) { diff --git a/methods/utils.js b/methods/utils.js index 17f8d89..11dfdc1 100644 --- a/methods/utils.js +++ b/methods/utils.js @@ -18,63 +18,6 @@ const strHasValue = (inputsArr, value, exactMatch) => { return contains; }; -const conditionMet = (inputsArr, value, condition) => { - for (let j = 0; j < inputsArr.length; j += 1) { - try { - switch (condition) { - case '==': - if (inputsArr[j] === String(value)) { - return true; - } - break; - case '!=': - if (inputsArr[j] !== String(value)) { - return true; - } - break; - case '>': - if (inputsArr[j] > Number(value)) { - return true; - } - break; - case '>=': - if (inputsArr[j] >= Number(value)) { - return true; - } - break; - case '<': - if (inputsArr[j] < Number(value)) { - return true; - } - break; - - case '<=': - if (inputsArr[j] <= Number(value)) { - return true; - } - break; - case 'includes': - if (String(value).includes(inputsArr[j])) { - return true; - } - break; - case 'not includes': - if (!String(value).includes(inputsArr[j])) { - return true; - } - break; - default: - } - } catch (err) { - // eslint-disable-next-line no-console - console.log(err); - } - } - - return false; -}; - module.exports = { strHasValue, - conditionMet, }; diff --git a/tests/Community/Tdarr_Plugin_00td_filter_by_file_property.js b/tests/Community/Tdarr_Plugin_00td_filter_by_file_property.js index 4911788..9d318d6 100644 --- a/tests/Community/Tdarr_Plugin_00td_filter_by_file_property.js +++ b/tests/Community/Tdarr_Plugin_00td_filter_by_file_property.js @@ -12,7 +12,10 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property container of mkv being one of mkv,mp4 has been found, breaking out of stack \n', + infoLog: ' Checking property value of mkv == input value of mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', }, }, @@ -30,7 +33,10 @@ const tests = [ }, output: { processFile: true, - infoLog: 'File property container of mkv being one of avi has not been found, continuing to next plugin \n', + infoLog: ' Checking property value of mkv == input value of avi \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', }, }, @@ -48,7 +54,10 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property container of mkv being one of mkv has been found, breaking out of stack \n', + infoLog: ' Checking property value of mkv == input value of mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', }, }, @@ -66,7 +75,11 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property video_resolution of 1080p being one of 720p,1080p has been found, breaking out of stack \n', + infoLog: ' Checking property value of 1080p == input value of 720p \n' + + ' Checking property value of 1080p == input value of 1080p \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', }, }, @@ -84,7 +97,11 @@ const tests = [ }, output: { processFile: true, - infoLog: 'File property video_resolution of 1080p being one of 721p,1081p has not been found, continuing to next plugin \n', + infoLog: ' Checking property value of 1080p == input value of 721p \n' + + ' Checking property value of 1080p == input value of 1081p \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', }, }, @@ -103,7 +120,10 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property file of C:/Transcode/Source Folder/h264.mkv being one of Source Folder/h264.mkv has been found, breaking out of stack \n', + infoLog: ' Checking property value of C:/Transcode/Source Folder/h264.mkv includes input value of Source Folder/h264.mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', }, }, @@ -123,7 +143,10 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property container of mkv being one of avi has not been found, breaking out of stack \n', + infoLog: ' Checking property value of mkv == input value of avi \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: true \n' + + 'Breaking out of stack \n', }, }, @@ -141,7 +164,10 @@ const tests = [ }, output: { processFile: true, - infoLog: 'File property container of mkv being one of mkv has been found, continuing to next plugin \n', + infoLog: ' Checking property value of mkv == input value of mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: true \n' + + 'Continuing to next plugin \n', }, }, @@ -159,7 +185,10 @@ const tests = [ }, output: { processFile: true, - infoLog: 'File property container of mkv being one of mkv,mp4 has been found, continuing to next plugin \n', + infoLog: ' Checking property value of mkv == input value of mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: true \n' + + 'Continuing to next plugin \n', }, }, @@ -177,7 +206,11 @@ const tests = [ }, output: { processFile: false, - infoLog: 'File property video_resolution of 1080p being one of 721p,1081p has not been found, breaking out of stack \n', + infoLog: ' Checking property value of 1080p == input value of 721p \n' + + ' Checking property value of 1080p == input value of 1081p \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: true \n' + + 'Breaking out of stack \n', }, }, @@ -195,7 +228,306 @@ const tests = [ }, output: { processFile: true, - infoLog: 'File property file of C:/Transcode/Source Folder/h264.mkv being one of Source Folder/h264.mkv has been found, continuing to next plugin \n', + infoLog: ' Checking property value of C:/Transcode/Source Folder/h264.mkv includes input value of Source Folder/h264.mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: true \n' + + 'Continuing to next plugin \n', + }, + }, + + // check other conditions + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'mkv', + condition: '==', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of mkv == input value of mkv \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'avi', + condition: '==', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of mkv == input value of avi \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '60', + condition: '>', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of 64.9300765991211 > input value of 60 \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '70', + condition: '>', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of 64.9300765991211 > input value of 70 \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '60', + condition: '>=', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of 64.9300765991211 >= input value of 60 \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '70', + condition: '>=', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of 64.9300765991211 >= input value of 70 \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '60', + condition: '<', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of 64.9300765991211 < input value of 60 \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '70', + condition: '<', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of 64.9300765991211 < input value of 70 \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '60', + condition: '<=', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of 64.9300765991211 <= input value of 60 \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'file_size', + propertyValues: '70', + condition: '<=', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of 64.9300765991211 <= input value of 70 \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'mk', + condition: 'includes', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of mkv includes input value of mk \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'av', + condition: 'includes', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of mkv includes input value of av \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'mk', + condition: 'not includes', + + }, + otherArguments: {}, + }, + output: { + processFile: true, + infoLog: ' Checking property value of mkv not includes input value of mk \n' + + ' isConditionMet: false \n' + + ' continueIfPropertyFound: false \n' + + 'Continuing to next plugin \n', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_2.json')), + librarySettings: {}, + inputs: { + propertyName: 'container', + propertyValues: 'av', + condition: 'not includes', + + }, + otherArguments: {}, + }, + output: { + processFile: false, + infoLog: ' Checking property value of mkv not includes input value of av \n' + + ' isConditionMet: true \n' + + ' continueIfPropertyFound: false \n' + + 'Breaking out of stack \n', }, }, ];