feat: import a Calibre library

Reads metadata.db and copies the books into a library — from a zip uploaded on
the library settings page, or from a path with `litestar calibre-import`. The
source is never touched, and re-running only picks up what is new.

Also names the formats mimetypes does not know: a Calibre library is full of
MOBI and AZW3, and a null content type used to fail the book endpoint.
This commit is contained in:
2026-08-17 13:38:44 -04:00
parent 85367daf7e
commit 55e00ba960
34 changed files with 4723 additions and 50 deletions
+91
View File
@@ -1272,3 +1272,94 @@ class TestFileManagement:
)
# Should succeed (idempotent)
assert response2.status_code == 204
@pytest.mark.asyncio
class TestUnnameableFormats:
"""
A file whose format nothing can name must still round-trip.
`mimetypes.guess_type` answers None for `.mobi`, `.azw`, `.fb2` and `.lit`, which
is most of what a library imported from elsewhere carries alongside its EPUBs.
`FileMetadataRead.content_type` used to be a required string, so such a book was
created and then failed serialisation on its way back out — a 500 on a book the
reader can otherwise download.
"""
def upload(self, name: str) -> list[tuple[str, tuple]]:
# `application/octet-stream` is what a browser posts for these, and it is not
# an answer — the extension is what names the format.
return [("files", (name, b"BOOKMOBI\x00 payload", "application/octet-stream"))]
async def test_a_mobi_is_named_from_its_extension(
self, authenticated_client: AsyncClient
) -> None:
response = await authenticated_client.post(
"/books?library_id=1", files=self.upload("Dune.mobi"), data={"library_id": 1}
)
assert response.status_code == 201
book = response.json()
assert book["files"][0]["content_type"] == "application/x-mobipocket-ebook"
detail = await authenticated_client.get(f"/books/{book['id']}")
assert detail.status_code == 200
async def test_an_unknown_extension_stores_no_content_type(
self, authenticated_client: AsyncClient
) -> None:
"""Null, not a placeholder — and the book still serialises either way."""
response = await authenticated_client.post(
"/books?library_id=1",
files=self.upload("Notes.xyzzy"),
data={"library_id": 1},
)
assert response.status_code == 201
book = response.json()
assert book["files"][0]["content_type"] is None
detail = await authenticated_client.get(f"/books/{book['id']}")
assert detail.status_code == 200
assert detail.json()["files"][0]["content_type"] is None
async def test_the_file_downloads(
self, authenticated_client: AsyncClient
) -> None:
"""Litestar supplies its own media type when the row carries none."""
created = await authenticated_client.post(
"/books?library_id=1",
files=self.upload("Notes.xyzzy"),
data={"library_id": 1},
)
book = created.json()
response = await authenticated_client.get(
f"/books/download/{book['id']}/{book['files'][0]['id']}"
)
assert response.status_code == 200
assert response.headers["content-type"] == "application/octet-stream"
async def test_the_opds_feed_survives_a_null_content_type(
self, authenticated_client: AsyncClient
) -> None:
"""
The one place the type has to be a string.
`Link.type` is required, so a null fails the whole feed rather than one entry.
OPDS clients speak Basic, not the JWT the rest of the API uses.
"""
await authenticated_client.post(
"/books?library_id=1",
files=self.upload("Notes.xyzzy"),
data={"library_id": 1},
)
feed = await authenticated_client.get(
"/opds/acquisition?feed_id=all&feed_title=All+Books",
auth=("user1@example.com", "password123"),
)
assert feed.status_code == 200
assert 'type="application/octet-stream"' in feed.text