2
0

FontConfigLoader.cs 6.3 KB

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