Organize downloaded files into per-job subdirectories under "downloads" instead of a flat structure. Update DownloadController and VideoService to read, write, and delete files within these job-specific directories. Add helper methods for directory management and improve logging. Update download/delete endpoints and result URLs to reflect the new structure. Clean up empty job directories after file deletion.
5.5 KiB
5.5 KiB
Session Management Implementation
This document describes the session management feature implemented for TuneRipper to organize MP3 files by user sessions.
Overview
The session management system automatically creates and manages user sessions to organize downloaded files in separate directories. Each session gets its own subdirectory under the downloads folder.
Key Features
- Automatic Session Creation: When the app starts, it creates or retrieves an existing session ID
- Session-Based File Organization: All MP3 files are stored in
/downloads/{sessionId}/directories - Session Persistence: Session IDs are stored in browser's localStorage
- Session Validation: Server validates session IDs and handles expired sessions
- Backward Compatibility: Legacy endpoints still work for existing files
Architecture Changes
Server-Side (TuneRipper.Api)
New Services
ISessionService: Interface for session management operationsSessionService: Implementation handling session creation, validation, and cleanup- Creates unique 16-character session IDs
- Manages session directories under
/downloads/{sessionId}/ - Automatic cleanup of expired sessions (24-hour timeout)
- Background timer for periodic cleanup
Updated Controllers
VideoController: Now accepts and manages session IDs in processing requestsDownloadController: Enhanced with session-aware endpointsSessionController: New controller for session CRUD operations
New Endpoints
POST /api/session/create - Create new session
GET /api/session/validate/{sessionId} - Validate existing session
POST /api/session/refresh/{sessionId} - Refresh session timeout
GET /api/download/{sessionId}/{jobId} - Session-aware file download
GET /api/download/info/{sessionId}/{jobId} - Session-aware file info
DELETE /api/download/{sessionId}/{jobId} - Session-aware file deletion
GET /api/video/jobs/{sessionId} - Get jobs for specific session
File Organization
downloads/
??? legacy_files.mp3 # Legacy files (root level)
??? {sessionId1}/ # Session 1 directory
? ??? {jobId}_video_title.mp3 # Session 1 files
? ??? {jobId}_video_title.mp3
??? {sessionId2}/ # Session 2 directory
??? {jobId}_video_title.mp3 # Session 2 files
??? {jobId}_video_title.mp3
Client-Side (TuneRipper)
New Services
IClientSessionService: Interface for client-side session managementClientSessionService: Implementation handling browser-side session storage- Manages session ID in localStorage
- Validates sessions with server
- Automatic session creation when needed
Updated Components
Home.razor: Enhanced to use session management- Displays current session ID (truncated)
- Includes session ID in processing requests
- Constructs session-aware download URLs
Updated API Service
TuneRipperApiService: Extended with session-aware methods- Session creation and validation
- Session-aware file downloads
- Session-specific job queries
Data Flow
-
App Initialization:
Client loads ? Check localStorage for sessionId ? Validate with server ? Create new if invalid -
Video Processing:
User submits URL ? Include sessionId in request ? Server processes to session directory -
File Download:
User clicks download ? Use session-aware URL ? Server serves from session directory
Session Lifecycle
- Creation: 16-character unique ID generated
- Storage: Stored in browser localStorage and server memory
- Validation: Checked on each request, 24-hour timeout
- Cleanup: Automatic background cleanup of expired sessions
- Directory Management: Session directories created/deleted as needed
Backward Compatibility
- Legacy download endpoints still function
- Old files remain accessible through fallback logic
- Gradual migration as users create new sessions
Configuration
Session Settings
- Timeout: 24 hours (configurable in
SessionService) - Cleanup Interval: 1 hour (configurable in
SessionService) - Directory Structure:
/downloads/{sessionId}/
Environment Variables
No new environment variables required - uses existing web root path configuration.
Security Considerations
- Session IDs are random and non-guessable
- Server validates all session operations
- Automatic cleanup prevents disk space accumulation
- No sensitive data stored in sessions
Monitoring and Logging
- Session creation/validation logged
- File operations include session context
- Cleanup operations logged with statistics
- Error handling for session failures
Future Enhancements
- Database Storage: Replace in-memory session storage with persistent storage
- Session Sharing: Allow sharing sessions between devices
- Session Analytics: Track usage patterns and statistics
- Extended Metadata: Store additional session information
- Admin Interface: Management interface for session administration
API Examples
Create Session
POST /api/session/create
Response: { "sessionId": "abc123def456", "message": "Session created successfully" }
Process Video with Session
POST /api/video/process
{ "url": "https://youtube.com/watch?v=...", "sessionId": "abc123def456", "format": "mp3" }
Download File by Session
GET /api/download/abc123def456/job-id-123
Response: Binary audio file