mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-05 18:59:51 +00:00
* Fix: stop full document saves from writing file path columns A full save() of a Document instance loaded before another process moved its file wrote the old filename back. The DB then pointed at a path that no longer existed, and recovery only worked when the recomputed path happened to match the file's real location. Document.save() now leaves filename and archive_filename out of full saves on existing rows. Writers that own those columns opt in with write_file_paths=True or an explicit update_fields. A full save of a row deleted elsewhere now raises instead of re-inserting it. * Fix: make the global media lock reentrant within a thread Every caller built its own FileLock on media.lock, so code already holding it could not call into code that takes it again without blocking itself. That made it impossible to take media.lock before opening a transaction in paths that also attach files or move files inside that transaction. media_lock() hands out one FileLock instance per path with per-thread hold counts, so a thread re-enters while other threads and processes stay excluded. All media.lock call sites now use it. * Fix: extract document file moves out of the signal receiver The file move receiver refreshed the caller's own instance from the database, which wiped unsaved in-memory changes. It ran on every m2m_changed action including pre_add and pre_remove, wrote modified even when nothing moved, and its no-move branch skipped trashed documents. The move body now lives in documents.file_moves.move_files_for_document. It skips documents without a filename before taking media.lock, then takes the lock with a 300 second timeout, loads a fresh instance by pk, writes only when a filename column changes, and returns the instance. The receiver ignores reverse and pre_* m2m actions and copies just the final filename columns back onto the caller's instance. * Fix: collect document file moves per scope and move once on exit Each save of a document moved its files on the spot, so one logical change could move them several times, and moves could run inside another open transaction. defer_file_moves() opens a ContextVar-backed scope. Inside it, move requests are recorded and the outermost scope moves each document once on exit, or on commit if a transaction is still open. A failing move is logged and does not stop the others. Outside any scope moves stay immediate. * Fix: move document files once per request, after its writes commit A tag PATCH moved files from inside the tags.set() transaction while holding the document row lock. A concurrent save holding media.lock and waiting on that row deadlocked with it, and every later save on the instance queued behind media.lock until the webserver restarted. FileMoveMiddleware opens a file move scope around every request, in sync or async mode, so each changed document is moved once after the view returns and never while a transaction holds row locks. * Fix: move document files once per Celery task Tasks such as bulk_update_documents moved a document's files once for its manual post_save and again for every save its workflows made, each time taking the global media lock. All tasks now run through FileMoveScopedTask, which opens a file move scope around the task body, including when the task raises. bulk_update_documents flushes after each document so long bulk edits still make steady progress. * Fix: take media.lock before the archiver's transaction The archiver updated the document row inside a transaction and then waited on media.lock. A concurrent file move holding media.lock and updating the same row could deadlock with it, and the unique archive filename was generated outside the lock from an instance loaded before the parse. The archiver now acquires media.lock before opening its transaction, refreshes the document under the lock, and moves the archive and thumbnail inside that block, so it never waits on media.lock while holding the row. * Fix: take media.lock before emptying the trash Emptying the trash deleted documents inside Django's delete transaction, and the post_delete file cleanup waited on media.lock while the deleted rows were locked. A concurrent file move holding media.lock and updating one of those rows could deadlock with it. empty_trash now holds media.lock around the delete, so the cleanup handler re-enters the lock instead of waiting while rows are locked. * Fix: take media.lock before the consumer's storage transaction Consuming a new version locked the root document row with select_for_update and then waited on media.lock to place the file, the same lock ordering that let concurrent saves deadlock. On MariaDB even the version row's foreign key check holds a shared lock on the root. The consumer's storage phase now takes media.lock before opening its transaction and runs inside its own file move scope. File placement and workflow email actions inside the transaction re-enter the lock instead of waiting, and filename follow-ups run once after commit. * Fix: keep Celery's Django task base for file move scoped tasks Passing task_cls to Celery replaces the Django fixup's default DjangoTask, so FileMoveScopedTask, built on the plain Task, left every task without delay_on_commit. The workflow AI suggestions trigger failed with an AttributeError. FileMoveScopedTask now subclasses DjangoTask, which only needs django.db.transaction and is safe to import before apps load. The task scope test also asserts tasks are DjangoTask instances. * Fix: rename and retag documents one file move at a time document_renamer sent a fake post_save per document to trigger the move, and document_retagger moved a document once for each field it assigned. The renamer calls move_files_for_document directly for each document, so progress reflects real work. The retagger opens a file move scope per document, so each document is moved once after all of its assignments. * Fix: move files per document during scheduled workflow runs check_scheduled_workflows runs the workflow once per matched document inside the task-wide file move scope, so every rename it triggered was deferred to the end of the task. A long run killed by the Celery time limit would then discard all of the renames. The task now flushes the pending file moves after each document's workflow run and websocket notification, the same way bulk_update_documents does, so renames land document by document while the scope stays open. * Fix: place consumed files before running the post-consume script The consumer's storage phase opens its own file move scope, which nests under the consume task's scope. Moves requested while storing the document therefore stayed pending until the task ended, and the post-consume script could be handed a path from before the move. The consumer now flushes the pending file moves once after the storage block, outside the media lock and the transaction, and before the post-consume script runs. * Fix: skip the thread hop for async file move scopes with nothing to move adefer_file_moves hopped to a worker thread on every exit, even when no file move had been requested, which costs an executor round trip for most async requests. The collector is now closed on the event loop, which touches no database, and the flush only runs through sync_to_async when something was collected. The transaction check still happens inside the flush in the sync thread. The comment above the lock-free filename precheck in move_files_for_document now gives the real reason it uses values_list(): it keeps the check off Document.global_objects.filter(), which tests patch to inject database errors and observe the mover's writes. * Fix: return the post-move modified time from document updates Editing a document in a way that renames its file returned a response carrying the modified time from before the move. The deferred file move runs after the response is built and bumps modified, so the stored value was newer than the one the client received. The frontend then saw a newer server modified time and showed a spurious "document was updated" prompt whose reload discarded local edits. DocumentViewSet.update now flushes pending file moves right after saving and before reading the document back, so the response reflects the final filename and modified time. * Trimming and simpliciation * Fix: refresh the document after placing files, before the post-consume script * Fix: move documents when tags change through the reverse relation * Fix: move versions of trashed documents * Fix: rename only root documents in document_renamer * Improves test coverage even for the exception cases * Fix: move documents when a tag is cleared through the reverse relation * Fix: flush file moves before the scheduled workflow websocket notification * Fix: move files before the bulk update websocket notification * Fix: bump modified for documents changed by a bulk edit * Fix: defer unscoped file moves to commit inside a transaction * Fix: allow deferred file move callback in zone OCR reindex test * Fix: return 404 instead of 500 when a document is trashed during its own update DocumentViewSet.update saves the change and then re-reads the document through the default manager, which excludes soft deleted rows. A concurrent request that moved the document to the trash in between made that read raise DoesNotExist, so the client got a 500 for a document that no longer exists. Raise Http404 instead, the same answer get_object() gives when the document is already trashed at the start of the request. The committed change is kept. Found while hammering the API with concurrent PATCH and delete requests. * Fix: remove the superseded archive file when a reprocess changes its path Reprocessing a document writes the new archive to the path its current metadata generates and updates archive_filename, but never removed the file the old archive_filename pointed at. Normally both are the same path. They differ when a committed edit has not had its file move applied yet, which deferred file moves make a real window: the new archive lands at the new path and the old one stays on disk with no document referencing it. The deferred move then finds the archive already at its target and only moves the original. Delete the replaced archive, and its directory if that leaves it empty, once the new one is in place and only when the two paths differ. * Fix: keep the trash cleanup handler connected while any empty_trash call runs empty_trash connects cleanup_document_deletion to post_delete before deleting and disconnects it in a finally block. The signal is process wide and the trash endpoint calls empty_trash directly in the web process, so overlapping calls interfere. One caller finishing disconnects the handler while another is still waiting for media.lock; the waiter then hard deletes its documents with no handler attached and their files stay on disk, unreferenced. Hold the connection with a lock protected reference count, connecting for the first active caller and disconnecting after the last one finishes.
746 lines
30 KiB
Python
746 lines
30 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
import tempfile
|
|
from collections import defaultdict
|
|
from collections.abc import Generator
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from typing import TypeAlias
|
|
from zipfile import ZipFile
|
|
from zipfile import is_zipfile
|
|
|
|
import ijson
|
|
from django.apps import apps
|
|
from django.conf import settings
|
|
from django.contrib.auth.models import Permission
|
|
from django.contrib.auth.models import User
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.exceptions import FieldDoesNotExist
|
|
from django.core.management import call_command
|
|
from django.core.management.base import CommandError
|
|
from django.core.management.color import no_style
|
|
from django.core.serializers.base import DeserializationError
|
|
from django.db import IntegrityError
|
|
from django.db import connection
|
|
from django.db import models as django_models
|
|
from django.db import transaction
|
|
from django.db.models import GeneratedField
|
|
from django.db.models import Model
|
|
from django.db.models.signals import m2m_changed
|
|
from django.db.models.signals import post_save
|
|
from guardian.shortcuts import clear_ct_cache
|
|
|
|
from documents.export.compression import compress_type_readable
|
|
from documents.export.compression import unreadable_method_names
|
|
from documents.file_handling import create_source_path_directory
|
|
from documents.management.commands.base import PaperlessCommand
|
|
from documents.management.commands.mixins import CryptMixin
|
|
from documents.media_lock import media_lock
|
|
from documents.models import Correspondent
|
|
from documents.models import CustomField
|
|
from documents.models import CustomFieldInstance
|
|
from documents.models import Document
|
|
from documents.models import DocumentType
|
|
from documents.models import Note
|
|
from documents.models import ShareLinkBundle
|
|
from documents.models import Tag
|
|
from documents.settings import EXPORTER_ARCHIVE_NAME
|
|
from documents.settings import EXPORTER_CRYPTO_SETTINGS_NAME
|
|
from documents.settings import EXPORTER_FILE_NAME
|
|
from documents.settings import EXPORTER_SHARE_LINK_BUNDLE_NAME
|
|
from documents.settings import EXPORTER_THUMBNAIL_NAME
|
|
from documents.signals.handlers import check_paths_and_prune_custom_fields
|
|
from documents.signals.handlers import update_filename_and_move_files
|
|
from documents.utils import copy_file_with_basic_stats
|
|
from paperless import version
|
|
|
|
if settings.AUDIT_LOG_ENABLED:
|
|
from auditlog.registry import auditlog
|
|
|
|
# Maps M2M field names to the list of related PKs to apply after bulk_create.
|
|
M2MData: TypeAlias = dict[str, list[int]]
|
|
|
|
|
|
def iter_manifest_records(path: Path) -> Generator[dict, None, None]:
|
|
"""Yield records one at a time from a manifest JSON array via ijson."""
|
|
try:
|
|
with path.open("rb") as f:
|
|
yield from ijson.items(f, "item", use_float=True)
|
|
except ijson.JSONError as e:
|
|
raise CommandError(f"Failed to parse manifest file {path}: {e}") from e
|
|
|
|
|
|
def _deserialize_record(
|
|
record: dict,
|
|
) -> tuple[type[Model], Model, M2MData]:
|
|
"""
|
|
Convert a single manifest record dict into a model instance and M2M data.
|
|
|
|
Returns (Model class, unsaved instance, m2m_data) where m2m_data maps
|
|
M2M field names to lists of integer PKs to be applied after the instance
|
|
is saved via bulk_create.
|
|
|
|
Raises DeserializationError for unknown models or bad field values.
|
|
Raises FieldDoesNotExist for fields not present on the model.
|
|
|
|
Note: CommandError from iter_manifest_records (malformed JSON mid-stream)
|
|
propagates through the caller unchanged, it is not caught here.
|
|
"""
|
|
model_label = record["model"]
|
|
pk_value = record.get("pk")
|
|
|
|
try:
|
|
Model = apps.get_model(model_label)
|
|
except (LookupError, TypeError) as e:
|
|
raise DeserializationError(
|
|
f"Invalid model identifier: {model_label}",
|
|
) from e
|
|
|
|
data: dict = {}
|
|
m2m_data: M2MData = {}
|
|
|
|
try:
|
|
data[Model._meta.pk.attname] = Model._meta.pk.to_python(pk_value)
|
|
except Exception as e:
|
|
raise DeserializationError(
|
|
f"Could not coerce pk={pk_value} for {model_label}: {e}",
|
|
) from e
|
|
|
|
for field_name, field_value in record.get("fields", {}).items():
|
|
# Older exports include django-treenode's derived bookkeeping. The
|
|
# parent FK is authoritative; accept only these known removed fields.
|
|
if Model is Tag and field_name in {
|
|
"tn_ancestors_count",
|
|
"tn_ancestors_pks",
|
|
"tn_children_count",
|
|
"tn_children_pks",
|
|
"tn_depth",
|
|
"tn_descendants_count",
|
|
"tn_descendants_pks",
|
|
"tn_index",
|
|
"tn_level",
|
|
"tn_order",
|
|
"tn_priority",
|
|
"tn_siblings_count",
|
|
"tn_siblings_pks",
|
|
}:
|
|
continue
|
|
field = Model._meta.get_field(field_name)
|
|
remote = field.remote_field
|
|
|
|
if isinstance(remote, django_models.ManyToManyRel):
|
|
# Collect M2M PKs; .set() is called after bulk_create in flush_model.
|
|
target_pk = field.related_model._meta.pk
|
|
m2m_data[field.name] = [
|
|
target_pk.to_python(pk) for pk in (field_value or [])
|
|
]
|
|
|
|
elif isinstance(remote, django_models.ManyToOneRel):
|
|
# FK: store the integer PK on field.attname (e.g. correspondent_id)
|
|
# to avoid triggering the descriptor and avoid an extra DB lookup.
|
|
if field_value is None:
|
|
data[field.attname] = None
|
|
else:
|
|
data[field.attname] = field.related_model._meta.pk.to_python(
|
|
field_value,
|
|
)
|
|
|
|
else:
|
|
try:
|
|
data[field.name] = field.to_python(field_value)
|
|
except Exception as e:
|
|
raise DeserializationError(
|
|
f"Could not coerce {field_name}={field_value!r} "
|
|
f"for {model_label}(pk={pk_value}): {e}",
|
|
) from e
|
|
|
|
return Model, Model(**data), m2m_data
|
|
|
|
|
|
def _iter_document_copy_records(
|
|
manifest_paths: list[Path],
|
|
) -> Generator[dict, None, None]:
|
|
"""Yield one lightweight dict per Document record without buffering all records."""
|
|
for manifest_path in manifest_paths:
|
|
for record in iter_manifest_records(manifest_path):
|
|
if record["model"] == "documents.document":
|
|
yield {
|
|
"pk": record["pk"],
|
|
EXPORTER_FILE_NAME: record[EXPORTER_FILE_NAME],
|
|
EXPORTER_THUMBNAIL_NAME: record.get(EXPORTER_THUMBNAIL_NAME),
|
|
EXPORTER_ARCHIVE_NAME: record.get(EXPORTER_ARCHIVE_NAME),
|
|
}
|
|
|
|
|
|
def _iter_share_link_bundle_copy_records(
|
|
manifest_paths: list[Path],
|
|
) -> Generator[dict, None, None]:
|
|
"""Yield one dict per ShareLinkBundle record that has a bundle file."""
|
|
for manifest_path in manifest_paths:
|
|
for record in iter_manifest_records(manifest_path):
|
|
if record["model"] == "documents.sharelinkbundle" and record.get(
|
|
EXPORTER_SHARE_LINK_BUNDLE_NAME,
|
|
):
|
|
yield {
|
|
"pk": record["pk"],
|
|
EXPORTER_SHARE_LINK_BUNDLE_NAME: record[
|
|
EXPORTER_SHARE_LINK_BUNDLE_NAME
|
|
],
|
|
}
|
|
|
|
|
|
@contextmanager
|
|
def disable_signal(sig, receiver, sender, *, weak: bool | None = None) -> Generator:
|
|
try:
|
|
sig.disconnect(receiver=receiver, sender=sender)
|
|
yield
|
|
finally:
|
|
kwargs = {"weak": weak} if weak is not None else {}
|
|
sig.connect(receiver=receiver, sender=sender, **kwargs)
|
|
|
|
|
|
class Command(CryptMixin, PaperlessCommand):
|
|
help = (
|
|
"Using a manifest.json file, load the data from there, and import the "
|
|
"documents it refers to."
|
|
)
|
|
|
|
supports_progress_bar = True
|
|
supports_multiprocessing = False
|
|
|
|
def add_arguments(self, parser) -> None:
|
|
super().add_arguments(parser)
|
|
parser.add_argument("source")
|
|
|
|
parser.add_argument(
|
|
"--data-only",
|
|
default=False,
|
|
action="store_true",
|
|
help="If set, only the database will be exported, not files",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--passphrase",
|
|
help="If provided, is used to sensitive fields in the export",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--batch-size",
|
|
type=int,
|
|
default=500,
|
|
help="Number of records to insert per batch during database load. "
|
|
"Lower values reduce peak memory usage.",
|
|
)
|
|
|
|
def pre_check(self) -> None:
|
|
"""
|
|
Runs some initial checks against the state of the install and source, including:
|
|
- Does the target exist?
|
|
- Can we access the target?
|
|
- Does the target have a manifest file?
|
|
- Are there existing files in the document folders?
|
|
- Are there existing users or documents in the database?
|
|
"""
|
|
|
|
def pre_check_maybe_not_empty() -> None:
|
|
# Skip this check if operating only on the database
|
|
# We can expect data to exist in that case
|
|
if not self.data_only:
|
|
for document_dir in [settings.ORIGINALS_DIR, settings.ARCHIVE_DIR]:
|
|
if document_dir.exists() and document_dir.is_dir():
|
|
for entry in document_dir.glob("**/*"):
|
|
if entry.is_dir():
|
|
continue
|
|
self.stdout.write(
|
|
self.style.WARNING(
|
|
f"Found file {entry.relative_to(document_dir)}, this might indicate a non-empty installation",
|
|
),
|
|
)
|
|
break
|
|
# But existing users or other data still matters in a data only
|
|
if (
|
|
User.objects.exclude(username__in=["consumer", "AnonymousUser"]).count()
|
|
!= 0
|
|
):
|
|
self.stdout.write(
|
|
self.style.WARNING(
|
|
"Found existing user(s), this might indicate a non-empty installation",
|
|
),
|
|
)
|
|
if Document.global_objects.count() != 0:
|
|
self.stdout.write(
|
|
self.style.WARNING(
|
|
"Found existing documents(s), this might indicate a non-empty installation",
|
|
),
|
|
)
|
|
|
|
def pre_check_manifest_exists() -> None:
|
|
if not (self.source / "manifest.json").exists():
|
|
raise CommandError(
|
|
"That directory doesn't appear to contain a manifest.json file.",
|
|
)
|
|
|
|
if not self.source.exists():
|
|
raise CommandError("That path doesn't exist")
|
|
|
|
if not os.access(self.source, os.R_OK):
|
|
raise CommandError("That path doesn't appear to be readable")
|
|
|
|
pre_check_maybe_not_empty()
|
|
pre_check_manifest_exists()
|
|
|
|
def load_manifest_files(self) -> None:
|
|
"""
|
|
Loads manifest data from the various JSON files for parsing and loading the database
|
|
"""
|
|
main_manifest_path: Path = self.source / "manifest.json"
|
|
self.manifest_paths.append(main_manifest_path)
|
|
|
|
for file in Path(self.source).glob("**/*-manifest.json"):
|
|
self.manifest_paths.append(file)
|
|
|
|
def load_metadata(self) -> None:
|
|
"""
|
|
Loads either just the version information or the version information and extra data
|
|
|
|
Must account for the old style of export as well, with just version.json
|
|
"""
|
|
version_path: Path = self.source / "version.json"
|
|
metadata_path: Path = self.source / "metadata.json"
|
|
if not version_path.exists() and not metadata_path.exists():
|
|
self.stdout.write(
|
|
self.style.NOTICE("No version.json or metadata.json file located"),
|
|
)
|
|
return
|
|
|
|
if metadata_path.exists():
|
|
with metadata_path.open() as infile:
|
|
data = json.load(infile)
|
|
self.version = data["version"]
|
|
if not self.passphrase and EXPORTER_CRYPTO_SETTINGS_NAME in data:
|
|
raise CommandError(
|
|
"No passphrase was given, but this export contains encrypted fields",
|
|
)
|
|
elif EXPORTER_CRYPTO_SETTINGS_NAME in data:
|
|
self.load_crypt_params(data)
|
|
elif version_path.exists():
|
|
with version_path.open() as infile:
|
|
self.version = json.load(infile)["version"]
|
|
|
|
if self.version and self.version != version.__full_version_str__:
|
|
self.stdout.write(
|
|
self.style.WARNING(
|
|
"Version mismatch: "
|
|
f"Currently {version.__full_version_str__},"
|
|
f" importing {self.version}."
|
|
" Continuing, but import may fail.",
|
|
),
|
|
)
|
|
|
|
def _finalize_db_load(self, loaded_models: set[type[Model]]) -> None:
|
|
"""Verify referential integrity and reset auto-increment sequences."""
|
|
through_tables = {
|
|
field.remote_field.through._meta.db_table
|
|
for model in loaded_models
|
|
for field in model._meta.many_to_many
|
|
if field.remote_field.through is not None
|
|
and field.remote_field.through._meta.auto_created
|
|
}
|
|
table_names = [m._meta.db_table for m in loaded_models] + list(through_tables)
|
|
if table_names:
|
|
connection.check_constraints(table_names=table_names)
|
|
|
|
if loaded_models:
|
|
sequence_sql = connection.ops.sequence_reset_sql(
|
|
no_style(),
|
|
list(loaded_models),
|
|
)
|
|
with connection.cursor() as cursor:
|
|
for sql in sequence_sql:
|
|
cursor.execute(sql) # pragma: no cover
|
|
|
|
def _import_error_context_message(self) -> str:
|
|
"""Return a diagnostic string explaining a DB import failure."""
|
|
if ( # pragma: no cover
|
|
self.version is not None and self.version != version.__full_version_str__
|
|
):
|
|
return ( # pragma: no cover
|
|
"Version mismatch: "
|
|
f"Currently {version.__full_version_str__},"
|
|
f" importing {self.version}"
|
|
)
|
|
return "No version information present"
|
|
|
|
def load_data_to_database(self) -> None:
|
|
"""
|
|
Streams records from each manifest path and loads them into the database
|
|
using bulk_create with bounded batch sizes, avoiding holding the entire
|
|
manifest in memory at once.
|
|
|
|
Memory bound: at most batch_size * (number of distinct model types
|
|
present simultaneously in the manifest) instances at any time.
|
|
For the standard non-split manifest, records are grouped by model, so
|
|
in practice only one model's batch accumulates at a time.
|
|
"""
|
|
# Maps model class -> list of (instance, m2m_data) waiting to be flushed
|
|
pending: defaultdict[type[Model], list[tuple[Model, M2MData]]] = defaultdict(
|
|
list,
|
|
)
|
|
# All model classes inserted (needed for sequence reset after the load)
|
|
loaded_models: set[type[Model]] = set()
|
|
|
|
def flush_model(model: type[Model]) -> None:
|
|
"""bulk_create the pending batch for model, then apply M2M."""
|
|
batch = pending.pop(model, [])
|
|
if not batch: # pragma: no cover
|
|
return
|
|
instances = [inst for inst, _ in batch]
|
|
# GeneratedField is excluded because it is generated and trying to insert it will fail
|
|
update_fields = [
|
|
f.attname
|
|
for f in model._meta.concrete_fields
|
|
if not f.primary_key and not isinstance(f, GeneratedField)
|
|
]
|
|
if not update_fields: # pragma: no cover
|
|
raise DeserializationError(
|
|
f"{model.__name__} has no updatable fields; PK-only models are not supported by the importer",
|
|
)
|
|
# MySQL/MariaDB support upserts via ON DUPLICATE KEY UPDATE but,
|
|
# unlike PostgreSQL/SQLite, cannot target a specific unique field
|
|
# for the conflict -- passing unique_fields there raises
|
|
# NotSupportedError.
|
|
unique_fields = (
|
|
[model._meta.pk.attname]
|
|
if connection.features.supports_update_conflicts_with_target
|
|
else None
|
|
)
|
|
model.objects.bulk_create( # type: ignore[attr-defined]
|
|
instances,
|
|
update_conflicts=True,
|
|
unique_fields=unique_fields,
|
|
update_fields=update_fields,
|
|
)
|
|
loaded_models.add(model)
|
|
for instance, m2m_data in batch:
|
|
for field_name, pk_list in m2m_data.items():
|
|
getattr(instance, field_name).set(pk_list)
|
|
|
|
def flush_all() -> None:
|
|
for model in list(pending):
|
|
flush_model(model)
|
|
|
|
try:
|
|
with transaction.atomic():
|
|
# ContentType and Permission have auto-assigned PKs on a fresh
|
|
# install that conflict with exported PKs. Delete and re-import.
|
|
ContentType.objects.all().delete()
|
|
Permission.objects.all().delete()
|
|
|
|
# Constraint checks are disabled so FK/M2M inserts succeed
|
|
# regardless of record order within the manifest.
|
|
# Note: on SQLite inside a transaction this context manager is a
|
|
# no-op; the constraint-deferral path is only exercised on
|
|
# PostgreSQL in production.
|
|
with connection.constraint_checks_disabled():
|
|
for manifest_path in self.manifest_paths:
|
|
for record in iter_manifest_records(manifest_path):
|
|
model, instance, m2m_data = _deserialize_record(record)
|
|
pending[model].append((instance, m2m_data))
|
|
if len(pending[model]) >= self.batch_size:
|
|
flush_model(model)
|
|
|
|
flush_all()
|
|
|
|
self._finalize_db_load(loaded_models)
|
|
|
|
except (FieldDoesNotExist, DeserializationError, IntegrityError):
|
|
self.stdout.write(self.style.ERROR("Database import failed"))
|
|
self.stdout.write(self.style.ERROR(self._import_error_context_message()))
|
|
raise
|
|
|
|
# ContentType/Permission rows were deleted and reinserted above; stale
|
|
# in-process caches must be invalidated so permission checks use the
|
|
# new IDs rather than pre-import PKs.
|
|
ContentType.objects.clear_cache()
|
|
clear_ct_cache()
|
|
|
|
def handle(self, *args, **options) -> None:
|
|
logging.getLogger().handlers[0].level = logging.ERROR
|
|
|
|
self.source = Path(options["source"]).resolve()
|
|
self.data_only: bool = options["data_only"]
|
|
self.passphrase: str | None = options.get("passphrase")
|
|
self.batch_size: int = options["batch_size"]
|
|
self.version: str | None = None
|
|
self.salt: str | None = None
|
|
self.manifest_paths = []
|
|
|
|
# Create a temporary directory for extracting a zip file into it, even if supplied source is no zip file to keep code cleaner.
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
if is_zipfile(self.source):
|
|
with ZipFile(self.source) as zf:
|
|
unsupported = {
|
|
info.compress_type
|
|
for info in zf.infolist()
|
|
if not compress_type_readable(info.compress_type)
|
|
}
|
|
if unsupported:
|
|
names = sorted(unreadable_method_names(unsupported))
|
|
message = (
|
|
f"This archive uses compression this Python version cannot "
|
|
f"read ({', '.join(names)})."
|
|
)
|
|
if "zstd" in names:
|
|
message += " zstd archives require Python 3.14+."
|
|
raise CommandError(message)
|
|
zf.extractall(tmp_dir)
|
|
self.source = Path(tmp_dir)
|
|
self._run_import()
|
|
|
|
def _run_import(self) -> None:
|
|
self.pre_check()
|
|
self.load_metadata()
|
|
self.load_manifest_files()
|
|
self.check_manifest_validity()
|
|
self.decrypt_secret_fields()
|
|
|
|
# see /src/documents/signals/handlers.py
|
|
with (
|
|
disable_signal(
|
|
post_save,
|
|
receiver=update_filename_and_move_files,
|
|
sender=Document,
|
|
weak=False,
|
|
),
|
|
disable_signal(
|
|
m2m_changed,
|
|
receiver=update_filename_and_move_files,
|
|
sender=Document.tags.through,
|
|
weak=False,
|
|
),
|
|
disable_signal(
|
|
post_save,
|
|
receiver=update_filename_and_move_files,
|
|
sender=CustomFieldInstance,
|
|
weak=False,
|
|
),
|
|
disable_signal(
|
|
post_save,
|
|
receiver=check_paths_and_prune_custom_fields,
|
|
sender=CustomField,
|
|
),
|
|
):
|
|
if settings.AUDIT_LOG_ENABLED:
|
|
auditlog.unregister(Document)
|
|
auditlog.unregister(Correspondent)
|
|
auditlog.unregister(Tag)
|
|
auditlog.unregister(DocumentType)
|
|
auditlog.unregister(Note)
|
|
auditlog.unregister(CustomField)
|
|
auditlog.unregister(CustomFieldInstance)
|
|
|
|
# Fill up the database with whatever is in the manifest
|
|
self.load_data_to_database()
|
|
|
|
if not self.data_only:
|
|
self._import_files_from_manifest()
|
|
else:
|
|
self.stdout.write(self.style.NOTICE("Data only import completed"))
|
|
|
|
for tmp in getattr(self, "_decrypted_tmp_paths", []):
|
|
tmp.unlink(missing_ok=True)
|
|
|
|
self.stdout.write("Updating search index...")
|
|
call_command(
|
|
"document_index",
|
|
"reindex",
|
|
no_progress_bar=self.no_progress_bar,
|
|
)
|
|
|
|
def check_manifest_validity(self) -> None:
|
|
"""
|
|
Attempts to verify the manifest is valid. Namely checking the files
|
|
referred to exist and the files can be read from
|
|
"""
|
|
|
|
def check_document_validity(document_record: dict) -> None:
|
|
if EXPORTER_FILE_NAME not in document_record:
|
|
raise CommandError(
|
|
"The manifest file contains a record which does not "
|
|
"refer to an actual document file.",
|
|
)
|
|
|
|
doc_file = document_record[EXPORTER_FILE_NAME]
|
|
doc_path: Path = self.source / doc_file
|
|
if not doc_path.exists():
|
|
raise CommandError(
|
|
f'The manifest file refers to "{doc_file}" which does not '
|
|
"appear to be in the source directory.",
|
|
)
|
|
try:
|
|
with doc_path.open(mode="rb"):
|
|
pass
|
|
except Exception as e:
|
|
raise CommandError(
|
|
f"Failed to read from original file {doc_path}",
|
|
) from e
|
|
|
|
if EXPORTER_ARCHIVE_NAME in document_record:
|
|
archive_file = document_record[EXPORTER_ARCHIVE_NAME]
|
|
doc_archive_path: Path = self.source / archive_file
|
|
if not doc_archive_path.exists():
|
|
raise CommandError(
|
|
f"The manifest file refers to {archive_file} which "
|
|
f"does not appear to be in the source directory.",
|
|
)
|
|
try:
|
|
with doc_archive_path.open(mode="rb"):
|
|
pass
|
|
except Exception as e:
|
|
raise CommandError(
|
|
f"Failed to read from archive file {doc_archive_path}",
|
|
) from e
|
|
|
|
def check_share_link_bundle_validity(bundle_record: dict) -> None:
|
|
if EXPORTER_SHARE_LINK_BUNDLE_NAME not in bundle_record:
|
|
return
|
|
|
|
bundle_file = bundle_record[EXPORTER_SHARE_LINK_BUNDLE_NAME]
|
|
bundle_path: Path = self.source / bundle_file
|
|
if not bundle_path.exists():
|
|
raise CommandError(
|
|
f'The manifest file refers to "{bundle_file}" which does not '
|
|
"appear to be in the source directory.",
|
|
)
|
|
try:
|
|
with bundle_path.open(mode="rb"):
|
|
pass
|
|
except Exception as e:
|
|
raise CommandError(
|
|
f"Failed to read from share link bundle file {bundle_path}",
|
|
) from e
|
|
|
|
self.stdout.write("Checking the manifest")
|
|
for manifest_path in self.manifest_paths:
|
|
for record in iter_manifest_records(manifest_path):
|
|
# Only check if the document files exist if this is not data only
|
|
# We don't care about documents for a data only import
|
|
if self.data_only:
|
|
continue
|
|
if record["model"] == "documents.document":
|
|
check_document_validity(record)
|
|
elif record["model"] == "documents.sharelinkbundle":
|
|
check_share_link_bundle_validity(record)
|
|
|
|
def _import_files_from_manifest(self) -> None:
|
|
settings.ORIGINALS_DIR.mkdir(parents=True, exist_ok=True)
|
|
settings.THUMBNAIL_DIR.mkdir(parents=True, exist_ok=True)
|
|
settings.ARCHIVE_DIR.mkdir(parents=True, exist_ok=True)
|
|
settings.SHARE_LINK_BUNDLE_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
self.stdout.write("Copy files into paperless...")
|
|
|
|
for record in self.track(
|
|
_iter_document_copy_records(self.manifest_paths),
|
|
description="Copying files...",
|
|
):
|
|
document = Document.global_objects.get(pk=record["pk"])
|
|
|
|
doc_file = record[EXPORTER_FILE_NAME]
|
|
document_path = self.source / doc_file
|
|
|
|
if record[EXPORTER_THUMBNAIL_NAME]:
|
|
thumb_file = record[EXPORTER_THUMBNAIL_NAME]
|
|
thumbnail_path = (self.source / thumb_file).resolve()
|
|
else:
|
|
thumbnail_path = None
|
|
|
|
if record[EXPORTER_ARCHIVE_NAME]:
|
|
archive_file = record[EXPORTER_ARCHIVE_NAME]
|
|
archive_path = self.source / archive_file
|
|
else:
|
|
archive_path = None
|
|
|
|
with media_lock():
|
|
if Path(document.source_path).is_file():
|
|
raise FileExistsError(document.source_path)
|
|
|
|
create_source_path_directory(document.source_path)
|
|
|
|
copy_file_with_basic_stats(document_path, document.source_path)
|
|
|
|
if thumbnail_path:
|
|
copy_file_with_basic_stats(
|
|
thumbnail_path,
|
|
document.thumbnail_path,
|
|
)
|
|
|
|
if archive_path:
|
|
create_source_path_directory(document.archive_path)
|
|
# TODO: this assumes that the export is valid and
|
|
# archive_filename is present on all documents with
|
|
# archived files
|
|
copy_file_with_basic_stats(archive_path, document.archive_path)
|
|
|
|
for record in self.track(
|
|
_iter_share_link_bundle_copy_records(self.manifest_paths),
|
|
description="Copying share link bundles...",
|
|
):
|
|
bundle = ShareLinkBundle.objects.get(pk=record["pk"])
|
|
bundle_file = record[EXPORTER_SHARE_LINK_BUNDLE_NAME]
|
|
bundle_source_path = (self.source / bundle_file).resolve()
|
|
bundle_target_path = bundle.absolute_file_path
|
|
if bundle_target_path is None:
|
|
raise CommandError(
|
|
f"Share link bundle {bundle.pk} does not have a valid file path.",
|
|
)
|
|
|
|
with media_lock():
|
|
bundle_target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
copy_file_with_basic_stats(
|
|
bundle_source_path,
|
|
bundle_target_path,
|
|
)
|
|
|
|
def _decrypt_record_if_needed(self, record: dict) -> dict:
|
|
fields = self.CRYPT_FIELDS_BY_MODEL.get(record.get("model", ""))
|
|
if fields:
|
|
for field in fields:
|
|
if record["fields"].get(field):
|
|
record["fields"][field] = self.decrypt_string(
|
|
value=record["fields"][field],
|
|
)
|
|
return record
|
|
|
|
def decrypt_secret_fields(self) -> None:
|
|
"""
|
|
The converse decryption of some fields out of the export before importing to database.
|
|
Streams records from each manifest path and writes decrypted content to a temp file.
|
|
"""
|
|
if not self.passphrase:
|
|
return
|
|
# Salt has been loaded from metadata.json at this point, so it cannot be None
|
|
self.setup_crypto(passphrase=self.passphrase, salt=self.salt)
|
|
self._decrypted_tmp_paths: list[Path] = []
|
|
new_paths: list[Path] = []
|
|
for manifest_path in self.manifest_paths:
|
|
tmp = manifest_path.with_name(manifest_path.stem + ".decrypted.json")
|
|
with tmp.open("w", encoding="utf-8") as out:
|
|
out.write("[\n")
|
|
first = True
|
|
for record in iter_manifest_records(manifest_path):
|
|
if not first:
|
|
out.write(",\n")
|
|
json.dump(
|
|
self._decrypt_record_if_needed(record),
|
|
out,
|
|
indent=2,
|
|
ensure_ascii=False,
|
|
)
|
|
first = False
|
|
out.write("\n]\n")
|
|
self._decrypted_tmp_paths.append(tmp)
|
|
new_paths.append(tmp)
|
|
self.manifest_paths = new_paths
|