jellyfin/Emby.Server.Implementations/ResourceFileManager.cs

63 lines
2.1 KiB
C#
Raw Normal View History

using System;
2018-09-12 17:26:21 +00:00
using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Net;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
2018-09-12 17:26:21 +00:00
namespace Emby.Server.Implementations
{
public class ResourceFileManager : IResourceFileManager
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
private readonly IHttpResultFactory _resultFactory;
public ResourceFileManager(IHttpResultFactory resultFactory, ILogger logger, IFileSystem fileSystem)
{
_resultFactory = resultFactory;
_logger = logger;
_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)
{
var fullPath = Path.Combine(basePath, virtualPath.Replace('/', _fileSystem.DirectorySeparatorChar));
try
{
fullPath = _fileSystem.GetFullPath(fullPath);
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error in Path.GetFullPath");
2018-09-12 17:26:21 +00:00
}
// Don't allow file system access outside of the source folder
if (!_fileSystem.ContainsSubPath(basePath, fullPath))
{
throw new SecurityException("Access denied");
}
return fullPath;
}
}
}