tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
FileDownloader.cs
Go to the documentation of this file.
1using System;
2using System.Net.Http;
3using System.Net.Http.Headers;
4
5using Microsoft.Extensions.Logging;
6
9
11{
13 public sealed class FileDownloader : IFileDownloader
14 {
19
23 readonly ILogger<FileDownloader> logger;
24
31 {
32 this.httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
33 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
34 }
35
37 public IFileStreamProvider DownloadFile(Uri url, string? bearerToken)
38 {
39 ArgumentNullException.ThrowIfNull(url);
40
41 logger.LogDebug("Starting download of {url}...", url);
42 var httpClient = httpClientFactory.CreateClient();
43 try
44 {
45 var request = new HttpRequestMessage(
46 HttpMethod.Get,
47 url);
48 try
49 {
50 if (bearerToken != null)
51 request.Headers.Authorization = new AuthenticationHeaderValue(ApiHeaders.BearerAuthenticationScheme, bearerToken);
52
53 return new RequestFileStreamProvider(httpClient, request);
54 }
55 catch
56 {
57 request.Dispose();
58 throw;
59 }
60 }
61 catch
62 {
63 httpClient.Dispose();
64 throw;
65 }
66 }
67 }
68}
Represents the header that must be present for every server request.
Definition ApiHeaders.cs:25
const string BearerAuthenticationScheme
The JWT authentication header scheme.
Definition ApiHeaders.cs:44
IFileStreamProvider DownloadFile(Uri url, string? bearerToken)
Downloads a file from a given url .A new IFileStreamProvider for the downloaded file.
FileDownloader(IAbstractHttpClientFactory httpClientFactory, ILogger< FileDownloader > logger)
Initializes a new instance of the FileDownloader class.
readonly IAbstractHttpClientFactory httpClientFactory
The IAbstractHttpClientFactory for the FileDownloader.
readonly ILogger< FileDownloader > logger
The ILogger for the FileDownloader.
A IFileStreamProvider that represents the response of HttpRequestMessages.
IHttpClient CreateClient()
Create a IHttpClient.
Interface for asynchronously consuming Streams of files.