Files
thrillwiki_django_no_react/media/storage.py
2024-11-01 01:27:11 +00:00

39 lines
1.2 KiB
Python

from django.core.files.storage import FileSystemStorage
from django.conf import settings
import os
class MediaStorage(FileSystemStorage):
def __init__(self, *args, **kwargs):
kwargs['location'] = settings.MEDIA_ROOT
kwargs['base_url'] = settings.MEDIA_URL
super().__init__(*args, **kwargs)
def get_available_name(self, name, max_length=None):
"""
Returns a filename that's free on the target storage system.
"""
# Get the directory and filename
directory = os.path.dirname(name)
filename = os.path.basename(name)
# Create directory if it doesn't exist
full_dir = os.path.join(self.location, directory)
os.makedirs(full_dir, exist_ok=True)
# Return the name as is since our upload path already handles uniqueness
return name
def _save(self, name, content):
"""
Save with proper permissions
"""
# Save the file
name = super()._save(name, content)
# Set proper permissions
full_path = self.path(name)
os.chmod(full_path, 0o644)
os.chmod(os.path.dirname(full_path), 0o755)
return name