tgstation-server 6.19.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
8
10{
12 public sealed class FileDownloader : IFileDownloader
13 {
17 readonly IHttpClientFactory httpClientFactory;
18
22 readonly ILogger<FileDownloader> logger;
23
29 public FileDownloader(IHttpClientFactory httpClientFactory, ILogger<FileDownloader> logger)
30 {
31 this.httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
32 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
33 }
34
36 public IFileStreamProvider DownloadFile(Uri url, string? bearerToken)
37 {
38 ArgumentNullException.ThrowIfNull(url);
39
40 logger.LogDebug("Starting download of {url}...", url);
41 var httpClient = httpClientFactory.CreateClient();
42 try
43 {
44 var request = new HttpRequestMessage(
45 HttpMethod.Get,
46 url);
47 try
48 {
49 if (bearerToken != null)
50 request.Headers.Authorization = new AuthenticationHeaderValue(ApiHeaders.BearerAuthenticationScheme, bearerToken);
51
52 return new RequestFileStreamProvider(httpClient, request);
53 }
54 catch
55 {
56 request.Dispose();
57 throw;
58 }
59 }
60 catch
61 {
62 httpClient.Dispose();
63 throw;
64 }
65 }
66 }
67}
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(IHttpClientFactory httpClientFactory, ILogger< FileDownloader > logger)
Initializes a new instance of the FileDownloader class.
readonly IHttpClientFactory httpClientFactory
The IHttpClientFactory for the FileDownloader.
readonly ILogger< FileDownloader > logger
The ILogger for the FileDownloader.
A IFileStreamProvider that represents the response of HttpRequestMessages.
Interface for asynchronously consuming Streams of files.