112 lines
3.9 KiB
C#
112 lines
3.9 KiB
C#
using Jellyfin.Plugin.MediaCleaner.Helpers;
|
|
using Jellyfin.Plugin.MediaCleaner.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Plugin.MediaCleaner.Enums;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.Linq;
|
|
|
|
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
|
|
|
|
public record ConnectionTestRequest(string Address, string ApiKey);
|
|
|
|
public record RadarrMovie(
|
|
[property: JsonPropertyName("id")] int? Id,
|
|
[property: JsonPropertyName("title")] string? Title
|
|
);
|
|
|
|
[Route("radarr")]
|
|
public class RadarrController : Controller
|
|
{
|
|
private async Task<ObjectResult> GetRadarrMovieInfo(MovieInfo movieInfo)
|
|
{
|
|
HttpHelper httpHelper = new(ServerType.Radarr);
|
|
var responseBody = await httpHelper.SendHttpRequestAsync(
|
|
HttpMethod.Get,
|
|
$"/api/v3/movie?tmdbId={Uri.EscapeDataString(movieInfo.TmdbId ?? string.Empty)}&excludeLocalCovers=false"
|
|
).ConfigureAwait(false);
|
|
|
|
var movies = JsonSerializer.Deserialize<List<RadarrMovie>>(responseBody.GetRawText());
|
|
var movie = movies?.FirstOrDefault();
|
|
|
|
if (movie == null)
|
|
{
|
|
return NotFound("Movie not found in Radarr library.");
|
|
}
|
|
|
|
return Ok(movie);
|
|
}
|
|
|
|
[HttpPost("deleteMovieFromRadarr")]
|
|
public async Task<IActionResult> DeleteMovieFromRadarr([FromBody] MovieInfo movieInfo){
|
|
|
|
if (movieInfo == null || string.IsNullOrEmpty(movieInfo.TmdbId))
|
|
{
|
|
return BadRequest("Invalid movie information provided.");
|
|
}
|
|
|
|
try
|
|
{
|
|
var radarrMovieInfoResult = await GetRadarrMovieInfo(movieInfo).ConfigureAwait(false);
|
|
|
|
if(radarrMovieInfoResult.StatusCode != StatusCodes.Status200OK || radarrMovieInfoResult.Value is not RadarrMovie){
|
|
return radarrMovieInfoResult;
|
|
}
|
|
|
|
RadarrMovie movie = (RadarrMovie)radarrMovieInfoResult.Value;
|
|
|
|
HttpHelper httpHelper = new(ServerType.Radarr);
|
|
var responseBody = await httpHelper.SendHttpRequestAsync(
|
|
HttpMethod.Delete,
|
|
$"/api/v3/movie/{movie.Id}?deleteFiles=true&addImportExclusion=true"
|
|
).ConfigureAwait(false);
|
|
|
|
// Radarr typically returns an empty body on successful delete.
|
|
return Ok(responseBody);
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, $"An unexpected error occurred. {e.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPost("testConnection")]
|
|
public async Task<IActionResult> TestConnection([FromBody] ConnectionTestRequest request)
|
|
{
|
|
if (request == null || string.IsNullOrWhiteSpace(request.Address) || string.IsNullOrWhiteSpace(request.ApiKey))
|
|
{
|
|
return BadRequest("Address and ApiKey are required.");
|
|
}
|
|
|
|
var address = request.Address.Trim();
|
|
if (!address.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
|
|
!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
address = "http://" + address;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var testHttpClient = new HttpClient();
|
|
using var httpRequest = new HttpRequestMessage(HttpMethod.Get, address);
|
|
httpRequest.Headers.Add("X-Api-Key", request.ApiKey);
|
|
|
|
var response = await testHttpClient.SendAsync(httpRequest).ConfigureAwait(false);
|
|
return Ok(new { success = response.IsSuccessStatusCode });
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
return StatusCode(StatusCodes.Status502BadGateway, e.Message);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
|
|
}
|
|
}
|
|
}
|