Add "Download All in ZIP" feature for session downloads
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.
This commit is contained in:
@@ -18,6 +18,108 @@ public class DownloadController : ControllerBase
|
||||
_sessionService = sessionService;
|
||||
}
|
||||
|
||||
// New endpoint to download session ZIP file
|
||||
[HttpGet("zip/{sessionId}")]
|
||||
public IActionResult DownloadSessionZip(string sessionId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sessionId))
|
||||
{
|
||||
return BadRequest("Session ID is required");
|
||||
}
|
||||
|
||||
if (!_sessionService.IsValidSession(sessionId))
|
||||
{
|
||||
return NotFound("Invalid or expired session");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sessionDownloadsDir = _sessionService.GetSessionDownloadsPath(sessionId);
|
||||
|
||||
if (!Directory.Exists(sessionDownloadsDir))
|
||||
{
|
||||
_logger.LogWarning("Session downloads directory not found: {Path}", sessionDownloadsDir);
|
||||
return NotFound("Session downloads directory not found");
|
||||
}
|
||||
|
||||
var zipFileName = $"session_{sessionId}.zip";
|
||||
var zipFilePath = Path.Combine(sessionDownloadsDir, zipFileName);
|
||||
|
||||
if (!System.IO.File.Exists(zipFilePath))
|
||||
{
|
||||
_logger.LogWarning("Session ZIP file not found: {ZipFile}", zipFilePath);
|
||||
return NotFound("No ZIP file available for this session");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Serving session ZIP file: {ZipFile} for session: {SessionId}", zipFileName, sessionId);
|
||||
|
||||
var friendlyZipName = $"TuneRipper_Session_{sessionId.Substring(0, 8)}.zip";
|
||||
|
||||
var fileStream = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read);
|
||||
return File(fileStream, "application/zip", friendlyZipName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error downloading session ZIP for session: {SessionId}", sessionId);
|
||||
return StatusCode(500, $"Error downloading session ZIP: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// New endpoint to get session ZIP info
|
||||
[HttpGet("zip/info/{sessionId}")]
|
||||
public IActionResult GetSessionZipInfo(string sessionId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sessionId))
|
||||
{
|
||||
return BadRequest("Session ID is required");
|
||||
}
|
||||
|
||||
if (!_sessionService.IsValidSession(sessionId))
|
||||
{
|
||||
return NotFound("Invalid or expired session");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sessionDownloadsDir = _sessionService.GetSessionDownloadsPath(sessionId);
|
||||
|
||||
if (!Directory.Exists(sessionDownloadsDir))
|
||||
{
|
||||
return NotFound("Session downloads directory not found");
|
||||
}
|
||||
|
||||
var zipFileName = $"session_{sessionId}.zip";
|
||||
var zipFilePath = Path.Combine(sessionDownloadsDir, zipFileName);
|
||||
|
||||
if (!System.IO.File.Exists(zipFilePath))
|
||||
{
|
||||
return NotFound("No ZIP file available for this session");
|
||||
}
|
||||
|
||||
var fileInfo = new FileInfo(zipFilePath);
|
||||
var mp3Count = Directory.GetFiles(sessionDownloadsDir, "*.mp3").Length;
|
||||
|
||||
var info = new
|
||||
{
|
||||
sessionId,
|
||||
zipFileName,
|
||||
fileSize = FormatFileSize(fileInfo.Length),
|
||||
fileSizeBytes = fileInfo.Length,
|
||||
mp3FilesCount = mp3Count,
|
||||
createdAt = fileInfo.CreationTime,
|
||||
lastModified = fileInfo.LastWriteTime,
|
||||
downloadUrl = $"/api/download/zip/{sessionId}"
|
||||
};
|
||||
|
||||
return Ok(info);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting session ZIP info for session: {SessionId}", sessionId);
|
||||
return StatusCode(500, $"Error getting session ZIP info: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// New session-aware download endpoint
|
||||
[HttpGet("{sessionId}/{jobId}")]
|
||||
public IActionResult DownloadFileBySession(string sessionId, string jobId)
|
||||
|
||||
Reference in New Issue
Block a user