mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 08:31:08 -05:00
photos fix
This commit is contained in:
38
media/storage.py
Normal file
38
media/storage.py
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
Reference in New Issue
Block a user