2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2018-09-12 13:26:21 -04:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:23:38 -05:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Net;
|
2018-09-12 13:26:21 -04:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 14:23:38 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
namespace Emby.Server.Implementations
|
|
|
|
{
|
|
|
|
public class ResourceFileManager : IResourceFileManager
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IHttpResultFactory _resultFactory;
|
|
|
|
|
2019-01-17 17:55:05 -05:00
|
|
|
public ResourceFileManager(
|
|
|
|
IHttpResultFactory resultFactory,
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
IFileSystem fileSystem)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
_resultFactory = resultFactory;
|
2019-01-17 17:55:05 -05:00
|
|
|
_logger = loggerFactory.CreateLogger("ResourceManager");
|
2018-09-12 13:26:21 -04:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Stream GetResourceFileStream(string basePath, string virtualPath)
|
|
|
|
{
|
|
|
|
return _fileSystem.GetFileStream(GetResourcePath(basePath, virtualPath), FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<object> GetStaticFileResult(IRequest request, string basePath, string virtualPath, string contentType, TimeSpan? cacheDuration)
|
|
|
|
{
|
|
|
|
return _resultFactory.GetStaticFileResult(request, GetResourcePath(basePath, virtualPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ReadAllText(string basePath, string virtualPath)
|
|
|
|
{
|
|
|
|
return _fileSystem.ReadAllText(GetResourcePath(basePath, virtualPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetResourcePath(string basePath, string virtualPath)
|
|
|
|
{
|
2019-01-26 15:47:11 -05:00
|
|
|
var fullPath = Path.Combine(basePath, virtualPath.Replace('/', Path.DirectorySeparatorChar));
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-01-26 15:47:11 -05:00
|
|
|
fullPath = Path.GetFullPath(fullPath);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Error in Path.GetFullPath");
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow file system access outside of the source folder
|
|
|
|
if (!_fileSystem.ContainsSubPath(basePath, fullPath))
|
|
|
|
{
|
|
|
|
throw new SecurityException("Access denied");
|
|
|
|
}
|
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|