Skip to content

Replace regex with string logic in thumbnail_to_original_filename#1586

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/remove-regex-from-thumbnail-utility
Draft

Replace regex with string logic in thumbnail_to_original_filename#1586
Copilot wants to merge 2 commits intomasterfrom
copilot/remove-regex-from-thumbnail-utility

Conversation

Copy link

Copilot AI commented Mar 6, 2026

thumbnail_to_original_filename used a compiled regex to split on the __ separator between source filename and thumbnail options. This is equivalent to a plain rsplit('__', 1).

Changes

  • filer/utils/filer_easy_thumbnails.py: Replace RE_ORIGINAL_FILENAME regex and import re with pure-Python string logic:
    def thumbnail_to_original_filename(thumbnail_name):
        if '__' not in thumbnail_name:
            return None
        return thumbnail_name.rsplit('__', 1)[0]
  • tests/test_utils.py: Add ThumbnailToOriginalFilenameTestCase covering:
    • No separator → None
    • Normal case: "foo.jpg__200x200_q85.jpg""foo.jpg"
    • Multiple separators in source: "a__b.jpg__200x200.jpg""a__b.jpg"
    • Leading separator: "__opts.jpg"""
Original prompt

Create a pull request in django-cms/django-filer (base branch: master) that removes the regex usage for extracting the original filename from a thumbnail filename in filer/utils/filer_easy_thumbnails.py.

Context:

  • Current code (at least in commit 42090e83bca4a014948939d66722919a954b86ba) defines:
    • RE_ORIGINAL_FILENAME = re.compile(r"^(?P<source_filename>.*)__(?P<opts_and_ext>.*?)$")
    • thumbnail_to_original_filename(thumbnail_name) which does m = RE_ORIGINAL_FILENAME.match(thumbnail_name) and returns m.group(1) or None.
  • Replace this with equivalent pure-Python string logic using the __ separator, matching the same behavior:
    • If "__" not in thumbnail_name, return None.
    • Otherwise, split on the last occurrence of "__" and return the part before it.
    • Preserve edge-case behavior: if thumbnail_name begins with "__", return "".

Implementation requirements:

  1. Update thumbnail_to_original_filename to use rsplit('__', 1) rather than a regex.
  2. Remove now-unused regex constant/imports if they become unused (e.g., import re and RE_ORIGINAL_FILENAME).
  3. Add/adjust unit tests to cover:
    • No separator: "foo.jpg" -> None
    • Normal: "foo.jpg__200x200_q85.jpg" -> "foo.jpg"
    • Multiple separators in source filename: "a__b.jpg__200x200.jpg" -> "a__b.jpg"
    • Leading separator: "__opts.jpg" -> ""
  4. Ensure formatting and linting match repository style.

Notes:

  • The user referenced the file on master: https://github.com/django-cms/django-filer/blob/master/filer/utils/filer_easy_thumbnails.py.
  • Please confirm the file’s current contents on master and adjust changes accordingly (it may differ from the older commit).

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: fsbraun <16904477+fsbraun@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove regex usage in thumbnail filename extraction Replace regex with string logic in thumbnail_to_original_filename Mar 6, 2026
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.

2 participants