Skip to content

Commit

Permalink
refactor: simplify IImageMetadata in plugin abstraction
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
revam committed Dec 16, 2024
1 parent 1568143 commit e1abf14
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 28 deletions.
20 changes: 6 additions & 14 deletions Shoko.Plugin.Abstractions/DataModels/IImageMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Shoko.Plugin.Abstractions.Enums;

Expand Down Expand Up @@ -38,11 +39,6 @@ public interface IImageMetadata : IMetadata<int>, IEquatable<IImageMetadata>
/// </summary>
public bool IsLocked { get; }

/// <summary>
/// Indicates the image is readily available.
/// </summary>
public bool IsAvailable { get; }

/// <summary>
/// Indicates that the image is readily available from the local file system.
/// </summary>
Expand Down Expand Up @@ -95,25 +91,21 @@ public interface IImageMetadata : IMetadata<int>, IEquatable<IImageMetadata>
string? LocalPath { get; }

/// <summary>
/// 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.
/// </summary>
/// <param name="allowLocal">
/// Allow retrieving the stream from the local cache.
/// </param>
/// <param name="allowRemote">
/// Allow retrieving the stream from the remote cache.
/// </param>
/// <returns>
/// A stream of the image content, or null. The stream will never be
/// interrupted partway through.
/// </returns>
Stream? GetStream(bool allowLocal = true, bool allowRemote = true);
Stream? GetStream();

/// <summary>
/// Will attempt to download the remote copy of the image available at
/// <see cref="RemoteURL"/> to the <see cref="LocalPath"/>.
/// </summary>
/// <returns>Indicates that the image is available locally.</returns>
/// <exception cref="HttpRequestException">An error occurred while downloading the resource.</exception>
/// <exception cref="IOException">An error occurred while writing the file.</exception>
Task<bool> DownloadImage(bool force = false);
}
2 changes: 1 addition & 1 deletion Shoko.Plugin.Abstractions/Shoko.Plugin.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/ShokoAnime/ShokoServer</RepositoryUrl>
<PackageTags>plugins, shoko, anime, metadata, tagging</PackageTags>
<PackageReleaseNotes>Renamer Rewrite</PackageReleaseNotes>
<Version>4.1.0-beta3</Version>
<Version>4.1.0-beta4</Version>
<Configurations>Debug;Release;Benchmarks</Configurations>
<Platforms>AnyCPU;x64</Platforms>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/v2/Modules/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/v3/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 2 additions & 11 deletions Shoko.Server/Models/Image_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ public string ContentType
/// <inheritdoc/>
public virtual bool IsLocked => true;

/// <inheritdoc/>
public bool IsAvailable
{
get => IsLocalAvailable || IsRemoteAvailable;
}

[MemberNotNullWhen(true, nameof(LocalPath))]
public bool IsLocalAvailable
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e1abf14

Please sign in to comment.