ResourceFileManager.cs 2.1 KB

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