using System; using System.Threading; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Exceptions; namespace Kyoo.Core.Tasks { /// /// A task to register a new episode /// [TaskMetadata("register-sub", "Register subtitle", "Register a new subtitle")] public class RegisterSubtitle : ITask { /// /// An identifier to extract metadata from paths. /// private readonly IIdentifier _identifier; /// /// The library manager used to register the episode. /// private readonly ILibraryManager _libraryManager; /// /// Create a new task. /// /// An identifier to extract metadata from paths. /// The library manager used to register the episode. public RegisterSubtitle(IIdentifier identifier, ILibraryManager libraryManager) { _identifier = identifier; _libraryManager = libraryManager; } /// public TaskParameters GetParameters() { return new() { TaskParameter.CreateRequired("path", "The path of the subtitle file") }; } /// public async Task Run(TaskParameters arguments, IProgress progress, CancellationToken cancellationToken) { string path = arguments["path"].As(); try { progress.Report(0); Track track = await _identifier.IdentifyTrack(path); progress.Report(25); if (track.Episode == null) throw new TaskFailedException($"No episode identified for the track at {path}"); if (track.Episode.ID == 0) { if (track.Episode.Slug != null) track.Episode = await _libraryManager.Get(track.Episode.Slug); else if (track.Episode.Path != null) { track.Episode = await _libraryManager.GetOrDefault(x => x.Path.StartsWith(track.Episode.Path)); if (track.Episode == null) throw new TaskFailedException($"No episode found for the track at: {path}."); } else throw new TaskFailedException($"No episode identified for the track at {path}"); } progress.Report(50); await _libraryManager.Create(track); progress.Report(100); } catch (IdentificationFailedException ex) { throw new TaskFailedException(ex); } } } }