Compare commits

..

4 Commits

6 changed files with 42 additions and 15 deletions

View File

@ -80,11 +80,11 @@ public class RequestHandler
private Dictionary<string, string> GetRequestVariables(string query) private Dictionary<string, string> GetRequestVariables(string query)
{ {
Dictionary<string, string> ret = new(); Dictionary<string, string> ret = new();
Regex queryRex = new (@"\?{1}([A-z]+=[A-z]+)+(&[A-z]+=[A-z]+)*"); Regex queryRex = new (@"\?{1}&?([A-z]+=[A-z]+)+(&[A-z]+=[A-z]+)*");
if (!queryRex.IsMatch(query)) if (!queryRex.IsMatch(query))
return ret; return ret;
query = query.Substring(1); query = query.Substring(1);
foreach(string kvpair in query.Split('&')) foreach(string kvpair in query.Split('&').Where(str => str.Length>=3 ))
ret.Add(kvpair.Split('=')[0], kvpair.Split('=')[1]); ret.Add(kvpair.Split('=')[0], kvpair.Split('=')[1]);
return ret; return ret;
} }

View File

@ -67,8 +67,6 @@ public class Server
internal void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) internal void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{ {
if (!response.OutputStream.CanWrite)
return;
//logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}"); //logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}");
response.StatusCode = (int)statusCode; response.StatusCode = (int)statusCode;
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
@ -76,9 +74,16 @@ public class Server
response.AddHeader("Access-Control-Max-Age", "1728000"); response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*"); response.AppendHeader("Access-Control-Allow-Origin", "*");
response.ContentType = "application/json"; response.ContentType = "application/json";
response.OutputStream.Write(content is not null try
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) {
: Array.Empty<byte>()); response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>());
}
catch (HttpListenerException)
{
}
response.OutputStream.Close(); response.OutputStream.Close();
} }

View File

@ -6,18 +6,18 @@ namespace Tranga.NotificationManagers;
public class LunaSea : NotificationManager public class LunaSea : NotificationManager
{ {
public string webhook { get; } public string id { get; }
private readonly HttpClient _client = new(); private readonly HttpClient _client = new();
public LunaSea(string webhook, Logger? logger = null) : base(NotificationManagerType.LunaSea, logger) public LunaSea(string id, Logger? logger = null) : base(NotificationManagerType.LunaSea, logger)
{ {
this.webhook = webhook; this.id = id;
} }
public override void SendNotification(string title, string notificationText) public override void SendNotification(string title, string notificationText)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Sending notification: {title} - {notificationText}"); logger?.WriteLine(this.GetType().ToString(), $"Sending notification: {title} - {notificationText}");
MessageData message = new(title, notificationText); MessageData message = new(title, notificationText);
HttpRequestMessage request = new(HttpMethod.Post, webhook); HttpRequestMessage request = new(HttpMethod.Post, $"https://notify.lunasea.app/v1/custom/{id}");
request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json"); request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.Send(request); HttpResponseMessage response = _client.Send(request);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@ -363,9 +363,7 @@ public class TaskManager
private void ExportDataAndSettings() private void ExportDataAndSettings()
{ {
logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}"); logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}");
while(IsFileInUse(settings.settingsFilePath)) settings.ExportSettings();
Thread.Sleep(50);
File.WriteAllText(settings.settingsFilePath, JsonConvert.SerializeObject(settings));
logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}"); logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}");
while(IsFileInUse(settings.tasksFilePath)) while(IsFileInUse(settings.tasksFilePath))

View File

@ -43,6 +43,29 @@ public class TrangaSettings
return settings; return settings;
} }
public void ExportSettings()
{
if (File.Exists(settingsFilePath))
{
bool inUse = true;
while (inUse)
{
try
{
using FileStream stream = new (settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
stream.Close();
inUse = false;
}
catch (IOException)
{
inUse = true;
Thread.Sleep(50);
}
}
}
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this));
}
public void UpdateSettings(UpdateField field, Logger? logger = null, params string[] values) public void UpdateSettings(UpdateField field, Logger? logger = null, params string[] values)
{ {
switch (field) switch (field)
@ -81,6 +104,7 @@ public class TrangaSettings
newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!"); newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!");
break; break;
} }
ExportSettings();
} }
public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify, LunaSea} public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify, LunaSea}

View File

@ -142,7 +142,7 @@
<div> <div>
<span class="title">LunaSea</span> <span class="title">LunaSea</span>
<div>Configured: <span id="lunaseaConfigured">✅❌</span></div> <div>Configured: <span id="lunaseaConfigured">✅❌</span></div>
<label for="lunaseaWebhook"></label><input placeholder="Webhook-Url" id="lunaseaWebhook" type="text"> <label for="lunaseaWebhook"></label><input placeholder="device/:id or user/:id" id="lunaseaWebhook" type="text">
</div> </div>
<div> <div>
<label for="libraryUpdateTime" style="margin-right: 5px;">Update Time</label><input id="libraryUpdateTime" type="time" value="00:01:00" step="10"> <label for="libraryUpdateTime" style="margin-right: 5px;">Update Time</label><input id="libraryUpdateTime" type="time" value="00:01:00" step="10">