2024-10-25 14:15:12 -04:00
|
|
|
// Copyright (C) 2024 Intro-Skipper Contributors <intro-skipper.org>
|
|
|
|
// SPDX-License-Identifier: GNU General Public License v3.0 only.
|
|
|
|
|
2022-11-23 01:20:48 -06:00
|
|
|
using System;
|
2024-08-10 10:25:06 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
2022-08-25 00:39:20 -05:00
|
|
|
using System.Net.Mime;
|
|
|
|
using System.Text;
|
2024-10-19 23:50:41 +02:00
|
|
|
using IntroSkipper.Data;
|
|
|
|
using IntroSkipper.Helper;
|
2022-08-25 00:39:20 -05:00
|
|
|
using MediaBrowser.Common;
|
2024-05-13 23:50:51 +02:00
|
|
|
using MediaBrowser.Common.Api;
|
2024-08-10 10:25:06 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2022-08-25 00:39:20 -05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2024-10-19 23:50:41 +02:00
|
|
|
namespace IntroSkipper.Controllers;
|
2022-08-25 00:39:20 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Troubleshooting controller.
|
|
|
|
/// </summary>
|
2024-05-13 23:50:51 +02:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
2022-08-25 00:39:20 -05:00
|
|
|
[ApiController]
|
|
|
|
[Produces(MediaTypeNames.Application.Json)]
|
|
|
|
[Route("IntroSkipper")]
|
|
|
|
public class TroubleshootingController : ControllerBase
|
|
|
|
{
|
2024-08-10 10:25:06 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2022-08-25 00:39:20 -05:00
|
|
|
private readonly IApplicationHost _applicationHost;
|
|
|
|
private readonly ILogger<TroubleshootingController> _logger;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TroubleshootingController"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="applicationHost">Application host.</param>
|
2024-08-10 10:25:06 +00:00
|
|
|
/// <param name="libraryManager">Library Manager.</param>
|
2022-08-25 00:39:20 -05:00
|
|
|
/// <param name="logger">Logger.</param>
|
|
|
|
public TroubleshootingController(
|
|
|
|
IApplicationHost applicationHost,
|
2024-08-10 10:25:06 +00:00
|
|
|
ILibraryManager libraryManager,
|
2022-08-25 00:39:20 -05:00
|
|
|
ILogger<TroubleshootingController> logger)
|
|
|
|
{
|
2024-08-10 10:25:06 +00:00
|
|
|
_libraryManager = libraryManager;
|
2022-08-25 00:39:20 -05:00
|
|
|
_applicationHost = applicationHost;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a Markdown formatted support bundle.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Support bundle created.</response>
|
|
|
|
/// <returns>Support bundle.</returns>
|
|
|
|
[HttpGet("SupportBundle")]
|
|
|
|
[Produces(MediaTypeNames.Text.Plain)]
|
|
|
|
public ActionResult<string> GetSupportBundle()
|
|
|
|
{
|
2024-04-20 13:04:56 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(Plugin.Instance);
|
|
|
|
|
2022-08-25 00:39:20 -05:00
|
|
|
var bundle = new StringBuilder();
|
|
|
|
|
|
|
|
bundle.Append("* Jellyfin version: ");
|
|
|
|
bundle.Append(_applicationHost.ApplicationVersionString);
|
|
|
|
bundle.Append('\n');
|
|
|
|
|
2024-04-20 12:21:07 +02:00
|
|
|
var version = Plugin.Instance.Version.ToString(3);
|
2022-11-23 01:20:48 -06:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-09-21 18:35:10 +02:00
|
|
|
var commit = Commit.CommitHash;
|
2022-11-23 01:20:48 -06:00
|
|
|
if (!string.IsNullOrWhiteSpace(commit))
|
|
|
|
{
|
|
|
|
version += string.Concat("+", commit.AsSpan(0, 12));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Unable to append commit to version: {Exception}", ex);
|
|
|
|
}
|
|
|
|
|
2022-08-25 00:39:20 -05:00
|
|
|
bundle.Append("* Plugin version: ");
|
2022-11-23 01:20:48 -06:00
|
|
|
bundle.Append(version);
|
2022-08-25 00:39:20 -05:00
|
|
|
bundle.Append('\n');
|
|
|
|
|
|
|
|
bundle.Append("* Queue contents: ");
|
2024-04-20 12:21:07 +02:00
|
|
|
bundle.Append(Plugin.Instance.TotalQueued);
|
2023-06-08 00:51:18 -05:00
|
|
|
bundle.Append(" episodes, ");
|
2024-04-20 12:21:07 +02:00
|
|
|
bundle.Append(Plugin.Instance.TotalSeasons);
|
2023-06-08 00:51:18 -05:00
|
|
|
bundle.Append(" seasons\n");
|
2022-08-25 00:39:20 -05:00
|
|
|
|
2022-11-06 21:25:23 -06:00
|
|
|
bundle.Append("* Warnings: `");
|
|
|
|
bundle.Append(WarningManager.GetWarnings());
|
|
|
|
bundle.Append("`\n");
|
|
|
|
|
2022-08-28 22:35:43 -05:00
|
|
|
bundle.Append(FFmpegWrapper.GetChromaprintLogs());
|
2022-08-25 00:39:20 -05:00
|
|
|
|
|
|
|
return bundle.ToString();
|
|
|
|
}
|
2024-08-10 10:25:06 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a Markdown formatted support bundle.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Support bundle created.</response>
|
|
|
|
/// <returns>Support bundle.</returns>
|
|
|
|
[HttpGet("Storage")]
|
|
|
|
[Produces(MediaTypeNames.Text.Plain)]
|
|
|
|
public ActionResult<string> GetFreeSpace()
|
|
|
|
{
|
|
|
|
ArgumentNullException.ThrowIfNull(Plugin.Instance);
|
|
|
|
var bundle = new StringBuilder();
|
|
|
|
|
|
|
|
var libraries = _libraryManager.GetVirtualFolders();
|
|
|
|
foreach (var library in libraries)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DriveInfo driveInfo = new DriveInfo(library.Locations[0]);
|
|
|
|
// Get available free space in bytes
|
|
|
|
long availableFreeSpace = driveInfo.AvailableFreeSpace;
|
|
|
|
|
|
|
|
// Get total size of the drive in bytes
|
|
|
|
long totalSize = driveInfo.TotalSize;
|
|
|
|
|
|
|
|
// Get total used space in Percentage
|
|
|
|
double usedSpacePercentage = totalSize > 0 ? (totalSize - availableFreeSpace) / (double)totalSize * 100 : 0;
|
|
|
|
|
|
|
|
bundle.Append(CultureInfo.CurrentCulture, $"Library: {library.Name}\n");
|
|
|
|
bundle.Append(CultureInfo.CurrentCulture, $"Drive: {driveInfo.Name}\n");
|
|
|
|
bundle.Append(CultureInfo.CurrentCulture, $"Total Size: {GetHumanReadableSize(totalSize)}\n");
|
|
|
|
bundle.Append(CultureInfo.CurrentCulture, $"Available Free Space: {GetHumanReadableSize(availableFreeSpace)}\n");
|
|
|
|
bundle.Append(CultureInfo.CurrentCulture, $"Total used in Percentage: {Math.Round(usedSpacePercentage, 2)}%\n\n");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Unable to get DriveInfo: {Exception}", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bundle.ToString().TrimEnd('\n');
|
|
|
|
}
|
|
|
|
|
2024-09-10 18:08:42 +02:00
|
|
|
private static string GetHumanReadableSize(long bytes)
|
2024-08-10 10:25:06 +00:00
|
|
|
{
|
|
|
|
string[] sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
|
|
double len = bytes;
|
|
|
|
int order = 0;
|
|
|
|
|
|
|
|
while (len >= 1024 && order < sizes.Length - 1)
|
|
|
|
{
|
|
|
|
order++;
|
2024-09-10 18:08:42 +02:00
|
|
|
len /= 1024;
|
2024-08-10 10:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $"{len:0.##} {sizes[order]}";
|
|
|
|
}
|
2022-08-25 00:39:20 -05:00
|
|
|
}
|