Skip to content

Commit

Permalink
Reject unsupported file types
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedex committed Mar 2, 2017
1 parent 2a461b9 commit bfb8e84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/components/Converter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ArrowDown from './svg/ArrowDown'
import Cancel from './svg/Cancel'

import { convert } from '../api'
import { removeByKey, uniqueFiles, displayOutputFileName, filterImages } from '../helpers/util'
import { removeByKey, uniqueAndValidFiles, displayOutputFileName, filterImages } from '../helpers/util'
import {
fileTypes,
MERGE,
Expand All @@ -26,10 +26,10 @@ import {

const drop = (props, monitor, component) => {
const { files } = monitor.getItem()
const filesUnique = uniqueFiles(component.state.files, files)
const stagingFiles = uniqueAndValidFiles(component.state.files, files)
component.setState({
status: Object.keys(filesUnique).length ? STAGING : IDLE,
files: filesUnique
status: Object.keys(stagingFiles).length ? STAGING : IDLE,
files: stagingFiles
})

if (component.state.shifted) {
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { pluck, compose, map } = require('./functional')
const { fileTypes } = require('./constants')

const replaceSpaceCharacters = str =>
str.replace(/\s/g, '\\ ')
Expand Down Expand Up @@ -26,9 +27,15 @@ function centerEllipsis(str, length = 7) {
const displayOutputFileName = outputType =>
compose(filterImages, map(pluck('path')), createOutputFileName(outputType), centerEllipsis)

const uniqueFiles = (files, newArray) =>
const fileTypesArr = [].concat(...(Object.keys(fileTypes).map(key => fileTypes[key])))

const isValidFileType = fileType => fileTypesArr.indexOf(fileType) > -1

const getFileType = fileName => fileName.split('.').pop()

const uniqueAndValidFiles = (files, newArray) =>
newArray.reduce((accum, next) => {
if (accum[next.path]) return accum
if (accum[next.path] || !isValidFileType(getFileType(next.name))) return accum
return Object.assign(accum, {
[next.path]: next
})
Expand All @@ -48,5 +55,5 @@ module.exports = {
concatFiles,
removeByKey,
replaceSpaceCharacters,
uniqueFiles
uniqueAndValidFiles,
}

0 comments on commit bfb8e84

Please sign in to comment.