From ca95460218bcf2395cab86ebeed2e36851bd221e Mon Sep 17 00:00:00 2001 From: Glax Date: Sun, 11 Feb 2024 01:06:40 +0100 Subject: [PATCH] https://github.com/C9Glax/tranga/pull/122 https://github.com/C9Glax/tranga-website/pull/41 LogFile Enable LogFiles --- Dockerfile | 2 +- Tranga/Server.cs | 68 ++++++++++++++++++++++---------------------- Tranga/TrangaArgs.cs | 8 ++++-- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/Dockerfile b/Dockerfile index b495a44..81b2260 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,4 +26,4 @@ WORKDIR /publish COPY --from=build-env /publish . USER 0 RUN chown 1000:1000 /publish -ENTRYPOINT ["dotnet", "/publish/Tranga.dll", "-c"] +ENTRYPOINT ["dotnet", "/publish/Tranga.dll", "-f -c"] diff --git a/Tranga/Server.cs b/Tranga/Server.cs index a2af34a..64b5ab9 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -223,6 +223,40 @@ public class Server : GlobalBase case "Ping": SendResponse(HttpStatusCode.OK, response, "Pong"); break; + case "LogMessages": + if (logger is null || !File.Exists(logger?.logFilePath)) + { + SendResponse(HttpStatusCode.NotFound, response); + break; + } + + if (requestVariables.TryGetValue("count", out string? count)) + { + try + { + uint messageCount = uint.Parse(count); + SendResponse(HttpStatusCode.OK, response, logger.Tail(messageCount)); + } + catch (FormatException f) + { + SendResponse(HttpStatusCode.InternalServerError, response, f); + } + }else + SendResponse(HttpStatusCode.OK, response, logger.GetLog()); + break; + case "LogFile": + if (logger is null || !File.Exists(logger?.logFilePath)) + { + SendResponse(HttpStatusCode.NotFound, response); + break; + } + + string logDir = new FileInfo(logger.logFilePath).DirectoryName!; + string tmpFilePath = Path.Join(logDir, "Tranga.log"); + File.Copy(logger.logFilePath, tmpFilePath); + SendResponse(HttpStatusCode.OK, response, new FileStream(tmpFilePath, FileMode.Open)); + File.Delete(tmpFilePath); + break; default: SendResponse(HttpStatusCode.BadRequest, response); break; @@ -482,40 +516,6 @@ public class Server : GlobalBase SendResponse(HttpStatusCode.BadRequest, response); } break; - case "LogMessages": - if (logger is null || !File.Exists(logger?.logFilePath)) - { - SendResponse(HttpStatusCode.NotFound, response); - break; - } - - if (requestVariables.TryGetValue("count", out string? count)) - { - try - { - uint messageCount = uint.Parse(count); - SendResponse(HttpStatusCode.OK, response, logger.Tail(messageCount)); - } - catch (FormatException f) - { - SendResponse(HttpStatusCode.InternalServerError, response, f); - } - }else - SendResponse(HttpStatusCode.OK, response, logger.GetLog()); - break; - case "LogFile": - if (logger is null || !File.Exists(logger?.logFilePath)) - { - SendResponse(HttpStatusCode.NotFound, response); - break; - } - - string logDir = new FileInfo(logger.logFilePath).DirectoryName!; - string tmpFilePath = Path.Join(logDir, "Tranga.log"); - File.Copy(logger.logFilePath, tmpFilePath); - SendResponse(HttpStatusCode.OK, response, new FileStream(tmpFilePath, FileMode.Open)); - File.Delete(tmpFilePath); - break; default: SendResponse(HttpStatusCode.BadRequest, response); break; diff --git a/Tranga/TrangaArgs.cs b/Tranga/TrangaArgs.cs index 86d9028..72e4634 100644 --- a/Tranga/TrangaArgs.cs +++ b/Tranga/TrangaArgs.cs @@ -16,7 +16,9 @@ public partial class Tranga : GlobalBase string[]? consoleLogger = GetArg(args, ArgEnum.ConsoleLogger); string[]? fileLogger = GetArg(args, ArgEnum.FileLogger); - string? filePath = fileLogger?[0];//TODO validate path + string? filePath = GetArg(args, ArgEnum.FileLoggerPath)?[0]; + if (filePath is not null && !Directory.Exists(new FileInfo(filePath).DirectoryName)) + Directory.CreateDirectory(new FileInfo(filePath).DirectoryName!); List enabledLoggers = new(); if(consoleLogger is not null) @@ -105,7 +107,8 @@ public partial class Tranga : GlobalBase { ArgEnum.DownloadLocation, new(new []{"-d", "--downloadLocation"}, 1, "Directory to which downloaded Manga are saved") }, { ArgEnum.WorkingDirectory, new(new []{"-w", "--workingDirectory"}, 1, "Directory in which application-data is saved") }, { ArgEnum.ConsoleLogger, new(new []{"-c", "--consoleLogger"}, 0, "Enables the consoleLogger") }, - { ArgEnum.FileLogger, new(new []{"-f", "--fileLogger"}, 1, "Enables the fileLogger, Directory where logfiles are saved") }, + { ArgEnum.FileLogger, new(new []{"-f", "--fileLogger"}, 0, "Enables the fileLogger") }, + { ArgEnum.FileLoggerPath, new (new []{"-l", "--fPath"}, 1, "LogFilePath" ) }, { ArgEnum.Help, new(new []{"-h", "--help"}, 0, "Print this") } //{ ArgEnum., new(new []{""}, 1, "") } }; @@ -117,6 +120,7 @@ public partial class Tranga : GlobalBase WorkingDirectory, ConsoleLogger, FileLogger, + FileLoggerPath, Help }