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.
This commit is contained in:
1 parent
143d813949
commit
7ac8a5af62
4 files changed
+188
-124
No files matched your search
@@ -1,19 +1,41 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ForgejoReleaseSync.Services;
|
||||
|
||||
public class ReleaseAssetDownloadService(HttpClient httpClient)
|
||||
public class ReleaseAssetDownloadService(HttpClient httpClient, ILogger<ReleaseAssetDownloadService> logger)
|
||||
{
|
||||
public async Task<string> DownloadReleaseAssetAsync(Uri url)
|
||||
public async Task<string> DownloadReleaseAssetAsync(Uri url, CancellationToken token = default)
|
||||
{
|
||||
var fileName = Path.GetFileName(url.AbsoluteUri);
|
||||
var filePath = Path.Combine(Path.GetTempPath(), fileName);
|
||||
await using Stream resource = await httpClient.GetStreamAsync(url);
|
||||
await using FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
|
||||
await resource.CopyToAsync(fs, 8192);
|
||||
return filePath;
|
||||
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)
|
||||
public async Task DeleteReleaseAssetAsync(string downloadPath, CancellationToken token = default)
|
||||
{
|
||||
File.Delete(downloadPath);
|
||||
try
|
||||
{
|
||||
File.Delete(downloadPath);
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to delete asset {Path}", downloadPath);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user