Implement organized media folder structure for community posts
- Add media_folder field to Post model for organized file storage
- Create MediaConfig class for centralized media management settings
- Update community routes to use post-specific media folders
- Add thumbnail generation for uploaded images
- Implement structured folder layout: app/static/media/posts/{post_folder}/
- Add utility functions for image and GPX file handling
- Create media management script for migration and maintenance
- Add proper file validation and MIME type checking
- Include routes for serving images, thumbnails, and GPX files
- Maintain backward compatibility with existing uploads
- Add comprehensive documentation and migration tools
Each post now gets its own media folder with subfolders for:
- images/ (with thumbnails/ subfolder)
- gpx/
Post content remains in database for optimal query performance while
media files are organized in dedicated folders for better management.
This commit is contained in:
70
app/media_config.py
Normal file
70
app/media_config.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
Media configuration settings for the motorcycle adventure community app
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
class MediaConfig:
|
||||
"""Configuration for media file handling"""
|
||||
|
||||
# File upload settings
|
||||
MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB max file size
|
||||
UPLOAD_EXTENSIONS = {
|
||||
'images': ['jpg', 'jpeg', 'png', 'gif', 'webp'],
|
||||
'gpx': ['gpx']
|
||||
}
|
||||
|
||||
# Image processing settings
|
||||
IMAGE_MAX_SIZE = (1920, 1080) # Max dimensions for images
|
||||
IMAGE_QUALITY = 85 # JPEG quality (1-100)
|
||||
THUMBNAIL_SIZE = (300, 300) # Thumbnail dimensions
|
||||
|
||||
# Media folder settings
|
||||
MEDIA_FOLDER = 'app/static/media/posts'
|
||||
MEDIA_URL_PREFIX = '/static/media/posts'
|
||||
|
||||
# Folder structure
|
||||
MEDIA_SUBFOLDERS = ['images', 'gpx']
|
||||
|
||||
# Security settings
|
||||
ALLOWED_MIME_TYPES = {
|
||||
'image/jpeg': 'jpg',
|
||||
'image/png': 'png',
|
||||
'image/gif': 'gif',
|
||||
'image/webp': 'webp',
|
||||
'application/gpx+xml': 'gpx',
|
||||
'application/xml': 'gpx',
|
||||
'text/xml': 'gpx'
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_media_path(app, post_media_folder, subfolder=''):
|
||||
"""Get the full filesystem path to a media folder"""
|
||||
path = os.path.join(app.root_path, 'static', 'media', 'posts', post_media_folder)
|
||||
if subfolder:
|
||||
path = os.path.join(path, subfolder)
|
||||
return path
|
||||
|
||||
@staticmethod
|
||||
def get_media_url(post_media_folder, subfolder='', filename=''):
|
||||
"""Get the URL path to a media file"""
|
||||
url = f'/static/media/posts/{post_media_folder}'
|
||||
if subfolder:
|
||||
url += f'/{subfolder}'
|
||||
if filename:
|
||||
url += f'/{filename}'
|
||||
return url
|
||||
|
||||
@staticmethod
|
||||
def is_allowed_file(filename, file_type='images'):
|
||||
"""Check if file extension is allowed"""
|
||||
if '.' not in filename:
|
||||
return False
|
||||
|
||||
ext = filename.rsplit('.', 1)[1].lower()
|
||||
return ext in MediaConfig.UPLOAD_EXTENSIONS.get(file_type, [])
|
||||
|
||||
@staticmethod
|
||||
def is_allowed_mime_type(mime_type):
|
||||
"""Check if MIME type is allowed"""
|
||||
return mime_type in MediaConfig.ALLOWED_MIME_TYPES
|
||||
Reference in New Issue
Block a user