Skip to content

Commit

Permalink
fix(upload-core): fix file path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Christie Baker authored and Christie Baker committed May 10, 2018
1 parent d939c0d commit 8c5d974
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 374 deletions.
37 changes: 0 additions & 37 deletions packages/upload-core/package-lock.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Upload from './upload';
import Upload from '../upload';

const options = {
bucketId: 'a',
Expand Down Expand Up @@ -45,6 +45,7 @@ describe('upload.core', () => {

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

Expand All @@ -64,6 +65,7 @@ describe('upload.core', () => {

it('should use default options ', () => {
const file = Buffer.from('hello world'.split(''));
file.name = 'optionsFile.png';
const upload = new Upload(file, options);

expect(upload.options.endpoint).toBe(
Expand All @@ -73,6 +75,7 @@ describe('upload.core', () => {

it('should not allow files over maxSize', () => {
const file = Buffer.from('hello world!'.split(''));
file.name = 'sizeFile.pdf';
file.size = 1e7;
const upload = new Upload(file, optionsWithFileSize);
upload.start();
Expand Down Expand Up @@ -104,6 +107,7 @@ describe('upload.core', () => {

it('should parse error messages', () => {
const file = Buffer.from('hello world!'.split(''));
file.name = 'a';
const upload = new Upload(file, options);
upload.start();
const getResponseHeaderMethod = function() {
Expand All @@ -122,4 +126,29 @@ describe('upload.core', () => {
upload2.parseErrorMessage('Default Error');
expect(upload2.errorMessage).toBe('Default Error');
});

it('should check filePath for slashes', () => {
const file1 = Buffer.from('hello world!'.split(''));
file1.name = '\\bad\\file\\path\\file.pdf';
const upload1 = new Upload(
file1,
Object.assign(options, { stripFileNamePathSegments: false })
);
expect(upload1.trimFileName(file1.name)).toBe(file1.name);

const file2 = Buffer.from('hello world!'.split(''));
file2.name = '\\bad\\file\\path\\file2.pdf';
const upload2 = new Upload(file2, optionsWithMeta);
expect(upload2.trimFileName(file2.name)).toBe('file2.pdf');

const file3 = Buffer.from('hello world!'.split(''));
file3.name = '/bad/file/path/file3.pdf';
const upload3 = new Upload(file3, optionsWithMeta);
expect(upload3.trimFileName(file3.name)).toBe('file3.pdf');

const file4 = Buffer.from('hello world!'.split(''));
file4.name = 'goodFileName.pdf';
const upload4 = new Upload(file4, optionsWithMeta);
expect(upload4.trimFileName(file4.name)).toBe('goodFileName.pdf');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ class Upload {

start() {
const { file } = this;
const fileName = this.trimFileName(file.name);

const metadata = {
'availity-filename': file.name,
'availity-filename': fileName,
'availity-content-type': file.type,
'availity-attachment-name': 'N/A',
};
Expand Down Expand Up @@ -265,6 +266,14 @@ class Upload {
return true;
}

trimFileName(fileName) {
if (this.options.stripFileNamePathSegments !== false) {
fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
}
return fileName;
}

getResult(xhr) {
if (this.hasError()) {
return { status: 'rejected' };
Expand Down
Loading

0 comments on commit 8c5d974

Please sign in to comment.