2013-05-20 03:30:02 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-10-05 11:15:48 +03:00
|
|
|
using System.Linq;
|
|
|
|
using Nancy;
|
2013-08-24 05:42:45 +03:00
|
|
|
using Newtonsoft.Json;
|
2013-10-05 11:15:48 +03:00
|
|
|
using NzbDrone.Api.Extensions;
|
2013-05-20 03:30:02 +03:00
|
|
|
using NzbDrone.Api.REST;
|
|
|
|
using NzbDrone.Core.Update;
|
|
|
|
using NzbDrone.Api.Mapping;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Update
|
|
|
|
{
|
|
|
|
public class UpdateModule : NzbDroneRestModule<UpdateResource>
|
|
|
|
{
|
2013-09-28 21:48:30 +03:00
|
|
|
private readonly IRecentUpdateProvider _recentUpdateProvider;
|
2013-10-05 11:15:48 +03:00
|
|
|
private readonly IInstallUpdates _installUpdateService;
|
2013-05-20 03:30:02 +03:00
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
public UpdateModule(IRecentUpdateProvider recentUpdateProvider,
|
|
|
|
IInstallUpdates installUpdateService)
|
2013-05-20 03:30:02 +03:00
|
|
|
{
|
2013-09-28 21:48:30 +03:00
|
|
|
_recentUpdateProvider = recentUpdateProvider;
|
2013-10-05 11:15:48 +03:00
|
|
|
_installUpdateService = installUpdateService;
|
2013-09-28 21:48:30 +03:00
|
|
|
GetResourceAll = GetRecentUpdates;
|
2013-10-05 11:15:48 +03:00
|
|
|
Post["/"] = x=> InstallUpdate();
|
2013-05-20 03:30:02 +03:00
|
|
|
}
|
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
private List<UpdateResource> GetRecentUpdates()
|
2013-05-20 03:30:02 +03:00
|
|
|
{
|
2013-10-05 11:15:48 +03:00
|
|
|
var resources = _recentUpdateProvider.GetRecentUpdatePackages()
|
|
|
|
.OrderByDescending(u => u.Version)
|
|
|
|
.InjectTo<List<UpdateResource>>();
|
2013-05-20 03:30:02 +03:00
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
if (resources.Any())
|
2013-05-20 03:30:02 +03:00
|
|
|
{
|
2013-10-05 11:15:48 +03:00
|
|
|
resources.First().Latest = true;
|
2013-05-20 03:30:02 +03:00
|
|
|
}
|
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
return resources;
|
2013-05-20 03:30:02 +03:00
|
|
|
}
|
2013-09-28 21:48:30 +03:00
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
private Response InstallUpdate()
|
2013-09-28 21:48:30 +03:00
|
|
|
{
|
2013-10-05 11:15:48 +03:00
|
|
|
var updateResource = Request.Body.FromJson<UpdateResource>();
|
|
|
|
|
|
|
|
var updatePackage = updateResource.InjectTo<UpdatePackage>();
|
|
|
|
_installUpdateService.InstallUpdate(updatePackage);
|
|
|
|
|
|
|
|
return updateResource.AsResponse();
|
2013-09-28 21:48:30 +03:00
|
|
|
}
|
2013-05-20 03:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UpdateResource : RestResource
|
|
|
|
{
|
2013-08-24 05:42:45 +03:00
|
|
|
[JsonConverter(typeof(Newtonsoft.Json.Converters.VersionConverter))]
|
2013-05-20 03:30:02 +03:00
|
|
|
public Version Version { get; set; }
|
2013-08-24 05:42:45 +03:00
|
|
|
|
|
|
|
public String Branch { get; set; }
|
|
|
|
public DateTime ReleaseDate { get; set; }
|
2013-05-20 03:30:02 +03:00
|
|
|
public String FileName { get; set; }
|
|
|
|
public String Url { get; set; }
|
2013-10-05 11:15:48 +03:00
|
|
|
public Boolean Latest { get; set; }
|
2013-09-28 21:48:30 +03:00
|
|
|
public UpdateChanges Changes { get; set; }
|
2013-05-20 03:30:02 +03:00
|
|
|
}
|
|
|
|
}
|