Sort files by path and add logging

This commit is contained in:
Mike Heier 2024-09-08 14:08:55 -04:00
parent 34c63a4c15
commit 96b133b7b4
4 changed files with 130 additions and 3 deletions

View File

@ -119,5 +119,56 @@ namespace Emby.Server.Implementations.Configuration
EnsureWriteAccess(newPath); EnsureWriteAccess(newPath);
} }
} }
/// <summary>
/// Logs a message.
/// </summary>
/// <param name="message">The message to log.</param>
public void LogMessage(string message)
{
Logger.LogInformation("(MJH) {Message}", message);
}
private bool IsSubPath(string basePath, string testPath)
{
string normalizedBasePath = Path.GetFullPath(basePath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string normalizedTestPath = Path.GetFullPath(testPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var isSubPath = normalizedTestPath.StartsWith(normalizedBasePath, StringComparison.OrdinalIgnoreCase)
&& normalizedTestPath.Length > normalizedBasePath.Length
&& (normalizedTestPath[normalizedBasePath.Length] == Path.DirectorySeparatorChar
|| normalizedTestPath[normalizedBasePath.Length] == Path.AltDirectorySeparatorChar);
return isSubPath;
}
/// <summary>
/// Fetches the folder priority.
/// </summary>
/// <param name="path">The path to test.</param>
/// <returns>The priority int value (higher comes first).</returns>
public int FetchFolderPriority(string path)
{
if (this.IsSubPath("/mnt/media-pool", path))
{
return 4;
}
if (this.IsSubPath("/mnt/lee/laptop-new", path))
{
return 3;
}
if (this.IsSubPath("/mnt/lee/laptop-seagate", path))
{
return 2;
}
if (this.IsSubPath("/mnt/lee/pi-elements", path))
{
return 1;
}
return 0;
}
} }
} }

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
@ -173,6 +174,19 @@ public class VideosController : BaseJellyfinApiController
return NoContent(); return NoContent();
} }
private bool IsSubPath(string basePath, string testPath)
{
string normalizedBasePath = Path.GetFullPath(basePath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string normalizedTestPath = Path.GetFullPath(testPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var isSubPath = normalizedTestPath.StartsWith(normalizedBasePath, StringComparison.OrdinalIgnoreCase)
&& normalizedTestPath.Length > normalizedBasePath.Length
&& (normalizedTestPath[normalizedBasePath.Length] == Path.DirectorySeparatorChar
|| normalizedTestPath[normalizedBasePath.Length] == Path.AltDirectorySeparatorChar);
return isSubPath;
}
/// <summary> /// <summary>
/// Merges videos into a single record. /// Merges videos into a single record.
/// </summary> /// </summary>
@ -198,11 +212,21 @@ public class VideosController : BaseJellyfinApiController
return BadRequest("Please supply at least two videos to merge."); return BadRequest("Please supply at least two videos to merge.");
} }
var primaryVersion = items.FirstOrDefault(i => i.MediaSourceCount > 1 && string.IsNullOrEmpty(i.PrimaryVersionId)); var primaryVersion = items.FirstOrDefault(i =>
{
if (i.GetMediaSourceCount((message) => _serverConfigurationManager.LogMessage(message)) > 1 && string.IsNullOrEmpty(i.PrimaryVersionId))
{
return true;
}
return false;
});
if (primaryVersion is null) if (primaryVersion is null)
{ {
primaryVersion = items primaryVersion = items
.OrderBy(i => .OrderBy(i => _serverConfigurationManager.FetchFolderPriority(i.GetContainingFolderPath()))
.ThenBy(i =>
{ {
if (i.Video3DFormat.HasValue || i.VideoType != VideoType.VideoFile) if (i.Video3DFormat.HasValue || i.VideoType != VideoType.VideoFile)
{ {

View File

@ -19,5 +19,18 @@ namespace MediaBrowser.Controller.Configuration
/// </summary> /// </summary>
/// <value>The configuration.</value> /// <value>The configuration.</value>
ServerConfiguration Configuration { get; } ServerConfiguration Configuration { get; }
/// <summary>
/// Fetches the folder priority.
/// </summary>
/// <param name="path">The path to test.</param>
/// <returns>The priority int value (higher comes first).</returns>
int FetchFolderPriority(string path);
/// <summary>
/// Logs a message.
/// </summary>
/// <param name="message">The message to log.</param>
void LogMessage(string message);
} }
} }

View File

@ -155,7 +155,7 @@ namespace MediaBrowser.Controller.Entities
if (!string.IsNullOrEmpty(PrimaryVersionId)) if (!string.IsNullOrEmpty(PrimaryVersionId))
{ {
var item = LibraryManager.GetItemById(PrimaryVersionId); var item = LibraryManager.GetItemById(PrimaryVersionId);
if (item is Video video && video != this) if (item is Video video)
{ {
return video.MediaSourceCount; return video.MediaSourceCount;
} }
@ -550,5 +550,44 @@ namespace MediaBrowser.Controller.Entities
return list; return list;
} }
/// <summary> Gets hte media source count.</summary>
/// <param name="logMessage">Logs a message.</param>
/// <returns>The mediasourcecount.</returns>
public int GetMediaSourceCount(Action<string> logMessage)
{
if (!string.IsNullOrEmpty(PrimaryVersionId))
{
logMessage($"Got a PrimaryVersionId: {PrimaryVersionId}");
var item = LibraryManager.GetItemById(PrimaryVersionId);
if (item is Video video)
{
logMessage($"Entered the recursion!!!");
return video.MediaSourceCount;
}
}
return LinkedAlternateVersions.Length + LocalAlternateVersions.Length + 1;
}
/// <summary> Gets the containing folder path.</summary>
/// <returns>The path to the containing folder path.</returns>
public string GetContainingFolderPath()
{
if (IsStacked)
{
return System.IO.Path.GetDirectoryName(Path);
}
if (!IsPlaceHolder)
{
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
{
return Path;
}
}
return base.ContainingFolderPath;
}
} }
} }