namespace detect.device.Utils;

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

public static class FileUtil
{
    /// <summary>
    /// 下载文件到指定路径
    /// </summary>
    /// <param name="url">文件的 URL</param>
    /// <param name="destinationPath">下载文件的保存路径</param>
    /// <returns>表示异步操作的 Task</returns>
    public static async Task DownloadFileAsync(string url, string destinationPath)
    {
        using HttpClient client = new HttpClient();
        using HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

        response.EnsureSuccessStatusCode();

        await using FileStream fs = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None);
        await response.Content.CopyToAsync(fs);
    }
}
