2010-07-18 12:36:02 -07:00
|
|
|
using System;
|
2010-07-04 14:14:15 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2010-07-06 18:56:55 -07:00
|
|
|
using System.ServiceModel.Syndication;
|
|
|
|
using System.Xml;
|
2010-07-04 14:14:15 -07:00
|
|
|
using System.Xml.Linq;
|
|
|
|
using System.Xml.Serialization;
|
2010-07-22 22:09:34 -07:00
|
|
|
using Orchard.Environment.Extensions;
|
2010-07-04 14:14:15 -07:00
|
|
|
using Orchard.FileSystems.AppData;
|
|
|
|
|
2010-07-22 22:09:34 -07:00
|
|
|
namespace Orchard.Packaging.Services {
|
|
|
|
[OrchardFeature("PackagingServices")]
|
2010-07-18 13:09:45 -07:00
|
|
|
public class PackagingSourceManager : IPackagingSourceManager {
|
2010-07-22 22:09:34 -07:00
|
|
|
private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof (List<PackagingSource>), new XmlRootAttribute("Sources"));
|
2010-07-04 14:14:15 -07:00
|
|
|
private readonly IAppDataFolder _appDataFolder;
|
|
|
|
|
2010-07-18 13:09:45 -07:00
|
|
|
public PackagingSourceManager(IAppDataFolder appDataFolder) {
|
2010-07-04 14:14:15 -07:00
|
|
|
_appDataFolder = appDataFolder;
|
|
|
|
}
|
|
|
|
|
2010-07-22 22:09:34 -07:00
|
|
|
#region IPackagingSourceManager Members
|
2010-07-04 14:14:15 -07:00
|
|
|
|
2010-07-18 13:09:45 -07:00
|
|
|
public IEnumerable<PackagingSource> GetSources() {
|
2010-07-22 22:09:34 -07:00
|
|
|
string text = _appDataFolder.ReadFile(GetSourcesPath());
|
|
|
|
if (string.IsNullOrEmpty(text)) {
|
2010-07-18 13:09:45 -07:00
|
|
|
return Enumerable.Empty<PackagingSource>();
|
2010-07-22 22:09:34 -07:00
|
|
|
}
|
2010-07-04 14:14:15 -07:00
|
|
|
|
|
|
|
var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath()));
|
2010-07-22 22:09:34 -07:00
|
|
|
return (IEnumerable<PackagingSource>) _sourceSerializer.Deserialize(textReader);
|
2010-07-04 14:14:15 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 13:09:45 -07:00
|
|
|
public void AddSource(PackagingSource source) {
|
2010-07-04 14:14:15 -07:00
|
|
|
UpdateSource(source);
|
2010-07-22 22:09:34 -07:00
|
|
|
SaveSources(GetSources().Concat(new[] {source}));
|
2010-07-04 14:14:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveSource(Guid id) {
|
|
|
|
SaveSources(GetSources().Where(x => x.Id != id));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateLists() {
|
2010-07-22 22:09:34 -07:00
|
|
|
foreach (PackagingSource source in GetSources()) {
|
2010-07-04 14:14:15 -07:00
|
|
|
UpdateSource(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 16:37:25 -07:00
|
|
|
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
|
|
|
IEnumerable<PackagingEntry> packageInfos = ( packagingSource == null ? GetSources() : new [] { packagingSource })
|
2010-07-22 22:09:34 -07:00
|
|
|
.SelectMany(
|
|
|
|
source =>
|
|
|
|
Bind(ParseFeed(_appDataFolder.ReadFile(GetFeedCachePath(source))),
|
|
|
|
feed =>
|
|
|
|
feed.Items.SelectMany(
|
|
|
|
item =>
|
|
|
|
Unit(new PackagingEntry {
|
|
|
|
Source = source,
|
|
|
|
SyndicationFeed = feed,
|
|
|
|
SyndicationItem = item,
|
|
|
|
PackageStreamUri = item.Links.Single().GetAbsoluteUri().AbsoluteUri,
|
|
|
|
}))));
|
|
|
|
|
|
|
|
|
|
|
|
return packageInfos.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());
|
|
|
|
}
|
|
|
|
|
2010-07-18 13:09:45 -07:00
|
|
|
private void UpdateSource(PackagingSource source) {
|
2010-07-22 22:09:34 -07:00
|
|
|
XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace);
|
2010-07-04 14:14:15 -07:00
|
|
|
_appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-22 22:09:34 -07:00
|
|
|
private static XName Atom(string localName) {
|
2010-07-04 14:14:15 -07:00
|
|
|
return AtomExtensions.AtomXName(localName);
|
|
|
|
}
|
|
|
|
|
2010-07-22 22:09:34 -07:00
|
|
|
private static IEnumerable<T> Unit<T>(T t) where T : class {
|
|
|
|
return t != null ? new[] {t} : Enumerable.Empty<T>();
|
2010-07-06 18:56:55 -07:00
|
|
|
}
|
2010-07-22 22:09:34 -07:00
|
|
|
|
|
|
|
private static IEnumerable<T2> Bind<T, T2>(T t, Func<T, IEnumerable<T2>> f) where T : class {
|
2010-07-06 18:56:55 -07:00
|
|
|
return Unit(t).SelectMany(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private SyndicationFeed ParseFeed(string content) {
|
|
|
|
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
|
|
|
|
formatter.ReadFrom(XmlReader.Create(new StringReader(content)));
|
|
|
|
return formatter.Feed;
|
|
|
|
}
|
2010-07-04 14:14:15 -07:00
|
|
|
}
|
|
|
|
}
|