Merge branch 'refs/heads/cuttingedge-merge-ServerV2' into cuttingedge

This commit is contained in:
Glax 2024-06-29 19:20:16 +02:00
commit 7c217a7e33
6 changed files with 49 additions and 29 deletions

View File

@ -91,18 +91,22 @@ public readonly struct Chapter : IComparable
{ {
if (!Directory.Exists(Path.Join(downloadLocation, parentManga.folderName))) if (!Directory.Exists(Path.Join(downloadLocation, parentManga.folderName)))
return false; return false;
FileInfo[] archives = new DirectoryInfo(Path.Join(downloadLocation, parentManga.folderName)).GetFiles(); FileInfo[] archives = new DirectoryInfo(Path.Join(downloadLocation, parentManga.folderName)).GetFiles().Where(file => file.Name.Split('.')[^1] == "cbz").ToArray();
Regex volChRex = new(@"(?:Vol(?:ume)?\.([0-9]+)\D*)?Ch(?:apter)?\.([0-9]+(?:\.[0-9]+)*)"); Regex volChRex = new(@"(?:Vol(?:ume)?\.([0-9]+)\D*)?Ch(?:apter)?\.([0-9]+(?:\.[0-9]+)*)");
Chapter t = this; Chapter t = this;
return archives.Select(archive => archive.Name).Any(archiveFileName => string thisPath = GetArchiveFilePath(downloadLocation);
FileInfo? archive = archives.FirstOrDefault(archive =>
{ {
Match m = volChRex.Match(archiveFileName); Match m = volChRex.Match(archive.Name);
string archiveVolNum = m.Groups[1].Success ? m.Groups[1].Value : "0"; string archiveVolNum = m.Groups[1].Success ? m.Groups[1].Value : "0";
string archiveChNum = m.Groups[2].Value; string archiveChNum = m.Groups[2].Value;
return archiveVolNum == t.volumeNumber && return archiveVolNum == t.volumeNumber && archiveChNum == t.chapterNumber ||
archiveChNum == t.chapterNumber; archiveVolNum == "0" && archiveChNum == t.chapterNumber;
}); });
if(archive is not null && thisPath != archive.FullName)
archive.MoveTo(thisPath);
return archive is not null;
} }
/// <summary> /// <summary>
/// Creates full file path of chapter-archive /// Creates full file path of chapter-archive

View File

@ -299,6 +299,7 @@ public abstract class MangaConnector : GlobalBase
RequestResult coverResult = downloadClient.MakeRequest(url, requestType); RequestResult coverResult = downloadClient.MakeRequest(url, requestType);
using MemoryStream ms = new(); using MemoryStream ms = new();
coverResult.result.CopyTo(ms); coverResult.result.CopyTo(ms);
Directory.CreateDirectory(settings.coverImageCache);
File.WriteAllBytes(saveImagePath, ms.ToArray()); File.WriteAllBytes(saveImagePath, ms.ToArray());
Log($"Saving cover to {saveImagePath}"); Log($"Saving cover to {saveImagePath}");
return saveImagePath; return saveImagePath;

View File

@ -111,7 +111,7 @@ public class Mangaworld: MangaConnector
string posterUrl = document.DocumentNode.SelectSingleNode("//img[@class='rounded']").GetAttributeValue("src", ""); string posterUrl = document.DocumentNode.SelectSingleNode("//img[@class='rounded']").GetAttributeValue("src", "");
string coverFileNameInCache = SaveCoverImageToCache(posterUrl, publicationId, RequestType.MangaCover); string coverFileNameInCache = SaveCoverImageToCache(posterUrl, publicationId.Replace('/', '-'), RequestType.MangaCover);
string description = document.DocumentNode.SelectSingleNode("//div[@id='noidungm']").InnerText; string description = document.DocumentNode.SelectSingleNode("//div[@id='noidungm']").InnerText;

View File

@ -28,7 +28,7 @@ public class NotificationManagerJsonConverter : JsonConverter
case (byte)NotificationConnector.NotificationConnectorType.LunaSea: case (byte)NotificationConnector.NotificationConnectorType.LunaSea:
return new LunaSea(this._clone, jo.GetValue("id")!.Value<string>()!); return new LunaSea(this._clone, jo.GetValue("id")!.Value<string>()!);
case (byte)NotificationConnector.NotificationConnectorType.Ntfy: case (byte)NotificationConnector.NotificationConnectorType.Ntfy:
return new Ntfy(this._clone, jo.GetValue("endpoint")!.Value<string>()!, jo.GetValue("auth")!.Value<string>()!); return new Ntfy(this._clone, jo.GetValue("endpoint")!.Value<string>()!, jo.GetValue("topic")!.Value<string>()!, jo.GetValue("auth")!.Value<string>()!);
} }
throw new Exception(); throw new Exception();

View File

@ -11,30 +11,43 @@ public class Ntfy : NotificationConnector
public string auth { get; init; } public string auth { get; init; }
public string topic { get; init; } public string topic { get; init; }
private readonly HttpClient _client = new(); private readonly HttpClient _client = new();
public Ntfy(GlobalBase clone, string endpoint, string auth) : base(clone, NotificationConnectorType.Ntfy)
{
if (!baseUrlRex.IsMatch(endpoint))
throw new ArgumentException("endpoint does not match pattern");
Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*");
Match match = rootUriRex.Match(endpoint);
if(!match.Success)
Log($"Error getting URI from provided endpoint-URI: {endpoint}");
this.endpoint = match.Groups[1].Value;
if (match.Groups[2].Success)
topic = match.Groups[2].Value;
else
topic = "tranga";
this.auth = auth;
}
[JsonConstructor] [JsonConstructor]
public Ntfy(GlobalBase clone, string endpoint, string auth, string topic) : base(clone, NotificationConnectorType.Ntfy) public Ntfy(GlobalBase clone, string endpoint, string topic, string auth) : base(clone, NotificationConnectorType.Ntfy)
{ {
this.endpoint = endpoint; this.endpoint = endpoint;
this.topic = topic.Length > 0 ? topic : "tranga"; this.topic = topic;
this.auth = auth; this.auth = auth;
} }
public Ntfy(GlobalBase clone, string endpoint, string username, string password, string? topic = null) :
this(clone, EndpointAndTopicFromUrl(endpoint)[0], topic??EndpointAndTopicFromUrl(endpoint)[1], AuthFromUsernamePassword(username, password))
{
}
private static string AuthFromUsernamePassword(string username, string password)
{
string authHeader = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
string authParam = Convert.ToBase64String(Encoding.UTF8.GetBytes(authHeader)).Replace("=","");
return authParam;
}
private static string[] EndpointAndTopicFromUrl(string url)
{
string[] ret = new string[2];
if (!baseUrlRex.IsMatch(url))
throw new ArgumentException("url does not match pattern");
Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*");
Match match = rootUriRex.Match(url);
if(!match.Success)
throw new ArgumentException($"Error getting URI from provided endpoint-URI: {url}");
ret[0] = match.Groups[1].Value;
ret[1] = match.Groups[2].Success && match.Groups[2].Value.Length > 0 ? match.Groups[2].Value : "tranga";
return ret;
}
public override string ToString() public override string ToString()
{ {

View File

@ -492,12 +492,13 @@ public class Server : GlobalBase
}else if (notificationConnectorType is NotificationConnector.NotificationConnectorType.Ntfy) }else if (notificationConnectorType is NotificationConnector.NotificationConnectorType.Ntfy)
{ {
if (!requestVariables.TryGetValue("ntfyUrl", out string? ntfyUrl) || if (!requestVariables.TryGetValue("ntfyUrl", out string? ntfyUrl) ||
!requestVariables.TryGetValue("ntfyAuth", out string? ntfyAuth)) !requestVariables.TryGetValue("ntfyUser", out string? ntfyUser)||
!requestVariables.TryGetValue("ntfyPass", out string? ntfyPass))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
AddNotificationConnector(new Ntfy(this, ntfyUrl, ntfyAuth)); AddNotificationConnector(new Ntfy(this, ntfyUrl, ntfyUser, ntfyPass, null));
SendResponse(HttpStatusCode.Accepted, response); SendResponse(HttpStatusCode.Accepted, response);
} }
else else
@ -534,12 +535,13 @@ public class Server : GlobalBase
}else if (notificationConnectorType is NotificationConnector.NotificationConnectorType.Ntfy) }else if (notificationConnectorType is NotificationConnector.NotificationConnectorType.Ntfy)
{ {
if (!requestVariables.TryGetValue("ntfyUrl", out string? ntfyUrl) || if (!requestVariables.TryGetValue("ntfyUrl", out string? ntfyUrl) ||
!requestVariables.TryGetValue("ntfyAuth", out string? ntfyAuth)) !requestVariables.TryGetValue("ntfyUser", out string? ntfyUser)||
!requestVariables.TryGetValue("ntfyPass", out string? ntfyPass))
{ {
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
notificationConnector = new Ntfy(this, ntfyUrl, ntfyAuth); notificationConnector = new Ntfy(this, ntfyUrl, ntfyUser, ntfyPass, null);
} }
else else
{ {