Added API call to retrieve cover with internalId.

No need to mount imageCache over multiple containers.
This commit is contained in:
glax 2023-09-01 21:40:56 +02:00
parent 394829ee36
commit 32036df057
4 changed files with 63 additions and 22 deletions

View File

@ -105,37 +105,54 @@ public class Server : GlobalBase
private void HandleGet(HttpListenerRequest request, HttpListenerResponse response) private void HandleGet(HttpListenerRequest request, HttpListenerResponse response)
{ {
Dictionary<string, string> requestVariables = GetRequestVariables(request.Url!.Query); Dictionary<string, string> requestVariables = GetRequestVariables(request.Url!.Query);
string? connectorName, jobId; string? connectorName, jobId, internalId;
MangaConnector connector; MangaConnector? connector;
Manga? manga;
string path = Regex.Match(request.Url!.LocalPath, @"[A-z0-9]+(\/[A-z0-9]+)*").Value; string path = Regex.Match(request.Url!.LocalPath, @"[A-z0-9]+(\/[A-z0-9]+)*").Value;
switch (path) switch (path)
{ {
case "Connectors": case "Connectors":
SendResponse(HttpStatusCode.OK, response, _parent.GetConnectors().Select(con => con.name).ToArray()); SendResponse(HttpStatusCode.OK, response, _parent.GetConnectors().Select(con => con.name).ToArray());
break; 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": case "Manga/FromConnector":
if (!requestVariables.TryGetValue("connector", out connectorName) || if (!requestVariables.TryGetValue("connector", out connectorName) ||
!requestVariables.TryGetValue("title", out string? title) || !requestVariables.TryGetValue("title", out string? title) ||
_parent.GetConnector(connectorName) is null) !_parent.TryGetConnector(connectorName, out connector))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
connector = _parent.GetConnector(connectorName)!; SendResponse(HttpStatusCode.OK, response, connector!.GetPublications(title));
SendResponse(HttpStatusCode.OK, response, connector.GetPublications(title));
break; break;
case "Manga/Chapters": case "Manga/Chapters":
if(!requestVariables.TryGetValue("connector", out connectorName) || if(!requestVariables.TryGetValue("connector", out connectorName) ||
!requestVariables.TryGetValue("internalId", out string? internalId) || !requestVariables.TryGetValue("internalId", out internalId) ||
_parent.GetConnector(connectorName) is null || !_parent.TryGetConnector(connectorName, out connector) ||
_parent.GetPublicationById(internalId) is null) !_parent.TryGetPublicationById(internalId, out manga))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
connector = _parent.GetConnector(connectorName)!; SendResponse(HttpStatusCode.OK, response, connector!.GetChapters((Manga)manga!));
Manga manga = (Manga)_parent.GetPublicationById(internalId)!;
SendResponse(HttpStatusCode.OK, response, connector.GetChapters(manga));
break; break;
case "Jobs": case "Jobs":
if (!requestVariables.TryGetValue("jobId", out jobId)) 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-Allow-Methods", "GET, POST, DELETE");
response.AddHeader("Access-Control-Max-Age", "1728000"); response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*"); response.AppendHeader("Access-Control-Allow-Origin", "*");
response.ContentType = "application/json";
try if (content is not FileStream stream)
{ {
response.OutputStream.Write(content is not null response.ContentType = "application/json";
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) try
: Array.Empty<byte>()); {
response.OutputStream.Close(); response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>());
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();
} }
} }
} }

View File

@ -34,6 +34,12 @@ public partial class Tranga : GlobalBase
return null; return null;
} }
public bool TryGetConnector(string name, out MangaConnector? connector)
{
connector = GetConnector(name);
return connector is not null;
}
public IEnumerable<MangaConnector> GetConnectors() public IEnumerable<MangaConnector> GetConnectors()
{ {
return connectors; return connectors;
@ -46,6 +52,12 @@ public partial class Tranga : GlobalBase
return null; return null;
} }
public bool TryGetPublicationById(string internalId, out Manga? manga)
{
manga = GetPublicationById(internalId);
return manga is not null;
}
private void StartJobBoss() private void StartJobBoss()
{ {
Thread t = new (() => Thread t = new (() =>

View File

@ -127,4 +127,9 @@ public class TrangaSettings
Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!); Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!);
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this)); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this));
} }
public string GetFullCoverPath(Manga manga)
{
return Path.Join(this.coverImageCache, manga.coverFileNameInCache);
}
} }

View File

@ -4,7 +4,6 @@ services:
image: glax/tranga-api:latest image: glax/tranga-api:latest
container_name: tranga-api container_name: tranga-api
volumes: volumes:
- ./tranga:/usr/share/Tranga-API #1 when replacing ./tranga replace #2 with same value
- ./Manga:/Manga - ./Manga:/Manga
ports: ports:
- "6531:6531" - "6531:6531"
@ -12,8 +11,6 @@ services:
tranga-website: tranga-website:
image: glax/tranga-website:latest image: glax/tranga-website:latest
container_name: tranga-website container_name: tranga-website
volumes:
- ./tranga/imageCache:/usr/share/nginx/html/imageCache:ro #2 when replacing Point to same value as #1/imageCache
ports: ports:
- "9555:80" - "9555:80"
depends_on: depends_on: