2024-10-28 00:26:42 +01:00
|
|
|
using YamlDotNet.Serialization;
|
2024-10-24 23:53:21 +02:00
|
|
|
|
|
|
|
namespace Jellyfin.Plugin.SmartPlaylist {
|
|
|
|
public interface IStore {
|
2024-10-25 23:37:47 +02:00
|
|
|
Task<SmartPlaylistDto> GetSmartPlaylistAsync(SmartPlaylistId smartPlaylistId);
|
2024-10-24 23:53:21 +02:00
|
|
|
Task<SmartPlaylistDto[]> GetAllSmartPlaylistsAsync();
|
|
|
|
Task SaveSmartPlaylistAsync(SmartPlaylistDto smartPlaylist);
|
2024-12-21 01:09:41 +01:00
|
|
|
void DeleteSmartPlaylistById(SmartPlaylistId smartPlaylistId);
|
2024-10-25 02:18:13 +02:00
|
|
|
void DeleteSmartPlaylist(SmartPlaylistDto smartPlaylist);
|
2024-10-24 23:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Store : IStore {
|
|
|
|
private readonly ISmartPlaylistFileSystem _fileSystem;
|
|
|
|
public Store(ISmartPlaylistFileSystem fileSystem) {
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
2024-10-25 23:37:47 +02:00
|
|
|
private async Task<SmartPlaylistDto> LoadPlaylistAsync(string filename) {
|
2024-11-11 18:00:55 +01:00
|
|
|
var r = await File.ReadAllTextAsync(filename);
|
2024-11-08 03:41:24 +01:00
|
|
|
if (r.Equals("")) {
|
|
|
|
r = "{}";
|
|
|
|
}
|
2024-10-28 00:26:42 +01:00
|
|
|
var dto = new DeserializerBuilder().Build().Deserialize<SmartPlaylistDto>(r);
|
|
|
|
if (dto == null)
|
|
|
|
{
|
2024-10-25 23:37:47 +02:00
|
|
|
throw new ApplicationException("");
|
|
|
|
}
|
2024-11-08 03:09:17 +01:00
|
|
|
if (dto.Id != Path.GetFileNameWithoutExtension(filename)) {
|
2024-10-30 19:33:01 +01:00
|
|
|
dto.Id = Path.GetFileNameWithoutExtension(filename);
|
|
|
|
}
|
2024-11-08 03:09:17 +01:00
|
|
|
if (dto.Name != Path.GetFileNameWithoutExtension(filename)) {
|
|
|
|
dto.Name = Path.GetFileNameWithoutExtension(filename);
|
|
|
|
}
|
2024-10-30 19:33:01 +01:00
|
|
|
if (dto.Filename != filename) {
|
|
|
|
dto.Filename = filename;
|
|
|
|
}
|
2024-10-25 02:18:13 +02:00
|
|
|
return dto;
|
2024-10-24 23:53:21 +02:00
|
|
|
}
|
2024-10-25 23:37:47 +02:00
|
|
|
public async Task<SmartPlaylistDto> GetSmartPlaylistAsync(SmartPlaylistId smartPlaylistId) {
|
2024-10-24 23:53:21 +02:00
|
|
|
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);
|
2024-10-28 00:26:42 +01:00
|
|
|
var text = new SerializerBuilder().Build().Serialize(smartPlaylist);
|
2024-11-11 18:00:55 +01:00
|
|
|
await File.WriteAllTextAsync(filename, text);
|
2024-10-24 23:53:21 +02:00
|
|
|
}
|
2024-12-21 01:09:41 +01:00
|
|
|
public void DeleteSmartPlaylistById(SmartPlaylistId smartPlaylistId) {
|
2024-10-25 02:18:13 +02:00
|
|
|
try {
|
|
|
|
string filename = _fileSystem.FindSmartPlaylistFilePath(smartPlaylistId);
|
|
|
|
if (File.Exists(filename)) { File.Delete(filename); }
|
|
|
|
} catch (System.InvalidOperationException) {}
|
|
|
|
}
|
|
|
|
public void DeleteSmartPlaylist(SmartPlaylistDto smartPlaylist) {
|
|
|
|
if (File.Exists(smartPlaylist.Filename)) { File.Delete(smartPlaylist.Filename); }
|
|
|
|
DeleteSmartPlaylistById(smartPlaylist.Id);
|
2024-10-24 23:53:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|