using System; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using JacobTechEncryption; using Luski.net.Enums; using Luski.net.JsonTypes; namespace Luski.net; public class ServerStorage { public static readonly string[] Directories = new[] { "Info", "Assets", "Channels/Keys", "Keys", "Avatars", "Channels/Icons", "Channels/Messages" }; private static readonly int[] CantDelete = new[] { (int)StorageDirectory.ChannelKeys, (int)StorageDirectory.ServerKeys }; internal ServerStorage(string Domain) { if (!Directory.Exists(JT)) Directory.CreateDirectory(JT); Location = JT + "/Luski/"; if (!Directory.Exists(Location)) Directory.CreateDirectory(Location); Location += "Storage/"; if (!Directory.Exists(Location)) Directory.CreateDirectory(Location); Location += "Servers/"; if (!Directory.Exists(Location)) Directory.CreateDirectory(Location); Location += Domain+ "/"; if (!Directory.Exists(Location)) Directory.CreateDirectory(Location); if (!File.Exists(Location + "storage.json")) File.WriteAllText(Location + "storage.json", JsonSerializer.Serialize(new ServerStorageInfo(),ServerStorageInfoContext.Default.ServerStorageInfo)); ServerStorageInfo info = JsonSerializer.Deserialize(File.ReadAllText(Location + "storage.json"), ServerStorageInfoContext.Default.ServerStorageInfo)!; CacheMode = info.CacheMode; for (int i = 0; i < Directories.Length; i++) { string full = Location; string[] spl = Directories[i].Split('/'); if (!info.DontDelete && !CantDelete.Contains(i)) { try { if (Directory.Exists(full + spl[0])) Directory.Delete(full + spl[0], true); } catch { // ignored } } foreach (string d in spl) { full += d + "/"; if (!Directory.Exists(full)) Directory.CreateDirectory(full); } } } public string Location { get; internal set; } public string GetStorageDirectory(StorageDirectory Directory) { return Location + Directories[(byte)Directory] + "/"; } public string GetResourceKey(StorageDirectory Directory, string Resource, string Key) { return Encoding.UTF8.GetString(Encryption.AES.Decrypt(GetResourceBytes(Directory, Resource), Key)); } public string GetResourceKey(StorageDirectory Directory, string Resource, byte[] Key) { return Encoding.UTF8.GetString(Encryption.AES.Decrypt(GetResourceBytes(Directory, Resource), Encoding.UTF8.GetString(Key))); } public void SetResourceKey(StorageDirectory Directory, string Resource, string Key, string value) { File.WriteAllBytes(GetStorageDirectory(Directory) + Resource, Encryption.AES.Encrypt(Encoding.UTF8.GetBytes(value), Key)); } public void SetResourceKey(StorageDirectory Directory, string Resource, byte[] Key, string value) { File.WriteAllBytes(GetStorageDirectory(Directory) + Resource, Encryption.AES.Encrypt(Encoding.UTF8.GetBytes(value), Encoding.UTF8.GetString(Key))); } public byte[] GetResourceBytes(StorageDirectory Directory, string Resource) { return File.ReadAllBytes(Location + Directories[(byte)Directory] + "/" + Resource); } public CacheMode CacheMode { get; internal set; } internal static string JT { get { string tmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JacobTech"); if (OperatingSystem.IsLinux()) { tmp = Path.Combine(Environment.GetEnvironmentVariable("HOME")!, ".config/"); tmp += "JacobTech"; } return tmp; } } }