Skip to content

fix: adjust props and function to format filename on upload input#786

Open
tomrndom wants to merge 1 commit intomasterfrom
fix/image-upload-link-filename
Open

fix: adjust props and function to format filename on upload input#786
tomrndom wants to merge 1 commit intomasterfrom
fix/image-upload-link-filename

Conversation

@tomrndom
Copy link

@tomrndom tomrndom commented Feb 10, 2026

ref: https://app.clickup.com/t/86b883mp7

image

Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved handling of long filenames when uploading files to prevent compatibility issues.
    • Enhanced caching behavior for uploaded content to ensure the latest versions are displayed.

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
@tomrndom tomrndom requested a review from smarcet February 10, 2026 12:39
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

📝 Walkthrough

Walkthrough

This PR consolidates duplicate media input value transformation logic from three form components into a centralized getMediaInputValue utility function. The function handles filename truncation, public URL derivation, and cache-busting timestamps. Local implementations are removed and replaced with imports from the shared utility.

Changes

Cohort / File(s) Summary
Form Component Refactoring
src/components/forms/form-template-form.js, src/components/forms/inventory-item-form.js, src/pages/sponsors-global/form-templates/sponsor-inventory-popup.js
Removed local getMediaInputValue implementations and replaced with imports from shared utility. Updated UploadInputV2 value prop calls to pass entity/initialEntity and fieldName parameters.
Utility Functions & Constants
src/utils/methods.js, src/utils/constants.js
Added new exported getMediaInputValue(entity, fieldName, maxLength) function that transforms media items with filename truncation using FILENAME_TRUNCATE_SIDE_PERCENT, cache-busting timestamps, and URL handling. Added FILENAME_TRUNCATE_SIDE_PERCENT and TRIM_TEXT_LENGTH_20 constants; adjusted TRIM_TEXT_LENGTH_40 from 50 to 40.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • fntechgit/summit-admin#765: Addresses cache-busting query parameters in image filenames that are now centralized in the new getMediaInputValue utility.

Suggested reviewers

  • smarcet
  • romanetar

Poem

🐰 Three forms once shared one hidden task,
Duplicating logic—a tangled mask.
Now consolidated in shared utility's nest,
Filenames truncated, timestamps compressed! ✨
One function to rule them, clean and true.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title accurately describes the main change: refactoring how filenames are formatted on upload inputs by moving the logic to a shared utility function and adjusting component props.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/image-upload-link-filename

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +536 to +571
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())
};
});
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant