-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to load sources which are not a nginx file listing.
- Loading branch information
Showing
9 changed files
with
232 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Server.ReplayLoading; | ||
|
||
[ReplayProviderName("caddy")] | ||
public class CaddyProvider : ReplayProvider | ||
{ | ||
public override async Task RetrieveFilesRecursive(string directoryUrl, CancellationToken token) | ||
{ | ||
var httpClient = ReplayParser.ReplayParser.CreateHttpClient(); | ||
httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); | ||
|
||
var responseText = await httpClient.GetStringAsync(directoryUrl, token); | ||
var response = JsonSerializer.Deserialize<CaddyResponse[]>(responseText); | ||
if (response == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var caddyResponse in response) | ||
{ | ||
if (caddyResponse.Name.EndsWith(".zip", StringComparison.Ordinal)) | ||
{ | ||
if (caddyResponse.LastModified < ReplayParser.ReplayParser.CutOffDateTime) | ||
{ | ||
continue; | ||
} | ||
|
||
await ReplayParser.ReplayParser.AddReplayToQueue(directoryUrl + caddyResponse.Name); | ||
} | ||
else if (caddyResponse.IsDir) | ||
{ | ||
await RetrieveFilesRecursive(directoryUrl + caddyResponse.Name, token); | ||
} | ||
} | ||
} | ||
|
||
internal class CaddyResponse | ||
{ | ||
[JsonPropertyName("name")] | ||
public string Name { get; set; } | ||
[JsonPropertyName("size")] | ||
public int Size { get; set; } | ||
[JsonPropertyName("url")] | ||
public string Url { get; set; } | ||
[JsonPropertyName("mod_time")] | ||
public DateTime LastModified { get; set; } | ||
[JsonPropertyName("mode")] | ||
public long Mode { get; set; } | ||
[JsonPropertyName("is_dir")] | ||
public bool IsDir { get; set; } | ||
[JsonPropertyName("is_symlink")] | ||
public bool IsSymlink { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Server.ReplayLoading; | ||
|
||
/// <summary> | ||
/// Represents a replay provider that can retrieve replay files from a directory. | ||
/// This will never add any replays to the queue. It is used to temporarily disable some sources. | ||
/// </summary> | ||
[ReplayProviderName("dummy")] | ||
public class DummyProvider : ReplayProvider | ||
{ | ||
public override Task RetrieveFilesRecursive(string directoryUrl, CancellationToken token) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using HtmlAgilityPack; | ||
using Serilog; | ||
|
||
namespace Server.ReplayLoading; | ||
|
||
[ReplayProviderName("nginx")] | ||
public class NginxProvider : ReplayProvider | ||
{ | ||
public override async Task RetrieveFilesRecursive(string directoryUrl, CancellationToken token) | ||
{ | ||
Log.Information("Retrieving files from " + directoryUrl); | ||
var client = ReplayParser.ReplayParser.CreateHttpClient(); | ||
var htmlContent = await client.GetStringAsync(directoryUrl, token); | ||
var document = new HtmlDocument(); | ||
document.LoadHtml(htmlContent); | ||
|
||
var links = document.DocumentNode.SelectNodes("//a[@href]"); | ||
if (links == null) | ||
{ | ||
Log.Information("No links found on " + directoryUrl + "."); | ||
return; | ||
} | ||
|
||
foreach (var link in links) | ||
{ | ||
if (token.IsCancellationRequested) | ||
{ | ||
return; | ||
} | ||
|
||
var href = link.Attributes["href"].Value; | ||
|
||
if (href.StartsWith("..", StringComparison.Ordinal)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (!Uri.TryCreate(href, UriKind.Absolute, out _)) | ||
{ | ||
href = new Uri(new Uri(directoryUrl), href).ToString(); | ||
} | ||
|
||
if (href.EndsWith("/", StringComparison.Ordinal)) | ||
{ | ||
await RetrieveFilesRecursive(href, token); | ||
} | ||
|
||
if (href.EndsWith(".zip", StringComparison.Ordinal)) | ||
{ | ||
await ReplayParser.ReplayParser.AddReplayToQueue(href); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Server.ReplayLoading; | ||
|
||
public abstract class ReplayProvider | ||
{ | ||
public abstract Task RetrieveFilesRecursive(string directoryUrl, CancellationToken token); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public class ReplayProviderNameAttribute : Attribute | ||
{ | ||
public string Name { get; } | ||
|
||
public ReplayProviderNameAttribute(string name) | ||
{ | ||
Name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Reflection; | ||
|
||
namespace Server.ReplayLoading; | ||
|
||
public class ReplayProviderFactory | ||
{ | ||
public static ReplayProvider GetProvider(string providerName) | ||
{ | ||
var type = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.GetCustomAttribute<ReplayProviderNameAttribute>()?.Name == providerName); | ||
if (type == null) | ||
{ | ||
throw new ArgumentException("Invalid provider name."); | ||
} | ||
|
||
if (!typeof(ReplayProvider).IsAssignableFrom(type)) | ||
{ | ||
throw new ArgumentException("Invalid provider type."); | ||
} | ||
|
||
return (ReplayProvider) Activator.CreateInstance(type)!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters