mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 02:44:52 +08:00
92 lines
3.8 KiB
C#
92 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NuGet;
|
|
using Orchard.Data;
|
|
using Orchard.Environment.Extensions;
|
|
using Orchard.Environment.Extensions.Models;
|
|
using Orchard.Localization;
|
|
using Orchard.Packaging.GalleryServer;
|
|
using Orchard.Packaging.Models;
|
|
|
|
namespace Orchard.Packaging.Services {
|
|
[OrchardFeature("Gallery")]
|
|
public class PackagingSourceManager : IPackagingSourceManager {
|
|
public const string ThemesPrefix = "Orchard.Themes.";
|
|
public const string ModulesPrefix = "Orchard.Modules.";
|
|
|
|
public static string GetExtensionPrefix(string extensionType) {
|
|
switch (extensionType) {
|
|
case DefaultExtensionTypes.Theme:
|
|
return ThemesPrefix;
|
|
case DefaultExtensionTypes.Module:
|
|
return ModulesPrefix;
|
|
default:
|
|
throw new ArgumentException();
|
|
}
|
|
}
|
|
|
|
private readonly IRepository<PackagingSource> _packagingSourceRecordRepository;
|
|
|
|
public PackagingSourceManager(IRepository<PackagingSource> packagingSourceRecordRepository) {
|
|
_packagingSourceRecordRepository = packagingSourceRecordRepository;
|
|
T = NullLocalizer.Instance;
|
|
}
|
|
|
|
Localizer T { get; set; }
|
|
|
|
#region IPackagingSourceManager Members
|
|
|
|
public IEnumerable<PackagingSource> GetSources() {
|
|
return _packagingSourceRecordRepository.Table.ToList();
|
|
}
|
|
|
|
public void AddSource(string feedTitle, string feedUrl) {
|
|
var packagingSource = new PackagingSource {FeedTitle = feedTitle, FeedUrl = feedUrl};
|
|
_packagingSourceRecordRepository.Create(packagingSource);
|
|
}
|
|
|
|
public void RemoveSource(int id) {
|
|
var packagingSource = _packagingSourceRecordRepository.Get(id);
|
|
if(packagingSource != null) {
|
|
_packagingSourceRecordRepository.Delete(packagingSource);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
|
return GetExtensionList(DefaultExtensionTypes.Module, packagingSource);
|
|
}
|
|
public IEnumerable<PackagingEntry> GetThemeList(PackagingSource packagingSource = null) {
|
|
return GetExtensionList(DefaultExtensionTypes.Theme, packagingSource);
|
|
}
|
|
|
|
private IEnumerable<PackagingEntry> GetExtensionList(string filter = null, PackagingSource packagingSource = null) {
|
|
return (packagingSource == null ? GetSources() : new[] {packagingSource})
|
|
.SelectMany(
|
|
source => {
|
|
GalleryFeedContext galleryFeedContext = new GalleryFeedContext(new Uri(source.FeedUrl));
|
|
return galleryFeedContext.Packages
|
|
.Where(p => p.PackageType == filter)
|
|
.ToList()
|
|
.Select(p => CreatePackageEntry(p, packagingSource, galleryFeedContext.GetReadStreamUri(p)));
|
|
}
|
|
).ToArray();
|
|
}
|
|
|
|
private static PackagingEntry CreatePackageEntry(PublishedPackage package, PackagingSource source, Uri downloadUri) {
|
|
return new PackagingEntry {
|
|
Title = String.IsNullOrWhiteSpace(package.Title) ? package.Id : package.Title,
|
|
PackageId = package.Id,
|
|
PackageStreamUri = downloadUri.ToString(),
|
|
ProjectUrl = package.ProjectUrl,
|
|
Source = source,
|
|
Version = package.Version ?? String.Empty,
|
|
Description = package.Description,
|
|
Authors = package.Authors,
|
|
LastUpdated = package.LastUpdated
|
|
};
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
} |