- 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.
3.8 KiB
3.8 KiB
Media Management for Motorcycle Adventure Community
Overview
The application now uses an organized media folder structure where each post has its own dedicated folder for storing images, GPX files, and other media content.
Folder Structure
app/static/media/posts/
├── post_abc12345_20250723/
│ ├── images/
│ │ ├── cover_image.jpg
│ │ ├── route_photo_1.jpg
│ │ └── route_photo_2.jpg
│ └── gpx/
│ └── route_track.gpx
├── post_def67890_20250724/
│ ├── images/
│ └── gpx/
└── ...
Post Content Storage Strategy
Database Storage (Recommended ✅)
- Post metadata: title, subtitle, content, difficulty, publication status
- Relationships: author, comments, likes
- Media references: filenames and metadata stored in database
- Benefits:
- Fast queries and searching
- Proper relationships and constraints
- ACID compliance
- Easy backups with database tools
- Scalability for large amounts of posts
File Storage
- Media files: images, GPX files stored in organized folders
- Benefits:
- Direct web server access (faster serving)
- Easy file management
- Reduced database size
- CDN compatibility
Database Schema
Posts Table
media_folder: String field containing the unique folder name for this post's media- Content stored as TEXT in the database for optimal performance
PostImage Table
filename: Stored filename (unique)original_name: User's original filenameis_cover: Boolean indicating if this is the cover imagepost_id: Foreign key to posts table
GPXFile Table
filename: Stored filename (unique)original_name: User's original filenamepost_id: Foreign key to posts table
Media Folder Naming Convention
Format: post_{8-char-uuid}_{YYYYMMDD}
- Example:
post_abc12345_20250723 - Ensures uniqueness and chronological organization
File Upload Process
-
Post Creation:
- Generate unique media folder name
- Create post record in database
- Create folder structure in
app/static/media/posts/
-
File Upload:
- Save files to post-specific subfolders
- Store metadata in database
- Generate thumbnails (for images)
-
File Access:
- URLs:
/static/media/posts/{media_folder}/images/{filename} - Direct web server serving for performance
- URLs:
Migration and Management
Manual Migration Script
python manage_media.py --all
Available Commands
--create-folders: Create media folders for existing posts--migrate-files: Move files from old structure to new structure--clean-orphaned: Remove unused media folders--stats: Show storage statistics
Security Considerations
- File Type Validation: Only allowed image and GPX file types
- File Size Limits: Configurable maximum file sizes
- Filename Sanitization: Secure filename generation with UUIDs
- Access Control: Media files served through static file handler
Performance Optimizations
- Image Compression: Automatic JPEG compression with 85% quality
- Image Resizing: Automatic thumbnail generation and size limits
- Direct File Serving: Static files served directly by web server
- CDN Ready: File structure compatible with CDN distribution
Backup Strategy
- Database: Regular database backups include all post content and metadata
- Media Files: File system backups of
app/static/media/posts/directory - Synchronization: Media folder names in database ensure consistency
Development Notes
- Post content remains in database for optimal query performance
- Media files organized by post for easy management
- Backward compatibility maintained for existing installations
- Migration tools provided for seamless upgrades