diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 12933b2..f873d3c 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -105,37 +105,54 @@ public class Server : GlobalBase private void HandleGet(HttpListenerRequest request, HttpListenerResponse response) { Dictionary requestVariables = GetRequestVariables(request.Url!.Query); - string? connectorName, jobId; - MangaConnector connector; + string? connectorName, jobId, internalId; + MangaConnector? connector; + Manga? manga; string path = Regex.Match(request.Url!.LocalPath, @"[A-z0-9]+(\/[A-z0-9]+)*").Value; switch (path) { case "Connectors": SendResponse(HttpStatusCode.OK, response, _parent.GetConnectors().Select(con => con.name).ToArray()); break; + case "Manga/Cover": + if (!requestVariables.TryGetValue("internalId", out internalId) || + !_parent.TryGetPublicationById(internalId, out manga)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + + string filePath = settings.GetFullCoverPath((Manga)manga!); + if (File.Exists(filePath)) + { + FileStream coverStream = new(filePath, FileMode.Open); + SendResponse(HttpStatusCode.OK, response, coverStream); + } + else + { + SendResponse(HttpStatusCode.NotFound, response); + } + break; case "Manga/FromConnector": if (!requestVariables.TryGetValue("connector", out connectorName) || !requestVariables.TryGetValue("title", out string? title) || - _parent.GetConnector(connectorName) is null) + !_parent.TryGetConnector(connectorName, out connector)) { SendResponse(HttpStatusCode.BadRequest, response); break; } - connector = _parent.GetConnector(connectorName)!; - SendResponse(HttpStatusCode.OK, response, connector.GetPublications(title)); + SendResponse(HttpStatusCode.OK, response, connector!.GetPublications(title)); break; case "Manga/Chapters": if(!requestVariables.TryGetValue("connector", out connectorName) || - !requestVariables.TryGetValue("internalId", out string? internalId) || - _parent.GetConnector(connectorName) is null || - _parent.GetPublicationById(internalId) is null) + !requestVariables.TryGetValue("internalId", out internalId) || + !_parent.TryGetConnector(connectorName, out connector) || + !_parent.TryGetPublicationById(internalId, out manga)) { SendResponse(HttpStatusCode.BadRequest, response); break; } - connector = _parent.GetConnector(connectorName)!; - Manga manga = (Manga)_parent.GetPublicationById(internalId)!; - SendResponse(HttpStatusCode.OK, response, connector.GetChapters(manga)); + SendResponse(HttpStatusCode.OK, response, connector!.GetChapters((Manga)manga!)); break; case "Jobs": if (!requestVariables.TryGetValue("jobId", out jobId)) @@ -430,17 +447,27 @@ public class Server : GlobalBase response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE"); response.AddHeader("Access-Control-Max-Age", "1728000"); response.AppendHeader("Access-Control-Allow-Origin", "*"); - response.ContentType = "application/json"; - try + + if (content is not FileStream stream) { - response.OutputStream.Write(content is not null - ? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) - : Array.Empty()); - response.OutputStream.Close(); + response.ContentType = "application/json"; + try + { + response.OutputStream.Write(content is not null + ? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) + : Array.Empty()); + response.OutputStream.Close(); + } + catch (HttpListenerException e) + { + Log(e.ToString()); + } } - catch (HttpListenerException e) + else { - Log(e.ToString()); + stream.CopyTo(response.OutputStream); + response.OutputStream.Close(); + stream.Close(); } } } \ No newline at end of file diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 7ed14b3..b52a60a 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -34,6 +34,12 @@ public partial class Tranga : GlobalBase return null; } + public bool TryGetConnector(string name, out MangaConnector? connector) + { + connector = GetConnector(name); + return connector is not null; + } + public IEnumerable GetConnectors() { return connectors; @@ -46,6 +52,12 @@ public partial class Tranga : GlobalBase return null; } + public bool TryGetPublicationById(string internalId, out Manga? manga) + { + manga = GetPublicationById(internalId); + return manga is not null; + } + private void StartJobBoss() { Thread t = new (() => diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index 42ff3ea..7aacccb 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -127,4 +127,9 @@ public class TrangaSettings Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this)); } + + public string GetFullCoverPath(Manga manga) + { + return Path.Join(this.coverImageCache, manga.coverFileNameInCache); + } } \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 9dcf31f..0122f82 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,7 +4,6 @@ services: image: glax/tranga-api:latest container_name: tranga-api volumes: - - ./tranga:/usr/share/Tranga-API #1 when replacing ./tranga replace #2 with same value - ./Manga:/Manga ports: - "6531:6531" @@ -12,8 +11,6 @@ services: tranga-website: image: glax/tranga-website:latest container_name: tranga-website - volumes: - - ./tranga/imageCache:/usr/share/nginx/html/imageCache:ro #2 when replacing Point to same value as #1/imageCache ports: - "9555:80" depends_on: