2011-10-21 05:04:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-11-22 06:55:09 +00:00
|
|
|
|
using System.Globalization;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
2011-10-29 04:54:33 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-11-14 02:54:09 +00:00
|
|
|
|
public class UpdateProvider
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2012-03-07 02:59:43 +00:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2011-11-22 06:55:09 +00:00
|
|
|
|
|
2011-11-14 01:27:11 +00:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2013-04-12 23:28:05 +00:00
|
|
|
|
private readonly Logger _logger;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
private static readonly Regex parseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
|
2011-11-03 05:04:14 +00:00
|
|
|
|
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
|
public UpdateProvider(IHttpProvider httpProvider, IConfigService configService,
|
2013-04-12 23:28:05 +00:00
|
|
|
|
EnvironmentProvider environmentProvider, DiskProvider diskProvider, Logger logger)
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
|
|
|
|
_httpProvider = httpProvider;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
_configService = configService;
|
2012-03-07 02:59:43 +00:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2011-11-14 01:27:11 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2013-04-12 23:28:05 +00:00
|
|
|
|
_logger = logger;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UpdateProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-12 23:28:05 +00:00
|
|
|
|
private IEnumerable<UpdatePackage> GetAvailablePackages()
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
|
|
|
|
var updateList = new List<UpdatePackage>();
|
2013-02-24 06:48:52 +00:00
|
|
|
|
var updateUrl = _configService.UpdateUrl;
|
2011-12-01 07:10:50 +00:00
|
|
|
|
var rawUpdateList = _httpProvider.DownloadString(updateUrl);
|
2011-11-13 07:27:16 +00:00
|
|
|
|
var matches = parseRegex.Matches(rawUpdateList);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
|
|
|
|
foreach (Match match in matches)
|
|
|
|
|
{
|
|
|
|
|
var updatePackage = new UpdatePackage();
|
|
|
|
|
updatePackage.FileName = match.Groups["filename"].Value;
|
2011-12-01 07:10:50 +00:00
|
|
|
|
updatePackage.Url = updateUrl + updatePackage.FileName;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
updatePackage.Version = new Version(match.Groups["version"].Value);
|
|
|
|
|
updateList.Add(updatePackage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateList;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-12 23:28:05 +00:00
|
|
|
|
public virtual UpdatePackage GetAvailableUpdate(Version currentVersion)
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
|
|
|
|
var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
|
|
|
|
|
|
2012-02-05 06:34:36 +00:00
|
|
|
|
if (latestAvailable != null && latestAvailable.Version > currentVersion)
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
2013-04-12 23:28:05 +00:00
|
|
|
|
_logger.Debug("An update is available ({0}) => ({1})", currentVersion, latestAvailable.Version);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
return latestAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-12 23:28:05 +00:00
|
|
|
|
_logger.Trace("No updates available");
|
2011-10-21 05:04:26 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2011-11-22 06:55:09 +00:00
|
|
|
|
|
|
|
|
|
public virtual Dictionary<DateTime, string> UpdateLogFile()
|
|
|
|
|
{
|
|
|
|
|
var list = new Dictionary<DateTime, string>();
|
|
|
|
|
CultureInfo provider = CultureInfo.InvariantCulture;
|
|
|
|
|
|
2012-03-07 02:59:43 +00:00
|
|
|
|
if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder()))
|
2011-11-22 06:55:09 +00:00
|
|
|
|
{
|
2012-03-07 02:59:43 +00:00
|
|
|
|
var files = _diskProvider.GetFiles(_environmentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly).ToList();
|
2011-11-22 06:55:09 +00:00
|
|
|
|
|
2011-12-01 07:10:50 +00:00
|
|
|
|
foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name))
|
2011-11-22 06:55:09 +00:00
|
|
|
|
{
|
|
|
|
|
list.Add(DateTime.ParseExact(file.Name.Replace(file.Extension, string.Empty), "yyyy.MM.dd-H-mm", provider), file.FullName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2011-10-21 05:04:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|