Esempio n. 1
0
    public static async Task <(bool success, string?path)> DownloadFile(string uri)
    {
        var file = Path.Combine(cache, FileNameFromUrl.ConvertToFileName(uri));

        if (File.Exists(file))
        {
            var fileTimestamp = Timestamp.GetTimestamp(file);
            if (fileTimestamp.Expiry > DateTime.UtcNow)
            {
                return(true, file);
            }
        }

        Timestamp webTimeStamp;

        using (var request = new HttpRequestMessage(HttpMethod.Head, uri))
        {
            using var headResponse = await httpClient.SendAsync(request);

            if (headResponse.StatusCode != HttpStatusCode.OK)
            {
                return(false, null);
            }

            webTimeStamp = Timestamp.GetTimestamp(headResponse);

            if (File.Exists(file))
            {
                var fileTimestamp = Timestamp.GetTimestamp(file);
                if (fileTimestamp.LastModified == webTimeStamp.LastModified)
                {
                    return(true, file);
                }

                File.Delete(file);
            }
        }

        using var response = await httpClient.GetAsync(uri);

        using var httpStream = await response.Content.ReadAsStreamAsync();

        using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            await httpStream.CopyToAsync(fileStream);
        }

        webTimeStamp = Timestamp.GetTimestamp(response);

        Timestamp.SetTimestamp(file, webTimeStamp);
        return(true, file);
    }
Esempio n. 2
0
    public static async Task <string> DownloadFile(string requestUri)
    {
        var path = Path.Combine(cache, FileNameFromUrl.ConvertToFileName(requestUri));

        if (File.Exists(path))
        {
            var fileTimestamp = Timestamp.GetTimestamp(path);
            if (fileTimestamp.Expiry > DateTime.UtcNow)
            {
                return(File.ReadAllText(path));
            }
        }

        var requestMessage = new HttpRequestMessage(HttpMethod.Head, requestUri);

        Timestamp webTimeStamp;

        using (var headResponse = await httpClient.SendAsync(requestMessage))
        {
            webTimeStamp = Timestamp.GetTimestamp(headResponse);

            if (File.Exists(path))
            {
                var fileTimestamp = Timestamp.GetTimestamp(path);
                if (fileTimestamp.LastModified == webTimeStamp.LastModified)
                {
                    return(File.ReadAllText(path));
                }

                File.Delete(path);
            }
        }

        using (var response = await httpClient.GetAsync(requestUri))
            using (var httpStream = await response.Content.ReadAsStreamAsync())
            {
                using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    await httpStream.CopyToAsync(fileStream);
                }

                webTimeStamp = Timestamp.GetTimestamp(response);

                Timestamp.SetTimestamp(path, webTimeStamp);
            }

        return(File.ReadAllText(path));
    }