2011-10-21 08:04:26 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-11-14 03:57:03 +03:00
|
|
|
using System.Diagnostics;
|
2011-11-22 09:55:09 +03:00
|
|
|
using System.Globalization;
|
2011-10-21 08:04:26 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using NLog;
|
|
|
|
using Ninject;
|
2011-10-29 07:54:33 +03:00
|
|
|
using NzbDrone.Common;
|
2011-10-21 08:04:26 +03:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-11-13 07:07:06 +03:00
|
|
|
|
2011-10-21 08:04:26 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-11-14 05:54:09 +03:00
|
|
|
public class UpdateProvider
|
2011-10-21 08:04:26 +03:00
|
|
|
{
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
private readonly ConfigProvider _configProvider;
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-11-22 09:55:09 +03:00
|
|
|
|
2011-11-14 04:27:11 +03:00
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-11-13 10:27:16 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-10-21 08:04:26 +03:00
|
|
|
|
2011-11-13 10:27:16 +03:00
|
|
|
private static readonly Regex parseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
|
2012-02-03 03:11:18 +03:00
|
|
|
public const string DEFAULT_UPDATE_URL = @"http://update.nzbdrone.com/master/";
|
2011-11-03 08:04:14 +03:00
|
|
|
|
2011-10-21 08:04:26 +03:00
|
|
|
|
|
|
|
[Inject]
|
2011-11-22 09:55:09 +03:00
|
|
|
public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider,
|
|
|
|
EnviromentProvider enviromentProvider, DiskProvider diskProvider)
|
2011-10-21 08:04:26 +03:00
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_configProvider = configProvider;
|
|
|
|
_enviromentProvider = enviromentProvider;
|
2011-11-14 04:27:11 +03:00
|
|
|
_diskProvider = diskProvider;
|
2011-10-21 08:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public UpdateProvider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<UpdatePackage> GetAvailablePackages()
|
|
|
|
{
|
|
|
|
var updateList = new List<UpdatePackage>();
|
2011-12-01 10:10:50 +03:00
|
|
|
var updateUrl = _configProvider.UpdateUrl;
|
|
|
|
var rawUpdateList = _httpProvider.DownloadString(updateUrl);
|
2011-11-13 10:27:16 +03:00
|
|
|
var matches = parseRegex.Matches(rawUpdateList);
|
2011-10-21 08:04:26 +03:00
|
|
|
|
|
|
|
foreach (Match match in matches)
|
|
|
|
{
|
|
|
|
var updatePackage = new UpdatePackage();
|
|
|
|
updatePackage.FileName = match.Groups["filename"].Value;
|
2011-12-01 10:10:50 +03:00
|
|
|
updatePackage.Url = updateUrl + updatePackage.FileName;
|
2011-10-21 08:04:26 +03:00
|
|
|
updatePackage.Version = new Version(match.Groups["version"].Value);
|
|
|
|
updateList.Add(updatePackage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateList;
|
|
|
|
}
|
|
|
|
|
2012-02-05 09:34:36 +03:00
|
|
|
public virtual UpdatePackage GetAvilableUpdate(Version currentVersion)
|
2011-10-21 08:04:26 +03:00
|
|
|
{
|
|
|
|
var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
|
|
|
|
|
2012-02-05 09:34:36 +03:00
|
|
|
if (latestAvailable != null && latestAvailable.Version > currentVersion)
|
2011-10-21 08:04:26 +03:00
|
|
|
{
|
2012-02-05 09:34:36 +03:00
|
|
|
logger.Debug("An update is available ({0}) => ({1})", currentVersion, latestAvailable.Version);
|
2011-10-21 08:04:26 +03:00
|
|
|
return latestAvailable;
|
|
|
|
}
|
|
|
|
|
2011-11-13 10:27:16 +03:00
|
|
|
logger.Trace("No updates available");
|
2011-10-21 08:04:26 +03:00
|
|
|
return null;
|
|
|
|
}
|
2011-11-22 09:55:09 +03:00
|
|
|
|
|
|
|
public virtual Dictionary<DateTime, string> UpdateLogFile()
|
|
|
|
{
|
|
|
|
var list = new Dictionary<DateTime, string>();
|
|
|
|
CultureInfo provider = CultureInfo.InvariantCulture;
|
|
|
|
|
|
|
|
if (_diskProvider.FolderExists(_enviromentProvider.GetUpdateLogFolder()))
|
|
|
|
{
|
|
|
|
var files = _diskProvider.GetFiles(_enviromentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly).ToList();
|
|
|
|
|
2011-12-01 10:10:50 +03:00
|
|
|
foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name))
|
2011-11-22 09:55:09 +03: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 08:04:26 +03:00
|
|
|
}
|
|
|
|
}
|