Add an api to publish a rescan request

This commit is contained in:
Zoe Roux 2024-05-02 01:02:10 +02:00
parent d63ad87971
commit c1ed16b871
No known key found for this signature in database
4 changed files with 30 additions and 27 deletions

View File

@ -23,5 +23,6 @@ namespace Kyoo.Abstractions.Controllers;
public interface IScanner public interface IScanner
{ {
Task SendRescanRequest();
Task SendRefreshRequest(string kind, Guid id); Task SendRefreshRequest(string kind, Guid id);
} }

View File

@ -19,6 +19,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Core.Controllers; using Kyoo.Core.Controllers;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -65,6 +66,22 @@ public class Misc(MiscRepository repo) : BaseApi
return NoContent(); return NoContent();
} }
/// <summary>
/// Rescan library
/// </summary>
/// <remark>
/// Trigger a complete library rescan
/// </remark>
/// <returns>Nothing</returns>
[HttpPost("/rescan")]
[PartialPermission(Kind.Write)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> RescanLibrary([FromServices] IScanner scanner)
{
await scanner.SendRescanRequest();
return NoContent();
}
/// <summary> /// <summary>
/// List items to refresh. /// List items to refresh.
/// </summary> /// </summary>

View File

@ -50,7 +50,7 @@ public class ShowApi(ILibraryManager libraryManager) : CrudThumbsApi<Show>(libra
/// </remarks> /// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Show"/>.</param> /// <param name="identifier">The ID or slug of the <see cref="Show"/>.</param>
/// <returns>Nothing</returns> /// <returns>Nothing</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response> /// <response code="404">No show with the given ID or slug could be found.</response>
[HttpPost("{identifier:id}/refresh")] [HttpPost("{identifier:id}/refresh")]
[PartialPermission(Kind.Write)] [PartialPermission(Kind.Write)]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]

View File

@ -19,7 +19,6 @@
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Utils; using Kyoo.Utils;
using RabbitMQ.Client; using RabbitMQ.Client;
@ -35,29 +34,17 @@ public class ScannerProducer : IScanner
_channel.QueueDeclare("scanner", exclusive: false, autoDelete: false); _channel.QueueDeclare("scanner", exclusive: false, autoDelete: false);
} }
private IRepository<T>.ResourceEventHandler _Publish<T>( private Task _Publish<T>(T message)
string exchange,
string type,
string action
)
where T : IResource, IQuery
{ {
return (T resource) => var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message, Utility.JsonOptions));
{ _channel.BasicPublish("", routingKey: "scanner", body: body);
Message<T> message =
new()
{
Action = action,
Type = type,
Value = resource,
};
_channel.BasicPublish(
exchange,
routingKey: message.AsRoutingKey(),
body: message.AsBytes()
);
return Task.CompletedTask; return Task.CompletedTask;
}; }
public Task SendRescanRequest()
{
var message = new { Action = "rescan", };
return _Publish(message);
} }
public Task SendRefreshRequest(string kind, Guid id) public Task SendRefreshRequest(string kind, Guid id)
@ -68,8 +55,6 @@ public class ScannerProducer : IScanner
Kind = kind.ToLowerInvariant(), Kind = kind.ToLowerInvariant(),
Id = id Id = id
}; };
var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message, Utility.JsonOptions)); return _Publish(message);
_channel.BasicPublish("", routingKey: "scanner", body: body);
return Task.CompletedTask;
} }
} }