Lidarr/src/Lidarr.Api.V1/Logs/LogFileControllerBase.cs

74 lines
2.3 KiB
C#
Raw Normal View History

2017-10-31 01:28:29 +00:00
using System.Collections.Generic;
2017-09-04 02:20:56 +00:00
using System.IO;
using System.Linq;
2021-08-04 20:42:40 +00:00
using Microsoft.AspNetCore.Mvc;
using NLog;
2017-09-04 02:20:56 +00:00
using NzbDrone.Common.Disk;
using NzbDrone.Core.Configuration;
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.Logs
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
public abstract class LogFileControllerBase : Controller
2017-09-04 02:20:56 +00:00
{
protected const string LOGFILE_ROUTE = @"/(?<filename>[-.a-zA-Z0-9]+?\.txt)";
2021-08-04 20:42:40 +00:00
protected string _resource;
2017-09-04 02:20:56 +00:00
private readonly IDiskProvider _diskProvider;
private readonly IConfigFileProvider _configFileProvider;
2021-08-04 20:42:40 +00:00
public LogFileControllerBase(IDiskProvider diskProvider,
2017-09-04 02:20:56 +00:00
IConfigFileProvider configFileProvider,
2021-08-04 20:42:40 +00:00
string resource)
2017-09-04 02:20:56 +00:00
{
_diskProvider = diskProvider;
_configFileProvider = configFileProvider;
2021-08-04 20:42:40 +00:00
_resource = resource;
2017-09-04 02:20:56 +00:00
}
2021-08-04 20:42:40 +00:00
[HttpGet]
public List<LogFileResource> GetLogFilesResponse()
2017-09-04 02:20:56 +00:00
{
var result = new List<LogFileResource>();
var files = GetLogFiles().ToList();
for (var i = 0; i < files.Count; i++)
2017-09-04 02:20:56 +00:00
{
var file = files[i];
var filename = Path.GetFileName(file);
2017-09-04 02:20:56 +00:00
result.Add(new LogFileResource
{
Id = i + 1,
Filename = filename,
LastWriteTime = _diskProvider.FileGetLastWrite(file),
2021-08-04 20:42:40 +00:00
ContentsUrl = string.Format("{0}/api/v1/{1}/{2}", _configFileProvider.UrlBase, _resource, filename),
2017-09-04 02:20:56 +00:00
DownloadUrl = string.Format("{0}/{1}/{2}", _configFileProvider.UrlBase, DownloadUrlRoot, filename)
});
}
return result.OrderByDescending(l => l.LastWriteTime).ToList();
}
2021-08-04 20:42:40 +00:00
[HttpGet(@"{filename:regex([[-.a-zA-Z0-9]]+?\.txt)}")]
public IActionResult GetLogFileResponse(string filename)
2017-09-04 02:20:56 +00:00
{
2019-08-22 20:15:25 +00:00
LogManager.Flush();
2017-09-04 02:20:56 +00:00
var filePath = GetLogFilePath(filename);
if (!_diskProvider.FileExists(filePath))
{
2021-08-04 20:42:40 +00:00
return NotFound();
}
2017-09-04 02:20:56 +00:00
2021-08-04 20:42:40 +00:00
return PhysicalFile(filePath, "text/plain");
2017-09-04 02:20:56 +00:00
}
protected abstract IEnumerable<string> GetLogFiles();
protected abstract string GetLogFilePath(string filename);
protected abstract string DownloadUrlRoot { get; }
}
2017-10-31 01:28:29 +00:00
}