Skip to content

Commit

Permalink
Create image migration code
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Apr 3, 2024
1 parent 0f42775 commit 7731c4c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions back/src/Kyoo.Core/Controllers/ThumbnailsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ private static string _GetBaseImagePath<T>(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);
Expand Down
42 changes: 35 additions & 7 deletions back/src/Kyoo.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MeiliSync>();
Expand Down
22 changes: 2 additions & 20 deletions back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,9 @@ namespace Kyoo.Core.Api;
/// </summary>
/// <typeparam name="T">The type of resource to make CRUD and thumbnails apis for.</typeparam>
[ApiController]
public class CrudThumbsApi<T> : CrudApi<T>
public class CrudThumbsApi<T>(IRepository<T> repository, IThumbnailsManager thumbs) : CrudApi<T>(repository)
where T : class, IResource, IThumbnails, IQuery
{
/// <summary>
/// The thumbnail manager used to retrieve images paths.
/// </summary>
private readonly IThumbnailsManager _thumbs;

/// <summary>
/// Create a new <see cref="CrudThumbsApi{T}"/> that handles crud requests and thumbnails.
/// </summary>
/// <param name="repository">
/// The repository to use as a baking store for the type <typeparamref name="T"/>.
/// </param>
/// <param name="thumbs">The thumbnail manager used to retrieve images paths.</param>
public CrudThumbsApi(IRepository<T> repository, IThumbnailsManager thumbs)
: base(repository)
{
_thumbs = thumbs;
}

private async Task<IActionResult> _GetImage(
Identifier identifier,
string image,
Expand All @@ -67,7 +49,7 @@ private async Task<IActionResult> _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();

Expand Down

0 comments on commit 7731c4c

Please sign in to comment.