Ship the plugin sources, release zip, and SeerrFin-style manifest.json so Jellyfin can install from the Gitea raw repository URL. Co-authored-by: Cursor <cursoragent@cursor.com>
187 lines
6.1 KiB
C#
187 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MediaBrowser.Common.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.Request.Storage;
|
|
|
|
/// <summary>
|
|
/// Thread-safe JSON persistence for media requests.
|
|
/// </summary>
|
|
public sealed class RequestStore
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
WriteIndented = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
};
|
|
|
|
private readonly ILogger<RequestStore> _logger;
|
|
private readonly string _filePath;
|
|
private readonly SemaphoreSlim _gate = new(1, 1);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestStore"/> class.
|
|
/// </summary>
|
|
/// <param name="applicationPaths">Application paths.</param>
|
|
/// <param name="logger">Logger.</param>
|
|
public RequestStore(IApplicationPaths applicationPaths, ILogger<RequestStore> logger)
|
|
{
|
|
_logger = logger;
|
|
var directory = Path.Combine(applicationPaths.PluginConfigurationsPath, "Jellyfin.Plugin.Request");
|
|
Directory.CreateDirectory(directory);
|
|
_filePath = Path.Combine(directory, "requests.json");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads all requests.
|
|
/// </summary>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>Request list.</returns>
|
|
public async Task<IReadOnlyList<MediaRequest>> GetAllAsync(CancellationToken cancellationToken)
|
|
{
|
|
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
return await ReadUnlockedAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a request and persists it.
|
|
/// </summary>
|
|
/// <param name="request">Request to add.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>The stored request.</returns>
|
|
public async Task<MediaRequest> AddAsync(MediaRequest request, CancellationToken cancellationToken)
|
|
{
|
|
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
var items = await ReadUnlockedAsync(cancellationToken).ConfigureAwait(false);
|
|
items.Add(request);
|
|
await WriteUnlockedAsync(items, cancellationToken).ConfigureAwait(false);
|
|
return request;
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing request.
|
|
/// </summary>
|
|
/// <param name="request">Updated request.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>True when updated.</returns>
|
|
public async Task<bool> UpdateAsync(MediaRequest request, CancellationToken cancellationToken)
|
|
{
|
|
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
var items = await ReadUnlockedAsync(cancellationToken).ConfigureAwait(false);
|
|
var index = items.FindIndex(item => item.Id == request.Id);
|
|
if (index < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
items[index] = request;
|
|
await WriteUnlockedAsync(items, cancellationToken).ConfigureAwait(false);
|
|
return true;
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a request by identifier.
|
|
/// </summary>
|
|
/// <param name="id">Request identifier.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>The deleted request, or null.</returns>
|
|
public async Task<MediaRequest?> DeleteAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
var items = await ReadUnlockedAsync(cancellationToken).ConfigureAwait(false);
|
|
var existing = items.FirstOrDefault(item => item.Id == id);
|
|
if (existing is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
items.Remove(existing);
|
|
await WriteUnlockedAsync(items, cancellationToken).ConfigureAwait(false);
|
|
return existing;
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds a request by identifier.
|
|
/// </summary>
|
|
/// <param name="id">Request identifier.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>The request, or null.</returns>
|
|
public async Task<MediaRequest?> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
var items = await GetAllAsync(cancellationToken).ConfigureAwait(false);
|
|
return items.FirstOrDefault(item => item.Id == id);
|
|
}
|
|
|
|
private async Task<List<MediaRequest>> ReadUnlockedAsync(CancellationToken cancellationToken)
|
|
{
|
|
if (!File.Exists(_filePath))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
try
|
|
{
|
|
await using var stream = File.OpenRead(_filePath);
|
|
var items = await JsonSerializer.DeserializeAsync<List<MediaRequest>>(stream, JsonOptions, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
return items ?? [];
|
|
}
|
|
catch (Exception ex) when (ex is IOException or JsonException or UnauthorizedAccessException)
|
|
{
|
|
_logger.LogError(ex, "Failed to read request store at {Path}", _filePath);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
private async Task WriteUnlockedAsync(List<MediaRequest> items, CancellationToken cancellationToken)
|
|
{
|
|
var directory = Path.GetDirectoryName(_filePath);
|
|
if (!string.IsNullOrEmpty(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
var tempPath = _filePath + ".tmp";
|
|
await using (var stream = File.Create(tempPath))
|
|
{
|
|
await JsonSerializer.SerializeAsync(stream, items, JsonOptions, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
File.Move(tempPath, _filePath, overwrite: true);
|
|
}
|
|
}
|