2022-11-19 17:37:32 -08:00
|
|
|
import os
|
2021-02-20 16:09:29 +01:00
|
|
|
from zipfile import ZipFile
|
|
|
|
|
|
|
|
|
|
from documents.models import Document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulkArchiveStrategy:
|
2022-11-19 17:37:32 -08:00
|
|
|
def __init__(self, zipf: ZipFile, follow_formatting: bool = False):
|
2021-02-20 16:09:29 +01:00
|
|
|
self.zipf = zipf
|
2022-11-19 17:37:32 -08:00
|
|
|
if follow_formatting:
|
|
|
|
|
self.make_unique_filename = self._formatted_filepath
|
|
|
|
|
else:
|
|
|
|
|
self.make_unique_filename = self._filename_only
|
2021-02-20 16:09:29 +01:00
|
|
|
|
2022-11-19 17:37:32 -08:00
|
|
|
def _filename_only(
|
2022-03-11 10:55:51 -08:00
|
|
|
self,
|
|
|
|
|
doc: Document,
|
|
|
|
|
archive: bool = False,
|
|
|
|
|
folder: str = "",
|
2022-02-27 15:26:41 +01:00
|
|
|
):
|
2022-11-19 17:37:32 -08:00
|
|
|
"""
|
|
|
|
|
Constructs a unique name for the given document to be used inside the
|
|
|
|
|
zip file.
|
|
|
|
|
|
|
|
|
|
The filename might not be unique enough, so a counter is appended if needed
|
|
|
|
|
"""
|
2021-02-20 16:09:29 +01:00
|
|
|
counter = 0
|
|
|
|
|
while True:
|
|
|
|
|
filename = folder + doc.get_public_filename(archive, counter)
|
|
|
|
|
if filename in self.zipf.namelist():
|
|
|
|
|
counter += 1
|
|
|
|
|
else:
|
|
|
|
|
return filename
|
|
|
|
|
|
2022-11-19 17:37:32 -08:00
|
|
|
def _formatted_filepath(
|
|
|
|
|
self,
|
|
|
|
|
doc: Document,
|
|
|
|
|
archive: bool = False,
|
|
|
|
|
folder: str = "",
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Constructs a full file path for the given document to be used inside
|
|
|
|
|
the zipfile.
|
|
|
|
|
|
|
|
|
|
The path is already unique, as handled when a document is consumed or updated
|
|
|
|
|
"""
|
|
|
|
|
if archive and doc.has_archive_version:
|
|
|
|
|
in_archive_path = os.path.join(folder, doc.archive_filename)
|
|
|
|
|
else:
|
|
|
|
|
in_archive_path = os.path.join(folder, doc.filename)
|
|
|
|
|
|
|
|
|
|
return in_archive_path
|
|
|
|
|
|
2021-02-20 16:09:29 +01:00
|
|
|
def add_document(self, doc: Document):
|
|
|
|
|
raise NotImplementedError() # pragma: no cover
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OriginalsOnlyStrategy(BulkArchiveStrategy):
|
|
|
|
|
def add_document(self, doc: Document):
|
|
|
|
|
self.zipf.write(doc.source_path, self.make_unique_filename(doc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArchiveOnlyStrategy(BulkArchiveStrategy):
|
|
|
|
|
def add_document(self, doc: Document):
|
|
|
|
|
if doc.has_archive_version:
|
2022-02-27 15:26:41 +01:00
|
|
|
self.zipf.write(
|
2022-03-11 10:55:51 -08:00
|
|
|
doc.archive_path,
|
|
|
|
|
self.make_unique_filename(doc, archive=True),
|
2022-02-27 15:26:41 +01:00
|
|
|
)
|
2021-02-20 16:09:29 +01:00
|
|
|
else:
|
2022-02-27 15:26:41 +01:00
|
|
|
self.zipf.write(doc.source_path, self.make_unique_filename(doc))
|
2021-02-20 16:09:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class OriginalAndArchiveStrategy(BulkArchiveStrategy):
|
|
|
|
|
def add_document(self, doc: Document):
|
|
|
|
|
if doc.has_archive_version:
|
|
|
|
|
self.zipf.write(
|
2022-02-27 15:26:41 +01:00
|
|
|
doc.archive_path,
|
|
|
|
|
self.make_unique_filename(doc, archive=True, folder="archive/"),
|
2021-02-20 16:09:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.zipf.write(
|
2022-03-11 10:55:51 -08:00
|
|
|
doc.source_path,
|
|
|
|
|
self.make_unique_filename(doc, folder="originals/"),
|
2021-02-20 16:09:29 +01:00
|
|
|
)
|