The stored name is "<attachment_id>_<filename>", so a valid 240-byte filename
exceeded the 255-byte component limit and put() raised ENAMETOOLONG. Trim the
stem by bytes, keeping a short extension, so saving and lookup stay consistent.
Apply Unicode casefolding to filename and query instead of relying on SQLite LIKE ASCII-only folding. Preserve literal wildcard escaping, soft-deletion filtering and result ordering.
AI-assisted: implemented and validated with OpenAI Codex; actual SQLite stores and isolated API fixtures.
Translate only an actual active-SHA uniqueness conflict into a safe 409 response. Preserve both records and their bytes so restoring the earlier entry can be retried after deleting the replacement; unrelated database failures still propagate.
AI-assisted: implemented and validated with OpenAI Codex; isolated API and real SQLite constraints, no user workspace writes.
search_files promises case-insensitive substring matching, but _search_files_sync
interpolated the query into a LIKE pattern without escaping. SQLite LIKE treats
'_' as any single character and '%' as any run, so a query like 'report_v1'
matched 'reportXv1.pdf' too — filenames commonly contain '_', so the false
positives are routine. Escape the query's LIKE metacharacters and add ESCAPE.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Merge the six DT-22 regression cases (previously on a separate
test_file_library_deletion.py branch) into the existing per-module
test file, right after the delete_file / hard_delete_file sections:
- successful delete prunes now-empty parents, keeps non-empty ones
and the library root itself
- missing on-disk target is a silent no-op; hard delete still clears
the DB row when the disk file vanished out-of-band
- unlink failure is only logged (list-vs-disk inconsistency window)
- an exception while pruning parents is swallowed: file still
removed, empty dirs left behind, caller unaffected (current
behaviour pinned)
Coverage of deeptutor.services.storage.file_library: 84% -> 87%
(196-197 and 203-206 now covered). No product code changes.
Co-authored-by: XZH <cto@xzh.ai>
* feat: Persistent File Library with API router (issue #1437)
FileLibraryStore (deeptutor/services/storage/file_library.py):
- add_file: stores bytes to disk, deduplicates by SHA-256
- get_file: returns entry by id
- delete_file: soft-delete, idempotent
- hard_delete_file: permanent delete (requires prior soft-delete)
- restore_file: clears is_deleted, idempotent
- list_files: active entries, newest-first, paginated
- search_files: case-insensitive filename LIKE search
- resolve_path: returns absolute Path or None
API router (deeptutor/api/routers/file_library.py):
- GET /files/library/ — list files
- POST /files/library/ — multipart upload with SHA-256 deduplication
- GET /files/library/search — filename search
- GET /files/library/{id} — get entry
- GET /files/library/{id}/download — download file bytes
- DELETE /files/library/{id} — soft-delete
- POST /files/library/{id}/restore — restore soft-deleted
25 unit tests + 20 API integration tests, all passing.
* fix: ruff import-sort/unused-import fixes for file library
* fix: scope file library singleton per user, close upload dedup race
- get_file_library_store() was caching the FileLibraryStore under a
hardcoded key ("default") instead of the resolved per-user root, so
in multi-user deployments every user after the first shared one
library (list/get/download/delete/restore all leaked across
accounts). Now resolves get_path_service().get_user_root() on every
call and keys the cache by str(user_root), mirroring
get_attachment_store().
- _add_file_sync's SHA-256 dedup check was a plain SELECT followed by
an INSERT with no synchronization; concurrent uploads of identical
content (double-click, duplicate tabs, client retries) could both
miss the dedup lookup and each create a duplicate row/file. Added a
per-instance lock around the check-then-write-then-insert sequence,
plus a partial unique index on sha256 (active rows only) with
IntegrityError handling as a defense-in-depth backstop for
multi-process deployments.
- Fixed the test fixture in test_file_library_router.py: it patched
file_library._fl.get_file_library_store then reverted the patch
before any request ran, so the mock never actually intercepted
anything; removed the dead code and added tokens/auth_enabled
support for multi-user testing.
- Added regression tests: test_upload_and_list_are_isolated_between_users
and test_add_file_concurrent_uploads_of_same_content_do_not_duplicate,
both verified to fail against the pre-fix code.
- ruff format on the three touched files (pre-existing line-length
debt in code this change touches).