From ff3119b5cfad82e119c21b319035f306ba608dd2 Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Thu, 26 Jan 2023 06:24:06 +0530 Subject: [PATCH 1/9] added method to file utils for clean filename --- src/libs/fileDownload/FileUtils.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index 197b00e22eb6..ee5e48cb1f22 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -114,6 +114,15 @@ function splitExtensionFromFileName(fullFileName) { const splitFileName = fileName.split('.'); const fileExtension = splitFileName.pop(); 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 { @@ -123,4 +132,5 @@ export { splitExtensionFromFileName, getAttachmentName, getFileType, + cleanFileName, }; From 8ca25fd6983ea24292fb0f929f8b48043a4ea9a9 Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Thu, 26 Jan 2023 06:27:07 +0530 Subject: [PATCH 2/9] use clean filename for web upload --- src/components/AttachmentPicker/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 082853285bc4..7d0e0825c35d 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 { propTypes, defaultProps } from './attachmentPickerPropTypes'; +import { cleanFileName } from '../../libs//fileDownload/' /** * Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE @@ -34,8 +35,10 @@ class AttachmentPicker extends React.Component { const file = e.target.files[0]; if (file) { + const cleanName = cleanFileName(file.name); file.uri = URL.createObjectURL(file); - this.onPicked(file); + const updatedFile = new File([file], cleanName); + this.onPicked(updatedFile); } // Cleanup after selecting a file to start from a fresh state From 223dfe9d70001e25368a5a8fcfdbb3cf58934eaf Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Mon, 30 Jan 2023 23:40:58 +0530 Subject: [PATCH 3/9] fixed error --- src/libs/fileDownload/FileUtils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index ee5e48cb1f22..dfd1012a6e56 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -114,6 +114,7 @@ function splitExtensionFromFileName(fullFileName) { const splitFileName = fileName.split('.'); const fileExtension = splitFileName.pop(); return {fileName: splitFileName.join('.'), fileExtension}; +} /** * Returns the filename replacing special characters with underscore From 495c5dd8c29a17ba85b9e3bd640504b99f8a1bc4 Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 00:30:52 +0530 Subject: [PATCH 4/9] fix native name --- src/components/AttachmentPicker/index.js | 2 +- src/components/AttachmentPicker/index.native.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 7d0e0825c35d..2ca9a6424cb4 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.js @@ -1,7 +1,7 @@ import React from 'react'; import CONST from '../../CONST'; import { propTypes, defaultProps } from './attachmentPickerPropTypes'; -import { cleanFileName } from '../../libs//fileDownload/' +import {cleanFileName} from '../../libs/fileDownload/FileUtils'; /** * Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 6944f75fd2cf..7fff0b5fda2e 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -18,6 +18,7 @@ import withLocalize, {withLocalizePropTypes} from '../withLocalize'; import compose from '../../libs/compose'; import launchCamera from './launchCamera'; import CONST from '../../CONST'; +import {cleanFileName} from '../../libs/fileDownload/FileUtils'; const propTypes = { ...basePropTypes, @@ -72,7 +73,7 @@ function getDataForUpload(fileData) { fileName = `${fileName}${fileExtension}`; } const fileResult = { - name: fileName, + name: cleanFileName(fileName), type: fileData.type, width: fileData.width, height: fileData.height, From a86aa9994f9795558e55dfed5f6b5f567deedcbc Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 03:10:20 +0530 Subject: [PATCH 5/9] correct regex --- src/libs/fileDownload/FileUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index dfd1012a6e56..71d51932ba8c 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -123,7 +123,7 @@ function splitExtensionFromFileName(fullFileName) { * @returns {String} */ function cleanFileName(fileName) { - return fileName.replace(/[^a-zA-Z0-9\-\\._]+/g, '_'); + return fileName.replace(/[^a-zA-Z0-9\-\._]/g, '_'); } export { From 6ba344bb3057955046af3dcc07dbe9670acce82a Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 03:17:29 +0530 Subject: [PATCH 6/9] fix lint issues --- src/components/AttachmentPicker/index.js | 6 +++--- src/components/AttachmentPicker/index.native.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 2ca9a6424cb4..03856ecd7fd0 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.js @@ -1,7 +1,7 @@ import React from 'react'; import CONST from '../../CONST'; -import { propTypes, defaultProps } from './attachmentPickerPropTypes'; -import {cleanFileName} from '../../libs/fileDownload/FileUtils'; +import {propTypes, defaultProps} from './attachmentPickerPropTypes'; +import * as FileUtils from '../../libs/fileDownload/FileUtils'; /** * Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE @@ -35,7 +35,7 @@ class AttachmentPicker extends React.Component { const file = e.target.files[0]; if (file) { - const cleanName = cleanFileName(file.name); + const cleanName = FileUtils.cleanFileName(file.name); file.uri = URL.createObjectURL(file); const updatedFile = new File([file], cleanName); this.onPicked(updatedFile); diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 7fff0b5fda2e..56b34f272f9b 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -18,7 +18,7 @@ import withLocalize, {withLocalizePropTypes} from '../withLocalize'; import compose from '../../libs/compose'; import launchCamera from './launchCamera'; import CONST from '../../CONST'; -import {cleanFileName} from '../../libs/fileDownload/FileUtils'; +import * as FileUtils from '../../libs/fileDownload/FileUtils'; const propTypes = { ...basePropTypes, @@ -73,7 +73,7 @@ function getDataForUpload(fileData) { fileName = `${fileName}${fileExtension}`; } const fileResult = { - name: cleanFileName(fileName), + name: FileUtils.cleanFileName(fileName), type: fileData.type, width: fileData.width, height: fileData.height, From e0e1dd4ddae99c3778497cb1ad373e6f8a2ba8a8 Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 03:23:55 +0530 Subject: [PATCH 7/9] fix regex --- src/libs/fileDownload/FileUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index 71d51932ba8c..4ad682ff0bc2 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -123,7 +123,7 @@ function splitExtensionFromFileName(fullFileName) { * @returns {String} */ function cleanFileName(fileName) { - return fileName.replace(/[^a-zA-Z0-9\-\._]/g, '_'); + return fileName.replace(/[^a-zA-Z0-9\-._]/g, '_'); } export { From db9a299f84c658e59306582d7e07265f28ad04ba Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 22:39:20 +0530 Subject: [PATCH 8/9] make copy file optional --- src/components/AttachmentPicker/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 03856ecd7fd0..4f86ade1002e 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.js @@ -32,13 +32,15 @@ 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); - const updatedFile = new File([file], cleanName); - this.onPicked(updatedFile); + if (file.name !== cleanName) { + file = new File([file], cleanName); + } + this.onPicked(file); } // Cleanup after selecting a file to start from a fresh state From a572080ab8e25239bafc354059b91ab3ca610b18 Mon Sep 17 00:00:00 2001 From: Akshaya Salvi Date: Tue, 31 Jan 2023 22:51:35 +0530 Subject: [PATCH 9/9] remove trailing space --- src/components/AttachmentPicker/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.js index 4f86ade1002e..dddceb7cf7df 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.js @@ -38,7 +38,7 @@ class AttachmentPicker extends React.Component { const cleanName = FileUtils.cleanFileName(file.name); file.uri = URL.createObjectURL(file); if (file.name !== cleanName) { - file = new File([file], cleanName); + file = new File([file], cleanName); } this.onPicked(file); }