39 lines
2 KiB
C#
39 lines
2 KiB
C#
using System.Text.Json;
|
|
|
|
namespace Jellyfin.Plugin.SmartPlaylist {
|
|
public interface IStore {
|
|
Task<SmartPlaylistDto?> GetSmartPlaylistAsync(SmartPlaylistId smartPlaylistId);
|
|
Task<SmartPlaylistDto[]> GetAllSmartPlaylistsAsync();
|
|
Task SaveSmartPlaylistAsync(SmartPlaylistDto smartPlaylist);
|
|
void DeleteSmartPlaylist(SmartPlaylistId smartPlaylistId);
|
|
}
|
|
|
|
public class Store : IStore {
|
|
private readonly ISmartPlaylistFileSystem _fileSystem;
|
|
public Store(ISmartPlaylistFileSystem fileSystem) {
|
|
_fileSystem = fileSystem;
|
|
}
|
|
private async Task<SmartPlaylistDto?> LoadPlaylistAsync(string filename) {
|
|
await using var r = File.OpenRead(filename);
|
|
return await JsonSerializer.DeserializeAsync<SmartPlaylistDto>(r).ConfigureAwait(false);
|
|
}
|
|
public async Task<SmartPlaylistDto?> GetSmartPlaylistAsync(SmartPlaylistId smartPlaylistId) {
|
|
string filename = _fileSystem.FindSmartPlaylistFilePath(smartPlaylistId);
|
|
return await LoadPlaylistAsync(filename).ConfigureAwait(false);
|
|
}
|
|
public async Task<SmartPlaylistDto[]> GetAllSmartPlaylistsAsync() {
|
|
var t = _fileSystem.FindAllSmartPlaylistFilePaths().Select(LoadPlaylistAsync).ToArray();
|
|
await Task.WhenAll(t).ConfigureAwait(false);
|
|
return t.Where(x => x != null).Select(x => x.Result).ToArray();
|
|
}
|
|
public async Task SaveSmartPlaylistAsync(SmartPlaylistDto smartPlaylist) {
|
|
string filename = _fileSystem.GetSmartPlaylistFilePath(smartPlaylist.Id);
|
|
await using var w = File.Create(filename);
|
|
await JsonSerializer.SerializeAsync(w, smartPlaylist).ConfigureAwait(false);
|
|
}
|
|
public void DeleteSmartPlaylist(SmartPlaylistId smartPlaylistId) {
|
|
string filename = _fileSystem.FindSmartPlaylistFilePath(smartPlaylistId);
|
|
if (File.Exists(filename)) { File.Delete(filename); }
|
|
}
|
|
}
|
|
}
|