From 780df1cd6eb34b975577d8601cceb1e97eaf406a Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 25 May 2023 14:23:33 +0200 Subject: [PATCH] Created Image-Cache --- Tranga-API/Program.cs | 24 ++++++++++++++---------- Tranga.sln.DotSettings | 1 + Tranga/Connector.cs | 5 ++++- Tranga/Connectors/MangaDex.cs | 22 +++++++++++++++------- Tranga/Publication.cs | 6 +++--- Tranga/TaskManager.cs | 7 ++++--- Tranga/TrangaSettings.cs | 1 + 7 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Tranga-API/Program.cs b/Tranga-API/Program.cs index f3ea8b8..d4d1346 100644 --- a/Tranga-API/Program.cs +++ b/Tranga-API/Program.cs @@ -8,24 +8,28 @@ string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/va string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); -Directory.CreateDirectory(applicationFolderPath); -Directory.CreateDirectory(downloadFolderPath); Directory.CreateDirectory(logsFolderPath); - -Console.WriteLine($"Application-Folder: {applicationFolderPath}"); -Console.WriteLine($"Download-Folder-Path: {downloadFolderPath}"); -Console.WriteLine($"Logfile-Path: {logFilePath}"); -Console.WriteLine($"Settings-File-Path: {settingsFilePath}"); - Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); - -logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); + +logger.WriteLine("Tranga", "Loading settings."); + TrangaSettings settings; if (File.Exists(settingsFilePath)) settings = TrangaSettings.LoadSettings(settingsFilePath); else settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, null); +Directory.CreateDirectory(settings.workingDirectory); +Directory.CreateDirectory(settings.downloadLocation); +Directory.CreateDirectory(settings.coverImageCache); + +logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); +logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); +logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); +logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); +logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); + +logger.WriteLine("Tranga", "Loading Taskmanager."); TaskManager taskManager = new (settings, logger); var builder = WebApplication.CreateBuilder(args); diff --git a/Tranga.sln.DotSettings b/Tranga.sln.DotSettings index e445d6b..05f49b5 100644 --- a/Tranga.sln.DotSettings +++ b/Tranga.sln.DotSettings @@ -1,3 +1,4 @@  True + True True \ No newline at end of file diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index e6ea509..c2263ee 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -16,7 +16,9 @@ public abstract class Connector protected Logger? logger; - protected Connector(string downloadLocation, Logger? logger) + protected string imageCachePath; + + protected Connector(string downloadLocation, string imageCachePath, Logger? logger) { this.downloadLocation = downloadLocation; this.logger = logger; @@ -24,6 +26,7 @@ public abstract class Connector { //RequestTypes for RateLimits }, logger); + this.imageCachePath = imageCachePath; } public abstract string name { get; } //Name of the Connector (e.g. Website) diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index d6bc7d8..a61fb62 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -18,7 +18,7 @@ public class MangaDex : Connector Author, } - public MangaDex(string downloadLocation, Logger? logger) : base(downloadLocation, logger) + public MangaDex(string downloadLocation, string imageCachePath, Logger? logger) : base(downloadLocation, imageCachePath, logger) { name = "MangaDex"; this.downloadClient = new DownloadClient(new Dictionary() @@ -98,14 +98,11 @@ public class MangaDex : Connector authorId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue() == "author")!["id"]!.GetValue(); } string? coverUrl = GetCoverUrl(publicationId, posterId); - string? coverBase64 = null; + string? coverCacheName = null; if (coverUrl is not null) { DownloadClient.RequestResult coverResult = downloadClient.MakeRequest(coverUrl, (byte)RequestType.AtHomeServer); - using MemoryStream ms = new(); - coverResult.result.CopyTo(ms); - byte[] imageBytes = ms.ToArray(); - coverBase64 = Convert.ToBase64String(imageBytes); + coverCacheName = SaveImage(coverUrl, coverResult.result); } string? author = GetAuthor(authorId); @@ -136,7 +133,7 @@ public class MangaDex : Connector altTitlesDict, tags.ToArray(), coverUrl, - coverBase64, + coverCacheName, linksDict, year, originalLanguage, @@ -302,4 +299,15 @@ public class MangaDex : Connector //Download cover-Image DownloadImage(publication.posterUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}"), this.downloadClient, (byte)RequestType.AtHomeServer); } + + private string SaveImage(string url, Stream imageData) + { + string[] split = url.Split('/'); + string filename = split[^1]; + string saveImagePath = Path.Join(imageCachePath, filename); + using MemoryStream ms = new(); + imageData.CopyTo(ms); + File.WriteAllBytes(saveImagePath, ms.ToArray()); + return filename; + } } \ No newline at end of file diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index 3003800..3217129 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -15,7 +15,7 @@ public readonly struct Publication public string? description { get; } public string[] tags { get; } public string? posterUrl { get; } - public string? posterBase64 { get; } + public string? coverFileNameInCache { get; } public Dictionary links { get; } public int? year { get; } public string? originalLanguage { get; } @@ -24,14 +24,14 @@ public readonly struct Publication public string publicationId { get; } public string internalId { get; } - public Publication(string sortName, string? author, string? description, Dictionary altTitles, string[] tags, string? posterUrl, string? posterBase64, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId) + public Publication(string sortName, string? author, string? description, Dictionary altTitles, string[] tags, string? posterUrl, string? coverFileNameInCache, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId) { this.sortName = sortName; this.author = author; this.description = description; this.altTitles = altTitles; this.tags = tags; - this.posterBase64 = posterBase64; + this.coverFileNameInCache = coverFileNameInCache; this.posterUrl = posterUrl; this.links = links ?? new Dictionary(); this.year = year; diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index e13da8a..09ba120 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -21,11 +21,12 @@ public class TaskManager /// Local path to save data (Manga) to /// Path to the working directory + /// Path to the cover-image cache /// The Url of the Komga-instance that you want to update /// The Komga username /// The Komga password /// - public TaskManager(string downloadFolderPath, string workingDirectory, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) + public TaskManager(string downloadFolderPath, string workingDirectory, string imageCachePath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) { this.logger = logger; _allTasks = new HashSet(); @@ -37,7 +38,7 @@ public class TaskManager this.settings = new TrangaSettings(downloadFolderPath, workingDirectory, newKomga); ExportData(); - this._connectors = new Connector[]{ new MangaDex(downloadFolderPath, logger) }; + this._connectors = new Connector[]{ new MangaDex(downloadFolderPath, imageCachePath, logger) }; foreach(Connector cConnector in this._connectors) _taskQueue.Add(cConnector, new List()); @@ -58,7 +59,7 @@ public class TaskManager public TaskManager(TrangaSettings settings, Logger? logger = null) { this.logger = logger; - this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) }; + this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, settings.coverImageCache, logger) }; foreach(Connector cConnector in this._connectors) _taskQueue.Add(cConnector, new List()); _allTasks = new HashSet(); diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index b0195e6..bb2b065 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -9,6 +9,7 @@ public class TrangaSettings [JsonIgnore]public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); [JsonIgnore]public string tasksFilePath => Path.Join(workingDirectory, "tasks.json"); [JsonIgnore]public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json"); + [JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache"); public Komga? komga { get; set; } public TrangaSettings(string downloadLocation, string workingDirectory, Komga? komga)