Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace file names with underscore when they have slash / #14664

Merged
5 changes: 4 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 @@ -34,8 +35,10 @@ class AttachmentPicker extends React.Component {
const file = e.target.files[0];

if (file) {
const cleanName = FileUtils.cleanFileName(file.name);
file.uri = URL.createObjectURL(file);
this.onPicked(file);
const updatedFile = new File([file], cleanName);
akshayasalvi marked this conversation as resolved.
Show resolved Hide resolved
this.onPicked(updatedFile);
}

// Cleanup after selecting a file to start from a fresh state
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,
};