aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornyanmisaka <nst799610810@gmail.com>2020-07-29 02:44:11 +0800
committernyanmisaka <nst799610810@gmail.com>2020-07-29 02:44:11 +0800
commit45a97056ea9fe7bc247f205931f5b0e0f2a1ab75 (patch)
tree8919820cf29c39fb0450f68ee0c03104535e20f7
parenta78c8e38544a974f07c6efce1af4e7fbef83b2be (diff)
allows to provide multiple fonts
-rw-r--r--MediaBrowser.Api/Subtitles/SubtitleService.cs81
-rw-r--r--MediaBrowser.Model/Subtitles/FontFile.cs34
2 files changed, 107 insertions, 8 deletions
diff --git a/MediaBrowser.Api/Subtitles/SubtitleService.cs b/MediaBrowser.Api/Subtitles/SubtitleService.cs
index b06ffd185..b5f5213fe 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.Subtitles
public 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 10M
var 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.cs
new file mode 100644
index 000000000..13963a9cb
--- /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; }
+ }
+}