Completely Eliminated Downloader.DL.GetPath, Added Auth Basic Support, and added extension provided Home Pages (TYTD Frontend)

This commit is contained in:
Michael Nolan 2022-03-31 13:27:50 -05:00
parent 516cc487ab
commit 59b3b73d5a
16 changed files with 141 additions and 12 deletions

View File

@ -1,5 +1,5 @@
<Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True">
<MonoDevelop.Ide.Workbench ActiveDocument="TYTD.Api/Server/Models/InfoType.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="Program.cs">
<Files>
<File FileName="TYTD.Api/Server/Models/InfoType.cs" Line="171" Column="40" />
<File FileName="TYTD.Api/Server/Functions/Downloader.cs" Line="1141" Column="18" />
@ -7,7 +7,7 @@
<File FileName="TYTD.Api/Server/Models/SavedVideo.cs" Line="8" Column="1" />
<File FileName="TYTD.Api/Server/Models/InfomationQueueItem.cs" Line="269" Column="74" />
<File FileName="TYTD.Api/Server/Functions/ffmpeg.cs" Line="1" Column="1" />
<File FileName="Program.cs" Line="217" Column="117" />
<File FileName="Program.cs" Line="231" Column="28" />
<File FileName="TYTD.Api/MyClass.cs" Line="287" Column="37" />
<File FileName="TYTD.Api/Server/Models/SavedChannel.cs" Line="6" Column="10" />
<File FileName="TYTD.Api/Server/Models/SavedPlaylist.cs" Line="7" Column="1" />
@ -16,21 +16,14 @@
<File FileName="TYTD.Api/Server/Models/IEnumerator.cs" Line="1" Column="1" />
<File FileName="TYTD.Api/SimpleHttpExtensions.cs" Line="17" Column="19" />
<File FileName="Broadcast.cs" Line="23" Column="38" />
<File FileName="packages.config" Line="1" Column="1" />
<File FileName="../../../../usr/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets" Line="2101" Column="5" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State name="__root__">
<Node name="youtube-downloader" expanded="True">
<Node name="TYTD.Api" expanded="True">
<Node name="Server" expanded="True">
<Node name="Models" expanded="True">
<Node name="InfoType.cs" selected="True" />
</Node>
</Node>
</Node>
<Node name="youtube-downloader" expanded="True">
<Node name="Packages" selected="True" />
<Node name="Properties" expanded="True" />
</Node>
</Node>

View File

@ -26,6 +26,7 @@ using Dasync.Collections;
using YoutubeExplode.Search;
using Hyperlinq;
using System.Net.Sockets;
using System.IO.Compression;
namespace TYTD
{
@ -216,6 +217,67 @@ namespace TYTD
Downloader.RouteAdd("/api/HomePageChanger.html", "Admin", "Change Home Page", (HttpAction)ChangeFrontEnd);
Downloader.RouteAdd("/api/SetHomePage", "Admin", "Used by /api/HomePageChanger.html to actually change home page", (HttpAction)SetFrontEnd, "POST");
Downloader.RouteAdd("/api/RemoveUnwanted", "Admin", "Used by /api/InstallExtensionUpload", (req, resp, args) => {
if(AuthorizedAdmin(req, resp, args))
{
req.ParseBody(args);
foreach(var a in args)
{
try
{
ApiLoader.UninstallExtension(a.Key);
}
catch(Exception ex)
{
_ = ex;
}
}
}
},"POST");
Downloader.RouteAdd("/api/InstallExtensionUpload", "Admin", "Install Extension from Upload",(request, response, arguments) =>
{
if (Authorized(request, response, arguments))
{
Directory.CreateDirectory("extension_temp");
var files = request.ParseBody(arguments, (fieldName, fileName, contentType) =>
{
return File.Create($"extension_temp/{fileName}");
});
List<(ext_conf conf, string name)> ext_names = new List<(ext_conf, string)>();
foreach (var f in files)
{
f.Value.Dispose();
string key = f.Value.FileName;
ext_names.Add(ApiLoader.InstallExtension($"extension_temp/{key}"));
}
Directory.Delete("extension_temp",true);
StringBuilder fields = new StringBuilder();
foreach (var item in ext_names)
{
string name = WebUtility.HtmlEncode(item.name);
string name_text = WebUtility.HtmlEncode(item.conf.name);
string desc = string.Join("<br>", item.conf.desc.Split('\n').Select<string, string>(e => { return WebUtility.HtmlEncode(e); }));
string data = "";
string root = Path.Combine(Environment.CurrentDirectory, "config");
string fallbackIcon = Path.Combine(root, "default_icon.png");
string icon = Path.Combine(root, "apiicons", name + ".png");
string icon2 = File.Exists(icon) ? icon : fallbackIcon;
if (File.Exists(icon2))
{
data = Convert.ToBase64String(File.ReadAllBytes(icon2));
}
fields.Append("<td><input type=\"checkbox\" name=\"{name}\"></td><td><img src=\"data:image/png;base64,{data}\"</td> <td>{name_text}</td><td>{desc}</td>");
}
string html = $"<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Extensions Installed</title></head><body><h1>Do You Want all of these Extensions</h1><h3>If you kill the downloader before Accept, these extensions will be installed</h3> <form action=\"../api/RemoveUnwanted\" method=\"POST\"><table><tr><th>Remove This</th><th>Icon</th> <th>Title</th> <th>Description</th> </tr>{fields.ToString()} </table><input type=\"submit\" value=\"Remove\"></form></body></html>";
response.AsText(html);
}
},"POST");
Route.Add("/api/example_tripple_structure.json", (req, resp, args) =>
{
@ -1849,4 +1911,5 @@ namespace TYTD
}
}
}

View File

@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
using SimpleHttp;
using TYTD.Server.Functions;
using TYTD.Server.Models;
@ -152,6 +154,58 @@ namespace TYTD
}
public static class ApiLoader
{
public static void CopyDir(string src, string dest,bool canoverride=false)
{
Directory.CreateDirectory(dest);
//Console.WriteLine($"Created directory {dest}"); ;
foreach (var dir in Directory.EnumerateDirectories(src))
{
CopyDir(dir, Path.Combine(dest, Path.GetFileName(dir)));
}
foreach (var file in Directory.EnumerateFiles(src))
{
if(File.Exists(Path.Combine(dest, Path.GetFileName(file))))
{
if(canoverride)
{
File.Delete(Path.Combine(dest, Path.GetFileName(file)));
}
else { continue; }
}
File.Copy(file, Path.Combine(dest, Path.GetFileName(file)));
//Console.WriteLine($"Copied {file} -> {Path.Combine(dest, Path.GetFileName(file))}");
}
//Console.WriteLine($"Copied directory {src} -> {dest}");
}
public static void UninstallExtension(string name)
{
Directory.Delete($"config/apidll/{name}",true);
Directory.Delete($"config/apistore/{name}", true);
File.Delete($"config/apiicons/{name}.png");
}
public static (ext_conf conf,string name) InstallExtension(string archive)
{
if(Directory.Exists($"{archive}_contents"))
{
Directory.Delete($"{archive}_contents", true);
}
ZipFile.ExtractToDirectory(archive, $"{archive}_contents");
//we need to read manifest json
string manifest = $"{archive}_contents/manifest.json";
ext_conf conf = JsonConvert.DeserializeObject<ext_conf>(File.ReadAllText(manifest));
string extName = Path.GetFileName(conf.binPath);
Directory.CreateDirectory($"config/apidll/{extName}");
Directory.CreateDirectory($"config/apistore/{extName}");
Directory.CreateDirectory("config/apiicons");
File.Copy($"{archive}_contents/{conf.icon}", $"config/apiicons/{extName}.png");
string parDirOf = Path.GetDirectoryName($"{archive}_contents/{conf.binPath}");
CopyDir(parDirOf, $"config/apidll/{extName}", true);
CopyDir($"{archive}_contents/files", $"config/apistore/{extName}", true);
Directory.Delete($"{archive}_contents");
return (conf,extName);
}
static bool start_term=false;
public static bool StartTermination { get { return start_term; } set { start_term = value; if (start_term) { _cancel.Cancel(); } } }
public static bool Restart { get; set; }
@ -311,4 +365,13 @@ namespace TYTD
return _cancel.Token;
}
}
public class ext_conf
{
public string binPath { get; set; }
public string name { get; set; }
public string icon { get; set; }
public string desc { get; set; }
public bool hasHomePageOverride { get; set; }
}
}

View File

@ -108,6 +108,10 @@
<Reference Include="Hyperlinq">
<HintPath>..\packages\Hyperlinq.1.0.7\lib\net40-client\Hyperlinq.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression.ZipFile">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
<Compile Include="MyClass.cs" />

Binary file not shown.

View File

@ -1 +1 @@
2b6dff7469b08f0770687859f1e70f9debc18804
26643813a9972b2be5fd1ee18aabc6455f9a285b

Binary file not shown.

View File

@ -10,6 +10,7 @@
<package id="Simple-HTTP" version="1.0.6" targetFramework="net47" />
<package id="System.Buffers" version="4.5.1" targetFramework="net47" />
<package id="System.IO" version="4.3.0" targetFramework="net47" />
<package id="System.IO.Compression.ZipFile" version="4.3.0" targetFramework="net47" />
<package id="System.Memory" version="4.5.4" targetFramework="net47" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net47" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net47" />

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
7dbe06743fabd9c4c1020515efeec8905e352f88
e3eba772fa8089a24bf256884284d266adbd5661

View File

@ -13,6 +13,7 @@
<package id="Simple-HTTP" version="1.0.6" targetFramework="net47" />
<package id="System.Buffers" version="4.5.1" targetFramework="net47" />
<package id="System.IO" version="4.3.0" targetFramework="net47" />
<package id="System.IO.Compression.ZipFile" version="4.3.0" targetFramework="net47" />
<package id="System.Memory" version="4.5.4" targetFramework="net47" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net47" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net47" />

View File

@ -118,6 +118,10 @@
<Reference Include="Hyperlinq">
<HintPath>packages\Hyperlinq.1.0.7\lib\net40-client\Hyperlinq.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression.ZipFile">
<HintPath>packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />