added StorageVideo VideoRes

This commit is contained in:
Michael Nolan 2021-10-24 07:52:24 -05:00
parent cd418bfd01
commit 114f40836f
6 changed files with 104 additions and 10 deletions

View File

@ -1,14 +1,14 @@
<Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True"> <Properties StartupConfiguration="{E26F8159-6B4B-4660-A7A4-D0333DFEF0DD}|Default" NuGet.AddPackagesDialog.IncludePrerelease="True">
<MonoDevelop.Ide.Workbench ActiveDocument="../../../usr/lib/mono/msbuild/Current/bin/Microsoft.Common.CurrentVersion.targets"> <MonoDevelop.Ide.Workbench ActiveDocument="Program.cs">
<Files> <Files>
<File FileName="Program.cs" Line="312" Column="24" /> <File FileName="Program.cs" Line="42" Column="11" />
<File FileName="Server/Models/InfoType.cs" Line="45" Column="39" /> <File FileName="Server/Models/InfoType.cs" Line="45" Column="39" />
<File FileName="Server/Models/YoutubeDownloaderResponse.cs" Line="1" Column="1" /> <File FileName="Server/Models/YoutubeDownloaderResponse.cs" Line="1" Column="1" />
<File FileName="Server/Functions/Downloader.cs" Line="487" Column="41" /> <File FileName="Server/Functions/Downloader.cs" Line="675" Column="30" />
<File FileName="Server/Functions/SavedMedia.cs" Line="1" Column="1" /> <File FileName="Server/Functions/SavedMedia.cs" Line="1" Column="1" />
<File FileName="Server/Models/SavedVideo.cs" Line="35" Column="10" /> <File FileName="Server/Models/SavedVideo.cs" Line="35" Column="10" />
<File FileName="Server/Models/InfomationQueueItem.cs" Line="25" Column="27" /> <File FileName="Server/Models/InfomationQueueItem.cs" Line="25" Column="27" />
<File FileName="Server/Functions/ffmpeg.cs" Line="19" Column="15" /> <File FileName="Server/Functions/ffmpeg.cs" Line="30" Column="86" />
<File FileName="../Projects/tytdtool/tytdtool/Program.cs" Line="1" Column="1" /> <File FileName="../Projects/tytdtool/tytdtool/Program.cs" Line="1" Column="1" />
<File FileName="../../../usr/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets" Line="2101" Column="5" /> <File FileName="../../../usr/lib/mono/msbuild/15.0/bin/Microsoft.Common.CurrentVersion.targets" Line="2101" Column="5" />
</Files> </Files>
@ -18,11 +18,9 @@
<Node name="youtube-downloader" expanded="True"> <Node name="youtube-downloader" expanded="True">
<Node name="youtube-downloader" expanded="True"> <Node name="youtube-downloader" expanded="True">
<Node name="Server" expanded="True"> <Node name="Server" expanded="True">
<Node name="Functions" expanded="True"> <Node name="Functions" expanded="True" />
<Node name="Downloader.cs" selected="True" />
</Node>
<Node name="Models" expanded="True" />
</Node> </Node>
<Node name="Program.cs" selected="True" />
</Node> </Node>
</Node> </Node>
</State> </State>

View File

@ -38,6 +38,7 @@ namespace youtube_downloader
// we can create unique names for our mutex and our pipe // we can create unique names for our mutex and our pipe
webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite"); webSitePath = Server.Functions.Downloader.DL.GetPath(true, "WebSite");
Route.Before += Route_Before;
/* Generic */ /* Generic */
@ -84,7 +85,10 @@ namespace youtube_downloader
Route.Add("/api/Storage/DirectoryExists/{Path}", (HttpAction)StorageDirectoryExists); Route.Add("/api/Storage/DirectoryExists/{Path}", (HttpAction)StorageDirectoryExists);
Route.Add("/api/Storage/FileExists/{Path}", (HttpAction)StorageFileExists); Route.Add("/api/Storage/FileExists/{Path}", (HttpAction)StorageFileExists);
Route.Add("/api/Storage/File/{Path}", (HttpAction)StorageFile); 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"); Route.Add("/api/upload/", (HttpAction)UploadFiles, "POST");
/* Other */ /* Other */
Route.Add("/", (HttpAction)Index); Route.Add("/", (HttpAction)Index);
@ -141,6 +145,7 @@ namespace youtube_downloader
rp.AsRedirect("/"); rp.AsRedirect("/");
} }
public static void AddVideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args) public static void AddVideoRes(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{ {
Server.Functions.Downloader.DownloadVideo(System.Web.HttpUtility.UrlDecode(args["Id"]), (Resolution)int.Parse(args["R"])); 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); rp.AsFile(rq, path);
} }
} }
public static void Video(HttpListenerRequest rq,HttpListenerResponse rp,Dictionary<string,string> 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<string, string> 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<SavedVideo>(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<string, string> args) public static void UploadFiles(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{ {
var files = rq.ParseBody(args); var files = rq.ParseBody(args);
@ -341,5 +427,11 @@ namespace youtube_downloader
} }
#endregion #endregion
public static bool Route_Before(HttpListenerRequest request, HttpListenerResponse response)
{
response.WithCORS();
return true;
}
} }
} }

View File

@ -10,10 +10,14 @@ namespace youtube_downloader.Server.Functions
private static string get_ffmpeg() 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"; return "ffmpeg";
} }
public static void on_video_done(string id,int res) public static void on_video_done(string id,int res)

Binary file not shown.