#nullable enable using System; using System.Collections.Generic; using System.Linq; using Jellyfin.Api.Constants; using MediaBrowser.Model.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Jellyfin.Api.Controllers { /// /// Scheduled Tasks Controller. /// [Authorize(Policy = Policies.RequiresElevation)] public class ScheduledTasksController : BaseJellyfinApiController { private readonly ITaskManager _taskManager; /// /// Initializes a new instance of the class. /// /// Instance of the interface. public ScheduledTasksController(ITaskManager taskManager) { _taskManager = taskManager; } /// /// Get tasks. /// /// Optional filter tasks that are hidden, or not. /// Optional filter tasks that are enabled, or not. /// Scheduled tasks retrieved. /// The list of scheduled tasks. [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] public IEnumerable GetTasks( [FromQuery] bool? isHidden, [FromQuery] bool? isEnabled) { IEnumerable tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name); foreach (var task in tasks) { if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask) { if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden) { continue; } if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled) { continue; } } yield return task; } } /// /// Get task by id. /// /// Task Id. /// Task retrieved. /// Task not found. /// An containing the task on success, or a if the task could not be found. [HttpGet("{TaskID}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult GetTask([FromRoute] string taskId) { var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } return ScheduledTaskHelpers.GetTaskInfo(task); } /// /// Start specified task. /// /// Task Id. /// Task started. /// Task not found. /// An on success, or a if the file could not be found. [HttpPost("Running/{TaskID}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult StartTask([FromRoute] string taskId) { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } _taskManager.Execute(task, new TaskOptions()); return Ok(); } /// /// Stop specified task. /// /// Task Id. /// Task stopped. /// Task not found. /// An on success, or a if the file could not be found. [HttpDelete("Running/{TaskID}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult StopTask([FromRoute] string taskId) { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } _taskManager.Cancel(task); return Ok(); } /// /// Update specified task triggers. /// /// Task Id. /// Triggers. /// Task triggers updated. /// Task not found. /// An on success, or a if the file could not be found. [HttpPost("{TaskID}/Triggers")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult UpdateTask( [FromRoute] string taskId, [FromBody, BindRequired] TaskTriggerInfo[] triggerInfos) { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } task.Triggers = triggerInfos; return Ok(); } } }