Skip to content

Commit

Permalink
Downloaded Image processing:
Browse files Browse the repository at this point in the history
- Compression
- B/W threshold
  • Loading branch information
C9Glax committed Oct 27, 2024
1 parent fb7ed21 commit febce6b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 3 deletions.
27 changes: 25 additions & 2 deletions Tranga/MangaConnectors/MangaConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using System.Net;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Binarization;
using Tranga.Jobs;
using static System.IO.UnixFileMode;

Expand Down Expand Up @@ -216,6 +220,22 @@ private HttpStatusCode DownloadImage(string imageUrl, string fullPath, RequestTy
return requestResult.statusCode;
}

private void ProcessImage(string imagePath)
{
if (!TrangaSettings.bwImages && !TrangaSettings.compressImages)
return;
DateTime start = DateTime.Now;
using Image image = Image.Load(imagePath);
File.Delete(imagePath);
if(TrangaSettings.bwImages)
image.Mutate(i => i.ApplyProcessor(new AdaptiveThresholdProcessor()));
image.SaveAsJpeg(imagePath, new JpegEncoder()
{
Quality = TrangaSettings.compressImages ? 30 : 75
});
Log($"Image processing took {DateTime.Now.Subtract(start):s\\.fff} B/W:{TrangaSettings.bwImages} Compress:{TrangaSettings.compressImages}");
}

protected HttpStatusCode DownloadChapterImages(string[] imageUrls, Chapter chapter, RequestType requestType, string? referrer = null, ProgressToken? progressToken = null)
{
string saveArchiveFilePath = chapter.GetArchiveFilePath();
Expand Down Expand Up @@ -254,11 +274,14 @@ protected HttpStatusCode DownloadChapterImages(string[] imageUrls, Chapter chapt
progressToken?.Complete();
return HttpStatusCode.NoContent;
}

foreach (string imageUrl in imageUrls)
{
string extension = imageUrl.Split('.')[^1].Split('?')[0];
Log($"Downloading image {chapterNum + 1:000}/{imageUrls.Length:000}"); //TODO
HttpStatusCode status = DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapterNum++}.{extension}"), requestType, referrer);
Log($"Downloading image {chapterNum + 1:000}/{imageUrls.Length:000}");
string imagePath = Path.Join(tempFolder, $"{chapterNum++}.{extension}");
HttpStatusCode status = DownloadImage(imageUrl, imagePath, requestType, referrer);
ProcessImage(imagePath);
Log($"{saveArchiveFilePath} {chapterNum + 1:000}/{imageUrls.Length:000} {status}");
if ((int)status < 200 || (int)status >= 300)
{
Expand Down
4 changes: 4 additions & 0 deletions Tranga/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public Server(Tranga parent) : base(parent)
new ("POST", @"/v2/Settings/RateLimit/([a-zA-Z]+)", PostV2SettingsRateLimitType),
new ("GET", @"/v2/Settings/AprilFoolsMode", GetV2SettingsAprilFoolsMode),
new ("POST", @"/v2/Settings/AprilFoolsMode", PostV2SettingsAprilFoolsMode),
new ("GET", @"/v2/Settings/CompressImages", GetV2SettingsCompressImages),
new ("POST", @"/v2/Settings/CompressImages", PostV2SettingsCompressImages),
new ("GET", @"/v2/Settings/BWImages", GetV2SettingsBwImages),
new ("POST", @"/v2/Settings/BWImages", PostV2SettingsBwImages),
new ("POST", @"/v2/Settings/DownloadLocation", PostV2SettingsDownloadLocation),
new ("GET", @"/v2/LibraryConnector", GetV2LibraryConnector),
new ("GET", @"/v2/LibraryConnector/Types", GetV2LibraryConnectorTypes),
Expand Down
28 changes: 28 additions & 0 deletions Tranga/Server/v2Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ public partial class Server
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}

private ValueTuple<HttpStatusCode, object?> GetV2SettingsCompressImages(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, TrangaSettings.compressImages);
}

private ValueTuple<HttpStatusCode, object?> PostV2SettingsCompressImages(GroupCollection groups, Dictionary<string, string> requestParameters)
{
if (!requestParameters.TryGetValue("value", out string? trueFalseStr) ||
!bool.TryParse(trueFalseStr, out bool trueFalse))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Errors parsing 'value'");
TrangaSettings.UpdateCompressImages(trueFalse);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}

private ValueTuple<HttpStatusCode, object?> GetV2SettingsBwImages(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, TrangaSettings.bwImages);
}

private ValueTuple<HttpStatusCode, object?> PostV2SettingsBwImages(GroupCollection groups, Dictionary<string, string> requestParameters)
{
if (!requestParameters.TryGetValue("value", out string? trueFalseStr) ||
!bool.TryParse(trueFalseStr, out bool trueFalse))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Errors parsing 'value'");
TrangaSettings.UpdateBwImages(trueFalse);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}

private ValueTuple<HttpStatusCode, object?> PostV2SettingsDownloadLocation(GroupCollection groups, Dictionary<string, string> requestParameters)
{
if (!requestParameters.TryGetValue("location", out string? folderPath))
Expand Down
26 changes: 25 additions & 1 deletion Tranga/TrangaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class TrangaSettings
public static string userAgent { get; private set; } = DefaultUserAgent;
public static bool bufferLibraryUpdates { get; private set; } = false;
public static bool bufferNotifications { get; private set; } = false;
public static bool compressImages { get; private set; } = true;
public static bool bwImages { get; private set; } = false;
[JsonIgnore] public static string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public static string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
[JsonIgnore] public static string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
Expand Down Expand Up @@ -49,7 +51,9 @@ public static void LoadFromWorkingDirectory(string directory)
ExportSettings();
}

public static void CreateOrUpdate(string? downloadDirectory = null, string? pWorkingDirectory = null, int? pApiPortNumber = null, string? pUserAgent = null, bool? pAprilFoolsMode = null, bool? pBufferLibraryUpdates = null, bool? pBufferNotifications = null)
public static void CreateOrUpdate(string? downloadDirectory = null, string? pWorkingDirectory = null,
int? pApiPortNumber = null, string? pUserAgent = null, bool? pAprilFoolsMode = null,
bool? pBufferLibraryUpdates = null, bool? pBufferNotifications = null, bool? pCompressImages = null, bool? pbwImages = null)
{
if(pWorkingDirectory is null && File.Exists(settingsFilePath))
LoadFromWorkingDirectory(workingDirectory);
Expand All @@ -60,6 +64,8 @@ public static void CreateOrUpdate(string? downloadDirectory = null, string? pWor
aprilFoolsMode = pAprilFoolsMode ?? aprilFoolsMode;
bufferLibraryUpdates = pBufferLibraryUpdates ?? bufferLibraryUpdates;
bufferNotifications = pBufferNotifications ?? bufferNotifications;
compressImages = pCompressImages ?? compressImages;
bwImages = pbwImages ?? bwImages;
Directory.CreateDirectory(downloadLocation);
Directory.CreateDirectory(workingDirectory);
ExportSettings();
Expand Down Expand Up @@ -99,6 +105,18 @@ public static void UpdateAprilFoolsMode(bool enabled)
ExportSettings();
}

public static void UpdateCompressImages(bool enabled)
{
compressImages = enabled;
ExportSettings();
}

public static void UpdateBwImages(bool enabled)
{
bwImages = enabled;
ExportSettings();
}

public static void UpdateDownloadLocation(string newPath, bool moveFiles = true)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Expand Down Expand Up @@ -190,6 +208,8 @@ public static JObject AsJObject()
jobj.Add("requestLimits", JToken.FromObject(requestLimits));
jobj.Add("bufferLibraryUpdates", JToken.FromObject(bufferLibraryUpdates));
jobj.Add("bufferNotifications", JToken.FromObject(bufferNotifications));
jobj.Add("compressImages", JToken.FromObject(compressImages));
jobj.Add("bwImages", JToken.FromObject(bwImages));
return jobj;
}

Expand All @@ -214,5 +234,9 @@ public static void Deserialize(string serialized)
bufferLibraryUpdates = blu.Value<bool>()!;
if (jobj.TryGetValue("bufferNotifications", out JToken? bn))
bufferNotifications = bn.Value<bool>()!;
if (jobj.TryGetValue("compressImages", out JToken? ci))
compressImages = ci.Value<bool>()!;
if (jobj.TryGetValue("bwImages", out JToken? bwi))
bwImages = bwi.Value<bool>()!;
}
}
61 changes: 61 additions & 0 deletions docs/API_Calls_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,67 @@ Enables/Disables April-Fools-Mode.
| 500 | Parsing Error |
</details>

### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Settings/CompressImages`

Returns the current state of the Image-compression setting.

<details>
<summary>Returns</summary>

Boolean
</details>

### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Settings/CompressImages`

Enables/Disables Imagecompression.

<details>
<summary>Request</summary>

| Parameter | Value |
|-----------|------------|
| value | true/false |
</details>

<details>
<summary>Returns</summary>

| StatusCode | Meaning |
|------------|--------------------------------|
| 500 | Parsing Error |
</details>

### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Settings/BWImages`

Returns the current state of the Black/White Image setting.

<details>
<summary>Returns</summary>

Boolean
</details>

### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Settings/BWImages`

Enables/Disables converting Images to Black and White.

<details>
<summary>Request</summary>

| Parameter | Value |
|-----------|------------|
| value | true/false |
</details>

<details>
<summary>Returns</summary>

| StatusCode | Meaning |
|------------|--------------------------------|
| 500 | Parsing Error |
</details>


### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Settings/DownloadLocation`

Updates the default Download-Location.
Expand Down
2 changes: 2 additions & 0 deletions docs/Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
"bufferNotifications": boolean,
"version": number,
"aprilFoolsMode": boolean,
"compressImages": boolean,
"bwImages": boolean,
"requestLimits": {
"MangaInfo": number,
"MangaDexFeed": number,
Expand Down

0 comments on commit febce6b

Please sign in to comment.