ResourceFileManager.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Model.Extensions;
  7. using Microsoft.Extensions.Logging;
  8. using MediaBrowser.Model.Net;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Common.Plugins;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Globalization;
  18. using MediaBrowser.Model.Plugins;
  19. using MediaBrowser.Model.Reflection;
  20. using MediaBrowser.Model.Services;
  21. namespace Emby.Server.Implementations
  22. {
  23. public class ResourceFileManager : IResourceFileManager
  24. {
  25. private readonly IFileSystem _fileSystem;
  26. private readonly ILogger _logger;
  27. private readonly IHttpResultFactory _resultFactory;
  28. public ResourceFileManager(IHttpResultFactory resultFactory, ILogger logger, IFileSystem fileSystem)
  29. {
  30. _resultFactory = resultFactory;
  31. _logger = logger;
  32. _fileSystem = fileSystem;
  33. }
  34. public Stream GetResourceFileStream(string basePath, string virtualPath)
  35. {
  36. return _fileSystem.GetFileStream(GetResourcePath(basePath, virtualPath), FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, true);
  37. }
  38. public Task<object> GetStaticFileResult(IRequest request, string basePath, string virtualPath, string contentType, TimeSpan? cacheDuration)
  39. {
  40. return _resultFactory.GetStaticFileResult(request, GetResourcePath(basePath, virtualPath));
  41. }
  42. public string ReadAllText(string basePath, string virtualPath)
  43. {
  44. return _fileSystem.ReadAllText(GetResourcePath(basePath, virtualPath));
  45. }
  46. private string GetResourcePath(string basePath, string virtualPath)
  47. {
  48. var fullPath = Path.Combine(basePath, virtualPath.Replace('/', _fileSystem.DirectorySeparatorChar));
  49. try
  50. {
  51. fullPath = _fileSystem.GetFullPath(fullPath);
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.LogError(ex, "Error in Path.GetFullPath");
  56. }
  57. // Don't allow file system access outside of the source folder
  58. if (!_fileSystem.ContainsSubPath(basePath, fullPath))
  59. {
  60. throw new SecurityException("Access denied");
  61. }
  62. return fullPath;
  63. }
  64. }
  65. }