diff --git a/.vs/youtube-downloader/xs/UserPrefs.xml b/.vs/youtube-downloader/xs/UserPrefs.xml index e9f79ce..cb30664 100644 --- a/.vs/youtube-downloader/xs/UserPrefs.xml +++ b/.vs/youtube-downloader/xs/UserPrefs.xml @@ -1,14 +1,14 @@  - + - + - + - + @@ -18,11 +18,9 @@ - - - - + + diff --git a/Program.cs b/Program.cs index 46a9acd..13bcb33 100644 --- a/Program.cs +++ b/Program.cs @@ -38,6 +38,7 @@ namespace youtube_downloader // we can create unique names for our mutex and our pipe webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite"); + Route.Before += Route_Before; /* Generic */ @@ -84,7 +85,10 @@ namespace youtube_downloader Route.Add("/api/Storage/DirectoryExists/{Path}", (HttpAction)StorageDirectoryExists); Route.Add("/api/Storage/FileExists/{Path}", (HttpAction)StorageFileExists); Route.Add("/api/Storage/File/{Path}", (HttpAction)StorageFile); + Route.Add("/api/Storage/Video/{Id}",(HttpAction)Video); + Route.Add("/api/Storage/VideoRes/{Res}/{Id}",(HttpAction)VideoRes); Route.Add("/api/upload/", (HttpAction)UploadFiles, "POST"); + /* Other */ Route.Add("/", (HttpAction)Index); @@ -141,6 +145,7 @@ namespace youtube_downloader rp.AsRedirect("/"); } + public static void AddVideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary args) { Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"])); @@ -320,6 +325,87 @@ namespace youtube_downloader rp.AsFile(rq, path); } } + public static void Video(HttpListenerRequest rq,HttpListenerResponse rp,Dictionary args) + { + YoutubeExplode.Videos.VideoId? vid = YoutubeExplode.Videos.VideoId.TryParse(args["Id"]); + if (vid.HasValue) + { + string path = Server.Functions.Downloader.DL.GetPath(true, "NotConverted",vid.Value +".mp4"); + rp.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString()); + rp.AsFile(rq, path); + } + else + { + rp.WithCode(HttpStatusCode.BadRequest); + rp.AsText("Invalid Video ID or URL", "text/plain"); + } + } + public static void VideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary args) + { + YoutubeExplode.Videos.VideoId? vid = YoutubeExplode.Videos.VideoId.TryParse(args["Id"]); + if (vid.HasValue) + { + int res; + if(int.TryParse(args["Res"],out res)) + { + if (res > 2 || res < 0) + { + rp.WithCode(HttpStatusCode.BadRequest); + rp.AsText($"Invalid Resolution Number must be either 0, 1 or 2", "text/plain"); + } + else + { + string[] m = new string[] { "Converted", "NotConverted", "Audio" }; + string path = Server.Functions.Downloader.DL.GetPath(true, m[res], vid.Value + ".mp4"); + rp.AddHeader("Content-Disposition", GetVideoContentDisposition(vid.Value).ToString()); + rp.AsFile(rq, path); + } + } + + else + { + rp.WithCode(HttpStatusCode.BadRequest); + rp.AsText("Res is not a number", "text/plain"); + } + + } + else + { + rp.WithCode(HttpStatusCode.BadRequest); + rp.AsText("Invalid Video ID or URL", "text/plain"); + } + } + public static System.Net.Mime.ContentDisposition GetVideoContentDisposition(string id) + { + var cd = new System.Net.Mime.ContentDisposition(); + string filename = GetVideoName(id); + cd.FileName = filename; + + return cd; + } + public static string GetVideoName(string id) + { + string name = id + ".mp4"; + string path = Server.Functions.Downloader.DL.GetPath(true, "Info", id + ".json"); + if (File.Exists(path)) + { + string info=File.ReadAllText(path); + name= JsonConvert.DeserializeObject(info).Title + ".mp4"; + } + + string asAscii = Encoding.ASCII.GetString( + Encoding.Convert( + Encoding.UTF8, + Encoding.GetEncoding( + Encoding.ASCII.EncodingName, + new EncoderReplacementFallback(string.Empty), + new DecoderExceptionFallback() + ), + Encoding.UTF8.GetBytes(name) + ) + ); + return asAscii; + } public static void UploadFiles(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary args) { var files = rq.ParseBody(args); @@ -341,5 +427,11 @@ namespace youtube_downloader } #endregion + public static bool Route_Before(HttpListenerRequest request, HttpListenerResponse response) + { + response.WithCORS(); + return true; + } + } } diff --git a/Server/Functions/ffmpeg.cs b/Server/Functions/ffmpeg.cs index 237543e..35aa49c 100644 --- a/Server/Functions/ffmpeg.cs +++ b/Server/Functions/ffmpeg.cs @@ -10,10 +10,14 @@ namespace youtube_downloader.Server.Functions private static string get_ffmpeg() { - if (File.Exists("ffmpeg.txt")) + Directory.CreateDirectory("config"); + + string ffmpgloc = Path.Combine(Environment.CurrentDirectory, "config", "ffmpeg.txt"); + if (File.Exists(ffmpgloc)) { - return File.ReadAllText("ffmpeg.txt"); + return File.ReadAllText(ffmpgloc); } + return "ffmpeg"; } public static void on_video_done(string id,int res) diff --git a/bin/Release/youtube-downloader.exe b/bin/Release/youtube-downloader.exe index b7233ec..5b460b6 100644 Binary files a/bin/Release/youtube-downloader.exe and b/bin/Release/youtube-downloader.exe differ diff --git a/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache b/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache index 1bd9c12..f49390d 100644 Binary files a/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache and b/obj/x86/Release/youtube-downloader.csprojAssemblyReference.cache differ diff --git a/obj/x86/Release/youtube-downloader.exe b/obj/x86/Release/youtube-downloader.exe index b7233ec..5b460b6 100644 Binary files a/obj/x86/Release/youtube-downloader.exe and b/obj/x86/Release/youtube-downloader.exe differ