Remove a file's Tags Directory links when WROLPi deletes it

When WROLPi deleted a tagged file (a forced delete, duplicate Video cleanup, or a poster conversion), the
source was unlinked but its Tags Directory hardlink was not.  The link became the only copy, so the tags sync
refused to delete it and warned on every sync.  The links of a file are now removed before WROLPi removes it,
and only if the link is the same file as its source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-23 12:50:11 -06:00
parent 58192a7052
commit a20ef4893a
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 104 additions and 1 deletions

View file

@ -214,6 +214,10 @@ def convert_or_generate_poster(video: Video) -> Tuple[Optional[pathlib.Path], Op
old: pathlib.Path = video.poster_path
new = old.with_suffix('.jpg')
if old != new:
# The old poster is about to be removed; its Tags Directory link would be left as the only copy.
video.file_group.unlink_tag_directory_links(old)
if old != new and new.exists():
# Destination JPEG already exists (it may have the wrong format).
old.unlink()

View file

@ -515,6 +515,8 @@ class Video(ModelHelper, Base):
def delete_video(video_: Video):
"""Delete a Video with any of its tags."""
# Untagging forgets which links are this Video's, so remove them first.
video_.file_group.unlink_tag_directory_links()
for tag_name_ in video_.file_group.tag_names:
video_.file_group.untag(session, tag_name_)
video_.delete()

View file

@ -593,3 +593,20 @@ def test_download_settings_codec_validation():
with pytest.raises(ValidationError):
DownloadSettings(audio_codecs=['wma'])
@pytest.mark.asyncio
async def test_convert_poster_removes_tags_directory_link(async_client, test_session, test_directory, video_factory,
tag_factory, await_switches):
"""Converting a tagged Video's poster removes the old poster's Tags Directory link."""
tag = await tag_factory()
video = video_factory(with_video_file=True, with_poster_ext='webp', tag_names=[tag.name])
test_session.commit()
await await_switches()
webp_link = test_directory / 'tags' / tag.name / video.poster_path.name
assert webp_link.is_file()
new_poster, _ = lib.convert_or_generate_poster(video)
assert new_poster.suffix == '.jpg'
assert not webp_link.exists(), 'The converted poster left its link behind'

View file

@ -240,6 +240,33 @@ async def test_delete_duplicate_video_tagged_duplicate(async_client, test_sessio
assert video.file_group.tag_names == [tag.name, ]
@pytest.mark.asyncio
async def test_delete_duplicate_video_leaves_no_tags_directory_orphans(async_client, test_session, test_directory,
channel_factory, video_factory, tag_factory,
await_switches):
"""Deleting a tagged duplicate Video removes its Tags Directory links; only links to existing files remain."""
channel = channel_factory(name='Channel Name')
video_path = channel.directory / f'{channel.name}_20000101_ABC123456_The video title.mp4'
tag = await tag_factory()
for title in (f'{channel.name}_20000101_ABC123456_The video title',
f'{channel.name}_20000101_ABC123456_The video title '):
video = video_factory(channel_id=channel.id, title=title, upload_date=datetime(2000, 1, 1, tzinfo=timezone.utc),
source_id='ABC123456', with_video_file=True, with_info_json=True, tag_names=[tag.name])
video.file_group.url = 'https://example.com/video'
test_session.commit()
await await_switches()
tags_directory = test_directory / 'tags'
assert len([i for i in tags_directory.rglob('*.mp4')]) == 2
assert await Video.delete_duplicate_videos(test_session, 'https://example.com/video', 'ABC123456', video_path)
test_session.commit()
await await_switches()
links = [i for i in tags_directory.rglob('*') if i.is_file() and i.name != 'README.txt']
assert links, 'The kept Video is still linked'
assert all(i.stat().st_nlink > 1 for i in links), 'A link was left behind with no source file'
@pytest.mark.asyncio
async def test_delete_duplicate_video_smaller_copy_at_destination(async_client, test_session, channel_factory,
video_factory):