namespace Lentia.Core.Utils;

public static class FileHelper {
    public static async Task DownloadFileAsync(string url, string destinationPath) {
        string? directory = Path.GetDirectoryName(destinationPath);
        if (!string.IsNullOrEmpty(directory)) {
            Directory.CreateDirectory(directory);
        }

        if (File.Exists(destinationPath)) {
            File.Delete(destinationPath);
        }

        using var response = await HttpHelper.FetchAsync(url); 
        response.EnsureSuccessStatusCode();

        using var streamToReadFrom = await response.Content.ReadAsStreamAsync();
        using var streamToWriteTo = File.Create(destinationPath);
        
        await streamToReadFrom.CopyToAsync(streamToWriteTo);
    }
}