105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
|
/*
|
||
|
TYTD Lite: A YouTube Downloader website that archives videos when people download videos using it.
|
||
|
Copyright (C) 2024 Mike Nolan
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
using System;
|
||
|
using System.Globalization;
|
||
|
using System.IO;
|
||
|
using System.Reflection;
|
||
|
using System.Runtime.InteropServices;
|
||
|
using System.Security.Cryptography;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
using Tesses.WebServer;
|
||
|
|
||
|
namespace TYTDLite
|
||
|
{
|
||
|
//thanks to https://www.meziantou.net/getting-the-date-of-build-of-a-dotnet-assembly-at-runtime.htm
|
||
|
[AttributeUsage(AttributeTargets.Assembly)]
|
||
|
internal class BuildDateAttribute : Attribute
|
||
|
{
|
||
|
public BuildDateAttribute(string value)
|
||
|
{
|
||
|
DateTime = DateTime.ParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None);
|
||
|
}
|
||
|
|
||
|
public DateTime DateTime { get; }
|
||
|
}
|
||
|
public class AssetProvider : Server
|
||
|
{
|
||
|
internal static DateTime GetBuildDate()
|
||
|
{
|
||
|
var attribute = asm.GetCustomAttribute<BuildDateAttribute>();
|
||
|
return attribute != null ? attribute.DateTime : default(DateTime);
|
||
|
}
|
||
|
static Assembly asm=typeof(AssetProvider).Assembly;
|
||
|
public AssetProvider()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
public static Stream OpenRead(string path)
|
||
|
{
|
||
|
return asm.GetManifestResourceStream($"TYTDLite.Assets{path.Replace("/",".")}");
|
||
|
}
|
||
|
public override async Task GetAsync(ServerContext ctx)
|
||
|
{
|
||
|
try{
|
||
|
|
||
|
var build = GetBuildDate();
|
||
|
string nice = $"S_{Convert.ToBase64String(Encoding.UTF8.GetBytes($"{build.ToString()}-{ctx.UrlPath}"))}";
|
||
|
|
||
|
|
||
|
ctx.WithDate(build).WithLastModified(build);
|
||
|
ctx.ResponseHeaders.Add("ETag",nice);
|
||
|
|
||
|
|
||
|
await ctx.SendBytesAsync(await ReadAllBytesAsync(ctx.UrlPath),HeyRed.Mime.MimeTypesMap.GetMimeType(ctx.UrlPath));
|
||
|
}catch(ArgumentNullException ex)
|
||
|
{
|
||
|
_=ex;
|
||
|
await NotFoundServer.ServerNull.GetAsync(ctx);
|
||
|
}
|
||
|
}
|
||
|
public static async Task<byte[]> ReadAllBytesAsync(string path)
|
||
|
{
|
||
|
using(var s = OpenRead(path))
|
||
|
{
|
||
|
var ms = new MemoryStream();
|
||
|
await s.CopyToAsync(ms);
|
||
|
return ms.ToArray();
|
||
|
}
|
||
|
}
|
||
|
public static async Task<string> ReadAllTextAsync(string path)
|
||
|
{
|
||
|
using(var s = OpenRead(path))
|
||
|
using(var ms = new StreamReader(s))
|
||
|
{
|
||
|
return await ms.ReadToEndAsync();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static string ReadAllText(string path)
|
||
|
{
|
||
|
using(var s = OpenRead(path))
|
||
|
using(var ms = new StreamReader(s))
|
||
|
return ms.ReadToEnd();
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|