From 8d2757f27a5bd12f807edc224e812573b0a9f19d Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Thu, 29 Jul 2010 17:45:40 -0700 Subject: [PATCH] Refactored packaging module - Adding source and updating modules are safer --HG-- branch : dev --- .../DefaultPackagingUpdater.cs | 9 +++- .../Services/PackagingSourceManager.cs | 45 ++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Packaging/DefaultPackagingUpdater.cs b/src/Orchard.Web/Modules/Orchard.Packaging/DefaultPackagingUpdater.cs index 604cd5a32..e2216b9b2 100644 --- a/src/Orchard.Web/Modules/Orchard.Packaging/DefaultPackagingUpdater.cs +++ b/src/Orchard.Web/Modules/Orchard.Packaging/DefaultPackagingUpdater.cs @@ -2,19 +2,24 @@ using Orchard.Environment; using Orchard.Environment.Extensions; using Orchard.Environment.Extensions.Models; +using Orchard.Localization; using Orchard.Packaging.Services; +using Orchard.UI.Notify; namespace Orchard.Packaging { [OrchardFeature("Gallery")] public class DefaultPackagingUpdater : IFeatureEventHandler { private readonly IPackagingSourceManager _packagingSourceManager; + private readonly INotifier _notifier; - public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager) { + public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager, INotifier notifier) { _packagingSourceManager = packagingSourceManager; + _notifier = notifier; } + public Localizer T { get; set; } + public void Install(Feature feature) { - // add http://orchardproject.net/gallery/feed as the default Modules Feed _packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedTitle = "Orchard Module Gallery", FeedUrl = "http://orchardproject.net/gallery/feed" }); } diff --git a/src/Orchard.Web/Modules/Orchard.Packaging/Services/PackagingSourceManager.cs b/src/Orchard.Web/Modules/Orchard.Packaging/Services/PackagingSourceManager.cs index 7478e0c6d..95780d7f5 100644 --- a/src/Orchard.Web/Modules/Orchard.Packaging/Services/PackagingSourceManager.cs +++ b/src/Orchard.Web/Modules/Orchard.Packaging/Services/PackagingSourceManager.cs @@ -8,32 +8,38 @@ using System.Xml.Linq; using System.Xml.Serialization; using Orchard.Environment.Extensions; using Orchard.FileSystems.AppData; +using Orchard.Localization; +using Orchard.UI.Notify; namespace Orchard.Packaging.Services { [OrchardFeature("PackagingServices")] public class PackagingSourceManager : IPackagingSourceManager { - private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof (List), new XmlRootAttribute("Sources")); + private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof(List), new XmlRootAttribute("Sources")); private readonly IAppDataFolder _appDataFolder; + private readonly INotifier _notifier; - public PackagingSourceManager(IAppDataFolder appDataFolder) { + public PackagingSourceManager(IAppDataFolder appDataFolder, INotifier notifier) { _appDataFolder = appDataFolder; + _notifier = notifier; + T = NullLocalizer.Instance; } + Localizer T { get; set; } + #region IPackagingSourceManager Members public IEnumerable GetSources() { string text = _appDataFolder.ReadFile(GetSourcesPath()); - if (string.IsNullOrEmpty(text)) { + if ( string.IsNullOrEmpty(text) ) { return Enumerable.Empty(); } var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath())); - return (IEnumerable) _sourceSerializer.Deserialize(textReader); + return (IEnumerable)_sourceSerializer.Deserialize(textReader); } public void AddSource(PackagingSource source) { - UpdateSource(source); - SaveSources(GetSources().Concat(new[] {source})); + SaveSources(GetSources().Concat(new[] { source }).GroupBy(x => x.FeedUrl).Select(g => g.First())); } public void RemoveSource(Guid id) { @@ -41,16 +47,16 @@ namespace Orchard.Packaging.Services { } public void UpdateLists() { - foreach (PackagingSource source in GetSources()) { + foreach ( PackagingSource source in GetSources() ) { UpdateSource(source); } } public IEnumerable GetModuleList(PackagingSource packagingSource = null) { - IEnumerable packageInfos = ( packagingSource == null ? GetSources() : new [] { packagingSource }) + IEnumerable packageInfos = ( packagingSource == null ? GetSources() : new[] { packagingSource } ) .SelectMany( source => - Bind(ParseFeed(_appDataFolder.ReadFile(GetFeedCachePath(source))), + Bind(ParseFeed(GetModuleListForSource(source)), feed => feed.Items.SelectMany( item => @@ -65,6 +71,13 @@ namespace Orchard.Packaging.Services { return packageInfos.ToArray(); } + private string GetModuleListForSource(PackagingSource source) { + if ( !_appDataFolder.FileExists(GetFeedCachePath(source)) ) { + UpdateSource(source); + } + return _appDataFolder.ReadFile(GetFeedCachePath(source)); + } + #endregion private static string GetSourcesPath() { @@ -83,8 +96,13 @@ namespace Orchard.Packaging.Services { } private void UpdateSource(PackagingSource source) { - XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace); - _appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting)); + try { + XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace); + _appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting)); + } + catch ( Exception e ) { + _notifier.Warning(T("Error loading content of feed '{0}': {1}", source.FeedUrl, e.Message)); + } } @@ -93,7 +111,7 @@ namespace Orchard.Packaging.Services { } private static IEnumerable Unit(T t) where T : class { - return t != null ? new[] {t} : Enumerable.Empty(); + return t != null ? new[] { t } : Enumerable.Empty(); } private static IEnumerable Bind(T t, Func> f) where T : class { @@ -101,6 +119,9 @@ namespace Orchard.Packaging.Services { } private SyndicationFeed ParseFeed(string content) { + if ( string.IsNullOrEmpty(( content )) ) + return new SyndicationFeed(); + var formatter = new Atom10FeedFormatter(); formatter.ReadFrom(XmlReader.Create(new StringReader(content))); return formatter.Feed;