Skip to content

Commit

Permalink
feat(upload-core): upload core file types check (#7)
Browse files Browse the repository at this point in the history
* feat(upload-core): check fileTypes
* test(upload-core): uploads fileTypes tests
* docs(upload-core): add fileTypes documentation
  • Loading branch information
christieb7007 authored and robmcguinness committed Feb 23, 2018
1 parent 5af448e commit 080d887
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
18 changes: 15 additions & 3 deletions packages/upload-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@ npm install @availity/upload-core tus-js-client --save

## Usage

###Required params

- bucketId
- customerId
- clientId

###Optional params

- fileTypes: string array of file extensions to allow (error thrown if file.name does not contain one of the types)

```js
import Upload from '@availity/upload-core';

const upload = new Upload(file, {
bucketId: 'a',
customerId: 'b',
clientId: 'c',
bucketId: 'a',
customerId: 'b',
clientId: 'c',
fileTypes: ['.png', '.pdf']
});
upload.start();
```

27 changes: 27 additions & 0 deletions packages/upload-core/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Upload {
throw Error('[options.clientId] must be defined');
}

if (!this.isValidFileType(file.name, options)) {
throw Error(
`[options.fileTypes] was defined as ${options.fileTypes} but the file ${
file.name
} does not meet criteria`
);
}

this.file = file;
this.options = Object.assign(options, defaults);
this.percentage = 0;
Expand Down Expand Up @@ -170,6 +178,25 @@ class Upload {
upload.start();
}

isValidFileType(fileName, options) {
if (options.fileTypes) {
if (!fileName) {
return false;
}
for (let i = 0; i < options.fileTypes.length; i++) {
options.fileTypes[i] = options.fileTypes[i].toLowerCase();
}

const fileExt = fileName
.substring(fileName.lastIndexOf('.'))
.toLowerCase();
if (options.fileTypes.indexOf(fileExt) < 0) {
return false;
}
}
return true;
}

abort() {
if (this.upload) {
this.upload.abort();
Expand Down
25 changes: 24 additions & 1 deletion packages/upload-core/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const options = {
clientId: 'c',
};

const optionsWithFileTypes = {
bucketId: 'a',
customerId: 'b',
clientId: 'c',
fileTypes: ['.png', '.pdf'],
};

describe('upload.core', () => {
it('should be defined', () => {
expect(Upload).toBeTruthy();
Expand All @@ -23,11 +30,27 @@ describe('upload.core', () => {
}).toThrow('[options.bucketId] must be defined');
});

it('should allow single file as constructor arguement', () => {
it('should allow single file as constructor argument', () => {
const file = Buffer.from('hello world'.split(''));
new Upload(file, options); // eslint-disable-line
});

it('should throw error for invalid file type', () => {
const file = Buffer.from('hello world'.split(''));
file.name = 'notCoolFile.docx';
expect(() => {
new Upload(file, optionsWithFileTypes); // eslint-disable-line
}).toThrow(
'[options.fileTypes] was defined as .png,.pdf but the file notCoolFile.docx does not meet criteria'
);
});

it('should allow the correct file type', () => {
const file = Buffer.from('hello world'.split(''));
file.name = 'coolFile.PNG';
new Upload(file, optionsWithFileTypes); // eslint-disable-line
});

it('should use default options ', () => {
const file = [Buffer.from('hello world'.split(''))];
const upload = new Upload(file, options);
Expand Down

0 comments on commit 080d887

Please sign in to comment.