Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Fix import in Windows 10 + Chrome (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sanchez committed Jul 26, 2021
1 parent 8ddf40a commit 5d27d3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import {
} from '../utils'

describe('Address Book file validations', () => {
it('Should return wrong file extension error if file extension is not the allowed', () => {
it('Should return wrong file extension error if file type is not allowed', () => {
const file = new File([''], 'file.txt', { type: 'text/plain' })
const result = validateFile(file)
expect(result).toBe(WRONG_FILE_EXTENSION_ERROR)
})

it('Should return wrong file extension error if file extension is not valid', () => {
const file = new File([''], 'file.txt', { type: IMPORT_SUPPORTED_FORMATS[0] })
const result = validateFile(file)
expect(result).toBe(WRONG_FILE_EXTENSION_ERROR)
})

it('Should return file size error if file size is over the allowed', () => {
const file = new File([''], 'file.csv', { type: IMPORT_SUPPORTED_FORMATS[0] })
Object.defineProperty(file, 'size', { value: 1024 * 1024 + 1 })
Expand Down
5 changes: 4 additions & 1 deletion src/routes/safe/components/AddressBook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ export const WRONG_FILE_EXTENSION_ERROR = 'Only CSV files are allowed'
export const FILE_SIZE_TOO_BIG_ERROR = 'The size of the file is over 1 MB'
export const FILE_BYTES_LIMIT = 1000000
export const IMPORT_SUPPORTED_FORMATS = [
'',
'text/csv',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
]

const CSV_EXTENSION_REGEX = /([a-zA-Z0-9\s_\\.\-:])+(.csv|.ods|.xls|.xlsx)$/

export type CsvDataType = { data: string[] }[]

export const validateFile = (file: File): string | undefined => {
if (!IMPORT_SUPPORTED_FORMATS.includes(file.type)) {
if (!IMPORT_SUPPORTED_FORMATS.includes(file.type) || !CSV_EXTENSION_REGEX.test(file.name.toLowerCase())) {
return WRONG_FILE_EXTENSION_ERROR
}

Expand Down

0 comments on commit 5d27d3a

Please sign in to comment.