tytd-server/Program.cs

125 lines
4.7 KiB
C#
Raw Normal View History

2021-06-25 08:30:45 +00:00
using System.Linq;
using SimpleHttp;
2021-06-24 01:10:20 +00:00
using System;
2021-06-24 02:55:41 +00:00
using System.Collections.Generic;
2021-06-24 01:10:20 +00:00
using System.IO;
using System.IO.Pipes;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using TessesYoutubeDownloader.Server.Models;
2021-06-25 08:30:45 +00:00
using Newtonsoft.Json;
2021-06-24 01:10:20 +00:00
namespace youtube_downloader
{
class Program
{
2021-06-25 08:30:45 +00:00
static void Main()
2021-06-24 01:10:20 +00:00
{
2021-06-24 03:41:34 +00:00
Thread t = new Thread(new ThreadStart(() => {
Server.Functions.Downloader.DL.DownloadThread().GetAwaiter().GetResult();
}));
t.Start();
2021-06-25 08:30:45 +00:00
Thread t2 = new Thread(new ThreadStart(() => {
Server.Functions.Downloader.DL.ListenForQueueItem().GetAwaiter().GetResult();
}));
t2.Start();
2021-06-24 01:10:20 +00:00
// we need to get our app name so that
// we can create unique names for our mutex and our pipe
2021-06-25 08:30:45 +00:00
string webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite");
Route.Add("/", (rq, rp,args) =>
2021-06-24 07:31:55 +00:00
{
2021-06-25 08:30:45 +00:00
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
});
Route.Add("/api/Storage/GetDirectories/{Path}", (rq, rp, args) =>
2021-06-24 01:10:20 +00:00
{
2021-06-25 08:30:45 +00:00
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
2021-06-24 03:41:34 +00:00
2021-06-25 08:30:45 +00:00
if (Directory.Exists(path))
2021-06-24 03:41:34 +00:00
{
2021-06-25 08:30:45 +00:00
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateDirectories(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
rp.AsText(json, "application/json");
2021-06-24 03:41:34 +00:00
}
2021-06-25 08:30:45 +00:00
else
2021-06-24 03:41:34 +00:00
{
2021-06-25 08:30:45 +00:00
rp.AsText("[]", "application/json");
2021-06-24 03:41:34 +00:00
}
2021-06-25 08:30:45 +00:00
});
Route.Add("/api/Storage/GetFiles/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
2021-06-24 03:41:34 +00:00
2021-06-25 08:30:45 +00:00
if (Directory.Exists(path))
2021-06-24 01:10:20 +00:00
{
2021-06-25 08:30:45 +00:00
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Directory.EnumerateFiles(path).Select<string, string>((o) => { return Path.GetFileName(o); }));
rp.AsText(json, "application/json");
2021-06-24 01:10:20 +00:00
}
2021-06-25 08:30:45 +00:00
else
{
rp.AsText("[]", "application/json");
}
});
Route.Add("/api/Storage/FileExists/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
string json = File.Exists(path) ? "true" : "false";
rp.AsText(json, "text/plain");
});
Route.Add("/api/Storage/DirectoryExists/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
string json = Directory.Exists(path) ? "true" : "false";
rp.AsText(json, "text/plain");
2021-06-24 01:10:20 +00:00
2021-06-25 08:30:45 +00:00
});
Route.Add("/api/Storage/File/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
rp.AsFile(rq, path);
});
Route.Add("/api/Add/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.Download(System.Web.HttpUtility.UrlDecode(args["Id"]));
rp.AsRedirect("/");
});
Route.Add("/api/AddRes/{R}/{Id}", (rq, rp, args) =>
{
Server.Functions.Downloader.Download(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"]));
rp.AsRedirect("/");
});
Route.Add("/api/QueueMove/{From}/{To}", (rq, rp, args) =>
{
Server.Functions.Downloader.ModQueue(args["To"],args["From"]);
rp.AsRedirect("/");
});
Route.Add("/api/QueueList", (rq, rp, args) =>
{
string json = Server.Functions.Downloader.GetQueue();
rp.AsText(json, "application/json");
});
Route.Add("/api/Progress", (rq, rp, args) =>
{
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress());
rp.AsText(json, "application/json");
});
Route.Add("/{Path}", (rq, rp, args) =>
{
string path = Path.Combine(webSitePath, args["Path"]);
2021-06-24 01:10:20 +00:00
2021-06-25 08:30:45 +00:00
rp.AsFile(rq, path);
});
2021-06-25 09:55:14 +00:00
HttpServer.ListenAsync(3250, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
2021-06-24 01:10:20 +00:00
}
2021-06-25 08:30:45 +00:00
2021-06-24 01:10:20 +00:00
2021-06-25 08:30:45 +00:00
2021-06-24 01:10:20 +00:00
}
}