Skip to content

Commit

Permalink
Search path
Browse files Browse the repository at this point in the history
  • Loading branch information
buldo committed Nov 17, 2024
1 parent d96679f commit c871de4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OpenHdWebUi.Server.Configuration;
[UsedImplicitly]
public class ServiceConfiguration
{
public string FilesFolder { get; set; }
public List<string> FilesFolders { get; set; }

public List<SystemCommandConfiguration> SystemCommands { get; set; }

Expand Down
21 changes: 11 additions & 10 deletions src/OpenHdWebUi.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ public static async Task Main(string[] args)
app.UseDefaultFiles();
app.MapStaticAssets();

var config = app.Services.GetRequiredService<IOptions<ServiceConfiguration>>().Value;
var absoluteMediaPath = Path.GetFullPath(config.FilesFolder);
FileSystemHelpers.EnsureFolderCreated(absoluteMediaPath);

app.UseStaticFiles(new StaticFileOptions
var mediaService = app.Services.GetRequiredService<MediaService>();
var absoluteMediaPath = mediaService.MediaDirectoryFullPath;
if (absoluteMediaPath != null)
{
FileProvider = new PhysicalFileProvider(absoluteMediaPath),
RequestPath = "/media",
ServeUnknownFileTypes = true,
ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>{{".mkv", "video/x-matroska" } })
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(absoluteMediaPath),
RequestPath = "/media",
ServeUnknownFileTypes = true,
ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string> { { ".mkv", "video/x-matroska" } })
});
}

//app.UseRouting();
app.MapControllers();
Expand Down
10 changes: 0 additions & 10 deletions src/OpenHdWebUi.Server/Services/Media/MediaConsts.cs

This file was deleted.

29 changes: 27 additions & 2 deletions src/OpenHdWebUi.Server/Services/Media/MediaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ public MediaService(
ILogger<MediaService> 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"))
Expand All @@ -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<string> configFilesFolder)
{
foreach (var path in configFilesFolder)
{
var fullPath = Path.GetFullPath(path);
if (Directory.Exists(fullPath))
{
return fullPath;
}
}

return null;
}
}
6 changes: 5 additions & 1 deletion src/OpenHdWebUi.Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"FilesFolder": "C:\\testData\\media"
"FilesFolders": [
"/home/openhd/Videos/",
"/Videos",
"C:\\testData\\media"
]
}
5 changes: 4 additions & 1 deletion src/OpenHdWebUi.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
}
},
"AllowedHosts": "*",
"FilesFolder": "/home/openhd/Videos/",
"FilesFolders": [
"/home/openhd/Videos/",
"/Videos"
],
"SystemCommands": [
{
"Id": "sys-res-opendh",
Expand Down

0 comments on commit c871de4

Please sign in to comment.