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

Fix import in Windows 10 + Chrome #2582

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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