Coverage for documents/management/commands/document_thumbnails.py: 0%
31 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 09:07 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 09:07 +0000
1import logging
2import shutil
4from documents.management.commands.base import PaperlessCommand
5from documents.models import Document
6from paperless.parsers.registry import get_parser_registry
8logger = logging.getLogger("paperless.management.thumbnails")
11def _process_document(doc_id: int) -> None:
12 document: Document = Document.objects.get(id=doc_id)
13 parser_class = get_parser_registry().get_parser_for_file(
14 document.mime_type,
15 document.original_filename or "",
16 document.source_path,
17 )
19 if parser_class is None:
20 logger.warning(
21 "%s: No parser for mime type %s",
22 document,
23 document.mime_type,
24 )
25 return
27 with parser_class() as parser:
28 thumb = parser.get_thumbnail(document.source_path, document.mime_type)
29 shutil.move(thumb, document.thumbnail_path)
32class Command(PaperlessCommand):
33 help = "This will regenerate the thumbnails for all documents."
35 supports_progress_bar = True
36 supports_multiprocessing = True
38 def add_arguments(self, parser) -> None:
39 super().add_arguments(parser)
40 parser.add_argument(
41 "-d",
42 "--document",
43 default=None,
44 type=int,
45 required=False,
46 help=(
47 "Specify the ID of a document, and this command will only "
48 "run on this specific document."
49 ),
50 )
52 def handle(self, *args, **options):
53 logging.getLogger().handlers[0].level = logging.ERROR
55 if options["document"]:
56 documents = Document.objects.filter(pk=options["document"])
57 else:
58 documents = Document.objects.all()
60 ids = list(documents.values_list("id", flat=True))
62 for result in self.process_parallel(
63 _process_document,
64 ids,
65 description="Regenerating thumbnails...",
66 ):
67 if result.error: # pragma: no cover
68 self.console.print(
69 f"[red]Failed document {result.item}: {result.error}[/red]",
70 )