diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 082853285bc4..dddceb7cf7df 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.js @@ -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 @@ -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); + } this.onPicked(file); } diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index a7a96bc5cb7f..fed0957e8fd8 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -17,6 +17,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, @@ -67,7 +68,7 @@ const documentPickerOptions = { function getDataForUpload(fileData) { const fileName = fileData.fileName || fileData.name || 'chat_attachment'; const fileResult = { - name: fileName, + name: FileUtils.cleanFileName(fileName), type: fileData.type, width: fileData.width, height: fileData.height, diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index 197b00e22eb6..4ad682ff0bc2 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -116,6 +116,16 @@ 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, @@ -123,4 +133,5 @@ export { splitExtensionFromFileName, getAttachmentName, getFileType, + cleanFileName, };