Skip to content

Commit

Permalink
Clean directories
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Apr 20, 2024
1 parent cb827c6 commit c0257e1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
22 changes: 16 additions & 6 deletions Protest/Database/DeviceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ public static byte[] Save(HttpListenerContext ctx, Dictionary<string, string> pa
return Data.CODE_INVALID_ARGUMENT.Array;
}

StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding);
string payload = reader.ReadToEnd();
try {
if (!Directory.Exists(Data.DIR_CONFIG)) {
Directory.CreateDirectory(Data.DIR_CONFIG);
}

StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding);
string payload = reader.ReadToEnd();

byte[] plain = Encoding.UTF8.GetBytes(FormatRouterOs(payload));
byte[] gzip = Http.Cache.GZip(plain);
byte[] cipher = Cryptography.Encrypt(gzip, Configuration.DB_KEY, Configuration.DB_KEY_IV);
File.WriteAllBytes($"{Data.DIR_CONFIG}{Data.DELIMITER}{file}", cipher);
byte[] plain = Encoding.UTF8.GetBytes(FormatRouterOs(payload));
byte[] gzip = Http.Cache.GZip(plain);
byte[] cipher = Cryptography.Encrypt(gzip, Configuration.DB_KEY, Configuration.DB_KEY_IV);
File.WriteAllBytes($"{Data.DIR_CONFIG}{Data.DELIMITER}{file}", cipher);
}
catch (Exception ex){
Logger.Error(ex);
return Data.CODE_FAILED.ToArray();
}

Logger.Action(origin, $"Modify the device conficuration for file: {file}");

Expand Down
2 changes: 1 addition & 1 deletion Protest/Front/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class Watchdog extends Window {
this.Seek();
}
catch (ex) {
setTimeout(()=>this.ConfirmBox(ex, true, "mono/error.svg"), 200);
setTimeout(()=>this.ConfirmBox(ex, true, "mono/error.svg"), 250);
}

cancelButton.onclick();
Expand Down
6 changes: 5 additions & 1 deletion Protest/Misc/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ static Backup() {
}

internal static byte[] Create(Dictionary<string, string> parameters) {
if (parameters is null || !parameters.TryGetValue("name", out string name)) {
if (parameters is null || !parameters.TryGetValue("name", out string name) || String.IsNullOrEmpty(name)) {
name = $"backup-{DateTime.UtcNow.ToString(Data.DATE_FORMAT_FILE)}";
}

Console.WriteLine(name);

foreach (char c in Path.GetInvalidFileNameChars()) {
name = name.Replace (c, '_');
}

Console.WriteLine(name);

try {
DirectoryInfo backupDirectory = new DirectoryInfo(Data.DIR_BACKUP);
Expand Down
17 changes: 9 additions & 8 deletions Protest/Misc/Data.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace Protest;
Expand Down Expand Up @@ -68,20 +69,15 @@ public static class Data {

public static readonly string FILE_CONFIG = $"{DIR_ROOT}{DELIMITER}protest.cfg";

private static readonly byte[] DEBIT_TEMPLATE = "The aforementioned items are company property and must be returned in pristine condition at the conclusion of the contract. Any loss, damage, or failure to return these items will result in their value being deducted from the employee's salary."u8.ToArray();

public static void InitializeDirectories() {
DirectoryInfo[] dirs = new DirectoryInfo[] {
new DirectoryInfo(DIR_LOG),
new DirectoryInfo(DIR_LASTSEEN),
new DirectoryInfo(DIR_LIFELINE),
new DirectoryInfo(DIR_WATCHDOG),
new DirectoryInfo(DIR_DOCUMENTATION),
new DirectoryInfo(DIR_DEBIT),
new DirectoryInfo(DIR_DEBIT_SHORT),
new DirectoryInfo(DIR_DEBIT_LONG),
new DirectoryInfo(DIR_DEBIT_RETURNED),
new DirectoryInfo(DIR_DEBIT_TEMPLATE),
new DirectoryInfo(DIR_CONFIG),
new DirectoryInfo(DIR_DATA),
//new DirectoryInfo(DIR_DEBIT_TEMPLATE),
new DirectoryInfo(DIR_DEVICES),
new DirectoryInfo(DIR_USERS)
};
Expand All @@ -96,6 +92,11 @@ public static void InitializeDirectories() {
Logger.Error(ex);
}
}

if (!Directory.Exists(DIR_DEBIT_TEMPLATE)) {
Directory.CreateDirectory(DIR_DEBIT_TEMPLATE);
File.WriteAllBytes($"{DIR_DEBIT_TEMPLATE}{DELIMITER}Company.txt", DEBIT_TEMPLATE);
}
}

public static string SizeToString(long size) {
Expand Down
4 changes: 4 additions & 0 deletions Protest/Tasks/Watchdog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ public static byte[] Create(HttpListenerContext ctx, Dictionary<string, string>

byte[] content = JsonSerializer.SerializeToUtf8Bytes(watcher, watcherSerializerOptions);

if (!Directory.Exists(Data.DIR_WATCHDOG)) {
Directory.CreateDirectory(Data.DIR_WATCHDOG);
}

File.WriteAllBytes($"{Data.DIR_WATCHDOG}{Data.DELIMITER}{file}", content);

DirectoryInfo dirInfo = new DirectoryInfo($"{Data.DIR_WATCHDOG}{Data.DELIMITER}{file}_");
Expand Down
4 changes: 4 additions & 0 deletions Protest/Tools/DebitNotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ public static byte[] Return(Dictionary<string, string> parameters, string origin
public static byte[] ListTemplate() {
DirectoryInfo dir = new DirectoryInfo(Data.DIR_DEBIT_TEMPLATE);

if (!dir.Exists) {
return "{}"u8.ToArray();
}

StringBuilder builder = new StringBuilder();
builder.Append('[');
FileInfo[] files = dir.GetFiles();
Expand Down
25 changes: 16 additions & 9 deletions Protest/Tools/Documentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public static byte[] List(Dictionary<string, string> parameters) {
string[] keywordsArray = keywords.Split(' ').Where(o=>o.Length > 0).ToArray();

DirectoryInfo dir = new DirectoryInfo(Data.DIR_DOCUMENTATION);
if (!dir.Exists)
return Data.CODE_FILE_NOT_FOUND.Array;

if (!dir.Exists) {
return "[]"u8.ToArray();
}

List<FileInfo> files = dir.GetFiles().ToList();
files.Sort((a, b) => String.Compare(a.Name, b.Name));

Expand Down Expand Up @@ -167,18 +168,23 @@ public static byte[] Create(HttpListenerContext ctx, string origin) {
keywords.Sort();

idx = 0;
if (keywords.Count > 1)
while (idx < keywords.Count - 1)
if (keywords[idx + 1].StartsWith(keywords[idx]))
if (keywords.Count > 1) {
while (idx < keywords.Count - 1) {
if (keywords[idx + 1].StartsWith(keywords[idx])) {
keywords.RemoveAt(idx);
else
}
else {
idx++;
}
}
}

lock (mutex)
lock (mutex) {
try {
DirectoryInfo dir = new DirectoryInfo(Data.DIR_DOCUMENTATION);
if (!dir.Exists)
if (!dir.Exists) {
dir.Create();
}

FileInfo html = new FileInfo($"{Data.DIR_DOCUMENTATION}\\{filename}.html.gz");

Expand All @@ -190,6 +196,7 @@ public static byte[] Create(HttpListenerContext ctx, string origin) {
catch {
return Data.CODE_FILE_NOT_FOUND.Array;
}
}

Logger.Action(origin, $"Create documentation: {filename}");

Expand Down

0 comments on commit c0257e1

Please sign in to comment.