mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Updating the gallery module
--HG-- branch : nuget
This commit is contained in:
@@ -1,26 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.ServiceModel.Syndication;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using NuGet;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.FileSystems.AppData;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Notify;
|
||||
using Orchard.Packaging.Models;
|
||||
|
||||
namespace Orchard.Packaging.Services {
|
||||
[OrchardFeature("PackagingServices")]
|
||||
public class PackagingSourceManager : IPackagingSourceManager {
|
||||
private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof(List<PackagingSource>), new XmlRootAttribute("Sources"));
|
||||
private readonly IAppDataFolder _appDataFolder;
|
||||
private readonly INotifier _notifier;
|
||||
private const string ModulesFilter = "Orchard.Module.";
|
||||
private const string ThemesFilter = "Orchard.Theme.";
|
||||
|
||||
public PackagingSourceManager(IAppDataFolder appDataFolder, INotifier notifier) {
|
||||
_appDataFolder = appDataFolder;
|
||||
_notifier = notifier;
|
||||
private readonly IRepository<PackagingSourceRecord> _packagingSourceRecordRepository;
|
||||
|
||||
public PackagingSourceManager(IRepository<PackagingSourceRecord> packagingSourceRecordRepository) {
|
||||
_packagingSourceRecordRepository = packagingSourceRecordRepository;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
@@ -28,100 +24,49 @@ namespace Orchard.Packaging.Services {
|
||||
|
||||
#region IPackagingSourceManager Members
|
||||
|
||||
public IEnumerable<PackagingSource> GetSources() {
|
||||
string text = _appDataFolder.ReadFile(GetSourcesPath());
|
||||
if ( string.IsNullOrEmpty(text) ) {
|
||||
return Enumerable.Empty<PackagingSource>();
|
||||
}
|
||||
|
||||
var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath()));
|
||||
return (IEnumerable<PackagingSource>)_sourceSerializer.Deserialize(textReader);
|
||||
public IEnumerable<PackagingSourceRecord> GetSources() {
|
||||
return _packagingSourceRecordRepository.Table.ToList();
|
||||
}
|
||||
|
||||
public void AddSource(PackagingSource source) {
|
||||
SaveSources(GetSources().Concat(new[] { source }).GroupBy(x => x.FeedUrl).Select(g => g.First()));
|
||||
public void AddSource(string feedTitle, string feedUrl) {
|
||||
_packagingSourceRecordRepository.Create(new PackagingSourceRecord {FeedTitle = feedTitle, FeedUrl = feedUrl});
|
||||
}
|
||||
|
||||
public void RemoveSource(Guid id) {
|
||||
SaveSources(GetSources().Where(x => x.Id != id));
|
||||
}
|
||||
|
||||
public void UpdateLists() {
|
||||
foreach ( PackagingSource source in GetSources() ) {
|
||||
UpdateSource(source);
|
||||
public void RemoveSource(int id) {
|
||||
var packagingSource = _packagingSourceRecordRepository.Get(id);
|
||||
if(packagingSource != null) {
|
||||
_packagingSourceRecordRepository.Delete(packagingSource);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
||||
return (packagingSource == null ? GetSources() : new[] {packagingSource})
|
||||
public IEnumerable<PackagingEntry> GetModuleList(PackagingSourceRecord packagingSource = null) {
|
||||
return GetExtensionList(ModulesFilter, packagingSource);
|
||||
}
|
||||
public IEnumerable<PackagingEntry> GetThemeList(PackagingSourceRecord packagingSource = null) {
|
||||
return GetExtensionList(ThemesFilter, packagingSource);
|
||||
}
|
||||
|
||||
private IEnumerable<PackagingEntry> GetExtensionList(string filter = null, PackagingSourceRecord packagingSource = null) {
|
||||
return ( packagingSource == null ? GetSources() : new[] { packagingSource } )
|
||||
.SelectMany(
|
||||
source =>
|
||||
Bind(ParseFeed(GetModuleListForSource(source)),
|
||||
feed =>
|
||||
feed.Items.SelectMany(
|
||||
item =>
|
||||
Unit(new PackagingEntry {
|
||||
Source = source,
|
||||
SyndicationFeed = feed,
|
||||
SyndicationItem = item,
|
||||
PackageStreamUri = item.Links.Where(l => String.IsNullOrEmpty(l.RelationshipType)).FirstOrDefault().GetAbsoluteUri().AbsoluteUri,
|
||||
})))).ToArray();
|
||||
}
|
||||
|
||||
private string GetModuleListForSource(PackagingSource source) {
|
||||
if ( !_appDataFolder.FileExists(GetFeedCachePath(source)) ) {
|
||||
UpdateSource(source);
|
||||
}
|
||||
return _appDataFolder.ReadFile(GetFeedCachePath(source));
|
||||
new DataServicePackageRepository(new Uri(source.FeedUrl))
|
||||
.GetPackages()
|
||||
.Where(p => p.Id.StartsWith(filter ?? String.Empty))
|
||||
.ToList()
|
||||
.Select(p => new PackagingEntry {
|
||||
Title = String.IsNullOrWhiteSpace(p.Title) ? p.Id : p.Title,
|
||||
PackageId = p.Id,
|
||||
PackageStreamUri = p.ProjectUrl != null ? p.ProjectUrl.ToString() : String.Empty,
|
||||
Source = source,
|
||||
Version = p.Version != null ? p.Version.ToString() : String.Empty,
|
||||
Description = p.Description,
|
||||
Authors = p.Authors != null ? String.Join(", ", p.Authors) : String.Empty,
|
||||
})
|
||||
).ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static string GetSourcesPath() {
|
||||
return ".Packaging/Sources.xml";
|
||||
}
|
||||
|
||||
private static string GetFeedCachePath(PackagingSource source) {
|
||||
return ".Packaging/Feed." + source.Id.ToString("n") + ".xml";
|
||||
}
|
||||
|
||||
private void SaveSources(IEnumerable<PackagingSource> sources) {
|
||||
var textWriter = new StringWriter();
|
||||
_sourceSerializer.Serialize(textWriter, sources.ToList());
|
||||
|
||||
_appDataFolder.CreateFile(GetSourcesPath(), textWriter.ToString());
|
||||
}
|
||||
|
||||
private void UpdateSource(PackagingSource source) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static XName Atom(string localName) {
|
||||
return AtomExtensions.AtomXName(localName);
|
||||
}
|
||||
|
||||
private static IEnumerable<T> Unit<T>(T t) where T : class {
|
||||
return t != null ? new[] { t } : Enumerable.Empty<T>();
|
||||
}
|
||||
|
||||
private static IEnumerable<T2> Bind<T, T2>(T t, Func<T, IEnumerable<T2>> f) where T : class {
|
||||
return Unit(t).SelectMany(f);
|
||||
}
|
||||
|
||||
private SyndicationFeed ParseFeed(string content) {
|
||||
if ( string.IsNullOrEmpty(( content )) )
|
||||
return new SyndicationFeed();
|
||||
|
||||
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
|
||||
formatter.ReadFrom(XmlReader.Create(new StringReader(content)));
|
||||
return formatter.Feed;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user