Skip to content

Commit fcd356c

Browse files
authored
fix: API v2 images (#1010)
* fix: consider OpenAPI images in the `ImageUsageAnalyzer` * fix: Restore API V2 images
1 parent b6ec9a9 commit fcd356c

14 files changed

+42
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ package-lock.json
55
build
66
.idea
77

8-
scripts/image_analysis/
8+
scripts/image_analysis/
9+
__pycache__/

scripts/analyze_image_usage.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, root_dir: str = "."):
2121
self.root_dir = Path(root_dir)
2222
self.docs_dir = self.root_dir / "docs"
2323
self.src_dir = self.root_dir / "src"
24+
self.openapi_dir = self.root_dir / "openapi-spec"
2425
self.static_dir = self.root_dir / "static"
2526
self.icons_dir = self.static_dir / "icons"
2627
# Create image_analysis folder in the same directory as the script
@@ -57,9 +58,9 @@ def __init__(self, root_dir: str = "."):
5758
r'["\']([^"\']*?/images/[^"\']*?\.(?:png|jpg|jpeg|gif|svg|webp))["\']',
5859
]
5960

60-
self.referenced_images = set()
61-
self.static_images = set()
62-
self.unused_images = set()
61+
self.referenced_images: Set[str] = set()
62+
self.static_images: Set[str] = set()
63+
self.unused_images: Set[str] = set()
6364

6465
def find_all_static_images(self) -> Set[str]:
6566
"""Find all images in the static directory (excluding icons)."""
@@ -95,6 +96,10 @@ def normalize_image_path(self, path: str) -> str:
9596
path = path[8:] # Remove '/static/'
9697
elif path.startswith('static/'):
9798
path = path[7:] # Remove 'static/'
99+
elif path.startswith('/docs/'):
100+
path = path[6:] # Remove '/docs/' (this maps to static directory)
101+
elif path.startswith('docs/'):
102+
path = path[5:] # Remove 'docs/' (this maps to static directory)
98103
elif path.startswith('../../../static/'):
99104
path = path[16:] # Remove '../../../static/' (16 characters)
100105
elif path.startswith('../../static/'):
@@ -168,6 +173,32 @@ def find_referenced_images(self) -> Set[str]:
168173
except Exception as e:
169174
print(f" ⚠️ Error reading {file_path}: {e}")
170175

176+
# Scan OpenAPI spec files (YAML)
177+
if self.openapi_dir.exists():
178+
print(f" 📋 Scanning openapi-spec directory...")
179+
yaml_files = []
180+
for pattern in ['**/*.yaml', '**/*.yml']:
181+
yaml_files.extend(glob.glob(str(self.openapi_dir / pattern), recursive=True))
182+
183+
print(f" 📋 Found {len(yaml_files)} YAML files")
184+
total_files += len(yaml_files)
185+
186+
for file_path in yaml_files:
187+
try:
188+
with open(file_path, 'r', encoding='utf-8') as f:
189+
content = f.read()
190+
191+
# Apply all image patterns
192+
for pattern in self.image_patterns:
193+
matches = re.findall(pattern, content, re.IGNORECASE | re.MULTILINE)
194+
for match in matches:
195+
normalized_path = self.normalize_image_path(match)
196+
if normalized_path:
197+
referenced_images.add(normalized_path)
198+
199+
except Exception as e:
200+
print(f" ⚠️ Error reading {file_path}: {e}")
201+
171202
print(f"📊 Scanned {total_files} total files")
172203
print(f"📊 Found {len(referenced_images)} unique image references")
173204
return referenced_images
@@ -280,6 +311,7 @@ def save_results(self, results: Dict):
280311
f.write("- Properly handles markdown image syntax with alt text\n")
281312
f.write("- Scans all .md and .mdx files in the /docs directory\n")
282313
f.write("- Scans all .js, .jsx, .ts, .tsx, .css, .scss files in the /src directory\n")
314+
f.write("- Scans all .yaml and .yml files in the /openapi-spec directory\n")
283315
f.write("- Detects both image syntax (![...]) and link syntax ([...]) pointing to images\n")
284316
f.write("- Excludes images in the /static/icons directory\n")
285317
f.write("- Excludes specific ignored images (logos, etc.) from cleanup analysis\n")

scripts/cleanup_unused_images.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ def calculate_total_size(self, file_paths: List[Path]) -> int:
6666

6767
def format_size(self, size_bytes: int) -> str:
6868
"""Format file size in human readable format."""
69+
size = float(size_bytes)
6970
for unit in ['B', 'KB', 'MB', 'GB']:
70-
if size_bytes < 1024.0:
71-
return f"{size_bytes:.1f} {unit}"
72-
size_bytes /= 1024.0
73-
return f"{size_bytes:.1f} TB"
71+
if size < 1024.0:
72+
return f"{size:.1f} {unit}"
73+
size /= 1024.0
74+
return f"{size:.1f} TB"
7475

7576
def confirm_deletion(self, file_paths: List[Path]) -> bool:
7677
"""Ask user for confirmation before deleting files."""
46.3 KB
Loading
40.5 KB
Loading
43.6 KB
Loading
47.4 KB
Loading
54.2 KB
Loading
49.9 KB
Loading
69.5 KB
Loading

0 commit comments

Comments
 (0)