2017-10-30 01:26:22 +00:00
|
|
|
using System;
|
2013-08-20 06:23:36 +00:00
|
|
|
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;
|
2014-01-06 06:20:08 +00:00
|
|
|
using NzbDrone.Common.Disk;
|
2013-08-20 06:23:36 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
namespace Lidarr.Http.Frontend.Mappers
|
2013-08-20 06:23:36 +00:00
|
|
|
{
|
|
|
|
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;
|
2013-08-20 06:23:36 +00:00
|
|
|
|
|
|
|
protected StaticResourceMapperBase(IDiskProvider diskProvider, Logger logger)
|
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_logger = logger;
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
_mimeTypeProvider = new FileExtensionContentTypeProvider();
|
2017-10-30 01:26:22 +00:00
|
|
|
_caseSensitive = RuntimeInfo.IsProduction ? DiskProviderBase.PathStringComparison : StringComparison.OrdinalIgnoreCase;
|
2013-08-20 06:23:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 07:40:23 +00:00
|
|
|
public abstract string Map(string resourceUrl);
|
2013-08-20 06:23:36 +00:00
|
|
|
|
|
|
|
public abstract bool CanHandle(string resourceUrl);
|
|
|
|
|
2021-08-04 20:42:41 +00:00
|
|
|
public FileStreamResult GetResponse(string resourceUrl)
|
2013-08-20 06:23:36 +00:00
|
|
|
{
|
|
|
|
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);
|
2013-08-20 06:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Warn("File {0} not found", filePath);
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
return null;
|
2013-08-20 06:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual Stream GetContentStream(string filePath)
|
|
|
|
{
|
|
|
|
return File.OpenRead(filePath);
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 01:26:22 +00:00
|
|
|
}
|