Skip to content

fix: When uploading files during application dialogue, there are special whitespace characters and file name parsing errors#2746

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_file_upload
Mar 31, 2025
Merged

fix: When uploading files during application dialogue, there are special whitespace characters and file name parsing errors#2746
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_file_upload

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: When uploading files during application dialogue, there are special whitespace characters and file name parsing errors

…ial whitespace characters and file name parsing errors
@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Mar 31, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Mar 31, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 merged commit 36809b3 into main Mar 31, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_file_upload branch March 31, 2025 06:59
)
if (f.length > 0) {
file.url = f[0].url
file.file_id = f[0].file_id
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code contains two main areas for improvement:

  1. Whitespace Handling: The comparison logic includes .replaceAll(' ', '') multiple times within each filter call. In JavaScript (replace() does not have an option to remove multiple spaces with a single space), this will result in unexpected behavior, potentially matching filenames that contain more than one consecutive space instead of removing them.

    Suggestion: Remove the redundant replaceAll(' ', '') calls to improve clarity and correctness.

  2. Potential Issues: Although the current usage seems correct given the context (searching by exact filename including whitespace, which could be necessary), it's generally considered good practice to avoid unnecessary string operations when searching for matches.

Optimization Suggestions:

  • Consider using regular expressions with a pattern like \s* to match optional whitespace around the filenames.

Here’s how you might update the code:

const uploadFile = async (file: any, fileList: any) => {
  // ... rest of the function remains unchanged ...
  uploadImageList.value.forEach((file: any) => {
    const f = response.data.filter((f: any) => f.name === file.name);
    if (f.length > 0) {
      file.url = f[0].url;
      file.file_id = f[0].file_id;
    }
  });
  uploadDocumentList.value.forEach((file: any) => {
    const f = response.data.filter((f: any) => f.name.match(/^.*\s*$|^$|.*\d$/));
    if (f.length > 0) {
      file.url = f[0].url;
      file.file_id = f[0].file_id;
    }
  });
  uploadAudioList.value.forEach((file: any) => {
    const f = response.data.filter((f: any) => f.name.match(/^.*\s*$|^$|.*\d$/));
    if (f.length > 0) {
      file.url = f[0].url;
      file.file_id = f[0].file_id;
    }
  });
  uploadVideoList.value.forEach((file: any) => {
    const f = response.data.filter((f: any) => f.name.match(/^.*\s*$|^$|.*\d$/));
    if (f.length > 0) {
      file.url = f[0].url;
      file.file_id = f[0].file_id;
    }
  });
};

In this updated version, we use a simplified regex pattern ^.*\s*$|^$|.*\d$ to account for files where there may be some content mixed with whitespace at various positions or digits at the end of the filename. This assumes that certain formats like PDFs containing text (PDF/A) or audio/video files ending with numbers are common cases you want to handle gracefully. Adjust the pattern based on your specific requirements and data distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant