using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Api;
using MediaBrowser.Model.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
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 ScheduledTaskHelpers.GetTaskInfo(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, Required] string taskId)
{
var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));
if (task is 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.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult StartTask([FromRoute, Required] string taskId)
{
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
if (task is null)
{
return NotFound();
}
_taskManager.Execute(task, new TaskOptions());
return NoContent();
}
///
/// 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.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult StopTask([FromRoute, Required] string taskId)
{
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
if (task is null)
{
return NotFound();
}
_taskManager.Cancel(task);
return NoContent();
}
///
/// 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.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult UpdateTask(
[FromRoute, Required] string taskId,
[FromBody, Required] TaskTriggerInfo[] triggerInfos)
{
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
if (task is null)
{
return NotFound();
}
task.Triggers = triggerInfos;
return NoContent();
}
}