From 7731c4c4857129d20634ca188c3d2b013d08b85c Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 4 Apr 2024 01:51:06 +0200 Subject: [PATCH] Create image migration code --- .env.example | 4 +- .../Controllers/ThumbnailsManager.cs | 4 +- back/src/Kyoo.Core/Program.cs | 42 +++++++++++++++---- .../Kyoo.Core/Views/Helper/CrudThumbsApi.cs | 22 +--------- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/.env.example b/.env.example index cd67a291b4..bb8b6db076 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,8 @@ # Library root can either be an absolute path or a relative path to your docker-compose.yml file. LIBRARY_ROOT=./video +# You should set this to a path where kyoo can write large amount of data, this is used as a cache by the transcoder. +# It will automatically be cleaned up on kyoo's startup/shutdown/runtime. CACHE_ROOT=/tmp/kyoo_cache LIBRARY_LANGUAGES=en # A pattern (regex) to ignore video files. @@ -61,8 +63,6 @@ OIDC_SERVICE_SCOPE="the list of scopes space separeted like email identity" # To debug the front end, you can set the following to an external backend KYOO_URL= -# The library root inside the container. -KYOO_LIBRARY_ROOT=/video # Database things POSTGRES_USER=KyooUser diff --git a/back/src/Kyoo.Core/Controllers/ThumbnailsManager.cs b/back/src/Kyoo.Core/Controllers/ThumbnailsManager.cs index 207d97184f..c9ba209aab 100644 --- a/back/src/Kyoo.Core/Controllers/ThumbnailsManager.cs +++ b/back/src/Kyoo.Core/Controllers/ThumbnailsManager.cs @@ -179,8 +179,8 @@ private static string _GetBaseImagePath(T item, string image) string directory = item switch { IResource res - => Path.Combine("./metadata", item.GetType().Name.ToLowerInvariant(), res.Slug), - _ => Path.Combine("./metadata", typeof(T).Name.ToLowerInvariant()) + => Path.Combine("/metadata", item.GetType().Name.ToLowerInvariant(), res.Slug), + _ => Path.Combine("/metadata", typeof(T).Name.ToLowerInvariant()) }; Directory.CreateDirectory(directory); return Path.Combine(directory, image); diff --git a/back/src/Kyoo.Core/Program.cs b/back/src/Kyoo.Core/Program.cs index 68c9462b38..95e57b83f1 100644 --- a/back/src/Kyoo.Core/Program.cs +++ b/back/src/Kyoo.Core/Program.cs @@ -27,7 +27,6 @@ using Kyoo.Swagger; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Serilog; using Serilog.Events; @@ -94,12 +93,41 @@ app.UseAuthentication(); app.MapControllers(); -// Set current directory, used by thumbnails for example. -string path = Path.GetFullPath(builder.Configuration.GetValue("DATADIR", "/kyoo")!); -if (!Directory.Exists(path)) - Directory.CreateDirectory(path); -Environment.CurrentDirectory = path; -Log.Information("Data directory: {DataDirectory}", Environment.CurrentDirectory); +// TODO: wait 4.5.0 and delete this +static void MoveAll(DirectoryInfo source, DirectoryInfo target) +{ + if (source.FullName == target.FullName) + return; + + Directory.CreateDirectory(target.FullName); + + foreach (FileInfo fi in source.GetFiles()) + fi.MoveTo(Path.Combine(target.ToString(), fi.Name), true); + + foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) + { + DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); + MoveAll(diSourceSubDir, nextTargetSubDir); + } + Directory.Delete(source.FullName); +} + +try +{ + string oldDir = "/kyoo/metadata"; + if (Path.Exists(oldDir)) + { + MoveAll(new DirectoryInfo(oldDir), new DirectoryInfo("/metadata")); + Log.Information("Old metadata directory migrated."); + } +} +catch (Exception ex) +{ + Log.Fatal( + ex, + "Unhandled error while trying to migrate old metadata images to new directory. Giving up and continuing normal startup." + ); +} // Activate services that always run in the background app.Services.GetRequiredService(); diff --git a/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs b/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs index 3d9ec37509..6c01e640d3 100644 --- a/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs +++ b/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs @@ -34,27 +34,9 @@ namespace Kyoo.Core.Api; /// /// The type of resource to make CRUD and thumbnails apis for. [ApiController] -public class CrudThumbsApi : CrudApi +public class CrudThumbsApi(IRepository repository, IThumbnailsManager thumbs) : CrudApi(repository) where T : class, IResource, IThumbnails, IQuery { - /// - /// The thumbnail manager used to retrieve images paths. - /// - private readonly IThumbnailsManager _thumbs; - - /// - /// Create a new that handles crud requests and thumbnails. - /// - /// - /// The repository to use as a baking store for the type . - /// - /// The thumbnail manager used to retrieve images paths. - public CrudThumbsApi(IRepository repository, IThumbnailsManager thumbs) - : base(repository) - { - _thumbs = thumbs; - } - private async Task _GetImage( Identifier identifier, string image, @@ -67,7 +49,7 @@ private async Task _GetImage( ); if (resource == null) return NotFound(); - string path = _thumbs.GetImagePath(resource, image, quality ?? ImageQuality.High); + string path = thumbs.GetImagePath(resource, image, quality ?? ImageQuality.High); if (!System.IO.File.Exists(path)) return NotFound();