Implemented new storage system, path builder, default resources, fixed lang stuff
This commit is contained in:
28
Moonlight/App/Helpers/PathBuilder.cs
Normal file
28
Moonlight/App/Helpers/PathBuilder.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace Moonlight.App.Helpers;
|
||||
|
||||
public class PathBuilder
|
||||
{
|
||||
public static string Dir(params string[] parts)
|
||||
{
|
||||
var res = "";
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
res += part + Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string File(params string[] parts)
|
||||
{
|
||||
var res = "";
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
res += part + (part == parts.Last() ? "" : Path.DirectorySeparatorChar);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public class SmartTranslateHelper
|
||||
{
|
||||
Languages = new();
|
||||
|
||||
foreach (var file in Directory.GetFiles("resources/lang"))
|
||||
foreach (var file in Directory.GetFiles(PathBuilder.Dir("storage", "resources", "lang")))
|
||||
{
|
||||
if (Path.GetExtension(file) == ".lang")
|
||||
{
|
||||
@@ -40,7 +40,7 @@ public class SmartTranslateHelper
|
||||
{
|
||||
Languages[langKey].Add(content, content);
|
||||
|
||||
File.WriteAllLines($"resources/lang/{langKey}.lang", GenerateData(Languages[langKey]));
|
||||
File.WriteAllLines(PathBuilder.File("storage", "resources", "lang", $"{langKey}.lang"), GenerateData(Languages[langKey]));
|
||||
}
|
||||
|
||||
return Languages[langKey][content];
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Logging.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.App.Helpers;
|
||||
using Moonlight.App.Models.Misc;
|
||||
using Moonlight.App.Services.LogServices;
|
||||
|
||||
@@ -28,14 +29,13 @@ public class ResourcesController : Controller
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (System.IO.File.Exists($"resources/public/images/{name}"))
|
||||
if (System.IO.File.Exists(PathBuilder.File("storage", "resources", "public", "images", name)))
|
||||
{
|
||||
var fs = new FileStream($"resources/public/images/{name}", FileMode.Open);
|
||||
var fs = new FileStream(PathBuilder.File("storage", "resources", "public", "images", name), FileMode.Open);
|
||||
|
||||
return File(fs, MimeTypes.GetMimeType(name), name);
|
||||
}
|
||||
|
||||
Logger.Debug("404 on resources");
|
||||
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
@@ -7,26 +7,44 @@ namespace Moonlight.App.Services;
|
||||
|
||||
public class ConfigService : IConfiguration
|
||||
{
|
||||
private readonly StorageService StorageService;
|
||||
|
||||
private IConfiguration Configuration;
|
||||
|
||||
public bool DebugMode { get; private set; } = false;
|
||||
|
||||
public ConfigService()
|
||||
|
||||
public ConfigService(StorageService storageService)
|
||||
{
|
||||
Configuration = new ConfigurationBuilder().AddJsonStream(
|
||||
new MemoryStream(Encoding.ASCII.GetBytes(File.ReadAllText("..\\..\\appsettings.json")))
|
||||
).Build();
|
||||
StorageService = storageService;
|
||||
StorageService.EnsureCreated();
|
||||
|
||||
Reload();
|
||||
|
||||
// Env vars
|
||||
var debugVar = Environment.GetEnvironmentVariable("ML_DEBUG");
|
||||
|
||||
if (debugVar != null)
|
||||
DebugMode = bool.Parse(debugVar);
|
||||
|
||||
if(DebugMode)
|
||||
|
||||
if (DebugMode)
|
||||
Logger.Debug("Debug mode enabled");
|
||||
}
|
||||
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
Logger.Info($"Reading config from '{PathBuilder.File("storage", "configs", "config.json")}'");
|
||||
|
||||
Configuration = new ConfigurationBuilder().AddJsonStream(
|
||||
new MemoryStream(Encoding.ASCII.GetBytes(
|
||||
File.ReadAllText(
|
||||
PathBuilder.File("storage", "configs", "config.json")
|
||||
)
|
||||
)
|
||||
)).Build();
|
||||
|
||||
Logger.Info("Reloaded configuration file");
|
||||
}
|
||||
|
||||
public IEnumerable<IConfigurationSection> GetChildren()
|
||||
{
|
||||
return Configuration.GetChildren();
|
||||
|
||||
58
Moonlight/App/Services/StorageService.cs
Normal file
58
Moonlight/App/Services/StorageService.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Logging.Net;
|
||||
using Moonlight.App.Helpers;
|
||||
|
||||
namespace Moonlight.App.Services;
|
||||
|
||||
public class StorageService
|
||||
{
|
||||
public StorageService()
|
||||
{
|
||||
EnsureCreated();
|
||||
}
|
||||
|
||||
public void EnsureCreated()
|
||||
{
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "uploads"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "configs"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "resources"));
|
||||
|
||||
if(IsEmpty(PathBuilder.Dir("storage", "resources")))
|
||||
{
|
||||
Logger.Info("Default resources not found. Copying default resources");
|
||||
|
||||
CopyFilesRecursively(
|
||||
PathBuilder.Dir("defaultstorage", "resources"),
|
||||
PathBuilder.Dir("storage", "resources")
|
||||
);
|
||||
}
|
||||
|
||||
if (IsEmpty(PathBuilder.Dir("storage", "configs")))
|
||||
{
|
||||
Logger.Info("Default configs not found. Copying default configs");
|
||||
|
||||
CopyFilesRecursively(
|
||||
PathBuilder.Dir("defaultstorage", "configs"),
|
||||
PathBuilder.Dir("storage", "configs")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsEmpty(string path)
|
||||
{
|
||||
return !Directory.EnumerateFiles(path).Any();
|
||||
}
|
||||
private static void CopyFilesRecursively(string sourcePath, string targetPath)
|
||||
{
|
||||
//Now Create all of the directories
|
||||
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
|
||||
}
|
||||
|
||||
//Copy all the files & Replaces any files with the same name
|
||||
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories))
|
||||
{
|
||||
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user