From c871de424d5e2304a909a5b818977952257a6e4b Mon Sep 17 00:00:00 2001 From: Roman Buldygin Date: Mon, 18 Nov 2024 01:44:32 +0300 Subject: [PATCH] Search path --- .../Configuration/ServiceConfiguration.cs | 2 +- src/OpenHdWebUi.Server/Program.cs | 21 +++++++------- .../Services/Media/MediaConsts.cs | 10 ------- .../Services/Media/MediaService.cs | 29 +++++++++++++++++-- .../appsettings.Development.json | 6 +++- src/OpenHdWebUi.Server/appsettings.json | 5 +++- 6 files changed, 48 insertions(+), 25 deletions(-) delete mode 100644 src/OpenHdWebUi.Server/Services/Media/MediaConsts.cs diff --git a/src/OpenHdWebUi.Server/Configuration/ServiceConfiguration.cs b/src/OpenHdWebUi.Server/Configuration/ServiceConfiguration.cs index a917741..e3cb029 100644 --- a/src/OpenHdWebUi.Server/Configuration/ServiceConfiguration.cs +++ b/src/OpenHdWebUi.Server/Configuration/ServiceConfiguration.cs @@ -7,7 +7,7 @@ namespace OpenHdWebUi.Server.Configuration; [UsedImplicitly] public class ServiceConfiguration { - public string FilesFolder { get; set; } + public List FilesFolders { get; set; } public List SystemCommands { get; set; } diff --git a/src/OpenHdWebUi.Server/Program.cs b/src/OpenHdWebUi.Server/Program.cs index 7924c5d..6c46afd 100644 --- a/src/OpenHdWebUi.Server/Program.cs +++ b/src/OpenHdWebUi.Server/Program.cs @@ -44,17 +44,18 @@ public static async Task Main(string[] args) app.UseDefaultFiles(); app.MapStaticAssets(); - var config = app.Services.GetRequiredService>().Value; - var absoluteMediaPath = Path.GetFullPath(config.FilesFolder); - FileSystemHelpers.EnsureFolderCreated(absoluteMediaPath); - - app.UseStaticFiles(new StaticFileOptions + var mediaService = app.Services.GetRequiredService(); + var absoluteMediaPath = mediaService.MediaDirectoryFullPath; + if (absoluteMediaPath != null) { - FileProvider = new PhysicalFileProvider(absoluteMediaPath), - RequestPath = "/media", - ServeUnknownFileTypes = true, - ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary{{".mkv", "video/x-matroska" } }) - }); + app.UseStaticFiles(new StaticFileOptions + { + FileProvider = new PhysicalFileProvider(absoluteMediaPath), + RequestPath = "/media", + ServeUnknownFileTypes = true, + ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary { { ".mkv", "video/x-matroska" } }) + }); + } //app.UseRouting(); app.MapControllers(); diff --git a/src/OpenHdWebUi.Server/Services/Media/MediaConsts.cs b/src/OpenHdWebUi.Server/Services/Media/MediaConsts.cs deleted file mode 100644 index def8559..0000000 --- a/src/OpenHdWebUi.Server/Services/Media/MediaConsts.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace OpenHdWebUi.Server.Services.Media; - -public static class MediaConsts -{ - private static readonly string PreviewsString = "previews"; - - public static string PreviewsFsPath => Path.GetFullPath($"{PreviewsString}/"); - - public static string PreviewsWebPath => $"{PreviewsString}"; -} \ No newline at end of file diff --git a/src/OpenHdWebUi.Server/Services/Media/MediaService.cs b/src/OpenHdWebUi.Server/Services/Media/MediaService.cs index 970ba7d..d771b06 100644 --- a/src/OpenHdWebUi.Server/Services/Media/MediaService.cs +++ b/src/OpenHdWebUi.Server/Services/Media/MediaService.cs @@ -12,13 +12,18 @@ public MediaService( ILogger logger) { _logger = logger; - MediaDirectoryFullPath = Path.GetFullPath(configuration.Value.FilesFolder); + MediaDirectoryFullPath = GetVideoFolderPath(configuration.Value.FilesFolders); } - public string MediaDirectoryFullPath { get; } + public string? MediaDirectoryFullPath { get; } public string[] GetMediaFilesPaths() { + if (MediaDirectoryFullPath == null) + { + return []; + } + return Directory.GetFiles(MediaDirectoryFullPath, "*.mkv") .Concat(Directory.GetFiles(MediaDirectoryFullPath, "*.mp4")) .Concat(Directory.GetFiles(MediaDirectoryFullPath, "*.avi")) @@ -27,10 +32,30 @@ public string[] GetMediaFilesPaths() public void DeleteFile(string fileName) { + _logger.LogInformation("Deleting file {FileName}", fileName); + if (MediaDirectoryFullPath == null) + { + return; + } + var fullFilePath = Path.Combine(MediaDirectoryFullPath, fileName); if (Path.Exists(fullFilePath)) { File.Delete(fullFilePath); } } + + private static string? GetVideoFolderPath(List configFilesFolder) + { + foreach (var path in configFilesFolder) + { + var fullPath = Path.GetFullPath(path); + if (Directory.Exists(fullPath)) + { + return fullPath; + } + } + + return null; + } } \ No newline at end of file diff --git a/src/OpenHdWebUi.Server/appsettings.Development.json b/src/OpenHdWebUi.Server/appsettings.Development.json index 663d4de..a15512f 100644 --- a/src/OpenHdWebUi.Server/appsettings.Development.json +++ b/src/OpenHdWebUi.Server/appsettings.Development.json @@ -7,5 +7,9 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "FilesFolder": "C:\\testData\\media" + "FilesFolders": [ + "/home/openhd/Videos/", + "/Videos", + "C:\\testData\\media" + ] } diff --git a/src/OpenHdWebUi.Server/appsettings.json b/src/OpenHdWebUi.Server/appsettings.json index c8123cd..988a59f 100644 --- a/src/OpenHdWebUi.Server/appsettings.json +++ b/src/OpenHdWebUi.Server/appsettings.json @@ -7,7 +7,10 @@ } }, "AllowedHosts": "*", - "FilesFolder": "/home/openhd/Videos/", + "FilesFolders": [ + "/home/openhd/Videos/", + "/Videos" + ], "SystemCommands": [ { "Id": "sys-res-opendh",