mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-02-22 22:46:01 +01:00
27 lines
994 B
C#
27 lines
994 B
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using DHT.Server.Database;
|
|
using DHT.Server.Download;
|
|
using DHT.Utils.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace DHT.Server.Endpoints;
|
|
|
|
sealed class GetDownloadedFileEndpoint(IDatabaseFile db) : BaseEndpoint(db) {
|
|
protected override async Task Respond(HttpRequest request, HttpResponse response, CancellationToken cancellationToken) {
|
|
string url = WebUtility.UrlDecode((string) request.RouteValues["url"]!);
|
|
string normalizedUrl = DiscordCdn.NormalizeUrl(url);
|
|
|
|
if (!await Db.Downloads.GetSuccessfulDownloadWithData(normalizedUrl, WriteDataTo(response), cancellationToken)) {
|
|
response.Redirect(url, permanent: false);
|
|
}
|
|
}
|
|
|
|
private static Func<Data.Download, Stream, CancellationToken, Task> WriteDataTo(HttpResponse response) {
|
|
return (download, stream, cancellationToken) => response.WriteStreamAsync(download.Type, download.Size, stream, cancellationToken);
|
|
}
|
|
}
|