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
{
Task SendRescanRequest();
Task SendRefreshRequest(string kind, Guid id);
}

View File

@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Core.Controllers;
using Microsoft.AspNetCore.Http;
@ -65,6 +66,22 @@ public class Misc(MiscRepository repo) : BaseApi
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>
/// List items to refresh.
/// </summary>

View File

@ -50,7 +50,7 @@ public class ShowApi(ILibraryManager libraryManager) : CrudThumbsApi<Show>(libra
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Show"/>.</param>
/// <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")]
[PartialPermission(Kind.Write)]
[ProducesResponseType(StatusCodes.Status204NoContent)]

View File

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