This commit is contained in:
Louis Mazin 2026-10-14 00:49:38 +02:00
parent 826f67f254
commit df47775b0a
4 changed files with 9 additions and 6 deletions

View File

@ -180,8 +180,8 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Les documents privés ne doivent pas être servis directement par STATIC_URL
# ou MEDIA_URL : les téléchargements sont contrôlés par les vues de GoodDrive.
DRIVE_STORAGE_ROOT = Path(config('DRIVE_STORAGE_ROOT', default='/data/files')).resolve()
FILE_UPLOAD_TEMP_DIR = str(DRIVE_STORAGE_ROOT.parent / 'tmp')
DRIVE_STORAGE_ROOT = Path(config('DRIVE_STORAGE_ROOT', default='/storage')).resolve()
FILE_UPLOAD_TEMP_DIR = str(Path(config('FILE_UPLOAD_TEMP_DIR', default=str(DRIVE_STORAGE_ROOT / '.uploads'))).resolve())
DRIVE_STORAGE_ROOT.mkdir(parents=True, exist_ok=True)
Path(FILE_UPLOAD_TEMP_DIR).mkdir(parents=True, exist_ok=True)

View File

@ -64,7 +64,7 @@ class DriveItem(models.Model):
is_folder = models.BooleanField(default=False)
storage_key = models.CharField(max_length=120, blank=True)
# Existing files keep a blank storage_location (legacy /files/ab/<uuid>).
# New files go under /files/root/0001/.../ab/<uuid>.
# Files go under /storage/0001/.../ab/<uuid>, without a /root layer.
storage_location = models.CharField(max_length=1200, blank=True)
size = models.BigIntegerField(default=0)
mime_type = models.CharField(max_length=180, default='application/octet-stream')

View File

@ -47,6 +47,9 @@ def ancestry(space):
def subtree_space_ids(space):
"""Use the stable DB path rather than a filesystem traversal."""
# The logical root has an empty relative_path and contains every space.
if space.parent_id is None and space.relative_path == '':
return DriveSpace.objects.all().values_list('pk', flat=True)
return DriveSpace.objects.filter(
Q(relative_path=space.relative_path) | Q(relative_path__startswith=space.relative_path + '/')
).values_list('pk', flat=True)

View File

@ -26,7 +26,7 @@ def ensure_space(user):
if root is None:
if not is_admin(user):
raise PermissionError('Un administrateur doit initialiser le Drive.')
obj = DriveSpace.objects.create(user=user, relative_path='root', display_name='Racine')
obj = DriveSpace.objects.create(user=user, relative_path='', display_name='Racine')
else:
obj = create_space(user, root, user.username)
return obj
@ -37,7 +37,7 @@ def _make_numbered_space(parent, **fields):
raise ValueError('Hiérarchie trop profonde.')
obj = DriveSpace.objects.create(parent=parent, relative_path=f'pending/{secrets.token_hex(10)}', **fields)
obj.directory_name = f'{obj.pk - 1:04d}'
obj.relative_path = f'{parent.relative_path}/{obj.directory_name}'
obj.relative_path = '/'.join(part for part in (parent.relative_path, obj.directory_name) if part)
obj.save(update_fields=['directory_name', 'relative_path'])
(Path(settings.DRIVE_STORAGE_ROOT) / obj.relative_path).mkdir(mode=0o700, parents=True, exist_ok=True)
return obj
@ -162,7 +162,7 @@ def storage_path(item):
def new_storage_location(space, key):
return f'{space.relative_path}/{key[:2]}/{key}'
return '/'.join(part for part in (space.relative_path, key[:2], key) if part)
def hash_token(token):