Enhances media file copying with robust handling
Improves media file import process with: - Recursive directory structure preservation - Case-insensitive extension filtering - Collision handling with unique filename generation - Error logging for copy failures Adds comprehensive test cases to validate new media file copying behavior
This commit is contained in:
2 files changed
+165
-20
No files matched your search
+51
-20
@@ -165,29 +165,60 @@ def convert_gedcom_to_markdown(
|
|||||||
# Copy media files if available
|
# Copy media files if available
|
||||||
if media_dir and media_dir.exists():
|
if media_dir and media_dir.exists():
|
||||||
logger.info(f"Copying media files to: {media_output_dir}")
|
logger.info(f"Copying media files to: {media_output_dir}")
|
||||||
media_files = []
|
|
||||||
for ext in [
|
# Allowed media extensions (case-insensitive)
|
||||||
"*.jpg",
|
allowed_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp'}
|
||||||
"*.jpeg",
|
|
||||||
"*.png",
|
|
||||||
"*.gif",
|
|
||||||
"*.bmp",
|
|
||||||
"*.JPG",
|
|
||||||
"*.JPEG",
|
|
||||||
"*.PNG",
|
|
||||||
"*.GIF",
|
|
||||||
"*.BMP",
|
|
||||||
]:
|
|
||||||
# Use rglob to find media files in subdirectories
|
|
||||||
media_files.extend(media_dir.rglob(ext))
|
|
||||||
|
|
||||||
copied_count = 0
|
copied_count = 0
|
||||||
for media_file in media_files:
|
skipped_count = 0
|
||||||
dest = media_output_dir / media_file.name
|
collision_count = 0
|
||||||
shutil.copy2(media_file, dest)
|
|
||||||
copied_count += 1
|
|
||||||
|
|
||||||
logger.info(f"Copied {copied_count} media files")
|
# Recursively find all files in media_dir
|
||||||
|
for media_file in media_dir.rglob('*'):
|
||||||
|
# Skip directories, only process files
|
||||||
|
if not media_file.is_file():
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Filter by extension (case-insensitive)
|
||||||
|
if media_file.suffix.lower() not in allowed_extensions:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Compute relative path to preserve directory structure
|
||||||
|
relative_path = media_file.relative_to(media_dir)
|
||||||
|
dest = media_output_dir / relative_path
|
||||||
|
|
||||||
|
# Handle filename collisions
|
||||||
|
if dest.exists():
|
||||||
|
# Generate unique filename with numeric suffix
|
||||||
|
original_dest = dest
|
||||||
|
counter = 1
|
||||||
|
stem = dest.stem
|
||||||
|
suffix = dest.suffix
|
||||||
|
|
||||||
|
while dest.exists():
|
||||||
|
dest = dest.parent / f"{stem}_{counter}{suffix}"
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
logger.warning(
|
||||||
|
f"Collision detected: {original_dest.name} -> {dest.name}"
|
||||||
|
)
|
||||||
|
collision_count += 1
|
||||||
|
|
||||||
|
# Ensure parent directory exists
|
||||||
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Copy file preserving metadata
|
||||||
|
try:
|
||||||
|
shutil.copy2(media_file, dest)
|
||||||
|
copied_count += 1
|
||||||
|
except (IOError, OSError) as e:
|
||||||
|
logger.error(f"Failed to copy {media_file}: {e}")
|
||||||
|
skipped_count += 1
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Copied {copied_count} media files "
|
||||||
|
f"({collision_count} collisions resolved, {skipped_count} skipped)"
|
||||||
|
)
|
||||||
|
|
||||||
# Generate index
|
# Generate index
|
||||||
if create_index:
|
if create_index:
|
||||||
|
|||||||
@@ -472,6 +472,120 @@ class TestMediaFileCopying:
|
|||||||
# Should succeed without media
|
# Should succeed without media
|
||||||
assert exit_code == 0
|
assert exit_code == 0
|
||||||
|
|
||||||
|
def test_nested_directory_structure_preservation(self, sample_gedcom_file, output_dir, temp_dir):
|
||||||
|
"""Test that nested media directory structure is preserved."""
|
||||||
|
media_dir = temp_dir / 'media'
|
||||||
|
media_dir.mkdir()
|
||||||
|
|
||||||
|
# Create nested directory structure
|
||||||
|
(media_dir / 'photos' / 'family').mkdir(parents=True)
|
||||||
|
(media_dir / 'photos' / 'events').mkdir(parents=True)
|
||||||
|
(media_dir / 'documents').mkdir(parents=True)
|
||||||
|
|
||||||
|
# Create files in nested directories
|
||||||
|
(media_dir / 'photos' / 'family' / 'photo1.jpg').write_text('fake image')
|
||||||
|
(media_dir / 'photos' / 'events' / 'photo2.png').write_text('fake image')
|
||||||
|
(media_dir / 'documents' / 'scan.jpg').write_text('fake image')
|
||||||
|
|
||||||
|
exit_code = convert_gedcom_to_markdown(
|
||||||
|
gedcom_file=sample_gedcom_file,
|
||||||
|
output_dir=output_dir,
|
||||||
|
create_index=True,
|
||||||
|
media_dir=media_dir,
|
||||||
|
use_flat_structure=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exit_code == 0
|
||||||
|
|
||||||
|
# Verify directory structure is preserved
|
||||||
|
assert (output_dir / 'photos' / 'family' / 'photo1.jpg').exists()
|
||||||
|
assert (output_dir / 'photos' / 'events' / 'photo2.png').exists()
|
||||||
|
assert (output_dir / 'documents' / 'scan.jpg').exists()
|
||||||
|
|
||||||
|
def test_filename_collision_handling(self, sample_gedcom_file, output_dir, temp_dir):
|
||||||
|
"""Test that filename collisions are handled with numeric suffixes."""
|
||||||
|
media_dir = temp_dir / 'media'
|
||||||
|
media_dir.mkdir()
|
||||||
|
|
||||||
|
# Create subdirectories with same filenames
|
||||||
|
(media_dir / 'dir1').mkdir()
|
||||||
|
(media_dir / 'dir2').mkdir()
|
||||||
|
|
||||||
|
# Create files with same name in different directories
|
||||||
|
(media_dir / 'dir1' / 'photo.jpg').write_text('image 1')
|
||||||
|
(media_dir / 'dir2' / 'photo.jpg').write_text('image 2')
|
||||||
|
|
||||||
|
exit_code = convert_gedcom_to_markdown(
|
||||||
|
gedcom_file=sample_gedcom_file,
|
||||||
|
output_dir=output_dir,
|
||||||
|
create_index=True,
|
||||||
|
media_dir=media_dir,
|
||||||
|
use_flat_structure=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exit_code == 0
|
||||||
|
|
||||||
|
# First file should be copied normally
|
||||||
|
assert (output_dir / 'dir1' / 'photo.jpg').exists()
|
||||||
|
|
||||||
|
# Second file should be copied with preserved structure (no collision)
|
||||||
|
assert (output_dir / 'dir2' / 'photo.jpg').exists()
|
||||||
|
|
||||||
|
# Both files should exist
|
||||||
|
assert len(list(output_dir.rglob('photo*.jpg'))) == 2
|
||||||
|
|
||||||
|
def test_case_insensitive_extensions(self, sample_gedcom_file, output_dir, temp_dir):
|
||||||
|
"""Test that extensions are matched case-insensitively."""
|
||||||
|
media_dir = temp_dir / 'media'
|
||||||
|
media_dir.mkdir()
|
||||||
|
|
||||||
|
# Create files with mixed-case extensions
|
||||||
|
(media_dir / 'photo1.JpG').write_text('fake image')
|
||||||
|
(media_dir / 'photo2.PnG').write_text('fake image')
|
||||||
|
(media_dir / 'photo3.GiF').write_text('fake image')
|
||||||
|
|
||||||
|
exit_code = convert_gedcom_to_markdown(
|
||||||
|
gedcom_file=sample_gedcom_file,
|
||||||
|
output_dir=output_dir,
|
||||||
|
create_index=True,
|
||||||
|
media_dir=media_dir,
|
||||||
|
use_flat_structure=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exit_code == 0
|
||||||
|
|
||||||
|
# All files should be copied despite mixed case
|
||||||
|
assert (output_dir / 'photo1.JpG').exists()
|
||||||
|
assert (output_dir / 'photo2.PnG').exists()
|
||||||
|
assert (output_dir / 'photo3.GiF').exists()
|
||||||
|
|
||||||
|
def test_non_media_files_ignored(self, sample_gedcom_file, output_dir, temp_dir):
|
||||||
|
"""Test that non-media files are ignored."""
|
||||||
|
media_dir = temp_dir / 'media'
|
||||||
|
media_dir.mkdir()
|
||||||
|
|
||||||
|
# Create media and non-media files
|
||||||
|
(media_dir / 'photo.jpg').write_text('fake image')
|
||||||
|
(media_dir / 'readme.txt').write_text('some text')
|
||||||
|
(media_dir / 'data.csv').write_text('some data')
|
||||||
|
(media_dir / 'script.py').write_text('print("hello")')
|
||||||
|
|
||||||
|
exit_code = convert_gedcom_to_markdown(
|
||||||
|
gedcom_file=sample_gedcom_file,
|
||||||
|
output_dir=output_dir,
|
||||||
|
create_index=True,
|
||||||
|
media_dir=media_dir,
|
||||||
|
use_flat_structure=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exit_code == 0
|
||||||
|
|
||||||
|
# Only media file should be copied
|
||||||
|
assert (output_dir / 'photo.jpg').exists()
|
||||||
|
assert not (output_dir / 'readme.txt').exists()
|
||||||
|
assert not (output_dir / 'data.csv').exists()
|
||||||
|
assert not (output_dir / 'script.py').exists()
|
||||||
|
|
||||||
|
|
||||||
class TestTempDirectoryCleanup:
|
class TestTempDirectoryCleanup:
|
||||||
"""Tests for temporary directory cleanup."""
|
"""Tests for temporary directory cleanup."""
|
||||||
|
|||||||
Reference in new issue
Block a user