Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/AttachmentPicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import CONST from '../../CONST';
import {propTypes, defaultProps} from './attachmentPickerPropTypes';
import * as FileUtils from '../../libs/fileDownload/FileUtils';

/**
* Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE
Expand Down Expand Up @@ -31,10 +32,14 @@ class AttachmentPicker extends React.Component {
type="file"
ref={el => this.fileInput = el}
onChange={(e) => {
const file = e.target.files[0];
let file = e.target.files[0];

if (file) {
const cleanName = FileUtils.cleanFileName(file.name);
file.uri = URL.createObjectURL(file);
if (file.name !== cleanName) {
file = new File([file], cleanName);
}
Comment on lines +40 to +42

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have included the filetype to prevent this bug . The bug arises after a backend change aimed at validating files by their types.

this.onPicked(file);
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import compose from '../../libs/compose';
import launchCamera from './launchCamera';
import CONST from '../../CONST';
import * as FileUtils from '../../libs/fileDownload/FileUtils';

const propTypes = {
...basePropTypes,
Expand Down Expand Up @@ -72,7 +73,7 @@ function getDataForUpload(fileData) {
fileName = `${fileName}${fileExtension}`;
}
const fileResult = {
name: fileName,
name: FileUtils.cleanFileName(fileName),
type: fileData.type,
width: fileData.width,
height: fileData.height,
Expand Down
11 changes: 11 additions & 0 deletions src/libs/fileDownload/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,22 @@ function splitExtensionFromFileName(fullFileName) {
return {fileName: splitFileName.join('.'), fileExtension};
}

/**
* Returns the filename replacing special characters with underscore
*
* @param {String} fileName
* @returns {String}
*/
function cleanFileName(fileName) {
return fileName.replace(/[^a-zA-Z0-9\-._]/g, '_');
}

export {
showGeneralErrorAlert,
showSuccessAlert,
showPermissionErrorAlert,
splitExtensionFromFileName,
getAttachmentName,
getFileType,
cleanFileName,
};