ResourceFileManager.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.IO;
  7. using Microsoft.Extensions.Logging;
  8. namespace Emby.Server.Implementations
  9. {
  10. public class ResourceFileManager : IResourceFileManager
  11. {
  12. private readonly IFileSystem _fileSystem;
  13. private readonly ILogger<ResourceFileManager> _logger;
  14. public ResourceFileManager(ILogger<ResourceFileManager> logger, IFileSystem fileSystem)
  15. {
  16. _logger = logger;
  17. _fileSystem = fileSystem;
  18. }
  19. public string GetResourcePath(string basePath, string virtualPath)
  20. {
  21. var fullPath = Path.Combine(basePath, virtualPath.Replace('/', Path.DirectorySeparatorChar));
  22. try
  23. {
  24. fullPath = Path.GetFullPath(fullPath);
  25. }
  26. catch (Exception ex)
  27. {
  28. _logger.LogError(ex, "Error retrieving full path");
  29. }
  30. // Don't allow file system access outside of the source folder
  31. if (!_fileSystem.ContainsSubPath(basePath, fullPath))
  32. {
  33. throw new SecurityException("Access denied");
  34. }
  35. return fullPath;
  36. }
  37. }
  38. }