fix: adjust props and function to format filename on upload input#786
fix: adjust props and function to format filename on upload input#786
Conversation
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
📝 WalkthroughWalkthroughThis PR consolidates duplicate media input value transformation logic from three form components into a centralized Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/utils/methods.js`:
- Around line 536-571: In getMediaInputValue, guard against undefined fileUrl
and names without extensions: first derive fileUrl safely (fallback to empty
string or use img.filename||img.file_path||img.file_url || ""), check existence
before calling includes/split, and compute basename robustly (use lastSegment =
fileUrl.includes("/") ? ... only if fileUrl; otherwise use an empty string or
img.name). When splitting the filename, detect if there is no dot and treat the
whole name as nameWithoutExtension with ext as an empty string (or a default
like "bin") so filename construction doesn't produce ".name"; ensure final
filename assembly handles empty ext (e.g., omit ".") and that the timestamp
concat is applied to a string (use String(filename) + "?t=" + Date.now()).
Update references inside getMediaInputValue (variables fileUrl, filename, parts,
ext, nameWithoutExtension) accordingly.
| export const getMediaInputValue = ( | ||
| entity, | ||
| fieldName = "images", | ||
| maxLength = TRIM_TEXT_LENGTH_20 | ||
| ) => { | ||
| const mediaFiles = entity?.[fieldName]; | ||
|
|
||
| if (!mediaFiles?.length) return []; | ||
|
|
||
| return mediaFiles.map((img) => { | ||
| const fileUrl = img.filename ?? img.file_path ?? img.file_url; | ||
|
|
||
| let filename = fileUrl.includes("/") ? fileUrl.split("/").pop() : fileUrl; | ||
|
|
||
| const parts = filename.split("."); | ||
| const ext = parts.pop(); | ||
| let nameWithoutExtension = parts.join("."); | ||
|
|
||
| if (nameWithoutExtension.length > maxLength) { | ||
| const startChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT); | ||
| const endChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT); | ||
| const start = nameWithoutExtension.substring(0, startChars); | ||
| const end = nameWithoutExtension.substring( | ||
| nameWithoutExtension.length - endChars | ||
| ); | ||
| nameWithoutExtension = `${start}...${end}`; | ||
| } | ||
|
|
||
| filename = `${nameWithoutExtension}.${ext}`; | ||
|
|
||
| return { | ||
| ...img, | ||
| public_url: img?.public_url || fileUrl, | ||
| filename: filename.concat("?t=", Date.now()) | ||
| }; | ||
| }); |
There was a problem hiding this comment.
Guard against missing file URLs and extension-less names.
Line 546 can yield fileUrl as undefined, which will throw on includes, split, and concat. Also, filenames without a dot become .name because the basename is treated as the extension.
🔧 Suggested fix
return mediaFiles.map((img) => {
const fileUrl = img.filename ?? img.file_path ?? img.file_url;
+ if (!fileUrl) {
+ return { ...img };
+ }
let filename = fileUrl.includes("/") ? fileUrl.split("/").pop() : fileUrl;
- const parts = filename.split(".");
- const ext = parts.pop();
- let nameWithoutExtension = parts.join(".");
+ const lastDot = filename.lastIndexOf(".");
+ const hasExt = lastDot > 0;
+ const ext = hasExt ? filename.slice(lastDot + 1) : "";
+ let nameWithoutExtension = hasExt ? filename.slice(0, lastDot) : filename;
if (nameWithoutExtension.length > maxLength) {
const startChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT);
const endChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT);
const start = nameWithoutExtension.substring(0, startChars);
const end = nameWithoutExtension.substring(
nameWithoutExtension.length - endChars
);
nameWithoutExtension = `${start}...${end}`;
}
- filename = `${nameWithoutExtension}.${ext}`;
+ filename = ext ? `${nameWithoutExtension}.${ext}` : nameWithoutExtension;
return {
...img,
public_url: img?.public_url || fileUrl,
filename: filename.concat("?t=", Date.now())
};
});🤖 Prompt for AI Agents
In `@src/utils/methods.js` around lines 536 - 571, In getMediaInputValue, guard
against undefined fileUrl and names without extensions: first derive fileUrl
safely (fallback to empty string or use
img.filename||img.file_path||img.file_url || ""), check existence before calling
includes/split, and compute basename robustly (use lastSegment =
fileUrl.includes("/") ? ... only if fileUrl; otherwise use an empty string or
img.name). When splitting the filename, detect if there is no dot and treat the
whole name as nameWithoutExtension with ext as an empty string (or a default
like "bin") so filename construction doesn't produce ".name"; ensure final
filename assembly handles empty ext (e.g., omit ".") and that the timestamp
concat is applied to a string (use String(filename) + "?t=" + Date.now()).
Update references inside getMediaInputValue (variables fileUrl, filename, parts,
ext, nameWithoutExtension) accordingly.
ref: https://app.clickup.com/t/86b883mp7
Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com
Summary by CodeRabbit
Release Notes