The Free Software Media System - Server Backend & API
allows to provide multiple fonts
nyanmisaka45a97056ea9f
diff --git a/MediaBrowser.Api/Subtitles/SubtitleService.cs b/MediaBrowser.Api/Subtitles/SubtitleService.csindex b06ffd1851..b5f5213fe3 100644--- a/MediaBrowser.Api/Subtitles/SubtitleService.cs+++ b/MediaBrowser.Api/Subtitles/SubtitleService.cs@@ -18,6 +18,7 @@ using MediaBrowser.Model.Entities;using MediaBrowser.Model.IO;using MediaBrowser.Model.Providers;using MediaBrowser.Model.Services;+using MediaBrowser.Model.Subtitles;using Microsoft.Extensions.Logging;using MimeTypes = MediaBrowser.Model.Net.MimeTypes;@@ -123,10 +124,18 @@ namespace MediaBrowser.Api.Subtitlespublic int SegmentLength { get; set; }}+ [Route("/FallbackFont/FontList", "GET", Summary = "Gets the fallback font list")]+ [Authenticated]+ public class GetFallbackFontList+ {+ }+[Route("/FallbackFont/Font", "GET", Summary = "Gets the fallback font file")][Authenticated]public class GetFallbackFont{+ [ApiMember(Name = "Name", Description = "The font file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]+ public string Name { get; set; }}public class SubtitleService : BaseApiService@@ -308,19 +317,74 @@ namespace MediaBrowser.Api.Subtitles});}- public async Task<object> Get(GetFallbackFont request)+ public object Get(GetFallbackFontList request){- var fallbackFontPath = EncodingConfigurationExtensions.GetEncodingOptions(_serverConfigurationManager).FallbackFontPath;+ IEnumerable<FileSystemMetadata> fontFiles = Enumerable.Empty<FileSystemMetadata>();++ var encodingOptions = EncodingConfigurationExtensions.GetEncodingOptions(_serverConfigurationManager);+ var fallbackFontPath = encodingOptions.FallbackFontPath;if (!string.IsNullOrEmpty(fallbackFontPath)){- var directoryService = new DirectoryService(_fileSystem);+ try+ {+ fontFiles = _fileSystem.GetFiles(fallbackFontPath, new[] { ".woff", ".woff2", ".ttf", ".otf" }, false, false);++ var result = fontFiles.Select(i => new FontFile+ {+ Name = i.Name,+ Size = i.Length,+ DateCreated = _fileSystem.GetCreationTimeUtc(i),+ DateModified = _fileSystem.GetLastWriteTimeUtc(i)+ }).OrderBy(i => i.Size)+ .ThenBy(i => i.Name)+ .ThenByDescending(i => i.DateModified)+ .ThenByDescending(i => i.DateCreated)+ .ToArray();++ // max total size 20M+ var maxSize = 20971520;+ var sizeCounter = 0L;+ for (int i = 0; i < result.Length; i++)+ {+ sizeCounter += result[i].Size;+ if (sizeCounter >= maxSize)+ {+ Logger.LogWarning("Some fonts will not be sent due to size limitations");+ Array.Resize(ref result, i);+ break;+ }+ }++ return ToOptimizedResult(result);+ }+ catch (Exception ex)+ {+ Logger.LogError(ex, "Error getting fallback font list");+ }+ }+ else+ {+ Logger.LogWarning("The path of fallback font folder has not been set");+ encodingOptions.EnableFallbackFont = false;+ }+ return ResultFactory.GetResult(Request, "[]", "application/json");+ }++ public async Task<object> Get(GetFallbackFont request)+ {+ var encodingOptions = EncodingConfigurationExtensions.GetEncodingOptions(_serverConfigurationManager);+ var fallbackFontPath = encodingOptions.FallbackFontPath;++ if (!string.IsNullOrEmpty(fallbackFontPath))+ {try{- // 10 Megabytes+ // max single font size 10Mvar maxSize = 10485760;- var fontFile = directoryService.GetFile(fallbackFontPath);+ var fontFile = _fileSystem.GetFiles(fallbackFontPath)+ .First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));var fileSize = fontFile?.Length;if (fileSize != null && fileSize > 0)@@ -341,15 +405,16 @@ namespace MediaBrowser.Api.Subtitles}catch (Exception ex){- Logger.LogError(ex, "Error reading fallback font file");+ Logger.LogError(ex, "Error reading fallback font");}}else{- Logger.LogWarning("The path of fallback font has not been set");+ Logger.LogWarning("The path of fallback font folder has not been set");+ encodingOptions.EnableFallbackFont = false;}- return string.Empty;+ return ResultFactory.GetResult(Request, string.Empty, "text/plain");}}}diff --git a/MediaBrowser.Model/Subtitles/FontFile.cs b/MediaBrowser.Model/Subtitles/FontFile.csnew file mode 100644index 0000000000..13963a9cb5--- /dev/null+++ b/MediaBrowser.Model/Subtitles/FontFile.cs@@ -0,0 +1,34 @@+#nullable disable+#pragma warning disable CS1591++using System;++namespace MediaBrowser.Model.Subtitles+{+ public class FontFile+ {+ /// <summary>+ /// Gets or sets the name.+ /// </summary>+ /// <value>The name.</value>+ public string Name { get; set; }++ /// <summary>+ /// Gets or sets the size.+ /// </summary>+ /// <value>The size.</value>+ public long Size { get; set; }++ /// <summary>+ /// Gets or sets the date created.+ /// </summary>+ /// <value>The date created.</value>+ public DateTime DateCreated { get; set; }++ /// <summary>+ /// Gets or sets the date modified.+ /// </summary>+ /// <value>The date modified.</value>+ public DateTime DateModified { get; set; }+ }+}