Merge 5a10d3839b
into 086fbd49cf
commit
89e5901cec
@ -0,0 +1,268 @@
|
||||
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Jellyfin.Data.Enums;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.IO;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.MediaInfo;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations.Routines;
|
||||
|
||||
/// <summary>
|
||||
/// Migration to move extracted files to the new directories.
|
||||
/// </summary>
|
||||
public class MoveExtractedFiles : IDatabaseMigrationRoutine
|
||||
{
|
||||
private readonly IApplicationPaths _appPaths;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly ILogger<MoveExtractedFiles> _logger;
|
||||
private readonly IMediaSourceManager _mediaSourceManager;
|
||||
private readonly IPathManager _pathManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MoveExtractedFiles"/> class.
|
||||
/// </summary>
|
||||
/// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
|
||||
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
|
||||
/// <param name="pathManager">Instance of the <see cref="IPathManager"/> interface.</param>
|
||||
public MoveExtractedFiles(
|
||||
IApplicationPaths appPaths,
|
||||
ILibraryManager libraryManager,
|
||||
ILogger<MoveExtractedFiles> logger,
|
||||
IMediaSourceManager mediaSourceManager,
|
||||
IPathManager pathManager)
|
||||
{
|
||||
_appPaths = appPaths;
|
||||
_libraryManager = libraryManager;
|
||||
_logger = logger;
|
||||
_mediaSourceManager = mediaSourceManager;
|
||||
_pathManager = pathManager;
|
||||
}
|
||||
|
||||
private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
|
||||
|
||||
private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments");
|
||||
|
||||
/// <inheritdoc />
|
||||
public Guid Id => new("9063b0Ef-CFF1-4EDC-9A13-74093681A89B");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => "MoveExtractedFiles";
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool PerformOnNewInstall => false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Perform()
|
||||
{
|
||||
const int Limit = 500;
|
||||
int itemCount = 0, offset = 0;
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
var itemsQuery = new InternalItemsQuery
|
||||
{
|
||||
MediaTypes = [MediaType.Video],
|
||||
SourceTypes = [SourceType.Library],
|
||||
IsVirtualItem = false,
|
||||
IsFolder = false,
|
||||
Limit = Limit,
|
||||
StartIndex = offset,
|
||||
EnableTotalRecordCount = true,
|
||||
};
|
||||
|
||||
var records = _libraryManager.GetItemsResult(itemsQuery).TotalRecordCount;
|
||||
_logger.LogInformation("Checking {Count} items for movable extracted files.", records);
|
||||
|
||||
// Make sure directories exist
|
||||
Directory.CreateDirectory(SubtitleCachePath);
|
||||
Directory.CreateDirectory(AttachmentCachePath);
|
||||
|
||||
itemsQuery.EnableTotalRecordCount = false;
|
||||
do
|
||||
{
|
||||
itemsQuery.StartIndex = offset;
|
||||
var result = _libraryManager.GetItemsResult(itemsQuery);
|
||||
|
||||
var items = result.Items;
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (MoveSubtitleAndAttachmentFiles(item))
|
||||
{
|
||||
itemCount++;
|
||||
}
|
||||
}
|
||||
|
||||
offset += Limit;
|
||||
if (offset % 5_000 == 0)
|
||||
{
|
||||
_logger.LogInformation("Checked extracted files for {Count} items in {Time}.", offset, sw.Elapsed);
|
||||
}
|
||||
} while (offset < records);
|
||||
|
||||
_logger.LogInformation("Checked {Checked} items - Moved files for {Items} items in {Time}.", records, itemCount, sw.Elapsed);
|
||||
|
||||
// Get all subdirectories with 1 character names (those are the legacy directories)
|
||||
var subdirectories = Directory.GetDirectories(SubtitleCachePath, "*", SearchOption.AllDirectories).Where(s => s.Length == SubtitleCachePath.Length + 2).ToList();
|
||||
subdirectories.AddRange(Directory.GetDirectories(AttachmentCachePath, "*", SearchOption.AllDirectories).Where(s => s.Length == AttachmentCachePath.Length + 2));
|
||||
|
||||
// Remove all legacy subdirectories
|
||||
foreach (var subdir in subdirectories)
|
||||
{
|
||||
Directory.Delete(subdir, true);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Cleaned up left over subtitles and attachments.");
|
||||
}
|
||||
|
||||
private bool MoveSubtitleAndAttachmentFiles(BaseItem item)
|
||||
{
|
||||
var mediaStreams = item.GetMediaStreams().Where(s => s.Type == MediaStreamType.Subtitle && !s.IsExternal);
|
||||
var modified = false;
|
||||
foreach (var mediaStream in mediaStreams)
|
||||
{
|
||||
if (mediaStream.Codec is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var mediaStreamIndex = mediaStream.Index;
|
||||
var extension = GetSubtitleExtension(mediaStream.Codec);
|
||||
var oldSubtitleCachePath = GetOldSubtitleCachePath(item.Path, mediaStream.Index, extension);
|
||||
if (string.IsNullOrEmpty(oldSubtitleCachePath) || !File.Exists(oldSubtitleCachePath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var newSubtitleCachePath = _pathManager.GetSubtitlePath(item.Id.ToString("N", CultureInfo.InvariantCulture), mediaStreamIndex, extension);
|
||||
if (File.Exists(newSubtitleCachePath))
|
||||
{
|
||||
File.Delete(oldSubtitleCachePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
var newDirectory = Path.GetDirectoryName(newSubtitleCachePath);
|
||||
if (newDirectory is not null)
|
||||
{
|
||||
Directory.CreateDirectory(newDirectory);
|
||||
File.Move(oldSubtitleCachePath, newSubtitleCachePath, false);
|
||||
_logger.LogDebug("Moved subtitle {Index} for {Item} from {Source} to {Destination}", mediaStreamIndex, item.Id, oldSubtitleCachePath, newSubtitleCachePath);
|
||||
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var attachment in _mediaSourceManager.GetMediaAttachments(item.Id))
|
||||
{
|
||||
var attachmentIndex = attachment.Index;
|
||||
var oldAttachmentCachePath = GetOldAttachmentCachePath(item.Path, attachmentIndex);
|
||||
if (string.IsNullOrEmpty(oldAttachmentCachePath) || !File.Exists(oldAttachmentCachePath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var newAttachmentCachePath = _pathManager.GetAttachmentPath(item.Id.ToString("N", CultureInfo.InvariantCulture), attachmentIndex);
|
||||
var newDirectory = Path.GetDirectoryName(newAttachmentCachePath);
|
||||
if (File.Exists(newAttachmentCachePath))
|
||||
{
|
||||
File.Delete(oldAttachmentCachePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (newDirectory is not null)
|
||||
{
|
||||
Directory.CreateDirectory(newDirectory);
|
||||
File.Move(oldAttachmentCachePath, newAttachmentCachePath, false);
|
||||
_logger.LogDebug("Moved attachment {Index} for {Item} from {Source} to {Destination}", attachmentIndex, item.Id, oldAttachmentCachePath, newAttachmentCachePath);
|
||||
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
private string? GetOldAttachmentCachePath(string? mediaPath, int attachmentStreamIndex)
|
||||
{
|
||||
if (mediaPath is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string filename;
|
||||
var protocol = _mediaSourceManager.GetPathProtocol(mediaPath);
|
||||
if (protocol == MediaProtocol.File)
|
||||
{
|
||||
DateTime? date;
|
||||
try
|
||||
{
|
||||
date = File.GetLastWriteTimeUtc(mediaPath);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_logger.LogDebug("Skipping attachment at index {Index} for {Path}: {Exception}", attachmentStreamIndex, mediaPath, e.Message);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Value.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D", CultureInfo.InvariantCulture);
|
||||
}
|
||||
else
|
||||
{
|
||||
filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return Path.Join(_appPaths.DataPath, "attachments", filename[..1], filename);
|
||||
}
|
||||
|
||||
private string? GetOldSubtitleCachePath(string path, int streamIndex, string outputSubtitleExtension)
|
||||
{
|
||||
DateTime? date;
|
||||
try
|
||||
{
|
||||
date = File.GetLastWriteTimeUtc(path);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_logger.LogDebug("Skipping subtitle at index {Index} for {Path}: {Exception}", streamIndex, path, e.Message);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
var ticksParam = string.Empty;
|
||||
ReadOnlySpan<char> filename = new Guid(MD5.HashData(Encoding.Unicode.GetBytes(path + "_" + streamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Value.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam))) + outputSubtitleExtension;
|
||||
|
||||
return Path.Join(SubtitleCachePath, filename[..1], filename);
|
||||
}
|
||||
|
||||
private static string GetSubtitleExtension(string codec)
|
||||
{
|
||||
if (codec.ToLower(CultureInfo.InvariantCulture).Equals("ass", StringComparison.OrdinalIgnoreCase)
|
||||
|| codec.ToLower(CultureInfo.InvariantCulture).Equals("ssa", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "." + codec;
|
||||
}
|
||||
else if (codec.Contains("pgs", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ".sup";
|
||||
}
|
||||
else
|
||||
{
|
||||
return ".srt";
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +1,49 @@
|
||||
#nullable disable
|
||||
namespace MediaBrowser.Model.Entities
|
||||
namespace MediaBrowser.Model.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Class MediaAttachment.
|
||||
/// </summary>
|
||||
public class MediaAttachment
|
||||
{
|
||||
/// <summary>
|
||||
/// Class MediaAttachment.
|
||||
/// Gets or sets the codec.
|
||||
/// </summary>
|
||||
public class MediaAttachment
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the codec.
|
||||
/// </summary>
|
||||
/// <value>The codec.</value>
|
||||
public string Codec { get; set; }
|
||||
/// <value>The codec.</value>
|
||||
public string? Codec { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the codec tag.
|
||||
/// </summary>
|
||||
/// <value>The codec tag.</value>
|
||||
public string CodecTag { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the codec tag.
|
||||
/// </summary>
|
||||
/// <value>The codec tag.</value>
|
||||
public string? CodecTag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the comment.
|
||||
/// </summary>
|
||||
/// <value>The comment.</value>
|
||||
public string Comment { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the comment.
|
||||
/// </summary>
|
||||
/// <value>The comment.</value>
|
||||
public string? Comment { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the index.
|
||||
/// </summary>
|
||||
/// <value>The index.</value>
|
||||
public int Index { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the index.
|
||||
/// </summary>
|
||||
/// <value>The index.</value>
|
||||
public int Index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the filename.
|
||||
/// </summary>
|
||||
/// <value>The filename.</value>
|
||||
public string FileName { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the filename.
|
||||
/// </summary>
|
||||
/// <value>The filename.</value>
|
||||
public string? FileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the MIME type.
|
||||
/// </summary>
|
||||
/// <value>The MIME type.</value>
|
||||
public string MimeType { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the MIME type.
|
||||
/// </summary>
|
||||
/// <value>The MIME type.</value>
|
||||
public string? MimeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delivery URL.
|
||||
/// </summary>
|
||||
/// <value>The delivery URL.</value>
|
||||
public string DeliveryUrl { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets or sets the delivery URL.
|
||||
/// </summary>
|
||||
/// <value>The delivery URL.</value>
|
||||
public string? DeliveryUrl { get; set; }
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue