123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using Microsoft.VisualBasic.FileIO;
- using SearchOption = Microsoft.VisualBasic.FileIO.SearchOption;
- namespace FNZCM.ConHost.Ver0
- {
- internal static class Program0
- {
- private static bool _isRunning;
- private static IReadOnlyDictionary<string, Library> _library;
- private static IReadOnlyDictionary<string, string> _pathMapping;
- private const int SmallFileSize = 1024 * 1024 * 5; //5MB
- private static IReadOnlyDictionary<string, byte[]> _smallFileCaches;
- private static readonly ConcurrentDictionary<string, MediaTag> MediaTags = new();
- private static DateTime _lastRequestAccepted;
- private static void Main0()
- {
- Console.WriteLine("Scanning libraries...");
- var libs = new Dictionary<string, Library>();
- var mappings = new Dictionary<string, string>();
- foreach (var library in ConfigFile.Instance.Libraries)
- {
- var libraryName = library.Key;
- var libraryPathName = libraryName.ToLower();
- var albums = Directory.GetDirectories(library.Value);
- var albumObjects = new List<Album>();
- foreach (var albumPath in albums)
- {
- var albumName = Path.GetFileName(albumPath);
- var albumPathName = albumName.ToLower();
- //media
- var mediaFiles = FileSystem.GetFiles(albumPath, SearchOption.SearchTopLevelOnly, ConfigFile.Instance.MediaFilePattern);
- foreach (var mediaFile in mediaFiles) mappings[$"/media/{library.Key}/{albumName}/{Path.GetFileName(mediaFile)}".ToLower()] = mediaFile;
- var tracks = mediaFiles.Select(Path.GetFileName).ToArray();
- var trackPaths = tracks.Select(p => p.ToLower()).ToArray();
- //bk
- var bkPath = Path.Combine(albumPath, "bk");
- IReadOnlyCollection<string> bks = null;
- if (Directory.Exists(bkPath))
- {
- var bkFiles = FileSystem.GetFiles(bkPath, SearchOption.SearchTopLevelOnly, ConfigFile.Instance.BkFilePattern);
- foreach (var bkFile in bkFiles) mappings[$"/bk/{library.Key}/{albumName}/{Path.GetFileName(bkFile)}".ToLower()] = bkFile;
- bks = bkFiles.Select(s => Path.GetFileName(s).ToLower()).ToArray();
- }
- //cover
- var coverPath = Path.Combine(albumPath, "cover.jpg");
- if (File.Exists(coverPath)) mappings[$"/cover/{library.Key}/{albumName}/{Path.GetFileName(coverPath)}".ToLower()] = coverPath;
- albumObjects.Add(new Album(albumName, albumPathName, tracks, trackPaths, bks));
- }
- libs[library.Key.ToLower()] = new Library(libraryName, libraryPathName, albumObjects);
- }
- _library = libs;
- _pathMapping = mappings;
- Console.WriteLine($" Scanned all {libs.Count} library");
- Console.WriteLine($" Total {libs.Sum(p => p.Value.Albums.Count)} Albums, {libs.Sum(p => p.Value.Albums.Sum(q => q.Tracks.Count))} Tracks");
- Console.Write("Reading small files to cache");
- Console.WriteLine();
- var caches = new Dictionary<string, byte[]>();
- foreach (var map in _pathMapping)
- {
- var fileSize = new FileInfo(map.Value).Length;
- if (fileSize < SmallFileSize)
- {
- Console.Write($" Reading: {fileSize / (1024 * 1024.0):N6} MB {map.Value} ");
- caches[map.Key] = File.ReadAllBytes(map.Value);
- var left = Console.CursorLeft;
- Console.CursorLeft = 0;
- Console.Write("".PadLeft(left));
- Console.CursorLeft = 0;
- }
- }
- _smallFileCaches = caches;
- Console.WriteLine($" Cached {_smallFileCaches.Count} small files, total {_smallFileCaches.Values.Sum(p => p.Length) / (1024.0 * 1024):N6} MB");
- Console.WriteLine("Starting...");
- var tWorker = new Thread(Working);
- _isRunning = true;
- tWorker.Start();
- Console.WriteLine("Press ENTER to Stop.");
- Console.ReadLine();
- Console.WriteLine("Shutting down...");
- _isRunning = false;
- tWorker.Join();
- Console.WriteLine("Stopped.");
- Console.WriteLine();
- Console.Write("Press ENTER to Exit.");
- Console.ReadLine();
- }
- private static void Working()
- {
- var listener = new HttpListener();
- listener.Prefixes.Add(ConfigFile.Instance.ListenPrefix);
- listener.Start();
- var upTime = DateTime.Now;
- Console.WriteLine($"HTTP Server started, listening on {ConfigFile.Instance.ListenPrefix}");
- listener.BeginGetContext(ContextGet, listener);
- _lastRequestAccepted = DateTime.Now;
- while (_isRunning)
- {
- var timeSpan = DateTime.Now - _lastRequestAccepted;
- var up = DateTime.Now - upTime;
- Console.Title =
- "FNZCM"
- + $" UP {up.Days:00}D {up.Hours:00}H {up.Minutes:00}M {up.Seconds:00}S {up.Milliseconds:000}"
- + $" / "
- + $" LA {timeSpan.Days:00}D {timeSpan.Hours:00}H {timeSpan.Minutes:00}M {timeSpan.Seconds:00}S {timeSpan.Milliseconds:000}"
- ;
- Thread.Sleep(1000);
- }
- listener.Close();
- Thread.Sleep(1000);
- }
- private static void ContextGet(IAsyncResult ar)
- {
- var listener = (HttpListener)ar.AsyncState;
- HttpListenerContext context;
- try
- {
- // ReSharper disable once PossibleNullReferenceException
- context = listener.EndGetContext(ar);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return;
- }
- if (_isRunning) listener.BeginGetContext(ContextGet, listener);
- ProcessRequest(context);
- }
- private static void ProcessRequest(HttpListenerContext context)
- {
- _lastRequestAccepted = DateTime.Now;
- var request = context.Request;
- Console.WriteLine($"Request from {request.RemoteEndPoint} {request.HttpMethod} {request.RawUrl}");
- // GET / show all libraries
- // foo=library bar=album
- // GET /list/foo/ show all album and cover with name, provide m3u path
- // GET /list/foo/bar/bk/ list all picture as grid
- // GET /list/foo/bar/tracks/ list all tracks as text list
- // GET /list/foo/bar/playlist.m3u8 auto gen
- // media streaming HTTP Partial RANGE SUPPORT
- // GET /cover/foo/bar/cover.jpg
- // GET /media/foo/bar/01.%20foobar.flac
- // GET /bk/foo/bar/foobar.jpg
- try
- {
- // ReSharper disable once PossibleNullReferenceException
- var requestPath = request.Url.LocalPath.ToLower();
- var pathParts = (IReadOnlyList<string>)requestPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- if (requestPath == "/")
- {
- var sb = new StringBuilder();
- sb.Append($"<title> Libraries - {ConfigFile.Instance.Title} </title>");
- sb.Append("<body bgColor=skyBlue style=font-size:3vh><h1>Libraries</h1>");
- sb.Append("<ul>");
- foreach (var library in ConfigFile.Instance.Libraries.Keys)
- {
- sb.Append("<li>");
- sb.Append($"<a href=/list/{library}/>{library}</a>");
- sb.Append("</li>");
- }
- sb.Append("</ul>");
- context.Response.WriteText(sb.ToString());
- }
- else if (pathParts.Count == 2 && pathParts[0] == "list")
- {
- var libName = pathParts[1];
- if (_library.TryGetValue(libName, out var lib))
- {
- var sb = new StringBuilder();
- sb.Append("<!DOCTYPE html><html lang=\"zh-cn\"><meta charset=\"UTF-8\">");
- sb.Append($"<title> Albums of {lib.Name} - {ConfigFile.Instance.Title} </title>");
- sb.Append(
- "<style>" +
- "a:link{ text-decoration: none; }" +
- "div.item{" +
- " vertical-align:top;" +
- " height:20vh;" +
- " margin-bottom:1vh;" +
- " padding:0.5vh;" +
- " border:solid 1px;" +
- " border-radius:0.5vh;" +
- " font-size:2.5vh;" +
- " overflow:scroll;" +
- "}" +
- "div.item::-webkit-scrollbar{" +
- " display: none;" +
- "}" +
- "img.cover{" +
- " float:left;" +
- " background-size:cover;" +
- " max-width:25vw;" +
- " max-height:20vh" +
- "}" +
- "div.buttons{" +
- "}" +
- "a.button{" +
- " margin-left:4vw" +
- "}" +
- "</style>");
- sb.Append($"<body bgColor=skyBlue><h1>Albums of {lib.Name}</h1>");
- sb.Append("<div><a href=/>Back to home</a></div>");
- //Cover list
- foreach (var a in lib.Albums)
- {
- sb.Append("<div class=item>");
- sb.Append($"<img class=cover src=\"/cover/{lib.PathName}/{a.PathName}/cover.jpg\" />");
- sb.Append("<div class=buttons>");
- sb.Append($"<a class=button href=\"/list/{lib.PathName}/{a.PathName}/tracks/\">[TRACKERS]</a>");
- if (a.Bks != null) sb.Append($"<a class=button href=\"/list/{lib.PathName}/{a.PathName}/bk/\">[BK]</a>");
- sb.Append($"<a class=button href=\"/list/{lib.PathName}/{a.PathName.FuckVlc()}/playlist.m3u8\">[M3U8]</a>");
- sb.Append("</div>");
- sb.Append($"<span>{a.Name}<span>");
- sb.Append("</div>");
- }
- context.Response.ContentType = "text/html";
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.WriteText(sb.ToString());
- }
- else
- {
- context.Response.Redirect("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
- }
- }
- else if (pathParts.Count == 4 && pathParts[0] == "list" && pathParts[3] == "tracks")
- {
- var libName = pathParts[1];
- var albPath = pathParts[2];
- Album alb;
- if (_library.TryGetValue(libName, out var lib) && null != (alb = lib.Albums.FirstOrDefault(p => p.PathName == albPath)))
- {
- var sb = new StringBuilder();
- sb.Append("<!DOCTYPE html><html lang=\"zh-cn\"><meta charset=\"UTF-8\">");
- sb.Append($"<body bgColor=skyBlue style=font-size:2vh><h2>Tracks of</h2><h1>{alb.Name}</h1>");
- sb.Append($"<div><a href=/list/{lib.PathName}/>Back to library</a></div>");
- for (var i = 0; i < alb.TrackPaths.Count; i++)
- {
- sb.Append($"<li><a href=\"/media/{lib.PathName.FuckVlc()}/{alb.PathName.FuckVlc()}/{alb.TrackPaths[i].FuckVlc()}\" >{alb.Tracks[i]}</a></li>");
- }
- context.Response.ContentType = "text/html";
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.WriteText(sb.ToString());
- }
- else
- {
- context.Response.Redirect("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
- }
- }
- else if (pathParts.Count == 4 && pathParts[0] == "list" && pathParts[3] == "bk")
- {
- var libName = pathParts[1];
- var albPath = pathParts[2];
- Album alb;
- if (_library.TryGetValue(libName, out var lib) && null != (alb = lib.Albums.FirstOrDefault(p => p.PathName == albPath)))
- {
- var sb = new StringBuilder();
- sb.Append("<!DOCTYPE html><html lang=\"zh-cn\"><meta charset=\"UTF-8\">");
- sb.Append($"<body bgColor=skyBlue style=font-size:2vh><h2>BK of </h2><h1>{alb.Name}</h1>");
- sb.Append($"<div><a href=/list/{lib.PathName}/>Back to library</a></div>");
- foreach (var albBk in alb.Bks)
- {
- //TODO: auto gen thumbnail
- sb.Append($"<img src='/bk/{lib.PathName}/{alb.PathName}/{albBk}' style=max-width:24vw;max-height:24vw;margin-right:1vw;margin-bottom:1vh; />");
- }
- context.Response.ContentType = "text/html";
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.WriteText(sb.ToString());
- }
- else
- {
- context.Response.Redirect("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
- }
- }
- else if (pathParts.Count == 4 && pathParts[0] == "list" && pathParts[3] == "playlist.m3u8")
- {
- var libName = pathParts[1];
- var albPath = pathParts[2];
- Album alb;
- if (_library.TryGetValue(libName, out var lib) && null != (alb = lib.Albums.FirstOrDefault(p => p.PathName == albPath)))
- {
- // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
- var prefix = $"{request.Url.GetLeftPart(UriPartial.Scheme | UriPartial.Authority)}";
- var sb = new StringBuilder();
- sb.AppendLine("#EXTM3U");
- foreach (var track in alb.TrackPaths)
- {
- var mediaKey = $"/media/{lib.PathName}/{alb.PathName}/{track}";
- var coverKey = $"/cover/{lib.PathName}/{alb.PathName}/cover.jpg";
- if (false == MediaTags.TryGetValue(mediaKey, out var mediaTag))
- {
- using var tagLib = TagLib.File.Create(_pathMapping[mediaKey]);
- mediaTag = MediaTags[mediaKey] = new MediaTag($"{string.Join(";", tagLib.Tag.Performers)} - {tagLib.Tag.Title}", (int)tagLib.Properties.Duration.TotalSeconds);
- }
- sb.AppendLine($"#EXTINF:{mediaTag.Duration} tvg-logo=\"{prefix + coverKey.FuckVlc()}\",{mediaTag.Title}");
- sb.AppendLine(prefix + mediaKey.FuckVlc());
- }
- context.Response.ContentType = "audio/mpegurl";
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.WriteText(sb.ToString());
- }
- else
- {
- context.Response.Redirect("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
- }
- }
- else if (_pathMapping.TryGetValue(requestPath, out var realPath))
- {
- var range = request.Headers.GetValues("Range");
- if (_smallFileCaches.TryGetValue(requestPath, out var data))
- {
- if (range is { Length: > 0 })
- {
- var rngParts = range[0].Split(new[] { "bytes=", "-" }, StringSplitOptions.RemoveEmptyEntries);
- if (rngParts.Length >= 1 && long.TryParse(rngParts[0], out var start))
- {
- context.Response.StatusCode = 206;
- context.Response.Headers.Add("Accept-Ranges", "bytes");
- context.Response.Headers.Add("Content-Range", $"bytes {start}-{data.Length - 1}/{data.Length}");
- context.Response.ContentLength64 = data.Length - start;
- context.Response.ContentType = "video/mp4";
- context.Response.OutputStream.Write(new ReadOnlySpan<byte>(data, (int)start, (int)(data.Length - start)));
- }
- }
- else
- {
- context.Response.ContentType = "video/mp4";
- context.Response.OutputStream.Write(data);
- }
- }
- else
- {
- FileStream fs = null;
- try
- {
- fs = File.OpenRead(realPath);
- if (range is { Length: > 0 })
- {
- var rngParts = range[0].Split(new[] { "bytes=", "-" }, StringSplitOptions.RemoveEmptyEntries);
- if (rngParts.Length >= 1 && long.TryParse(rngParts[0], out var start))
- {
- fs.Position = start;
- context.Response.StatusCode = 206;
- context.Response.Headers.Add("Accept-Ranges", "bytes");
- context.Response.Headers.Add("Content-Range", $"bytes {start}-{fs.Length - 1}/{fs.Length}");
- context.Response.ContentLength64 = fs.Length - start;
- context.Response.ContentType = "video/mp4";
- fs.CopyTo(context.Response.OutputStream);
- }
- }
- else
- {
- context.Response.ContentType = "video/mp4";
- context.Response.ContentLength64 = fs.Length;
- fs.CopyTo(context.Response.OutputStream);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- finally
- {
- fs?.Close();
- }
- }
- }
- else
- {
- context.Response.StatusCode = 404;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- try
- {
- context.Response.StatusCode = 500;
- }
- catch (Exception exception)
- {
- Console.WriteLine(exception);
- }
- }
- finally
- {
- try
- {
- context.Response.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
- }
- private static void WriteText(this HttpListenerResponse response, string content)
- {
- var bytes = Encoding.UTF8.GetBytes(content);
- response.OutputStream.Write(bytes);
- }
- private static string FuckVlc(this string input)
- {
- if (input == null) return null;
- return input
- .Replace("[", "%5B")
- .Replace("]", "%5D")
- ;
- }
- }
- }
|