AOSSIE-Org / AOSSIE-Org/PictoPy

BUG: Orphan thumbnail files permanently leak disk space when folders are deleted

未关闭
#1,159 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
283
派生
679
平均合并
7 天 2 小时
30 天内合并 PR
3

描述

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

When a user deletes a folder through **Settings → Folder Management**, the thumbnail image files on disk are never cleaned up. The SQL `ON DELETE CASCADE` removes the image records from the database, but the physical `thumbnail_*.jpg` files in the app's thumbnail directory remain on disk permanently. This causes a silent disk space leak that grows every time a folder is removed.

## Expected Behavior

When a folder is deleted, all associated thumbnail files on disk should also be removed. The `delete_folders` route should query all thumbnail paths for images belonging to the folders being deleted **before** removing the DB records, and then delete those files from disk.

## Root Cause

The `delete_folders` route in `backend/app/routes/folders.py` only calls the database batch delete function which performs a SQL DELETE — it never touches the filesystem to remove the corresponding thumbnail files.

Interestingly, the sync/obsolete flow in `backend/app/utils/images.py` (`image_util_remove_obsolete_images`) **does** correctly clean up thumbnails when images are found to be missing from the filesystem — but this function is never called during the folder deletion flow.

The same problem also exists in `folder_util_delete_obsolete_folders` in `backend/app/utils/folders.py` — it deletes folder records from the database without any thumbnail cleanup.

## Steps to Reproduce

1. Open PictoPy and add a folder containing images (e.g., 50 photos).
2. Wait for processing to complete (thumbnails generated).
3. Note the files in the thumbnails directory (`~/.local/share/PictoPy/thumbnails/`).
4. Go to **Settings → Folder Management** and delete the folder.
5. Check the thumbnails directory again — **all thumbnail files from step 3 are still present**.
6. Repeat steps 1–5 — orphan files keep accumulating.

## Notes

- Each thumbnail is a JPEG up to 600×600px (typically 20–100 KB each).
- A user adding/removing 10 folders of 500 photos = ~5,000 orphaned thumbnails = ~100–500 MB leaked.
- Users have no way to identify or clean these orphaned files without manually inspecting the directory.
- This is related to but distinct from #569 (face data persistence) and #843 (thumbnail generation failure). Neither of those issues covers orphan thumbnail cleanup on folder deletion.

## Files Involved

- `backend/app/routes/folders.py` — `delete_folders()` (primary fix needed)
- `backend/app/utils/folders.py` — `folder_util_delete_obsolete_folders()` (also needs fix)
- `backend/app/database/images.py` — `db_get_images_by_folder_ids()` (needed to query thumbnails before deletion)
- `backend/app/utils/images.py` — `image_util_remove_obsolete_images()` (reference for correct thumbnail cleanup)

### Record

- [x] I agree to follow this project's Code of Conduct

### Record

- [x] I agree to follow this project's Code of Conduct

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。