Skip to content

Commit 76c2055

Browse files
committed
refactor: enable image width/height behind a flag
Re-enable sending the image width/height in the v3 api behind a setting. Also add `#nullable enable` to the image model file.
1 parent 7a24c63 commit 76c2055

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

Shoko.Server/API/v3/Models/Common/Image.cs

+17-19
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
using System.ComponentModel.DataAnnotations;
44
using System.IO;
55
using System.Linq;
6-
// using ImageMagick;
6+
using ImageMagick;
77
using Newtonsoft.Json;
88
using Newtonsoft.Json.Converters;
99
using Shoko.Commons.Extensions;
1010
using Shoko.Models.Enums;
1111
using Shoko.Server.Extensions;
1212
using Shoko.Server.ImageDownload;
1313
using Shoko.Server.Repositories;
14+
using Shoko.Server.Utilities;
1415

16+
#nullable enable
1517
namespace Shoko.Server.API.v3.Models.Common;
1618

1719
/// <summary>
@@ -41,7 +43,7 @@ public class Image
4143
/// The relative path from the base image directory. A client with access to the server's filesystem can map
4244
/// these for quick access and no need for caching
4345
/// </summary>
44-
public string RelativeFilepath { get; set; }
46+
public string? RelativeFilepath { get; set; }
4547

4648
/// <summary>
4749
/// Is it marked as default. Only one default is possible for a given <see cref="Image.Type"/>.
@@ -71,24 +73,20 @@ public Image(int id, ImageEntityType type, bool preferred = false, bool disabled
7173
throw new ArgumentException("Static Resources do not use an integer ID");
7274
}
7375

74-
RelativeFilepath = GetImagePath(type, id)?.Replace(ImageUtils.GetBaseImagesPath(), "").Replace("\\", "/");
75-
/*
7676
var imagePath = GetImagePath(type, id);
77-
if (string.IsNullOrEmpty(imagePath)) {
78-
RelativeFilepath = null;
79-
Width = null;
80-
Height = null;
81-
}
82-
// This causes serious IO lag on some systems. Removing until we have better Image data structures
83-
else
77+
if (!string.IsNullOrEmpty(imagePath))
8478
{
85-
var info = new MagickImageInfo(imagePath);
8679
RelativeFilepath = imagePath.Replace(ImageUtils.GetBaseImagesPath(), "").Replace("\\", "/");
8780
if (!RelativeFilepath.StartsWith("/"))
8881
RelativeFilepath = "/" + RelativeFilepath;
89-
Width = info.Width;
90-
Height = info.Height;
91-
}*/
82+
// This causes serious IO lag on some systems. Enable at own risk.
83+
if (Utils.SettingsProvider.GetSettings().LoadImageMetadata)
84+
{
85+
var info = new MagickImageInfo(imagePath);
86+
Width = info.Width;
87+
Height = info.Height;
88+
}
89+
}
9290
}
9391

9492
public Image(string id, ImageEntityType type, bool preferred = false, bool disabled = false)
@@ -241,7 +239,7 @@ public static ImageSizeType GetImageSizeTypeFromType(ImageType imageType)
241239
};
242240
}
243241

244-
public static string GetImagePath(ImageEntityType type, int id)
242+
public static string? GetImagePath(ImageEntityType type, int id)
245243
{
246244
string path;
247245

@@ -528,7 +526,7 @@ internal static ImageSource GetRandomImageSource(ImageType imageType)
528526
.Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
529527
.SelectMany(a => RepoFactory.CrossRef_Anime_Staff.GetByAnimeID(a.AnimeID))
530528
.Where(a => a.RoleType == (int)StaffRoleType.Seiyuu && a.RoleID.HasValue)
531-
.Select(a => RepoFactory.AnimeCharacter.GetByID(a.RoleID.Value))
529+
.Select(a => RepoFactory.AnimeCharacter.GetByID(a.RoleID!.Value))
532530
.GetRandomElement()?.CharacterID,
533531
ImageEntityType.Staff => RepoFactory.AniDB_Anime.GetAll()
534532
.Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
@@ -620,8 +618,8 @@ public class DefaultImageBody
620618
/// from the API. Also see <seealso cref="Image.ID"/>.
621619
/// </summary>
622620
/// <value></value>
623-
[Required]
624-
public string ID { get; set; }
621+
[Required, MinLength(1)]
622+
public string ID { get; set; } = "";
625623

626624
/// <summary>
627625
/// The image source.

Shoko.Server/Settings/IServerSettings.cs

+8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ public interface IServerSettings
3535
DataSourceType EpisodeTitleSource { get; set; }
3636
DataSourceType SeriesDescriptionSource { get; set; }
3737
DataSourceType SeriesNameSource { get; set; }
38+
/// <summary>
39+
/// Path where the images are stored. If set to <c>null</c> then it will use
40+
/// the default location.
41+
/// </summary>
3842
string ImagesPath { get; set; }
43+
/// <summary>
44+
/// Load image metadata from the file system and send to the clients.
45+
/// </summary>
46+
bool LoadImageMetadata { get; set; }
3947
string UpdateChannel { get; set; }
4048
bool TraceLog { get; set; }
4149
int CachingDatabaseTimeout { get; set; }

Shoko.Server/Settings/ServerSettings.cs

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public List<string> EpisodeLanguagePreference {
105105

106106
[JsonIgnore] public string _ImagesPath;
107107

108+
/// <inheritdoc />
108109
public string ImagesPath
109110
{
110111
get => _ImagesPath;
@@ -115,6 +116,9 @@ public string ImagesPath
115116
}
116117
}
117118

119+
/// <inheritdoc/> />
120+
public bool LoadImageMetadata { get; set; } = false;
121+
118122
public TraktSettings TraktTv { get; set; } = new();
119123

120124
public string UpdateChannel { get; set; } = "Stable";

0 commit comments

Comments
 (0)