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
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
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,
};