mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-27 07:25:16 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9feb7aaa14 |
No files matched your search
+10
-21
@@ -6,7 +6,6 @@ import zipfile
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
from tempfile import mkstemp
|
|
||||||
|
|
||||||
from celery import Task
|
from celery import Task
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
@@ -897,26 +896,25 @@ def build_share_link_bundle(bundle_id: int) -> None:
|
|||||||
|
|
||||||
documents = list(bundle.documents.all().order_by("pk"))
|
documents = list(bundle.documents.all().order_by("pk"))
|
||||||
|
|
||||||
_, temp_zip_path_str = mkstemp(suffix=".zip", dir=settings.SCRATCH_DIR)
|
|
||||||
temp_zip_path = Path(temp_zip_path_str)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
strategy_class = (
|
strategy_class = (
|
||||||
ArchiveOnlyStrategy
|
ArchiveOnlyStrategy
|
||||||
if bundle.file_version == ShareLink.FileVersion.ARCHIVE
|
if bundle.file_version == ShareLink.FileVersion.ARCHIVE
|
||||||
else OriginalsOnlyStrategy
|
else OriginalsOnlyStrategy
|
||||||
)
|
)
|
||||||
with zipfile.ZipFile(temp_zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
|
||||||
strategy = strategy_class(zipf)
|
|
||||||
for document in documents:
|
|
||||||
strategy.add_document(document)
|
|
||||||
|
|
||||||
output_dir = settings.SHARE_LINK_BUNDLE_DIR
|
output_dir = settings.SHARE_LINK_BUNDLE_DIR
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
final_path = (output_dir / f"{bundle.slug}.zip").resolve()
|
final_path = (output_dir / f"{bundle.slug}.zip").resolve()
|
||||||
if final_path.exists():
|
|
||||||
final_path.unlink()
|
with TemporaryDirectory(dir=settings.SCRATCH_DIR) as tmp_dir:
|
||||||
shutil.move(temp_zip_path, final_path)
|
temp_zip_path = Path(tmp_dir) / "bundle.zip"
|
||||||
|
with zipfile.ZipFile(temp_zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
||||||
|
strategy = strategy_class(zipf)
|
||||||
|
for document in documents:
|
||||||
|
strategy.add_document(document)
|
||||||
|
|
||||||
|
final_path.unlink(missing_ok=True)
|
||||||
|
shutil.move(temp_zip_path, final_path)
|
||||||
|
|
||||||
bundle.file_path = f"{bundle.slug}.zip"
|
bundle.file_path = f"{bundle.slug}.zip"
|
||||||
bundle.size_bytes = final_path.stat().st_size
|
bundle.size_bytes = final_path.stat().st_size
|
||||||
@@ -947,16 +945,7 @@ def build_share_link_bundle(bundle_id: int) -> None:
|
|||||||
"timestamp": timezone.now().isoformat(),
|
"timestamp": timezone.now().isoformat(),
|
||||||
}
|
}
|
||||||
bundle.save(update_fields=["status", "last_error"])
|
bundle.save(update_fields=["status", "last_error"])
|
||||||
try:
|
|
||||||
temp_zip_path.unlink()
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
raise
|
raise
|
||||||
finally:
|
|
||||||
try:
|
|
||||||
temp_zip_path.unlink(missing_ok=True)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
|
|||||||
@@ -434,13 +434,9 @@ class ShareLinkBundleBuildTaskTests(DirectoriesMixin, APITestCase):
|
|||||||
"documents.tasks.OriginalsOnlyStrategy.add_document",
|
"documents.tasks.OriginalsOnlyStrategy.add_document",
|
||||||
side_effect=RuntimeError("zip failure"),
|
side_effect=RuntimeError("zip failure"),
|
||||||
),
|
),
|
||||||
mock.patch("pathlib.Path.unlink") as unlink_mock,
|
self.assertRaises(RuntimeError),
|
||||||
):
|
):
|
||||||
unlink_mock.side_effect = [OSError("unlink"), OSError("unlink-finally")] + [
|
build_share_link_bundle(bundle.pk)
|
||||||
None,
|
|
||||||
] * 5
|
|
||||||
with self.assertRaises(RuntimeError):
|
|
||||||
build_share_link_bundle(bundle.pk)
|
|
||||||
|
|
||||||
bundle.refresh_from_db()
|
bundle.refresh_from_db()
|
||||||
self.assertEqual(bundle.status, ShareLinkBundle.Status.FAILED)
|
self.assertEqual(bundle.status, ShareLinkBundle.Status.FAILED)
|
||||||
@@ -448,10 +444,6 @@ class ShareLinkBundleBuildTaskTests(DirectoriesMixin, APITestCase):
|
|||||||
assert isinstance(bundle.last_error, dict)
|
assert isinstance(bundle.last_error, dict)
|
||||||
self.assertEqual(bundle.last_error.get("message"), "zip failure")
|
self.assertEqual(bundle.last_error.get("message"), "zip failure")
|
||||||
self.assertEqual(bundle.last_error.get("exception_type"), "RuntimeError")
|
self.assertEqual(bundle.last_error.get("exception_type"), "RuntimeError")
|
||||||
scratch_zips = list(Path(settings.SCRATCH_DIR).glob("*.zip"))
|
|
||||||
self.assertTrue(scratch_zips)
|
|
||||||
for path in scratch_zips:
|
|
||||||
path.unlink(missing_ok=True)
|
|
||||||
|
|
||||||
def test_build_share_link_bundle_missing_bundle_noop(self) -> None:
|
def test_build_share_link_bundle_missing_bundle_noop(self) -> None:
|
||||||
# Should not raise when bundle does not exist
|
# Should not raise when bundle does not exist
|
||||||
|
|||||||
@@ -12,11 +12,6 @@ from paperless.utils import ocr_to_dateparser_languages
|
|||||||
pytest.param("eng", ["en"], id="single-language"),
|
pytest.param("eng", ["en"], id="single-language"),
|
||||||
pytest.param("fra+ita+lao", ["fr", "it", "lo"], id="multiple-languages"),
|
pytest.param("fra+ita+lao", ["fr", "it", "lo"], id="multiple-languages"),
|
||||||
pytest.param("fil", ["fil"], id="no-two-letter-equivalent"),
|
pytest.param("fil", ["fil"], id="no-two-letter-equivalent"),
|
||||||
pytest.param(
|
|
||||||
"afr+vie+kan+chr+mkd", # codespell:ignore
|
|
||||||
["af", "vi", "kn", "chr", "mk"],
|
|
||||||
id="previously-unmapped-languages",
|
|
||||||
),
|
|
||||||
pytest.param(
|
pytest.param(
|
||||||
"aze_cyrl+srp_latn",
|
"aze_cyrl+srp_latn",
|
||||||
["az-Cyrl", "sr-Latn"],
|
["az-Cyrl", "sr-Latn"],
|
||||||
@@ -54,19 +49,6 @@ def test_ocr_to_dateparser_languages(ocr_language: str, expected: list[str]) ->
|
|||||||
assert sorted(ocr_to_dateparser_languages(ocr_language)) == sorted(expected)
|
assert sorted(ocr_to_dateparser_languages(ocr_language)) == sorted(expected)
|
||||||
|
|
||||||
|
|
||||||
def test_ocr_to_dateparser_map_values_supported_by_dateparser() -> None:
|
|
||||||
from dateparser.languages.loader import LocaleDataLoader
|
|
||||||
|
|
||||||
loader = LocaleDataLoader()
|
|
||||||
unsupported = []
|
|
||||||
for ocr_code, dateparser_code in utils.OCR_TO_DATEPARSER_LANGUAGES.items():
|
|
||||||
try:
|
|
||||||
loader.get_locale_map(locales=[dateparser_code])
|
|
||||||
except Exception:
|
|
||||||
unsupported.append((ocr_code, dateparser_code))
|
|
||||||
assert unsupported == []
|
|
||||||
|
|
||||||
|
|
||||||
def test_ocr_to_dateparser_languages_exception(
|
def test_ocr_to_dateparser_languages_exception(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
|||||||
+16
-16
@@ -2,18 +2,19 @@ import logging
|
|||||||
|
|
||||||
logger = logging.getLogger("paperless.utils")
|
logger = logging.getLogger("paperless.utils")
|
||||||
|
|
||||||
# Translation map from languages supported by Tesseract OCR
|
|
||||||
# to languages supported by dateparser.
|
|
||||||
# To add a language, make sure it is supported by both libraries.
|
|
||||||
# The ISO 639-2 will help you link a 3-char to 2-char language code.
|
|
||||||
# Links:
|
|
||||||
# - Tesseract languages: https://tesseract-ocr.github.io/tessdoc/Data-Files-in-different-versions.html
|
|
||||||
# - Python dateparser languages: https://dateparser.readthedocs.io/en/latest/supported_locales.html
|
|
||||||
# - ISO 639-2: https://www.loc.gov/standards/iso639-2/php/code_list.php
|
|
||||||
OCR_TO_DATEPARSER_LANGUAGES = {
|
OCR_TO_DATEPARSER_LANGUAGES = {
|
||||||
# These dateparser languages have no equivalent in any Tesseract release
|
"""
|
||||||
# (checked against tessdata, tessdata_best and tessdata_fast), so they cannot
|
Translation map from languages supported by Tesseract OCR
|
||||||
# be reached from the OCR language. Set PAPERLESS_DATE_PARSER_LANGUAGES to use them:
|
to languages supported by dateparser.
|
||||||
|
To add a language, make sure it is supported by both libraries.
|
||||||
|
The ISO 639-2 will help you link a 3-char to 2-char language code.
|
||||||
|
Links:
|
||||||
|
- Tesseract languages: https://tesseract-ocr.github.io/tessdoc/Data-Files-in-different-versions.html
|
||||||
|
- Python dateparser languages: https://dateparser.readthedocs.io/en/latest/supported_locales.html
|
||||||
|
- ISO 639-2: https://www.loc.gov/standards/iso639-2/php/code_list.php
|
||||||
|
"""
|
||||||
|
# TODO check these Dateparser languages as they are not referenced on the ISO639-2 standard,
|
||||||
|
# so we didn't find the equivalent in Tesseract:
|
||||||
# agq, asa, bez, brx, cgg, ckb, dav, dje, dyo, ebu, guz, jgo, jmc, kde, kea, khq, kln,
|
# agq, asa, bez, brx, cgg, ckb, dav, dje, dyo, ebu, guz, jgo, jmc, kde, kea, khq, kln,
|
||||||
# ksb, ksf, ksh, lag, lkt, lrc, luy, mer, mfe, mgh, mgo, mua, mzn, naq, nmg, nnh, nus,
|
# ksb, ksf, ksh, lag, lkt, lrc, luy, mer, mfe, mgh, mgo, mua, mzn, naq, nmg, nnh, nus,
|
||||||
# rof, rwk, saq, sbp, she, ses, shi, teo, twq, tzm, vun, wae, xog, yav, yue
|
# rof, rwk, saq, sbp, she, ses, shi, teo, twq, tzm, vun, wae, xog, yav, yue
|
||||||
@@ -21,6 +22,7 @@ OCR_TO_DATEPARSER_LANGUAGES = {
|
|||||||
"amh": "am",
|
"amh": "am",
|
||||||
"ara": "ar",
|
"ara": "ar",
|
||||||
"asm": "as",
|
"asm": "as",
|
||||||
|
"ast": "ast",
|
||||||
"aze": "az",
|
"aze": "az",
|
||||||
"bel": "be",
|
"bel": "be",
|
||||||
"bul": "bg",
|
"bul": "bg",
|
||||||
@@ -29,7 +31,7 @@ OCR_TO_DATEPARSER_LANGUAGES = {
|
|||||||
"bre": "br",
|
"bre": "br",
|
||||||
"bos": "bs",
|
"bos": "bs",
|
||||||
"cat": "ca",
|
"cat": "ca",
|
||||||
"chr": "chr",
|
"cher": "chr",
|
||||||
"ces": "cs",
|
"ces": "cs",
|
||||||
"cym": "cy",
|
"cym": "cy",
|
||||||
"dan": "da",
|
"dan": "da",
|
||||||
@@ -63,21 +65,19 @@ OCR_TO_DATEPARSER_LANGUAGES = {
|
|||||||
"kat": "ka",
|
"kat": "ka",
|
||||||
"kaz": "kk",
|
"kaz": "kk",
|
||||||
"khm": "km",
|
"khm": "km",
|
||||||
"kan": "kn",
|
"knda": "kn",
|
||||||
"kor": "ko",
|
"kor": "ko",
|
||||||
"kir": "ky",
|
"kir": "ky",
|
||||||
"ltz": "lb",
|
"ltz": "lb",
|
||||||
"lao": "lo",
|
"lao": "lo",
|
||||||
"lit": "lt",
|
"lit": "lt",
|
||||||
"lav": "lv",
|
"lav": "lv",
|
||||||
"mkd": "mk",
|
|
||||||
"mal": "ml",
|
"mal": "ml",
|
||||||
"mon": "mn",
|
"mon": "mn",
|
||||||
"mar": "mr",
|
"mar": "mr",
|
||||||
"msa": "ms",
|
"msa": "ms",
|
||||||
"mlt": "mt",
|
"mlt": "mt",
|
||||||
"mya": "my",
|
"mya": "my",
|
||||||
"nor": "nb",
|
|
||||||
"nep": "ne",
|
"nep": "ne",
|
||||||
"nld": "nl",
|
"nld": "nl",
|
||||||
"ori": "or",
|
"ori": "or",
|
||||||
@@ -106,7 +106,7 @@ OCR_TO_DATEPARSER_LANGUAGES = {
|
|||||||
"ukr": "uk",
|
"ukr": "uk",
|
||||||
"urd": "ur",
|
"urd": "ur",
|
||||||
"uzb": "uz",
|
"uzb": "uz",
|
||||||
"vie": "vi", # codespell:ignore
|
"via": "vi",
|
||||||
"yid": "yi",
|
"yid": "yi",
|
||||||
"yor": "yo",
|
"yor": "yo",
|
||||||
"chi": "zh",
|
"chi": "zh",
|
||||||
|
|||||||
Reference in new issue
Block a user