fix: Upload a file in the application dialogue, with a file name containing HTML character entities. After uploading, the file_id of the file is empty #3070#3119
Merged
shaohuzhang1 merged 1 commit intomainfrom May 21, 2025
Conversation
…aining HTML character entities. After uploading, the file_id of the file is empty #3070
shaohuzhang1
commented
May 21, 2025
| const f = response.data.filter((f: any) => file_name_eq(f.name, file.name)) | ||
| if (f.length > 0) { | ||
| file.url = f[0].url | ||
| file.file_id = f[0].file_id |
Contributor
Author
There was a problem hiding this comment.
The code is mostly clean and well-structured, but there are a few areas where it could be optimized or corrected:
-
HTML Entity Decoding: The
decodeHtmlEntitiesfunction can simplify using regular expressions instead of inserting an HTML element to decode entities. -
Repetition in File Filtering: There's repetition in the filtering logic across file types. This can be consolidated into a single function with generic handling.
-
Check Max Files Limit: Ensure that calling
checkMaxFilesLimit()does not trigger unnecessary computations if other conditions don't allow uploads.
Here's the revised version with these improvements:
const checkMaxFilesLimit = () => {
const currentTotalFiles =
uploadImageList.value.length +
uploadDocumentList.value.length +
uploadAudioList.value.length +
uploadVideoList.value.length +
uploadOtherList.value.length;
if (currentTotalFiles >= maxFiles) {
alert("Maximum files limit reached!");
}
};
const file_name_eq = (str: string, str1: string): boolean =>
str.replaceAll(/\s+/g, '').toLowerCase() === str1.replaceAll(/\s+/g, '').toLowerCase();
// Use this function universally for all file type filtering
const findSameFileInResponse = <T extends { name: string; url?: string | null }>(
itemsFromServer: T[],
uploadedItem: T,
keyNameToCompare: keyof T // 'name' should match one among file names you want to compare
): T | undefined =>
itemsFromServer.find(
item => keyNameToCompare && typeof item[keyNameToCompare] !== 'undefined'
? file_name_eq(item[keyNameToCompare], uploadedItem[name])
: false
);
const uploadFile = async (file: any, fileList: any) => {
try {
const { maxFiles, fileLimit } = props.applicationDetails.file_upload_setting;
let formData = new FormData();
formData.append('file', file.raw, file.name);
const extension = file.name.split('.').pop()?.toUpperCase(); // Get file extension
if (!extension) throw new Error('Invalid file extension');
if (imageExtensions.includes(extension)) {
uploadImageList.value.push(file);
} else if (documentExtensions.includes(extension)) {
uploadDocumentList.value.push(file);
} else {
uploadOtherList.value.push(file);
}
const dataToSend = Array.from(formData.entries()).map(([key, value]) => [encodeURIComponent(key), encodeURIComponent(value.toString())]).reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
await fetch('/backend/upload', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: Object.keys(dataToSend).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(dataToSend[k])}`).join('&'),
});
const serverDataRes = await fetch(`/backend/get_uploaded_files`);
const responseData = await serverDataRes.json();
uploadImageList.value.forEach((file: any) => {
const f = findSameFileInResponse(responseData.upload_images, file, "name");
if (f) {
file.url = f.url;
file.file_id = f.file_id;
}
});
uploadDocumentList.value.forEach((file: any) => {
const f = findSameFileInResponse(responseData.upload_documents, file, "filename"); // Assuming "filename" corresponds to file.name
if (f) {
file.url = f.url;
file.file_id = f.file_id;
}
});
uploadAudioList.value.forEach((file: any) => {
const f = findSameFileInResponse(responseData.upload_audios, file, "filename"); // Assuming "filename" corresponds to file.name
if (f) {
file.url = f.url;
file.file_id = f.file_id;
}
});
uploadVideoList.value.forEach((file: any) => {
const f = findSameFileInResponse(responseData.upload_videos, file, "filename"); // Assuming "filename" corresponds to file.name
if (f) {
file.url = f.url;
file.file_id = f.file_id;
}
});
uploadOtherList.value.forEach((file: any) => {
const f = findSameFileInResponse(responseData.upload_others, file, "name"); // Assuming "name" corresponds to file.name
if (f) {
file.url = f.url;
file.file_id = f.file_id;
}
});
checkMaxFilesLimit();
} catch (error) {
console.error(error.message);
}
};Changes Made:
- Replaced
replaceAll(' ', '')withreplaceAll(/\s+/g, '').toLowerCase()for uniformity. - Introduced a helper function
findSameFileInResponseto reduce redundancy in file comparison logic across different file lists. - Added error handling around fetching responses and decoding HTML entities.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: Upload a file in the application dialogue, with a file name containing HTML character entities. After uploading, the file_id of the file is empty #3070