Lidarr/src/Lidarr.Http/Frontend/Mappers/StaticResourceMapperBase.cs

56 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
2021-08-04 20:42:40 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
2017-09-04 02:20:56 +00:00
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
2017-09-04 02:20:56 +00:00
namespace Lidarr.Http.Frontend.Mappers
{
public abstract class StaticResourceMapperBase : IMapHttpRequestsToDisk
{
private readonly IDiskProvider _diskProvider;
private readonly Logger _logger;
2014-12-07 20:54:07 +00:00
private readonly StringComparison _caseSensitive;
2021-08-04 20:42:40 +00:00
private readonly IContentTypeProvider _mimeTypeProvider;
protected StaticResourceMapperBase(IDiskProvider diskProvider, Logger logger)
{
_diskProvider = diskProvider;
_logger = logger;
2021-08-04 20:42:40 +00:00
_mimeTypeProvider = new FileExtensionContentTypeProvider();
_caseSensitive = RuntimeInfo.IsProduction ? DiskProviderBase.PathStringComparison : StringComparison.OrdinalIgnoreCase;
}
public abstract string Map(string resourceUrl);
public abstract bool CanHandle(string resourceUrl);
public FileStreamResult GetResponse(string resourceUrl)
{
var filePath = Map(resourceUrl);
if (_diskProvider.FileExists(filePath, _caseSensitive))
{
2021-08-04 20:42:40 +00:00
if (!_mimeTypeProvider.TryGetContentType(filePath, out var contentType))
{
contentType = "application/octet-stream";
}
return new FileStreamResult(GetContentStream(filePath), contentType);
}
_logger.Warn("File {0} not found", filePath);
2021-08-04 20:42:40 +00:00
return null;
}
protected virtual Stream GetContentStream(string filePath)
{
return File.OpenRead(filePath);
}
}
}