diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-08-15 15:49:14 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2025-08-18 10:59:32 +1000 |
| commit | 38c60cee378b9c2ad42fc9dc79bc492b919a68f5 (patch) | |
| tree | 6b62f84aab4b7866432e5da8ae8fcb889795d58b /Services/ConfigService.cs | |
| parent | 07a4c7ead01514bd3f304f00abc38140a1d73634 (diff) | |
Convert Razor pages to Blazor
Diffstat (limited to 'Services/ConfigService.cs')
| -rw-r--r-- | Services/ConfigService.cs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Services/ConfigService.cs b/Services/ConfigService.cs new file mode 100644 index 0000000..6ba3936 --- /dev/null +++ b/Services/ConfigService.cs @@ -0,0 +1,81 @@ +namespace HyperBooru.Services; + +public interface IConfigService { + public string DataPath { get; } + public string DbPath { get; } + public string MediaBasePath { get; } + + public string GetPath(Media media); + public string GetPath(Media media, int width, int height); +} + +public class ConfigService : IConfigService { + private IConfiguration config; + + private const string AppName = "HyperBooru"; + + public string DataPath { + get { + #if DEBUG + return "Data"; + #else + string? path = config["DataPath"]; + if(path is not null) + return path; + + switch(Environment.OSVersion.Platform) { + case PlatformID.Win32NT: + return Path.Join( + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + AppName); + case PlatformID.Unix: + return $"/var/lib/{AppName.ToLower()}"; + default: + throw new NotImplementedException( + $"Unknown Operating System: {Environment.OSVersion.Platform}"); + } + #endif + } + } + + public string DbPath => + Path.Join(DataPath, $"{AppName}.db"); + + public string MediaBasePath => + Path.Join(DataPath, "media"); + + public string ThumbnailBasePath => + Path.Join(DataPath, "thumb"); + + public ConfigService(IConfiguration config) { + this.config = config; + InitDirectoryStructure(); + } + + public string GetPath(Media media) { + var fileInfo = new FileInfo(Path.Join( + MediaBasePath, + media.Guid.ToString().Substring(0, 2), + media.Guid.ToString().Substring(2, 2), + media.Guid.ToString())); + + Directory.CreateDirectory(fileInfo.Directory.FullName); + return fileInfo.FullName; + } + + public string GetPath(Media media, int width, int height) { + var fileInfo = new FileInfo(Path.Join( + ThumbnailBasePath, + media.Guid.ToString().Substring(0, 2), + media.Guid.ToString().Substring(2, 2), + $"{media.Guid.ToString()}-{width}-{height}")); + + Directory.CreateDirectory(fileInfo.Directory.FullName); + return fileInfo.FullName; + } + + private void InitDirectoryStructure() { + Directory.CreateDirectory(DataPath); + Directory.CreateDirectory(MediaBasePath); + } +}
\ No newline at end of file |
