From e1abf14a9e99b180eded5f5d137d1e3d182b630d Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Mon, 16 Dec 2024 18:39:08 +0100 Subject: [PATCH] refactor: simplify `IImageMetadata` in plugin abstraction - Removed `IsAvailable` (use `IsLocalAvailable` or `IsRemoteAvailable`) - Changed `GetStream(bool allowLocal = true, bool allowRemote = true)` to `GetStream()`, which will now only search for local data. no remote data. - Kept the `DownloadImage(bool force = false)` method for now, so plugins can still instruct the core to download images not already present locally if needed, but added doc-comments for which exceptions may be thrown by the methods which plugins (and the core) should watch out for if they're using the method. --- .../DataModels/IImageMetadata.cs | 20 ++++++------------- .../Shoko.Plugin.Abstractions.csproj | 2 +- Shoko.Server/API/v2/Modules/Image.cs | 2 +- .../API/v3/Controllers/ImageController.cs | 2 +- Shoko.Server/Models/Image_Base.cs | 13 ++---------- 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/Shoko.Plugin.Abstractions/DataModels/IImageMetadata.cs b/Shoko.Plugin.Abstractions/DataModels/IImageMetadata.cs index 020d7a8e2..4555e54c3 100644 --- a/Shoko.Plugin.Abstractions/DataModels/IImageMetadata.cs +++ b/Shoko.Plugin.Abstractions/DataModels/IImageMetadata.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Net.Http; using System.Threading.Tasks; using Shoko.Plugin.Abstractions.Enums; @@ -38,11 +39,6 @@ public interface IImageMetadata : IMetadata, IEquatable /// public bool IsLocked { get; } - /// - /// Indicates the image is readily available. - /// - public bool IsAvailable { get; } - /// /// Indicates that the image is readily available from the local file system. /// @@ -95,25 +91,21 @@ public interface IImageMetadata : IMetadata, IEquatable string? LocalPath { get; } /// - /// Get a stream that reads the image contents from the local copy or remote - /// copy of the image. Returns null if the image is currently unavailable. + /// Get a stream that reads the image contents from the local copy. Returns + /// null if the image is currently unavailable. /// - /// - /// Allow retrieving the stream from the local cache. - /// - /// - /// Allow retrieving the stream from the remote cache. - /// /// /// A stream of the image content, or null. The stream will never be /// interrupted partway through. /// - Stream? GetStream(bool allowLocal = true, bool allowRemote = true); + Stream? GetStream(); /// /// Will attempt to download the remote copy of the image available at /// to the . /// /// Indicates that the image is available locally. + /// An error occurred while downloading the resource. + /// An error occurred while writing the file. Task DownloadImage(bool force = false); } diff --git a/Shoko.Plugin.Abstractions/Shoko.Plugin.Abstractions.csproj b/Shoko.Plugin.Abstractions/Shoko.Plugin.Abstractions.csproj index d80d20b0e..49109eae6 100644 --- a/Shoko.Plugin.Abstractions/Shoko.Plugin.Abstractions.csproj +++ b/Shoko.Plugin.Abstractions/Shoko.Plugin.Abstractions.csproj @@ -13,7 +13,7 @@ https://github.com/ShokoAnime/ShokoServer plugins, shoko, anime, metadata, tagging Renamer Rewrite - 4.1.0-beta3 + 4.1.0-beta4 Debug;Release;Benchmarks AnyCPU;x64 false diff --git a/Shoko.Server/API/v2/Modules/Image.cs b/Shoko.Server/API/v2/Modules/Image.cs index 170411cfd..4d7305670 100644 --- a/Shoko.Server/API/v2/Modules/Image.cs +++ b/Shoko.Server/API/v2/Modules/Image.cs @@ -130,7 +130,7 @@ public FileResult GetRandomImage(int type) while (tries++ < 5) { var metadata = ImageUtils.GetRandomImageID(imageType); - if (metadata is not null && metadata.GetStream(allowRemote: false) is { } stream) + if (metadata is not null && metadata.GetStream() is { } stream) return File(stream, metadata.ContentType); } diff --git a/Shoko.Server/API/v3/Controllers/ImageController.cs b/Shoko.Server/API/v3/Controllers/ImageController.cs index e0659707d..cc4d15c82 100644 --- a/Shoko.Server/API/v3/Controllers/ImageController.cs +++ b/Shoko.Server/API/v3/Controllers/ImageController.cs @@ -130,7 +130,7 @@ public ActionResult GetRandomImageForType([FromRoute] Image.ImageType imageType) if (series == null || (series.AniDB_Anime?.IsRestricted ?? false)) continue; - if (metadata.GetStream(allowRemote: false) is not { } stream) + if (metadata.GetStream() is not { } stream) continue; return File(stream, metadata.ContentType); diff --git a/Shoko.Server/Models/Image_Base.cs b/Shoko.Server/Models/Image_Base.cs index 727599c9b..8b14deeb5 100644 --- a/Shoko.Server/Models/Image_Base.cs +++ b/Shoko.Server/Models/Image_Base.cs @@ -115,12 +115,6 @@ public string ContentType /// public virtual bool IsLocked => true; - /// - public bool IsAvailable - { - get => IsLocalAvailable || IsRemoteAvailable; - } - [MemberNotNullWhen(true, nameof(LocalPath))] public bool IsLocalAvailable { @@ -295,12 +289,9 @@ private void RefreshMetadata() } } - public Stream? GetStream(bool allowLocal = true, bool allowRemote = true) + public Stream? GetStream() { - if (allowLocal && IsLocalAvailable) - return new FileStream(LocalPath, FileMode.Open, FileAccess.Read); - - if (allowRemote && DownloadImage().ConfigureAwait(false).GetAwaiter().GetResult() && IsLocalAvailable) + if (IsLocalAvailable) return new FileStream(LocalPath, FileMode.Open, FileAccess.Read); return null;