Introduce automatic ZIP file creation and management for session-based downloads. - Server: Add endpoints to serve and provide info about session ZIPs; VideoService now updates ZIP after each MP3 download with user-friendly filenames. - Client: Add "Download All in ZIP" button with loading state and ZIP info; trigger browser download via JS interop; responsive and styled UI. - API: Extend service interfaces for ZIP download/info; add SessionZipInfo model. - Docs: Update SESSION_MANAGEMENT.md with ZIP workflow, endpoints, and UX details. - Robust error handling and logging throughout.
10 KiB
10 KiB
Session Management Implementation
This document describes the session management feature implemented for TuneRipper to organize MP3 files by user sessions, including automatic ZIP file creation and management.
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, with automatic ZIP file creation and updates.
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 - Automatic ZIP File Management: Session ZIP files are created/updated automatically after each successful download
- Download All in ZIP: Users can download all session files in a single ZIP archive
- 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 Services
VideoService: Enhanced with ZIP file management- Automatically creates/updates session ZIP files after successful MP3 downloads
- Uses System.IO.Compression for ZIP operations
- Creates user-friendly filenames within ZIP archives
- Handles ZIP creation errors gracefully without breaking main process
Updated Controllers
VideoController: Now accepts and manages session IDs in processing requestsDownloadController: Enhanced with session-aware endpoints and ZIP download supportSessionController: 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/download/zip/{sessionId} - Download session ZIP file
GET /api/download/zip/info/{sessionId} - Get session ZIP file information
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 MP3 files
? ??? {jobId}_video_title.mp3
? ??? session_{sessionId1}.zip # Automatic ZIP containing all MP3s
??? {sessionId2}/ # Session 2 directory
??? {jobId}_video_title.mp3 # Session 2 MP3 files
??? {jobId}_video_title.mp3
??? session_{sessionId2}.zip # Automatic ZIP containing all MP3s
ZIP File Management
- Automatic Creation: ZIP files are created/updated after each successful MP3 download
- User-Friendly Names: Files within ZIP use clean titles instead of job IDs
- Atomic Updates: Old ZIP is deleted before creating new one to ensure consistency
- Error Handling: ZIP creation failures don't interrupt main download process
- Efficient Storage: Only MP3 files are included in ZIP archives
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 and ZIP downloads- Displays current session ID (truncated)
- Includes session ID in processing requests
- Constructs session-aware download URLs
- "Download All in ZIP" Button: Prominently displayed when multiple files exist
- ZIP file information display (file count, size)
- Loading states for ZIP preparation
Updated API Service
TuneRipperApiService: Extended with session-aware methods and ZIP support- Session creation and validation
- Session-aware file downloads
- Session-specific job queries
- ZIP download methods:
DownloadSessionZipAsync(),GetSessionZipInfoAsync()
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 ? Update ZIP file -
File Download (Individual):
User clicks download ? Use session-aware URL ? Server serves from session directory -
ZIP Download (All Files):
User clicks "Download All in ZIP" ? Server serves session ZIP file ? Browser downloads ZIP
ZIP File Features
Automatic ZIP Management
- Creation Trigger: ZIP file is created/updated after each successful MP3 download
- Content: Contains all MP3 files in the session with user-friendly names
- Naming Convention:
session_{sessionId}.zip - Download Name:
TuneRipper_Session_{sessionId_prefix}.zip
User Interface
- Visibility: "Download All in ZIP" button appears when 2+ files exist in session
- Information Display: Shows file count and ZIP file size
- Visual Design: Distinctive purple gradient button with package icon
- Loading States: Shows "Preparing ZIP..." during download initiation
- Responsive: Adapts to mobile screen sizes
Technical Implementation
- Server-Side: Uses
System.IO.Compression.ZipFilefor ZIP operations - Client-Side: Direct browser download via JavaScript
window.open() - Error Handling: Graceful fallback if ZIP is not available
- Performance: ZIP creation happens asynchronously without blocking UI
Session Lifecycle
- Creation: 16-character unique ID generated
- Storage: Stored in browser localStorage and server memory
- Validation: Checked on each request, 24-hour timeout
- ZIP Management: ZIP files created/updated automatically with each download
- Cleanup: Automatic background cleanup of expired sessions and their ZIP files
- Directory Management: Session directories and ZIP files 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
- No ZIP files created for legacy sessions
Configuration
Session Settings
- Timeout: 24 hours (configurable in
SessionService) - Cleanup Interval: 1 hour (configurable in
SessionService) - Directory Structure:
/downloads/{sessionId}/ - ZIP Naming:
session_{sessionId}.zip
ZIP Settings
- Compression: Default ZIP compression
- Content: MP3 files only (excludes temporary files)
- Entry Names: User-friendly titles extracted from file names
- Update Strategy: Replace entire ZIP file on each update
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
- ZIP files are only accessible with valid session IDs
Monitoring and Logging
- Session creation/validation logged
- File operations include session context
- ZIP creation/update operations logged
- Cleanup operations logged with statistics
- Error handling for session and ZIP failures
Future Enhancements
- Database Storage: Replace in-memory session storage with persistent storage
- ZIP Compression Options: Allow users to choose compression levels
- Selective ZIP Downloads: Allow users to choose which files to include in ZIP
- ZIP Password Protection: Optional password protection for ZIP files
- 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 Individual File
GET /api/download/abc123def456/job-id-123
Response: Binary audio file
Download Session ZIP
GET /api/download/zip/abc123def456
Response: Binary ZIP file containing all session MP3s
Get ZIP Information
GET /api/download/zip/info/abc123def456
Response: {
"sessionId": "abc123def456",
"zipFileName": "session_abc123def456.zip",
"fileSize": "15.2 MB",
"mp3FilesCount": 5,
"downloadUrl": "/api/download/zip/abc123def456"
}
User Experience
New User Flow
- User visits site ? Session created automatically
- User downloads videos ? Files organized in session, ZIP updated
- User sees "Download All in ZIP" button ? Can download all files at once
- User returns later ? Session restored from localStorage
Benefits for Users
- Organization: All downloads from a session grouped together
- Convenience: Single-click download of all files via ZIP
- Efficiency: No need to download files individually
- Clean Downloads: Files in ZIP have user-friendly names
- Session Persistence: Downloads available across browser sessions