2020-05-04 13:16:44 +02:00
|
|
|
import os
|
2020-05-04 16:05:23 +02:00
|
|
|
import logging
|
2020-05-04 14:39:23 +02:00
|
|
|
import shutil
|
2020-05-04 17:09:02 +02:00
|
|
|
from mglib.utils import safe_to_delete
|
2020-05-04 14:39:23 +02:00
|
|
|
|
2020-05-04 16:05:23 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-05-04 14:39:23 +02:00
|
|
|
|
2020-05-04 12:35:08 +02:00
|
|
|
class Storage:
|
|
|
|
"""
|
2020-05-04 14:39:23 +02:00
|
|
|
Default Storage class which works with DocumentPath and PagePath
|
|
|
|
on local host filesystem
|
2020-05-04 12:35:08 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-04 13:16:44 +02:00
|
|
|
def __init__(self, location=None):
|
2020-05-04 14:39:23 +02:00
|
|
|
# by default, this will be something like
|
|
|
|
# settings.MEDIA_ROOT
|
2020-05-04 13:16:44 +02:00
|
|
|
self._location = location
|
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
return self._location
|
|
|
|
|
2020-05-10 16:02:58 +02:00
|
|
|
def abspath(self, _path):
|
2020-05-04 13:16:44 +02:00
|
|
|
return os.path.join(
|
2020-05-04 13:58:56 +02:00
|
|
|
self.location, _path
|
2020-05-04 13:16:44 +02:00
|
|
|
)
|
|
|
|
|
2020-05-10 16:02:58 +02:00
|
|
|
def path(self, _path):
|
|
|
|
return self.abspath(_path)
|
|
|
|
|
2020-05-04 13:58:56 +02:00
|
|
|
def delete_document(self, doc_path):
|
|
|
|
"""
|
|
|
|
Receives a mglib.path.DocumentPath instance
|
|
|
|
"""
|
2020-05-04 14:39:23 +02:00
|
|
|
# where original documents and their versions are stored
|
|
|
|
abs_dirname_docs = self.path(
|
|
|
|
doc_path.dirname_docs
|
|
|
|
)
|
|
|
|
# where OCRed information and generated thumbnails
|
|
|
|
# are stored
|
|
|
|
abs_dirname_results = self.path(
|
2020-05-04 16:05:23 +02:00
|
|
|
doc_path.dirname_results
|
2020-05-04 14:39:23 +02:00
|
|
|
)
|
|
|
|
# Before recursively deleting everything in folder
|
|
|
|
# double check that there are only
|
|
|
|
# .pdf, .txt, .hocr, .jpg files.
|
|
|
|
if safe_to_delete(
|
2020-05-04 17:09:02 +02:00
|
|
|
abs_dirname_docs
|
2020-05-04 14:39:23 +02:00
|
|
|
):
|
|
|
|
shutil.rmtree(abs_dirname_docs)
|
2020-05-04 17:09:02 +02:00
|
|
|
if os.path.exists(abs_dirname_docs):
|
|
|
|
os.rmdir(abs_dirname_docs)
|
2020-05-04 14:39:23 +02:00
|
|
|
|
|
|
|
if safe_to_delete(
|
2020-05-04 17:09:02 +02:00
|
|
|
abs_dirname_results
|
2020-05-04 14:39:23 +02:00
|
|
|
):
|
|
|
|
shutil.rmtree(abs_dirname_results)
|
2020-05-04 17:09:02 +02:00
|
|
|
if os.path.exists(abs_dirname_results):
|
|
|
|
os.rmdir(abs_dirname_results)
|
2020-05-04 13:58:56 +02:00
|
|
|
|
2020-05-10 16:02:58 +02:00
|
|
|
def copy_doc(self, src, dst):
|
|
|
|
"""
|
|
|
|
copy given file src file path to destination
|
|
|
|
as absolute doc_path
|
|
|
|
"""
|
|
|
|
|
|
|
|
dirname = os.path.dirname(
|
|
|
|
self.abspath(dst)
|
|
|
|
)
|
|
|
|
if not os.path.exists(
|
|
|
|
dirname
|
|
|
|
):
|
|
|
|
os.makedirs(
|
|
|
|
dirname, exist_ok=True
|
|
|
|
)
|
|
|
|
logger.debug(
|
|
|
|
f"copy_doc: {src} to {dst}"
|
|
|
|
)
|
|
|
|
shutil.copyfile(
|
|
|
|
src,
|
|
|
|
self.abspath(dst)
|
|
|
|
)
|
|
|
|
|
2020-05-04 13:58:56 +02:00
|
|
|
def exists(self, _path):
|
|
|
|
return os.path.exists(
|
|
|
|
self.path(_path)
|
2020-05-04 13:16:44 +02:00
|
|
|
)
|
2020-05-04 17:34:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FileSystemStorage(Storage):
|
|
|
|
pass
|