Add refresh route

This commit is contained in:
Zoe Roux 2024-04-22 17:45:51 +02:00
parent 526ac1b8ab
commit 2273e99074
No known key found for this signature in database
4 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,27 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Threading.Tasks;
namespace Kyoo.Abstractions.Controllers;
public interface IScanner
{
Task SendRefreshRequest(string kind, Guid id);
}

View File

@ -41,6 +41,29 @@ namespace Kyoo.Core.Api;
public class EpisodeApi(ILibraryManager libraryManager)
: TranscoderApi<Episode>(libraryManager.Episodes)
{
/// <summary>
/// Refresh
/// </summary>
/// <remarks>
/// Ask a metadata refresh.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Episode"/>.</param>
/// <returns>Nothing</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response>
[HttpPost("{identifier:id}/refresh")]
[PartialPermission(Kind.Write)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> Refresh(Identifier identifier, [FromServices] IScanner scanner)
{
Guid id = await identifier.Match(
id => Task.FromResult(id),
async slug => (await libraryManager.Episodes.Get(slug)).Id
);
await scanner.SendRefreshRequest(nameof(Episode), id);
return NoContent();
}
/// <summary>
/// Get episode's show
/// </summary>

View File

@ -16,6 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using Kyoo.Abstractions.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -41,5 +42,6 @@ public static class RabbitMqModule
return factory.CreateConnection();
});
builder.Services.AddSingleton<RabbitProducer>();
builder.Services.AddSingleton<IScanner, ScannerProducer>();
}
}

View File

@ -0,0 +1,71 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Text;
using System.Text.Json;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Utils;
using RabbitMQ.Client;
namespace Kyoo.RabbitMq;
public class ScannerProducer : IScanner
{
private readonly IModel _channel;
public ScannerProducer(IConnection rabbitConnection)
{
_channel = rabbitConnection.CreateModel();
_channel.QueueDeclare("scanner", exclusive: false, autoDelete: false);
}
private IRepository<T>.ResourceEventHandler _Publish<T>(
string exchange,
string type,
string action
)
where T : IResource, IQuery
{
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;
};
}
public Task SendRefreshRequest(string kind, Guid id)
{
var message = new { Action = "refresh", Kind = kind.ToLowerInvariant(), Id = id };
var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message, Utility.JsonOptions));
_channel.BasicPublish("", routingKey: "scanner", body: body);
return Task.CompletedTask;
}
}