Skip to content

Commit

Permalink
Merge pull request #14664 from akshayasalvi/ak-replace-file-name-with…
Browse files Browse the repository at this point in the history
…-underscore
  • Loading branch information
dangrous authored Jan 31, 2023
2 parents 07462cf + a572080 commit 3a06fc5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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);
}
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 @@ -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,
Expand Down Expand Up @@ -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,
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,
};

0 comments on commit 3a06fc5

Please sign in to comment.