Files
ForgejoReleaseSync/Services/ReleaseAssetDownloadService.cs
T
mahdium 7ac8a5af62 feat: enhance release synchronization with cancellation support, error handling, and logging
- Added `CancellationToken` support across SyncService, GithubService, ForgejoService, and ReleaseAssetDownloadService.
- Improved error handling with detailed logging for asset downloads, deletions, and API operations.
- Refactored SyncService for clearer repository and asset synchronization logic.
- Enhanced performance with async file streams and refined conditional checks.
2026-11-04 16:37:41 +03:30

41 lines
1.3 KiB
C#

using Microsoft.Extensions.Logging;
namespace ForgejoReleaseSync.Services;
public class ReleaseAssetDownloadService(HttpClient httpClient, ILogger<ReleaseAssetDownloadService> logger)
{
public async Task<string> DownloadReleaseAssetAsync(Uri url, CancellationToken token = default)
{
try
{
var fileName = Path.GetFileName(url.AbsoluteUri);
var filePath = Path.Combine(Path.GetTempPath(), fileName);
await using var resource = await httpClient.GetStreamAsync(url, token);
await using var fs = new FileStream(
filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, useAsync: true);
await resource.CopyToAsync(fs, token);
return filePath;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to download asset from {Url}", url);
throw;
}
}
public async Task DeleteReleaseAssetAsync(string downloadPath, CancellationToken token = default)
{
try
{
File.Delete(downloadPath);
await Task.CompletedTask;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete asset {Path}", downloadPath);
throw;
}
}
}