diff --git a/Tranga/Manga.cs b/Tranga/Manga.cs index 357f940..6989d7d 100644 --- a/Tranga/Manga.cs +++ b/Tranga/Manga.cs @@ -67,7 +67,7 @@ public struct Manga while (this.folderName.EndsWith('.')) this.folderName = this.folderName.Substring(0, this.folderName.Length - 1); string onlyLowerLetters = string.Concat(this.sortName.ToLower().Where(Char.IsLetter)); - this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerLetters}{this.year}")); + this.internalId = DateTime.Now.Ticks.ToString(); this.ignoreChaptersBelow = ignoreChaptersBelow ?? 0f; this.latestChapterDownloaded = 0; this.latestChapterAvailable = 0; diff --git a/Tranga/MangaConnectors/MangaConnectorJsonConverter.cs b/Tranga/MangaConnectors/MangaConnectorJsonConverter.cs index 2213a48..3b3e6da 100644 --- a/Tranga/MangaConnectors/MangaConnectorJsonConverter.cs +++ b/Tranga/MangaConnectors/MangaConnectorJsonConverter.cs @@ -1,4 +1,6 @@ -using Newtonsoft.Json; +using System.Data; +using System.Diagnostics; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Tranga.MangaConnectors; @@ -22,29 +24,22 @@ public class MangaConnectorJsonConverter : JsonConverter public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); - switch (jo.GetValue("name")!.Value()!) + string? connectorName = jo.Value("name"); + if (connectorName is null) + throw new ConstraintException("Name can not be null."); + return connectorName switch { - case "MangaDex": - return this._connectors.First(c => c is MangaDex); - case "Manganato": - return this._connectors.First(c => c is Manganato); - case "MangaKatana": - return this._connectors.First(c => c is MangaKatana); - case "Mangasee": - return this._connectors.First(c => c is Mangasee); - case "Mangaworld": - return this._connectors.First(c => c is Mangaworld); - case "Bato": - return this._connectors.First(c => c is Bato); - case "Manga4Life": - return this._connectors.First(c => c is MangaLife); - case "ManhuaPlus": - return this._connectors.First(c => c is ManhuaPlus); - case "MangaHere": - return this._connectors.First(c => c is MangaHere); - } - - throw new Exception(); + "MangaDex" => this._connectors.First(c => c is MangaDex), + "Manganato" => this._connectors.First(c => c is Manganato), + "MangaKatana" => this._connectors.First(c => c is MangaKatana), + "Mangasee" => this._connectors.First(c => c is Mangasee), + "Mangaworld" => this._connectors.First(c => c is Mangaworld), + "Bato" => this._connectors.First(c => c is Bato), + "Manga4Life" => this._connectors.First(c => c is MangaLife), + "ManhuaPlus" => this._connectors.First(c => c is ManhuaPlus), + "MangaHere" => this._connectors.First(c => c is MangaHere), + _ => throw new UnreachableException($"Could not find Connector with name {connectorName}") + }; } public override bool CanWrite => false; diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index 005da07..cbb8ffb 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -243,7 +243,7 @@ public class MangaDex : MangaConnector continue; } - if(chapterNum is not "null") + if(chapterNum is not "null" && !chapters.Any(chp => chp.volumeNumber.Equals(volume) && chp.chapterNumber.Equals(chapterNum))) chapters.Add(new Chapter(manga, title, volume, chapterNum, chapterId)); } } diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 4e387f1..dbf80a5 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -63,10 +63,11 @@ public class Server : GlobalBase { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; - if(request.HttpMethod == "OPTIONS") - SendResponse(HttpStatusCode.OK, context.Response); - if(request.Url!.LocalPath.Contains("favicon")) + if (request.Url!.LocalPath.Contains("favicon")) + { SendResponse(HttpStatusCode.NoContent, response); + return; + } switch (request.HttpMethod) { @@ -79,7 +80,10 @@ public class Server : GlobalBase case "DELETE": HandleDelete(request, response); break; - default: + case "OPTIONS": + SendResponse(HttpStatusCode.OK, context.Response); + break; + default: SendResponse(HttpStatusCode.BadRequest, response); break; } @@ -707,14 +711,15 @@ public class Server : GlobalBase private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) { //Log($"Response: {statusCode} {content}"); + response.StatusCode = (int)statusCode; response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE"); response.AddHeader("Access-Control-Max-Age", "1728000"); response.AppendHeader("Access-Control-Allow-Origin", "*"); - try { + if (content is not Stream) { response.ContentType = "application/json"; @@ -750,7 +755,7 @@ public class Server : GlobalBase stream.Close(); } } - catch (HttpListenerException e) + catch (Exception e) { Log(e.ToString()); }