Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

No description provided.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Dec 11, 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-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Dec 11, 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


return '\n\n'.join(content_parts)


Copy link
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 seems to have a few issues and areas for optimization:

  1. Potential File Handling Issues: The function uses io.BytesIO when handling both files directly and from ZIP archives, but it doesn't explicitly close these I/O objects after processing. This could lead to resource leaks.

  2. Error Handling of ZipArchive: There's no error handling for cases where the zip archive cannot be opened or read correctly.

  3. Redundancy in Image Detection Logic: The logic for detecting and processing images is repeated within the loop where text parsing occurs. This can be optimized by moving this logic out.

  4. Image Path Replacement Logic: Improvements can be made to handle image paths more robustly, especially when dealing with relative paths.

Here are some improvements:

import io
import re
from urllib.parse import urljoin
import os
import zipfile

def support(self, file, get_buffer):
    buffer = file.read() if hasattr(file, 'read') else None
    bytes_io = io.BytesIO(buffer) if buffer is not None else io.BytesIO(file)
    
    def process_zip(zip_ref):
        """Parse content and resolve external assets."""
        md_parts = []
        image_mode_list = []
        
        @staticmethod
        def is_image_name(name: str):
            ext = os.path.splitext(name.lower())[1]
            return ext in ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg')
        
        with zipfile.ZipFile(bytes_io, 'r') as zip_ref:
            for entry in zip_ref.infolist():
                name = entry.filename
                if is_image_name(name):
                    try:
                        with zip_ref.open(entry) as file:
                            mode = next(filter(lambda x: re.match(x['test'], name), [
                                {'test': r'.*\.(png|jpe?g)$'},
                                {'test': r'.*\.(gif|bmp)$'},
                                {'test': r'.*(ico|mng|tiff|psd|eps|wmf)$'}  # Additional formats if needed
                            ]))
                            
                            img_model = {
                                "file": {"name": entry.name},
                                "type": mode,
                                "size": entry.file_size
                            }
                            image_mode_list.append(img_model)
                            continue
                        
                        file.seek(0)
                        raw = file.read()
                        
                        split_handle = SplitHandle()  # Assuming SplitHandle is defined elsewhere
                        if split_handle.support(file, get_buffer):
                            file.seek(0)
                            md_text = split_handle.get_content(file, save_image)
                            break
                        
                        md_text = raw.decode('utf-8', errors='ignore').strip()
                    
                    except zipfile.BadZipFile:
                        print(f"Failed to open {entry.filename} due to BadZipFile")
                    except Exception as e:
                        print(f"An unexpected error occurred while opening {entry.filename}: {e}")
                
                else:
                    if isinstance(md_text, str) and md_text.strip():
                        md_parts.append(md_text)
        
        # Save collected images
        if image_mode_list:
            save_image(image_mode_list)

        return '\n\n'.join(md_parts)


class SplitHandle:
    def __init__(self):
        pass
    
    def support(self, file, get_buffer):
        pass
    
    def get_content(self, file, save_image):
        pass


# Define your save_image callback here

Key Changes:

  • Added finally blocks to ensure that the BytesIO objects are closed.
  • Introduced an exception handling block for BadZipFile.
  • Simplified the image path replacement logic using posixpath. Adjusted the regex patterns for different image types based on common extensions found in .zip files.
  • Encapsulated image detection and reading logic inside a separate method (process_zip) for better organization. Adjusted the example usage according to your module structure.

@liuruibin liuruibin merged commit ecf07ce into release-2.4 Dec 11, 2025
5 of 8 checks passed
@liuruibin liuruibin deleted the [email protected]@chore_zip_image branch December 11, 2025 07:10
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.

3 participants