2
0

FontConfigLoader.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using CommonIO;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Net;
  12. namespace MediaBrowser.MediaEncoding.Encoder
  13. {
  14. public class FontConfigLoader
  15. {
  16. private readonly IHttpClient _httpClient;
  17. private readonly IApplicationPaths _appPaths;
  18. private readonly ILogger _logger;
  19. private readonly IZipClient _zipClient;
  20. private readonly IFileSystem _fileSystem;
  21. private readonly string[] _fontUrls =
  22. {
  23. "https://github.com/MediaBrowser/MediaBrowser.Resources/raw/master/ffmpeg/ARIALUNI.7z"
  24. };
  25. public FontConfigLoader(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, IZipClient zipClient, IFileSystem fileSystem)
  26. {
  27. _httpClient = httpClient;
  28. _appPaths = appPaths;
  29. _logger = logger;
  30. _zipClient = zipClient;
  31. _fileSystem = fileSystem;
  32. }
  33. /// <summary>
  34. /// Extracts the fonts.
  35. /// </summary>
  36. /// <param name="targetPath">The target path.</param>
  37. /// <returns>Task.</returns>
  38. public async Task DownloadFonts(string targetPath)
  39. {
  40. try
  41. {
  42. var fontsDirectory = Path.Combine(targetPath, "fonts");
  43. _fileSystem.CreateDirectory(fontsDirectory);
  44. const string fontFilename = "ARIALUNI.TTF";
  45. var fontFile = Path.Combine(fontsDirectory, fontFilename);
  46. if (_fileSystem.FileExists(fontFile))
  47. {
  48. await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
  49. }
  50. else
  51. {
  52. // Kick this off, but no need to wait on it
  53. var task = Task.Run(async () =>
  54. {
  55. await DownloadFontFile(fontsDirectory, fontFilename, new Progress<double>()).ConfigureAwait(false);
  56. await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
  57. });
  58. }
  59. }
  60. catch (HttpException ex)
  61. {
  62. // Don't let the server crash because of this
  63. _logger.ErrorException("Error downloading ffmpeg font files", ex);
  64. }
  65. catch (Exception ex)
  66. {
  67. // Don't let the server crash because of this
  68. _logger.ErrorException("Error writing ffmpeg font files", ex);
  69. }
  70. }
  71. /// <summary>
  72. /// Downloads the font file.
  73. /// </summary>
  74. /// <param name="fontsDirectory">The fonts directory.</param>
  75. /// <param name="fontFilename">The font filename.</param>
  76. /// <returns>Task.</returns>
  77. private async Task DownloadFontFile(string fontsDirectory, string fontFilename, IProgress<double> progress)
  78. {
  79. var existingFile = Directory
  80. .EnumerateFiles(_appPaths.ProgramDataPath, fontFilename, SearchOption.AllDirectories)
  81. .FirstOrDefault();
  82. if (existingFile != null)
  83. {
  84. try
  85. {
  86. _fileSystem.CopyFile(existingFile, Path.Combine(fontsDirectory, fontFilename), true);
  87. return;
  88. }
  89. catch (IOException ex)
  90. {
  91. // Log this, but don't let it fail the operation
  92. _logger.ErrorException("Error copying file", ex);
  93. }
  94. }
  95. string tempFile = null;
  96. foreach (var url in _fontUrls)
  97. {
  98. progress.Report(0);
  99. try
  100. {
  101. tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
  102. {
  103. Url = url,
  104. Progress = progress
  105. }).ConfigureAwait(false);
  106. break;
  107. }
  108. catch (Exception ex)
  109. {
  110. // The core can function without the font file, so handle this
  111. _logger.ErrorException("Failed to download ffmpeg font file from {0}", ex, url);
  112. }
  113. }
  114. if (string.IsNullOrEmpty(tempFile))
  115. {
  116. return;
  117. }
  118. Extract7zArchive(tempFile, fontsDirectory);
  119. try
  120. {
  121. _fileSystem.DeleteFile(tempFile);
  122. }
  123. catch (IOException ex)
  124. {
  125. // Log this, but don't let it fail the operation
  126. _logger.ErrorException("Error deleting temp file {0}", ex, tempFile);
  127. }
  128. }
  129. private void Extract7zArchive(string archivePath, string targetPath)
  130. {
  131. _logger.Info("Extracting {0} to {1}", archivePath, targetPath);
  132. _zipClient.ExtractAllFrom7z(archivePath, targetPath, true);
  133. }
  134. /// <summary>
  135. /// Writes the font config file.
  136. /// </summary>
  137. /// <param name="fontsDirectory">The fonts directory.</param>
  138. /// <returns>Task.</returns>
  139. private async Task WriteFontConfigFile(string fontsDirectory)
  140. {
  141. const string fontConfigFilename = "fonts.conf";
  142. var fontConfigFile = Path.Combine(fontsDirectory, fontConfigFilename);
  143. if (!_fileSystem.FileExists(fontConfigFile))
  144. {
  145. var contents = string.Format("<?xml version=\"1.0\"?><fontconfig><dir>{0}</dir><alias><family>Arial</family><prefer>Arial Unicode MS</prefer></alias></fontconfig>", fontsDirectory);
  146. var bytes = Encoding.UTF8.GetBytes(contents);
  147. using (var fileStream = _fileSystem.GetFileStream(fontConfigFile, FileMode.Create, FileAccess.Write,
  148. FileShare.Read, true))
  149. {
  150. await fileStream.WriteAsync(bytes, 0, bytes.Length);
  151. }
  152. }
  153. }
  154. }
  155. }