Cleanup extracted files (#13760)

* Cleanup extracted files

* Pagination and fixes

* Add migration for attachments to MigrateLibraryDb

* Unify attachment handling

* Don't extract again if files were already extracted

* Fix MKS attachment extraction

* Always run full extraction on mks

* Don't try to extract mjpeg streams as attachments

* Fallback to check if attachments were extracted to cache folder

* Fixup
This commit is contained in:
Tim Eisele
2025-04-03 17:17:14 +02:00
committed by GitHub
parent 0bde7bae05
commit 596b635511
17 changed files with 2397 additions and 416 deletions

View File

@@ -1,14 +1,14 @@
#pragma warning disable CS1591
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Trickplay;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@@ -19,15 +19,18 @@ public class CleanDatabaseScheduledTask : ILibraryPostScanTask
private readonly ILibraryManager _libraryManager;
private readonly ILogger<CleanDatabaseScheduledTask> _logger;
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
private readonly IPathManager _pathManager;
public CleanDatabaseScheduledTask(
ILibraryManager libraryManager,
ILogger<CleanDatabaseScheduledTask> logger,
IDbContextFactory<JellyfinDbContext> dbProvider)
IDbContextFactory<JellyfinDbContext> dbProvider,
IPathManager pathManager)
{
_libraryManager = libraryManager;
_logger = logger;
_dbProvider = dbProvider;
_pathManager = pathManager;
}
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
@@ -56,6 +59,38 @@ public class CleanDatabaseScheduledTask : ILibraryPostScanTask
{
_logger.LogInformation("Cleaning item {Item} type: {Type} path: {Path}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
foreach (var mediaSource in item.GetMediaSources(false))
{
// Delete extracted subtitles
try
{
var subtitleFolder = _pathManager.GetSubtitleFolderPath(mediaSource.Id);
if (Directory.Exists(subtitleFolder))
{
Directory.Delete(subtitleFolder, true);
}
}
catch (Exception e)
{
_logger.LogWarning("Failed to remove subtitle cache folder for {Item}: {Exception}", item.Id, e.Message);
}
// Delete extracted attachments
try
{
var attachmentFolder = _pathManager.GetAttachmentFolderPath(mediaSource.Id);
if (Directory.Exists(attachmentFolder))
{
Directory.Delete(attachmentFolder, true);
}
}
catch (Exception e)
{
_logger.LogWarning("Failed to remove attachment cache folder for {Item}: {Exception}", item.Id, e.Message);
}
}
// Delete item
_libraryManager.DeleteItem(item, new DeleteOptions
{
DeleteFileLocation = false

View File

@@ -492,7 +492,24 @@ namespace Emby.Server.Implementations.Library
if (item is Video video)
{
// Trickplay
list.Add(_pathManager.GetTrickplayDirectory(video));
// Subtitles and attachments
foreach (var mediaSource in item.GetMediaSources(false))
{
var subtitleFolder = _pathManager.GetSubtitleFolderPath(mediaSource.Id);
if (subtitleFolder is not null)
{
list.Add(subtitleFolder);
}
var attachmentFolder = _pathManager.GetAttachmentFolderPath(mediaSource.Id);
if (attachmentFolder is not null)
{
list.Add(attachmentFolder);
}
}
}
return list;

View File

@@ -1,5 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
@@ -12,22 +14,56 @@ namespace Emby.Server.Implementations.Library;
public class PathManager : IPathManager
{
private readonly IServerConfigurationManager _config;
private readonly IApplicationPaths _appPaths;
/// <summary>
/// Initializes a new instance of the <see cref="PathManager"/> class.
/// </summary>
/// <param name="config">The server configuration manager.</param>
/// <param name="appPaths">The application paths.</param>
public PathManager(
IServerConfigurationManager config)
IServerConfigurationManager config,
IApplicationPaths appPaths)
{
_config = config;
_appPaths = appPaths;
}
private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments");
/// <inheritdoc />
public string GetAttachmentPath(string mediaSourceId, string fileName)
{
return Path.Join(GetAttachmentFolderPath(mediaSourceId), fileName);
}
/// <inheritdoc />
public string GetAttachmentFolderPath(string mediaSourceId)
{
var id = Guid.Parse(mediaSourceId);
return Path.Join(AttachmentCachePath, id.ToString("D", CultureInfo.InvariantCulture));
}
/// <inheritdoc />
public string GetSubtitleFolderPath(string mediaSourceId)
{
var id = Guid.Parse(mediaSourceId);
return Path.Join(SubtitleCachePath, id.ToString("D", CultureInfo.InvariantCulture));
}
/// <inheritdoc />
public string GetSubtitlePath(string mediaSourceId, int streamIndex, string extension)
{
return Path.Join(GetSubtitleFolderPath(mediaSourceId), streamIndex.ToString(CultureInfo.InvariantCulture) + extension);
}
/// <inheritdoc />
public string GetTrickplayDirectory(BaseItem item, bool saveWithMedia = false)
{
var basePath = _config.ApplicationPaths.TrickplayPath;
var idString = item.Id.ToString("N", CultureInfo.InvariantCulture);
var idString = item.Id.ToString("D", CultureInfo.InvariantCulture);
return saveWithMedia
? Path.Combine(item.ContainingFolderPath, Path.ChangeExtension(item.Path, ".trickplay"))