repo creation with initial code after cloning public repo
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
#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.Entities;
|
||||
using MediaBrowser.Controller.IO;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Emby.Server.Implementations.Data;
|
||||
|
||||
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,
|
||||
IPathManager pathManager)
|
||||
{
|
||||
_libraryManager = libraryManager;
|
||||
_logger = logger;
|
||||
_dbProvider = dbProvider;
|
||||
_pathManager = pathManager;
|
||||
}
|
||||
|
||||
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
await CleanDeadItems(cancellationToken, progress).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
|
||||
{
|
||||
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
||||
{
|
||||
HasDeadParentId = true
|
||||
});
|
||||
|
||||
var numComplete = 0;
|
||||
var numItems = itemIds.Count + 1;
|
||||
|
||||
_logger.LogDebug("Cleaning {Number} items with dead parents", numItems);
|
||||
|
||||
IProgress<double> subProgress = new Progress<double>((val) => progress.Report(val / 2));
|
||||
|
||||
foreach (var itemId in itemIds)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var item = _libraryManager.GetItemById(itemId);
|
||||
if (item is not null)
|
||||
{
|
||||
_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 data
|
||||
var mediaSourceItem = _libraryManager.GetItemById(mediaSource.Id);
|
||||
if (mediaSourceItem is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var extractedDataFolders = _pathManager.GetExtractedDataPaths(mediaSourceItem);
|
||||
foreach (var folder in extractedDataFolders)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(folder, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning("Failed to remove {Folder}: {Exception}", folder, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete item
|
||||
_libraryManager.DeleteItem(item, new DeleteOptions
|
||||
{
|
||||
DeleteFileLocation = false
|
||||
});
|
||||
}
|
||||
|
||||
numComplete++;
|
||||
double percent = numComplete;
|
||||
percent /= numItems;
|
||||
subProgress.Report(percent * 100);
|
||||
}
|
||||
|
||||
subProgress = new Progress<double>((val) => progress.Report((val / 2) + 50));
|
||||
var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (context.ConfigureAwait(false))
|
||||
{
|
||||
var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (transaction.ConfigureAwait(false))
|
||||
{
|
||||
await context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
|
||||
subProgress.Report(50);
|
||||
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
subProgress.Report(100);
|
||||
}
|
||||
}
|
||||
|
||||
progress.Report(100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Channels;
|
||||
using Emby.Server.Implementations.Playlists;
|
||||
using Jellyfin.Data.Enums;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Entities.TV;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Controller.Playlists;
|
||||
|
||||
namespace Emby.Server.Implementations.Data;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class ItemTypeLookup : IItemTypeLookup
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<string> MusicGenreTypes { get; } = [
|
||||
typeof(Audio).FullName!,
|
||||
typeof(MusicVideo).FullName!,
|
||||
typeof(MusicAlbum).FullName!,
|
||||
typeof(MusicArtist).FullName!,
|
||||
];
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyDictionary<BaseItemKind, string> BaseItemKindNames { get; } = new Dictionary<BaseItemKind, string>()
|
||||
{
|
||||
{ BaseItemKind.AggregateFolder, typeof(AggregateFolder).FullName! },
|
||||
{ BaseItemKind.Audio, typeof(Audio).FullName! },
|
||||
{ BaseItemKind.AudioBook, typeof(AudioBook).FullName! },
|
||||
{ BaseItemKind.BasePluginFolder, typeof(BasePluginFolder).FullName! },
|
||||
{ BaseItemKind.Book, typeof(Book).FullName! },
|
||||
{ BaseItemKind.BoxSet, typeof(BoxSet).FullName! },
|
||||
{ BaseItemKind.Channel, typeof(Channel).FullName! },
|
||||
{ BaseItemKind.CollectionFolder, typeof(CollectionFolder).FullName! },
|
||||
{ BaseItemKind.Episode, typeof(Episode).FullName! },
|
||||
{ BaseItemKind.Folder, typeof(Folder).FullName! },
|
||||
{ BaseItemKind.Genre, typeof(Genre).FullName! },
|
||||
{ BaseItemKind.Movie, typeof(Movie).FullName! },
|
||||
{ BaseItemKind.LiveTvChannel, typeof(LiveTvChannel).FullName! },
|
||||
{ BaseItemKind.LiveTvProgram, typeof(LiveTvProgram).FullName! },
|
||||
{ BaseItemKind.MusicAlbum, typeof(MusicAlbum).FullName! },
|
||||
{ BaseItemKind.MusicArtist, typeof(MusicArtist).FullName! },
|
||||
{ BaseItemKind.MusicGenre, typeof(MusicGenre).FullName! },
|
||||
{ BaseItemKind.MusicVideo, typeof(MusicVideo).FullName! },
|
||||
{ BaseItemKind.Person, typeof(Person).FullName! },
|
||||
{ BaseItemKind.Photo, typeof(Photo).FullName! },
|
||||
{ BaseItemKind.PhotoAlbum, typeof(PhotoAlbum).FullName! },
|
||||
{ BaseItemKind.Playlist, typeof(Playlist).FullName! },
|
||||
{ BaseItemKind.PlaylistsFolder, typeof(PlaylistsFolder).FullName! },
|
||||
{ BaseItemKind.Season, typeof(Season).FullName! },
|
||||
{ BaseItemKind.Series, typeof(Series).FullName! },
|
||||
{ BaseItemKind.Studio, typeof(Studio).FullName! },
|
||||
{ BaseItemKind.Trailer, typeof(Trailer).FullName! },
|
||||
{ BaseItemKind.TvChannel, typeof(LiveTvChannel).FullName! },
|
||||
{ BaseItemKind.TvProgram, typeof(LiveTvProgram).FullName! },
|
||||
{ BaseItemKind.UserRootFolder, typeof(UserRootFolder).FullName! },
|
||||
{ BaseItemKind.UserView, typeof(UserView).FullName! },
|
||||
{ BaseItemKind.Video, typeof(Video).FullName! },
|
||||
{ BaseItemKind.Year, typeof(Year).FullName! }
|
||||
}.ToFrozenDictionary();
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
public static class SqliteExtensions
|
||||
{
|
||||
private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
|
||||
private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
|
||||
|
||||
/// <summary>
|
||||
/// An array of ISO-8601 DateTime formats that we support parsing.
|
||||
/// </summary>
|
||||
private static readonly string[] _datetimeFormats = new string[]
|
||||
{
|
||||
"THHmmssK",
|
||||
"THHmmK",
|
||||
"HH:mm:ss.FFFFFFFK",
|
||||
"HH:mm:ssK",
|
||||
"HH:mmK",
|
||||
DatetimeFormatUtc,
|
||||
"yyyy-MM-dd HH:mm:ssK",
|
||||
"yyyy-MM-dd HH:mmK",
|
||||
"yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
|
||||
"yyyy-MM-ddTHH:mmK",
|
||||
"yyyy-MM-ddTHH:mm:ssK",
|
||||
"yyyyMMddHHmmssK",
|
||||
"yyyyMMddHHmmK",
|
||||
"yyyyMMddTHHmmssFFFFFFFK",
|
||||
"THHmmss",
|
||||
"THHmm",
|
||||
"HH:mm:ss.FFFFFFF",
|
||||
"HH:mm:ss",
|
||||
"HH:mm",
|
||||
DatetimeFormatLocal,
|
||||
"yyyy-MM-dd HH:mm:ss",
|
||||
"yyyy-MM-dd HH:mm",
|
||||
"yyyy-MM-ddTHH:mm:ss.FFFFFFF",
|
||||
"yyyy-MM-ddTHH:mm",
|
||||
"yyyy-MM-ddTHH:mm:ss",
|
||||
"yyyyMMddHHmmss",
|
||||
"yyyyMMddHHmm",
|
||||
"yyyyMMddTHHmmssFFFFFFF",
|
||||
"yyyy-MM-dd",
|
||||
"yyyyMMdd",
|
||||
"yy-MM-dd"
|
||||
};
|
||||
|
||||
public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
|
||||
{
|
||||
if (sqliteConnection.State != ConnectionState.Open)
|
||||
{
|
||||
sqliteConnection.Open();
|
||||
}
|
||||
|
||||
using var command = sqliteConnection.CreateCommand();
|
||||
command.CommandText = commandText;
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
yield return reader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Execute(this SqliteConnection sqliteConnection, string commandText)
|
||||
{
|
||||
using var command = sqliteConnection.CreateCommand();
|
||||
command.CommandText = commandText;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static string ToDateTimeParamValue(this DateTime dateValue)
|
||||
{
|
||||
var kind = DateTimeKind.Utc;
|
||||
|
||||
return (dateValue.Kind == DateTimeKind.Unspecified)
|
||||
? DateTime.SpecifyKind(dateValue, kind).ToString(
|
||||
GetDateTimeKindFormat(kind),
|
||||
CultureInfo.InvariantCulture)
|
||||
: dateValue.ToString(
|
||||
GetDateTimeKindFormat(dateValue.Kind),
|
||||
CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private static string GetDateTimeKindFormat(DateTimeKind kind)
|
||||
=> (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
|
||||
|
||||
public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var dateText = reader.GetString(index);
|
||||
|
||||
if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out var dateTimeResult))
|
||||
{
|
||||
// If the resulting DateTimeKind is Unspecified it is actually Utc.
|
||||
// This is required downstream for the Json serializer.
|
||||
if (dateTimeResult.Kind == DateTimeKind.Unspecified)
|
||||
{
|
||||
dateTimeResult = DateTime.SpecifyKind(dateTimeResult, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
result = dateTimeResult;
|
||||
return true;
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
result = reader.GetGuid(index);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
result = Guid.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
|
||||
{
|
||||
result = string.Empty;
|
||||
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetString(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetBoolean(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetInt32(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetInt64(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetFloat(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
|
||||
{
|
||||
if (reader.IsDBNull(index))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = reader.GetDouble(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void TryBind(this SqliteCommand statement, string name, Guid value)
|
||||
{
|
||||
statement.TryBind(name, value, true);
|
||||
}
|
||||
|
||||
public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
|
||||
{
|
||||
var preparedValue = value ?? DBNull.Value;
|
||||
if (statement.Parameters.Contains(name))
|
||||
{
|
||||
statement.Parameters[name].Value = preparedValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Blobs aren't always detected automatically
|
||||
if (isBlob)
|
||||
{
|
||||
statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
|
||||
}
|
||||
else
|
||||
{
|
||||
statement.Parameters.AddWithValue(name, preparedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void TryBindNull(this SqliteCommand statement, string name)
|
||||
{
|
||||
statement.TryBind(name, DBNull.Value);
|
||||
}
|
||||
|
||||
public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
|
||||
{
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
yield return reader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int SelectScalarInt(this SqliteCommand command)
|
||||
{
|
||||
var result = command.ExecuteScalar();
|
||||
// Can't be null since the method is used to retrieve Count
|
||||
return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
|
||||
{
|
||||
var command = sqliteConnection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
|
||||
namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Class TypeMapper.
|
||||
/// </summary>
|
||||
public class TypeMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// This holds all the types in the running assemblies
|
||||
/// so that we can de-serialize properly when we don't have strong types.
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">Name of the type.</param>
|
||||
/// <returns>Type.</returns>
|
||||
/// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
|
||||
public Type? GetType(string typeName)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(typeName);
|
||||
|
||||
return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
|
||||
.Select(a => a.GetType(k))
|
||||
.FirstOrDefault(t => t is not null));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user