Skip to content

Commit 9c58fb3

Browse files
CyanVoxelSola-ris
andcommitted
fix(ui): register other supported tar types to render previews
Co-authored-by: Sola-ris <190788035+Sola-ris@users.noreply.github.com>
1 parent aea66fd commit 9c58fb3

3 files changed

Lines changed: 68 additions & 8 deletions

File tree

‎docs/preview-support.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ Preview support for office documents or well-known project file formats varies b
105105

106106
Archive thumbnails will display the first image from the archive within the Preview Panel.
107107

108-
| Filetype | Extensions |
109-
| -------- | --------------------------------- |
110-
| 7-Zip | `.7z`, `.s7z` |
111-
| RAR | `.rar` |
112-
| Tar | `.tar`, `.tar.gz`, `.taz`, `.tgz` |
113-
| Zip | `.zip` |
108+
| Filetype | Extensions |
109+
| --------------- | ------------------------------------------------------ |
110+
| 7-Zip | `.7z`, `.s7z` |
111+
| RAR | `.rar` |
112+
| Tar | `.tar` |
113+
| Tar (bzip2) | `.tar.bz2`, `.tar.bz`, `.tbz2`, `.tbz`, `.tb2`, `.tz2` |
114+
| Tar (gzip) | `.tar.gz`, `.tgz`, `.taz` |
115+
| Tar (LZMA) | `.tar.lzma`, `.tlz` |
116+
| Tar (XZ) | `.tar.xz`, `.txz` |
117+
| Tar (Zstandard) | `.tar.zst`, `.tzst` |
118+
| Zip | `.zip` |
114119

115120
### :material-book: eBooks
116121

‎src/tagstudio/previews/renderers/archive.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def register_types(cls) -> None:
3737
MediaTypes.register("archive", ".s7z", RENDER)
3838
MediaTypes.register("archive", ".tar", RENDER)
3939
MediaTypes.register("archive", ".zip", RENDER)
40+
MediaTypes.register("archive", ".tar.bz", RENDER)
41+
MediaTypes.register("archive", ".tar.bz2", RENDER)
4042
MediaTypes.register("archive", ".tar.gz", RENDER)
43+
MediaTypes.register("archive", ".tar.lzma", RENDER)
44+
MediaTypes.register("archive", ".tar.xz", RENDER)
45+
MediaTypes.register("archive", ".tar.zst", RENDER)
4146

4247
@override
4348
@classmethod
@@ -105,7 +110,16 @@ def open_archive(filepath: Path) -> Archive:
105110
archiver = SevenZipFile
106111
elif exts & {".cbr", ".rar"}:
107112
archiver = rarfile.RarFile
108-
elif exts & {".cbt", ".tar", ".tar.gz"}:
113+
elif exts & {
114+
".cbt",
115+
".tar",
116+
".tar.bz",
117+
".tar.bz2",
118+
".tar.gz",
119+
".tar.lzma",
120+
".tar.xz",
121+
".tar.zst",
122+
}:
109123
archiver = TarFile
110124
return archiver(filepath, "r")
111125

‎tests/previews/test_file_renderer.py‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# pyright: reportPrivateUsage=false
55

66

7-
from compression import gzip, lzma
7+
import tarfile
8+
from collections.abc import Callable
9+
from compression import bz2, gzip, lzma, zstd
810
from io import BytesIO
911
from pathlib import Path
1012

@@ -13,6 +15,7 @@
1315

1416
from tagstudio.core.enums import Theme
1517
from tagstudio.previews.file_renderer import FileRenderer
18+
from tagstudio.previews.renderers.archive import ArchivePreview
1619

1720

1821
def _example_png_bytes() -> bytes:
@@ -37,6 +40,44 @@ def test_render_decompressed(tmp_path: Path, filename: str):
3740
assert image.size == (40, 20)
3841

3942

43+
def _example_tar_bytes() -> bytes:
44+
data = BytesIO()
45+
with tarfile.open(fileobj=data, mode="w") as tar:
46+
png = _example_png_bytes()
47+
info = tarfile.TarInfo("images/photo.png")
48+
info.size = len(png)
49+
tar.addfile(info, BytesIO(png))
50+
return data.getvalue()
51+
52+
53+
@pytest.mark.parametrize(
54+
["filename", "compress"],
55+
[
56+
("bundle.tar.bz2", bz2.compress),
57+
("bundle.tbz2", bz2.compress),
58+
("bundle.tar.lzma", lambda data: lzma.compress(data, format=lzma.FORMAT_ALONE)),
59+
("bundle.tlz", lambda data: lzma.compress(data, format=lzma.FORMAT_ALONE)),
60+
("my.cool.bundle.tar.xz", lzma.compress),
61+
("BUNDLE.TXZ", lzma.compress),
62+
("bundle.tar.zst", zstd.compress),
63+
("bundle.tzst", zstd.compress),
64+
],
65+
)
66+
def test_render_compressed_tarball(
67+
tmp_path: Path, filename: str, compress: Callable[[bytes], bytes]
68+
):
69+
source = tmp_path / filename
70+
source.write_bytes(compress(_example_tar_bytes()))
71+
72+
assert FileRenderer._find_preview(source) is ArchivePreview
73+
image = ArchivePreview.render(
74+
source, is_small=True, theme=Theme.DARK, size=(128, 128), dpi_scale=1.0
75+
)
76+
77+
assert image is not None
78+
assert image.size == (40, 20)
79+
80+
4081
def test_render_decompressed_without_inner_extension(tmp_path: Path):
4182
source = tmp_path / "log.gz"
4283
source.write_bytes(gzip.compress(b"plain text"))

0 commit comments

Comments
 (0)