2021-12-08 01:07:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2022-03-31 10:00:04 +00:00
|
|
|
|
using System.Net;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
using System.Reflection;
|
2021-12-08 07:09:16 +00:00
|
|
|
|
using System.Text;
|
2022-03-31 10:00:04 +00:00
|
|
|
|
using System.Threading;
|
2021-12-08 07:09:16 +00:00
|
|
|
|
using SimpleHttp;
|
2022-03-31 10:00:04 +00:00
|
|
|
|
using TYTD.Server.Functions;
|
|
|
|
|
using TYTD.Server.Models;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
namespace TYTD
|
|
|
|
|
{
|
|
|
|
|
public abstract class Api : IDisposable
|
|
|
|
|
{
|
2022-03-31 10:00:04 +00:00
|
|
|
|
public virtual bool OnHomePage(string path,HttpListenerRequest req,HttpListenerResponse resp,Dictionary<string,string> args)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool OnTemplatePage(string path,out string template_string)
|
|
|
|
|
{
|
|
|
|
|
template_string = "";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public bool CanProvideHomePage { get; protected set; }
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public virtual void OnStart()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Extension Loaded");
|
|
|
|
|
}
|
|
|
|
|
internal string storage;
|
|
|
|
|
protected string StorageLocation { get { return storage; } }
|
|
|
|
|
|
2021-12-08 07:09:16 +00:00
|
|
|
|
public virtual string Name { get { return Path.GetFileName(StorageLocation); } }
|
2021-12-08 01:07:43 +00:00
|
|
|
|
private System.Timers.Timer t = null;
|
|
|
|
|
protected void SetTimer(TimeSpan timespan)
|
|
|
|
|
{
|
|
|
|
|
if(t == null)
|
|
|
|
|
{
|
|
|
|
|
t = new System.Timers.Timer();
|
2021-12-08 04:34:43 +00:00
|
|
|
|
t.Elapsed += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
TimerElapsed();
|
|
|
|
|
};
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
t.Interval = timespan.TotalMilliseconds;
|
2021-12-08 04:34:43 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
t.Start();
|
2021-12-09 04:31:45 +00:00
|
|
|
|
}
|
|
|
|
|
public virtual void SetProgress(double progress)
|
|
|
|
|
{
|
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
2021-12-12 10:04:41 +00:00
|
|
|
|
public virtual void OnEnd()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public bool TimerEnabled { get { if (t != null) { return t.Enabled; } else { return false; } } set { if (t != null) { t.Enabled = value; } } }
|
2021-12-08 07:09:16 +00:00
|
|
|
|
public virtual IEnumerable<KeyValuePair<string, string>> GetUrls()
|
|
|
|
|
{
|
|
|
|
|
yield return new KeyValuePair<string, string>("Home",$"api/Extensions/{Name}");
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
protected virtual void TimerElapsed()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Event Fired");
|
|
|
|
|
}
|
|
|
|
|
public Api AddItem(string id)
|
|
|
|
|
{
|
|
|
|
|
Downloader.DownloadItem(id);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
internal void SendDLStart(object sender,DownloadStartEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(DownloadStart != null)
|
|
|
|
|
{
|
|
|
|
|
DownloadStart.Invoke(sender, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 22:02:30 +00:00
|
|
|
|
internal void SendDLInfoStart(object sender, DownloadInfoStartEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (DownloadInfoStart != null)
|
|
|
|
|
{
|
|
|
|
|
DownloadInfoStart.Invoke(sender, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
internal void SendDLComplete(object sender, DownloadCompleteEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (DownloadComplete != null)
|
|
|
|
|
{
|
|
|
|
|
DownloadComplete.Invoke(sender, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 22:02:30 +00:00
|
|
|
|
public event EventHandler<DownloadInfoStartEventArgs> DownloadInfoStart;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public event EventHandler<DownloadStartEventArgs> DownloadStart;
|
|
|
|
|
public event EventHandler<DownloadCompleteEventArgs> DownloadComplete;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if( t != null)
|
|
|
|
|
{
|
|
|
|
|
t.Dispose();
|
|
|
|
|
}
|
2021-12-12 10:04:41 +00:00
|
|
|
|
OnEnd();
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DownloadCompleteEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public bool RegularFile { get; set; }
|
|
|
|
|
public SavedVideo Video { get; set; }
|
|
|
|
|
|
2021-12-08 22:02:30 +00:00
|
|
|
|
}
|
2021-12-09 04:31:45 +00:00
|
|
|
|
|
2021-12-08 22:02:30 +00:00
|
|
|
|
public class DownloadInfoStartEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public SavedVideo Video { get; set; }
|
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
public class DownloadStartEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public bool RegularFile { get; set; }
|
|
|
|
|
public SavedVideo Video { get; set; }
|
|
|
|
|
public bool Cancel { get; set; }
|
|
|
|
|
}
|
2021-12-08 07:09:16 +00:00
|
|
|
|
|
2022-03-31 10:00:04 +00:00
|
|
|
|
public class ApiHomePageInfo
|
|
|
|
|
{
|
|
|
|
|
public event EventHandler Changed;
|
|
|
|
|
|
|
|
|
|
public ApiHomePageInfo()
|
|
|
|
|
{
|
|
|
|
|
HasHomePage = false;
|
|
|
|
|
HomePage = null;
|
|
|
|
|
}
|
|
|
|
|
public ApiHomePageInfo(Api api)
|
|
|
|
|
{
|
|
|
|
|
HasHomePage = api != null;
|
|
|
|
|
HomePage = api;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Change(Api api)
|
|
|
|
|
{
|
|
|
|
|
HasHomePage = api != null;
|
|
|
|
|
HomePage = api;
|
|
|
|
|
if(Changed != null)
|
|
|
|
|
{
|
|
|
|
|
Changed.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool HasHomePage { get; private set; }
|
|
|
|
|
public Api HomePage { get; private set; }
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static class ApiLoader
|
|
|
|
|
{
|
2022-03-31 10:00:04 +00:00
|
|
|
|
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; }
|
|
|
|
|
public static void StopApp()
|
|
|
|
|
{
|
|
|
|
|
Restart = false;
|
|
|
|
|
StartTermination = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public static void RestartApp()
|
|
|
|
|
{
|
|
|
|
|
Restart = true;
|
|
|
|
|
StartTermination = true;
|
|
|
|
|
|
|
|
|
|
}
|
2022-02-24 20:36:15 +00:00
|
|
|
|
public static string RenderFileOrDefault(string file,string defaultData,Dictionary<string,string> arg)
|
|
|
|
|
{
|
|
|
|
|
return Templating.RenderString(ReadAllTextOrDefault(file, defaultData), arg);
|
|
|
|
|
}
|
|
|
|
|
public static string ReadAllTextOrDefault(string file,string defaultData)
|
|
|
|
|
{
|
2022-03-31 10:00:04 +00:00
|
|
|
|
if(_info.HasHomePage)
|
|
|
|
|
{
|
|
|
|
|
//starts with website so remove WebSite\
|
|
|
|
|
string path = file.Substring(7);
|
|
|
|
|
string data;
|
|
|
|
|
if(_info.HomePage.OnTemplatePage(path,out data))
|
|
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-24 20:36:15 +00:00
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
{
|
|
|
|
|
return File.ReadAllText(file);
|
|
|
|
|
}
|
|
|
|
|
return defaultData;
|
|
|
|
|
}
|
|
|
|
|
public static string Page { get; private set; }
|
2021-12-08 07:09:16 +00:00
|
|
|
|
internal static List<Api> apis = new List<Api>();
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
|
|
|
|
internal static void DownloadStarted(object sender,DownloadStartEventArgs evt)
|
|
|
|
|
{
|
|
|
|
|
foreach(var api in apis)
|
|
|
|
|
{
|
|
|
|
|
api.SendDLStart(sender, evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
internal static void DownloadComplete(object sender, DownloadCompleteEventArgs evt)
|
|
|
|
|
{
|
|
|
|
|
foreach (var api in apis)
|
|
|
|
|
{
|
|
|
|
|
api.SendDLComplete(sender, evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-09 04:31:45 +00:00
|
|
|
|
internal static void SetProgress(double progress)
|
|
|
|
|
{
|
|
|
|
|
foreach (var api in apis)
|
|
|
|
|
{
|
|
|
|
|
api.SetProgress(progress);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 22:02:30 +00:00
|
|
|
|
internal static void DownloadInfoStart(object sender, DownloadInfoStartEventArgs evt)
|
|
|
|
|
{
|
|
|
|
|
foreach (var api in apis)
|
|
|
|
|
{
|
|
|
|
|
api.SendDLInfoStart(sender, evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach(var api in apis)
|
|
|
|
|
{
|
|
|
|
|
api.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 07:09:16 +00:00
|
|
|
|
public static void GenerateTr(Api api,StringBuilder b)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string data = "";
|
|
|
|
|
string root = Path.Combine(Environment.CurrentDirectory, "config");
|
|
|
|
|
string fallbackIcon = Path.Combine(root, "default_icon.png");
|
|
|
|
|
string icon = Path.Combine(root, "apiicons", api.Name + ".png");
|
|
|
|
|
string icon2 = File.Exists(icon) ? icon : fallbackIcon;
|
|
|
|
|
if (File.Exists(icon2))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
data = Convert.ToBase64String(File.ReadAllBytes(icon2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b.Append($"<tr><td><h3>{api.Name}</h3><img src=\"data:image/png;base64,{data}\" alt=\"{api.Name}\"></td><td>");
|
|
|
|
|
foreach (var link in api.GetUrls())
|
|
|
|
|
{
|
|
|
|
|
b.Append($"<a href=\"{link.Value}\">{link.Key}</a><br>");
|
|
|
|
|
}
|
|
|
|
|
b.Append("</td></tr>");
|
2021-12-09 05:37:39 +00:00
|
|
|
|
|
2021-12-08 07:09:16 +00:00
|
|
|
|
}
|
2021-12-09 05:37:39 +00:00
|
|
|
|
internal static void Load(StringBuilder b,string dll,string confpath)
|
2021-12-08 01:07:43 +00:00
|
|
|
|
{
|
2021-12-08 04:39:03 +00:00
|
|
|
|
var asm=Assembly.LoadFrom(dll);
|
2021-12-09 05:37:39 +00:00
|
|
|
|
|
2021-12-08 07:09:16 +00:00
|
|
|
|
foreach (var item in asm.GetTypes())
|
2021-12-08 01:07:43 +00:00
|
|
|
|
{
|
|
|
|
|
if(typeof(Api).IsAssignableFrom(item))
|
|
|
|
|
{
|
|
|
|
|
Api api = (Api)Activator.CreateInstance(item);
|
|
|
|
|
api.storage = confpath;
|
|
|
|
|
apis.Add(api);
|
2021-12-08 07:09:16 +00:00
|
|
|
|
api.OnStart();
|
|
|
|
|
GenerateTr(api, b);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 07:09:16 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
2022-03-31 10:00:04 +00:00
|
|
|
|
public static IEnumerable<Api> EnumerateExtensions()
|
|
|
|
|
{
|
|
|
|
|
return apis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CancellationTokenSource _cancel;
|
|
|
|
|
private static ApiHomePageInfo _info;
|
|
|
|
|
public static CancellationToken Init(ApiHomePageInfo info)
|
2021-12-08 01:07:43 +00:00
|
|
|
|
{
|
2022-03-31 10:00:04 +00:00
|
|
|
|
_cancel = new CancellationTokenSource();
|
|
|
|
|
_info = info;
|
2021-12-09 05:37:39 +00:00
|
|
|
|
StringBuilder b = new StringBuilder();
|
2021-12-08 04:34:43 +00:00
|
|
|
|
string root = Path.Combine(Environment.CurrentDirectory,"config", "apidll");
|
|
|
|
|
string appconfroot = Path.Combine(Environment.CurrentDirectory,"config", "apistore");
|
2021-12-08 01:07:43 +00:00
|
|
|
|
foreach(var dir in Directory.GetDirectories(root))
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(dir);
|
|
|
|
|
string dllpath = Path.Combine(root, name, name + ".dll");
|
|
|
|
|
string confpath = Path.Combine(appconfroot, name);
|
|
|
|
|
if(File.Exists(dllpath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(confpath);
|
2021-12-09 05:37:39 +00:00
|
|
|
|
Load(b,dllpath, confpath);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-09 05:37:39 +00:00
|
|
|
|
Dictionary<string, string> templating = new Dictionary<string, string>();
|
|
|
|
|
templating.Add("Items", b.ToString());
|
2022-03-31 10:00:04 +00:00
|
|
|
|
string combined= "WebSite/extensions.html";
|
|
|
|
|
|
|
|
|
|
info.Changed += (sender, e) => {
|
|
|
|
|
//it will change immediately
|
|
|
|
|
string template = "<!DOCTYPE html><head><title>Extensions</title></head><body><h1>Extensions</h1><table><thead><tr><th>Name</th><th>Urls</th></tr></thead><tbody>{Items}</tbody></table></body></html>";
|
|
|
|
|
|
|
|
|
|
template = ReadAllTextOrDefault(combined, template);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Page = Templating.RenderString(template, templating);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
return _cancel.Token;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|