Skip to content

Commit

Permalink
Merge pull request #889 from City-of-Helsinki/hds-1483-fix-broken-path
Browse files Browse the repository at this point in the history
Hds 1483 fix broken path
  • Loading branch information
tuomashatakka authored Nov 22, 2022
2 parents 8c60162 + 66f468c commit 961ca2d
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions packages/react/src/components/fileInput/FileInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'path';

import React, { ChangeEvent, useEffect, useRef, useState } from 'react';
import uniqueId from 'lodash.uniqueid';

Expand Down Expand Up @@ -271,9 +269,34 @@ type ValidationError = {
text: string;
};

// Return the extension of the path, from the last '.' to end of string in the last portion of the path.
const getExtension = (path: string): string => {
if (!path || typeof path !== 'string' || path === '') {
// eslint-disable-next-line no-console
console.warn(`FileInput: Path must be a non-empty string. Path is now ${JSON.stringify(path)}`);
return '';
}

const lastDotIndex = path.lastIndexOf('.');
if (lastDotIndex === -1) {
// eslint-disable-next-line no-console
console.warn('FileInput: File is missing extension');
return '';
}

const extensionWithDot = path.substring(lastDotIndex);
if (extensionWithDot.length <= 1) {
// eslint-disable-next-line no-console
console.warn('FileInput: File is missing extension');
return '';
}

return extensionWithDot;
};

const validateAccept = (language: Language, accept: string) => (file: File): true | ValidationError => {
const extension = path.extname(file.name);
const fileType = file.type;
const extension: string = getExtension(file.name);
const fileType: string = file.type;
const acceptedExtensions = accept.split(',').map((str) => str.trim());
const isMatchingType = !!acceptedExtensions.find(
(acceptExtension) => acceptExtension.includes(fileType) || acceptExtension.includes(`${fileType.split('/')[0]}/*`),
Expand Down

0 comments on commit 961ca2d

Please sign in to comment.