From c4e0e054869fca8f85e6fcde5a20b1c05e4467ae Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 14 Dec 2017 22:14:57 +0100 Subject: [PATCH] Replace dirlist with function filtering out images --- src/components/Settings.jsx | 2 +- src/components/background/RandomImage.jsx | 2 +- src/index.js | 3 +++ src/mock.js | 10 ++++++++++ src/reducers/settings.jsx | 20 +++++++++++++++++++- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/components/Settings.jsx b/src/components/Settings.jsx index 4fac3b3..9d0efc6 100644 --- a/src/components/Settings.jsx +++ b/src/components/Settings.jsx @@ -80,7 +80,7 @@ const colorSchemes = [< MenuItem value = "random" key = "random" primaryText = { } imageReadyHandler(event) {} updateImages() { - const imagePaths = greeterutil.dirlist(this.props.directory); + const imagePaths = greeterutil.dirimages(this.props.directory); // THESE LINKS ARE NSFW -- DO NOT UNCOMMENT // const imagePaths = [ diff --git a/src/components/background/RandomImage.jsx b/src/components/background/RandomImage.jsx index 98f6d51..63eac4c 100644 --- a/src/components/background/RandomImage.jsx +++ b/src/components/background/RandomImage.jsx @@ -8,7 +8,7 @@ class RandomImage extends React.Component { if (!this.props.directory) return console.error('Images directory prop not given.'); - this.imagePaths = greeterutil.dirlist(this.props.directory); + this.imagePaths = greeterutil.dirimages(this.props.directory); if (typeof this.imagePaths === 'undefined') this.imagePaths = ['']; } diff --git a/src/index.js b/src/index.js index c11d4e7..42c36e4 100755 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,9 @@ if (!lightdm && process.env.NODE_ENV === 'development') { window.greeterutil = { dirlist: function(dir) { return undefined; + }, + dirimages: function(dir) { + return undefined; } } } diff --git a/src/mock.js b/src/mock.js index 4a162e6..dc578eb 100755 --- a/src/mock.js +++ b/src/mock.js @@ -278,6 +278,16 @@ class GreeterUtil { return this._mock_data.dirlist; } + /** + * Returns list of images inside directory at `path`. + * + * @param path + * @returns {String[]} List of abs paths for the images found in `path`. + */ + dirimages(path) { + return this._mock_data.dirimages; + } + /** * Escape HTML entities in a string. * diff --git a/src/reducers/settings.jsx b/src/reducers/settings.jsx index 1d5b54f..f9a2548 100644 --- a/src/reducers/settings.jsx +++ b/src/reducers/settings.jsx @@ -1,6 +1,24 @@ const moment = require('moment'); require('moment-timezone'); +function dirimages(dir) +{ + let imagesExtensions = ["jpg", "jpeg", "png"]; + function isImage(path) { + let filename = path.substring(path.lastIndexOf("/") + 1); + let dotPosition = filename.lastIndexOf("."); + if (dotPosition > -1) { + let extension = filename.substring(dotPosition + 1); + if (imagesExtensions.indexOf(extension) > -1) { + return true; + } + } + return false; + } + return greeterutil.dirlist(dir).filter(isImage); +} +greeterutil.dirimages = dirimages; + let initialState = { 'language': lightdm.languages[0].name, 'backgroundEngine': 'trianglify', @@ -16,7 +34,7 @@ let initialState = { 'scaling': 1, 'alignment': 1 // 0: left, 1: center, 2: right }; -let firstImage = greeterutil.dirlist(initialState['backgroundImagesDirectory']) ? greeterutil.dirlist(initialState['backgroundImagesDirectory'])[0] : false; +let firstImage = greeterutil.dirimages(initialState['backgroundImagesDirectory']) ? greeterutil.dirimages(initialState['backgroundImagesDirectory'])[0] : false; initialState['backgroundImageLocation'] = firstImage || ''; // If hide users is set with lightdm, then clear the default users