debut changement vers google drive
CI - Security, Lint & Tests / validate (push) Failing after 1m14s
CI - Security, Lint & Tests / validate (push) Failing after 1m14s
This commit is contained in:
+76
-7
@@ -28,8 +28,17 @@ absolute path and are returned untouched, so this change needs no data
|
||||
migration and can ship before Alembic does (DB-002).
|
||||
"""
|
||||
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
|
||||
from app.google_drive import (
|
||||
GoogleDriveStorageError,
|
||||
delete_file as delete_google_drive_file,
|
||||
download_file as download_google_drive_file,
|
||||
upload_file as upload_google_drive_file,
|
||||
)
|
||||
|
||||
#: Environment override for the document root. What a release-directory
|
||||
#: deployment sets, to a path outside the releases — alongside them, not
|
||||
#: inside whichever one is current.
|
||||
@@ -45,6 +54,16 @@ BACKUP_DIR_ENV = 'BACKUP_DIR'
|
||||
#: Sub-directory holding uploaded contracts, under the document root.
|
||||
CONTRACTS_DIR = 'contrats signés'
|
||||
|
||||
#: Database marker for documents stored remotely. Existing rows continue to
|
||||
#: hold relative or absolute filesystem paths, so switching storage does not
|
||||
#: invalidate contracts already uploaded before the Google Drive move.
|
||||
GOOGLE_DRIVE_PATH_PREFIX = 'gdrive://'
|
||||
|
||||
#: Storage backends deliberately stay explicit. Google Drive is the
|
||||
#: production default; local disk exists only for legacy rows and isolated
|
||||
#: test/development environments.
|
||||
DOCUMENT_STORAGE_BACKEND_ENV = 'DOCUMENT_STORAGE_BACKEND'
|
||||
|
||||
|
||||
def project_root():
|
||||
"""Absolute path of the project, derived from this file's location.
|
||||
@@ -91,6 +110,56 @@ def _rooted(env_name, default_name):
|
||||
return os.path.join(project_root(), default_name)
|
||||
|
||||
|
||||
def document_storage_backend():
|
||||
"""Return the configured backend for new document uploads."""
|
||||
backend = os.getenv(DOCUMENT_STORAGE_BACKEND_ENV, 'google_drive').strip().lower()
|
||||
if backend not in {'google_drive', 'local'}:
|
||||
raise ValueError(
|
||||
f'{DOCUMENT_STORAGE_BACKEND_ENV} must be "google_drive" or "local", not {backend!r}'
|
||||
)
|
||||
return backend
|
||||
|
||||
|
||||
def is_google_drive_path(stored_path):
|
||||
"""Whether a database path references a Google Drive file."""
|
||||
return bool(stored_path and stored_path.startswith(GOOGLE_DRIVE_PATH_PREFIX))
|
||||
|
||||
|
||||
def _google_drive_id(stored_path):
|
||||
file_id = stored_path.removeprefix(GOOGLE_DRIVE_PATH_PREFIX)
|
||||
if not file_id:
|
||||
raise GoogleDriveStorageError('The stored Google Drive file identifier is empty.')
|
||||
return file_id
|
||||
|
||||
|
||||
def store_uploaded_document(file_storage, relative_path):
|
||||
"""Store an uploaded document and return its durable database reference.
|
||||
|
||||
Local storage retains the relative-path format used by existing rows. A
|
||||
Google Drive upload returns an opaque Drive file identifier prefixed with
|
||||
``gdrive://`` so it cannot be mistaken for a filesystem path.
|
||||
"""
|
||||
if document_storage_backend() == 'local':
|
||||
absolute_path = document_path(relative_path)
|
||||
os.makedirs(os.path.dirname(absolute_path), exist_ok=True)
|
||||
file_storage.save(absolute_path)
|
||||
return relative_path
|
||||
|
||||
file_id = upload_google_drive_file(
|
||||
stream=file_storage.stream,
|
||||
filename=os.path.basename(relative_path),
|
||||
mimetype=file_storage.mimetype or 'application/pdf',
|
||||
)
|
||||
return f'{GOOGLE_DRIVE_PATH_PREFIX}{file_id}'
|
||||
|
||||
|
||||
def open_document(stored_path):
|
||||
"""Return a filesystem path or in-memory stream suitable for ``send_file``."""
|
||||
if is_google_drive_path(stored_path):
|
||||
return io.BytesIO(download_google_drive_file(_google_drive_id(stored_path)))
|
||||
return document_path(stored_path)
|
||||
|
||||
|
||||
def discard_documents(stored_paths):
|
||||
"""Remove these documents from disk. Returns how many went (DATA-012).
|
||||
|
||||
@@ -107,27 +176,27 @@ def discard_documents(stored_paths):
|
||||
A path that cannot be removed is logged and skipped. Nothing here should
|
||||
be able to abort the deletion of an account.
|
||||
"""
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
removed = 0
|
||||
for stored_path in stored_paths:
|
||||
if not stored_path:
|
||||
continue
|
||||
target = document_path(stored_path)
|
||||
try:
|
||||
os.remove(target)
|
||||
if is_google_drive_path(stored_path):
|
||||
delete_google_drive_file(_google_drive_id(stored_path))
|
||||
else:
|
||||
os.remove(document_path(stored_path))
|
||||
removed += 1
|
||||
except FileNotFoundError:
|
||||
# Already gone. Two contracts sharing a stem, or a previous
|
||||
# attempt: not a problem, and not worth an error line.
|
||||
logger.info('Document already absent: %s', target)
|
||||
except OSError as exc:
|
||||
logger.info('Document already absent: %s', stored_path)
|
||||
except (GoogleDriveStorageError, OSError) as exc:
|
||||
logger.error(
|
||||
'Could not remove %s (%s). It is now an orphan: no database row '
|
||||
'refers to it, so nothing in the application will ever offer to '
|
||||
'delete it again.',
|
||||
target,
|
||||
stored_path,
|
||||
exc,
|
||||
)
|
||||
return removed
|
||||
|
||||
Reference in New Issue
Block a user