Incremental work on update media

This commit is contained in:
Sebastien Ros 2013-09-30 10:44:43 -07:00
parent 61b614a0bb
commit 5e93dfa190
28 changed files with 343 additions and 95 deletions

View File

@ -2,6 +2,7 @@
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.MediaLibrary.Models;
@ -40,14 +41,7 @@ namespace Orchard.MediaLibrary.Controllers {
public ILogger Logger { get; set; }
public ActionResult Index(string folderPath = "", bool dialog = false) {
var mediaTypes = new List<string>();
foreach(var contentTypeDefinition in _contentDefinitionManager.ListTypeDefinitions()) {
string stereotype;
if (contentTypeDefinition.Settings.TryGetValue("Stereotype", out stereotype) && stereotype == "Media")
mediaTypes.Add(contentTypeDefinition.Name);
}
// let other modules enhance the ui by providing custom navigation and actions
var explorer = Services.ContentManager.New("MediaLibraryExplorer");
explorer.Weld(new MediaLibraryExplorerPart());
@ -58,7 +52,7 @@ namespace Orchard.MediaLibrary.Controllers {
DialogMode = dialog,
Folders = _mediaLibraryService.GetMediaFolders(null).Select(GetFolderHierarchy),
FolderPath = folderPath,
MediaTypes = mediaTypes.ToArray(),
MediaTypes = _mediaLibraryService.GetMediaTypes(),
CustomActionsShapes = explorerShape.Actions,
CustomNavigationShapes = explorerShape.Navigation,
};
@ -75,13 +69,15 @@ namespace Orchard.MediaLibrary.Controllers {
}
public ActionResult Import(string folderPath) {
var mediaProviderMenu = _navigationManager.BuildMenu("mediaproviders");
var imageSets = _navigationManager.BuildImageSets("mediaproviders");
var viewModel = new MediaManagerImportViewModel {
Menu = mediaProviderMenu,
ImageSets = imageSets,
FolderPath = folderPath
FolderPath = folderPath,
MediaTypes = _mediaLibraryService.GetMediaTypes()
};
return View(viewModel);
@ -94,7 +90,7 @@ namespace Orchard.MediaLibrary.Controllers {
var mediaItems = mediaParts.Select(x => new MediaManagerMediaItemViewModel {
MediaPart = x,
Shape = Services.ContentManager.BuildDisplay(x, "Thumbnail")
Shape = Services.ContentManager.BuildDisplay(x.ContentItem, "Thumbnail")
}).ToList();
var viewModel = new MediaManagerMediaItemsViewModel {

View File

@ -1,7 +1,10 @@
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.MediaLibrary.Services;
using Orchard.MediaLibrary.ViewModels;
using Orchard.Themes;
using Orchard.UI.Admin;
@ -9,18 +12,25 @@ namespace Orchard.MediaLibrary.Controllers {
[Admin, Themed(false)]
public class ClientStorageController : Controller {
private readonly IMediaLibraryService _mediaLibraryService;
private readonly IContentManager _contentManager;
public ClientStorageController(IMediaLibraryService mediaManagerService) {
public ClientStorageController(IMediaLibraryService mediaManagerService, IContentManager contentManager) {
_mediaLibraryService = mediaManagerService;
_contentManager = contentManager;
}
public ActionResult Index(string folderPath) {
public ActionResult Index(string folderPath, string type) {
return View((object)folderPath);
var viewModel = new ImportMediaViewModel {
FolderPath = folderPath,
Type = type
};
return View(viewModel);
}
[HttpPost]
public ActionResult Upload(string folderPath) {
public ActionResult Upload(string folderPath, string type) {
var statuses = new List<object>();
// Loop through each file in the request
@ -34,7 +44,8 @@ namespace Orchard.MediaLibrary.Controllers {
filename = "clipboard.png";
}
var mediaPart = _mediaLibraryService.ImportMedia(file.InputStream, folderPath, filename);
var mediaPart = _mediaLibraryService.ImportMedia(file.InputStream, folderPath, filename, type);
_contentManager.Create(mediaPart);
statuses.Add(new {
id = mediaPart.Id,

View File

@ -5,7 +5,6 @@ using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.MediaLibrary.Models;
using Orchard.MediaLibrary.Services;
using Orchard.MediaLibrary.ViewModels;
using Orchard.Themes;
using Orchard.UI.Admin;
@ -14,30 +13,28 @@ using Orchard.ContentManagement;
namespace Orchard.MediaLibrary.Controllers {
[Admin, Themed(false)]
public class OEmbedController : Controller {
private readonly IMediaLibraryService _mediaLibraryService;
public OEmbedController(
IMediaLibraryService mediaManagerService,
IOrchardServices services) {
_mediaLibraryService = mediaManagerService;
public OEmbedController(IOrchardServices services) {
Services = services;
}
public IOrchardServices Services { get; set; }
public ActionResult Index(string folderPath) {
public ActionResult Index(string folderPath, string type) {
var viewModel = new OEmbedViewModel {
FolderPath = folderPath
FolderPath = folderPath,
Type = type
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(string folderPath, string url) {
[ActionName("Index")]
public ActionResult IndexPOST(string folderPath, string url, string type) {
var viewModel = new OEmbedViewModel {
Url = url,
FolderPath = folderPath
FolderPath = folderPath,
Type = type
};
var webClient = new WebClient {Encoding = Encoding.UTF8};
@ -76,14 +73,20 @@ namespace Orchard.MediaLibrary.Controllers {
}
[HttpPost, ValidateInput(false)]
public ActionResult MediaPost(string folderPath, string url, string document) {
public ActionResult MediaPost(string folderPath, string url, string document, string type) {
var content = XDocument.Parse(document);
var oembed = content.Root;
var part = Services.ContentManager.New<MediaPart>("OEmbed");
if (String.IsNullOrEmpty(type)) {
type = "OEmbed";
}
var part = Services.ContentManager.New<MediaPart>(type);
part.MimeType = "text/html";
part.FolderPath = folderPath;
part.LogicalType = "OEmbed";
if (oembed.Element("title") != null) {
part.Title = oembed.Element("title").Value;
}
@ -93,18 +96,23 @@ namespace Orchard.MediaLibrary.Controllers {
if (oembed.Element("description") != null) {
part.Caption = oembed.Element("description").Value;
}
var oembedPart = part.As<OEmbedPart>();
oembedPart.Source = url;
if (oembedPart != null) {
foreach (var element in oembed.Elements()) {
oembedPart[element.Name.LocalName] = element.Value;
oembedPart.Source = url;
foreach (var element in oembed.Elements()) {
oembedPart[element.Name.LocalName] = element.Value;
}
Services.ContentManager.Create(oembedPart);
}
Services.ContentManager.Create(oembedPart);
var viewModel = new OEmbedViewModel {
FolderPath = folderPath
FolderPath = folderPath,
Type = type
};
return View("Index", viewModel);

View File

@ -2,7 +2,9 @@
using System.IO;
using System.Net;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.MediaLibrary.Services;
using Orchard.MediaLibrary.ViewModels;
using Orchard.Themes;
using Orchard.UI.Admin;
@ -10,24 +12,32 @@ namespace Orchard.MediaLibrary.Controllers {
[Admin, Themed(false)]
public class WebSearchController : Controller {
private readonly IMediaLibraryService _mediaLibraryService;
private readonly IContentManager _contentManager;
public WebSearchController(IMediaLibraryService mediaManagerService) {
public WebSearchController(IMediaLibraryService mediaManagerService, IContentManager contentManager) {
_mediaLibraryService = mediaManagerService;
_contentManager = contentManager;
}
public ActionResult Index(string folderPath) {
public ActionResult Index(string folderPath, string type) {
var viewModel = new ImportMediaViewModel {
FolderPath = folderPath,
Type = type
};
return View((object)folderPath);
return View(viewModel);
}
[HttpPost]
public JsonResult ImagePost(string folderPath, string url) {
public JsonResult ImagePost(string folderPath, string type, string url) {
try {
var buffer = new WebClient().DownloadData(url);
var stream = new MemoryStream(buffer);
var mediaPart = _mediaLibraryService.ImportMedia(stream, folderPath, Path.GetFileName(url));
var mediaPart = _mediaLibraryService.ImportMedia(stream, folderPath, Path.GetFileName(url), type);
_contentManager.Create(mediaPart);
return new JsonResult {
Data = new {folderPath, MediaPath = mediaPart.FileName}

View File

@ -1,4 +1,5 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Drivers {

View File

@ -1,21 +1,33 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Factories {
public class AudioFactorySelector : IMediaFactorySelector {
private readonly IContentManager _contentManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
public AudioFactorySelector(IContentManager contentManager) {
public AudioFactorySelector(IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) {
_contentManager = contentManager;
_contentDefinitionManager = contentDefinitionManager;
}
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType) {
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType, string contentType) {
if (!mimeType.StartsWith("audio/")) {
return null;
}
if (!String.IsNullOrEmpty(contentType)) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof(AudioPart).Name)) {
return null;
}
}
return new MediaFactorySelectorResult {
Priority = -5,
MediaFactory = new AudioFactory(_contentManager)
@ -33,13 +45,23 @@ namespace Orchard.MediaLibrary.Factories {
_contentManager = contentManager;
}
public MediaPart CreateMedia(Stream stream, string path, string mimeType) {
var part = _contentManager.New<MediaPart>("Audio");
public MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType) {
if (String.IsNullOrEmpty(contentType)) {
contentType = "Audio";
}
var part = _contentManager.New<MediaPart>(contentType);
part.LogicalType = "Audio";
part.MimeType = mimeType;
part.Title = Path.GetFileNameWithoutExtension(path);
var audioPart = part.As<AudioPart>();
if (audioPart == null) {
return null;
}
audioPart.Length = 0;
return part;

View File

@ -1,5 +1,8 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Factories {
@ -10,12 +13,21 @@ namespace Orchard.MediaLibrary.Factories {
/// </summary>
public class DocumentFactorySelector : IMediaFactorySelector {
private readonly IContentManager _contentManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
public DocumentFactorySelector(IContentManager contentManager) {
public DocumentFactorySelector(IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) {
_contentManager = contentManager;
_contentDefinitionManager = contentDefinitionManager;
}
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType) {
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType, string contentType) {
if (!String.IsNullOrEmpty(contentType)) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof(DocumentPart).Name)) {
return null;
}
}
return new MediaFactorySelectorResult {
Priority = -10,
MediaFactory = new DocumentFactory(_contentManager)
@ -30,14 +42,24 @@ namespace Orchard.MediaLibrary.Factories {
_contentManager = contentManager;
}
public MediaPart CreateMedia(Stream stream, string path, string mimeType) {
public MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType) {
var part = _contentManager.New<MediaPart>("Document");
if (String.IsNullOrEmpty(contentType)) {
contentType = "Document";
}
var part = _contentManager.New<MediaPart>(contentType);
part.LogicalType = "Document";
part.MimeType = mimeType;
part.Title = Path.GetFileNameWithoutExtension(path);
var documentPart = part.As<DocumentPart>();
if (documentPart == null) {
return null;
}
documentPart.Length = stream.Length;
return part;

View File

@ -4,7 +4,7 @@ using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Factories {
public interface IMediaFactory {
MediaPart CreateMedia(Stream stream, string path, string mimeType);
MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType);
}
public class NullMediaFactory : IMediaFactory {
@ -12,7 +12,7 @@ namespace Orchard.MediaLibrary.Factories {
get { return new NullMediaFactory(); }
}
public MediaPart CreateMedia(Stream stream, string path, string mimeType) {
public MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType) {
return null;
}
}

View File

@ -2,6 +2,6 @@
namespace Orchard.MediaLibrary.Factories {
public interface IMediaFactorySelector : IDependency {
MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType);
MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType, string contentType);
}
}

View File

@ -1,22 +1,35 @@
using System.Drawing;
using System;
using System.IO;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.MediaLibrary.Models;
using Image = System.Drawing.Image;
namespace Orchard.MediaLibrary.Factories {
public class ImageFactorySelector : IMediaFactorySelector {
private readonly IContentManager _contentManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
public ImageFactorySelector(IContentManager contentManager) {
public ImageFactorySelector(IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) {
_contentManager = contentManager;
_contentDefinitionManager = contentDefinitionManager;
}
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType) {
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType, string contentType) {
if (!mimeType.StartsWith("image/")) {
return null;
}
if (!String.IsNullOrEmpty(contentType)) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof (ImagePart).Name)) {
return null;
}
}
return new MediaFactorySelectorResult {
Priority = -5,
MediaFactory = new ImageFactory(_contentManager)
@ -32,14 +45,22 @@ namespace Orchard.MediaLibrary.Factories {
_contentManager = contentManager;
}
public MediaPart CreateMedia(Stream stream, string path, string mimeType) {
public MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType) {
if (String.IsNullOrEmpty(contentType)) {
contentType = "Image";
}
var part = _contentManager.New<MediaPart>("Image");
var part = _contentManager.New<MediaPart>(contentType);
part.LogicalType = "Image";
part.MimeType = mimeType;
part.Title = Path.GetFileNameWithoutExtension(path);
var imagePart = part.As<ImagePart>();
if (imagePart == null) {
return null;
}
using (var image = Image.FromStream(stream)) {
imagePart.Width = image.Width;
imagePart.Height = image.Height;

View File

@ -1,21 +1,33 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Factories {
public class VideoFactorySelector : IMediaFactorySelector {
private readonly IContentManager _contentManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
public VideoFactorySelector(IContentManager contentManager) {
public VideoFactorySelector(IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) {
_contentManager = contentManager;
_contentDefinitionManager = contentDefinitionManager;
}
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType) {
public MediaFactorySelectorResult GetMediaFactory(Stream stream, string mimeType, string contentType) {
if (!mimeType.StartsWith("video/")) {
return null;
}
if (!String.IsNullOrEmpty(contentType)) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof(VideoPart).Name)) {
return null;
}
}
return new MediaFactorySelectorResult {
Priority = -5,
MediaFactory = new VideoFactory(_contentManager)
@ -31,13 +43,23 @@ namespace Orchard.MediaLibrary.Factories {
_contentManager = contentManager;
}
public MediaPart CreateMedia(Stream stream, string path, string mimeType) {
var part = _contentManager.New<MediaPart>("Video");
public MediaPart CreateMedia(Stream stream, string path, string mimeType, string contentType) {
if (String.IsNullOrEmpty(contentType)) {
contentType = "Video";
}
var part = _contentManager.New<MediaPart>(contentType);
part.LogicalType = "Video";
part.MimeType = mimeType;
part.Title = Path.GetFileNameWithoutExtension(path);
var videoPart = part.As<VideoPart>();
if (videoPart == null) {
return null;
}
videoPart.Length = 0;
return part;

View File

@ -1,4 +1,5 @@
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
namespace Orchard.MediaLibrary {
@ -86,5 +87,39 @@ namespace Orchard.MediaLibrary {
return 3;
}
public int UpdateFrom3() {
ContentDefinitionManager.AlterPartDefinition("MediaPart", part => part
.Attachable()
.WithDescription("Turns a content type into a Media. Note: you need to set the stereotype to \"Media\" as well.")
);
ContentDefinitionManager.AlterPartDefinition("ImagePart", part => part
.Attachable()
.WithDescription("Provides common metadata for an Image Media.")
);
ContentDefinitionManager.AlterPartDefinition("VideoPart", part => part
.Attachable()
.WithDescription("Provides common metadata for a Video Media.")
);
ContentDefinitionManager.AlterPartDefinition("AudioPart", part => part
.Attachable()
.WithDescription("Provides common metadata for an Audio Media.")
);
ContentDefinitionManager.AlterPartDefinition("DocumentPart", part => part
.Attachable()
.WithDescription("Provides common metadata for a Document Media.")
);
ContentDefinitionManager.AlterPartDefinition("OEmbedPart", part => part
.Attachable()
.WithDescription("Provides common metadata for an OEmbed Media.")
);
return 4;
}
}
}

View File

@ -1,4 +1,6 @@
using Orchard.ContentManagement;
using System;
using Orchard.ContentManagement;
using Orchard.ContentManagement.FieldStorage.InfosetStorage;
using Orchard.ContentManagement.Utilities;
using Orchard.Core.Title.Models;
@ -48,7 +50,7 @@ namespace Orchard.MediaLibrary.Models {
}
/// <summary>
/// Gets or set the name of the media when <see cref="IMediaService"/> is used
/// Gets or sets the name of the media when <see cref="IMediaService"/> is used
/// to store the physical media. If <value>null</value> then the media is not associated
/// with a local file.
/// </summary>
@ -63,5 +65,16 @@ namespace Orchard.MediaLibrary.Models {
public string MediaUrl {
get { return _publicUrl.Value; }
}
/// <summary>
/// Get or sets the logical type of the media. For instance a custom type could be rendered as an Image
/// </summary>
/// <remarks>
/// The logical type is used to drive the thumbnails generation in the admin.
/// </remarks>
public string LogicalType {
get { return Convert.ToString(this.As<InfosetPart>().Get<MediaPart>("LogicalType")); }
set { this.As<InfosetPart>().Set<MediaPart>("LogicalType", value); }
}
}
}

View File

@ -167,6 +167,7 @@
<Compile Include="Settings\MediaLibraryPickerFieldEditorEvents.cs" />
<Compile Include="Settings\MediaLibraryPickerFieldSettings.cs" />
<Compile Include="Tokens\FieldTokens.cs" />
<Compile Include="ViewModels\ImportMediaViewModel.cs" />
<Compile Include="ViewModels\MediaLibraryPickerFieldViewModel.cs" />
<Compile Include="ViewModels\MediaManagerMediaItemsViewModel.cs" />
<Compile Include="ViewModels\MediaManagerFolderCreateViewModel.cs" />
@ -336,6 +337,9 @@
<ItemGroup>
<Content Include="Views\Parts\MediaLibrary.Navigation.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\EditorTemplates\Parts.Image.Edit.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@ -10,7 +10,7 @@
Parts_OEmbed="Content"
/>
</Match>
<!-- MediaPart -->
<Match DisplayType="SummaryAdmin">
<Place Parts_Media_SummaryAdmin="Meta:5"

View File

@ -2,20 +2,23 @@
using System.IO;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.MediaLibrary.Factories;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Services {
public interface IMediaLibraryService : IDependency {
IEnumerable<string> GetMediaTypes();
IEnumerable<ContentTypeDefinition> GetMediaTypes();
IContentQuery<MediaPart, MediaPartRecord> GetMediaContentItems();
IEnumerable<MediaPart> GetMediaContentItems(string folderPath, int skip, int count, string order, string mediaType);
IEnumerable<MediaPart> GetMediaContentItems(int skip, int count, string order, string mediaType);
int GetMediaContentItemsCount(string folderPath, string mediaType);
int GetMediaContentItemsCount(string mediaType);
MediaPart ImportMedia(string relativePath, string filename);
MediaPart ImportMedia(string relativePath, string filename, string contentType);
MediaPart ImportMedia(Stream stream, string relativePath, string filename);
IMediaFactory GetMediaFactory(Stream stream, string mimeType);
MediaPart ImportMedia(Stream stream, string relativePath, string filename, string contentType);
IMediaFactory GetMediaFactory(Stream stream, string mimeType, string contentType);
/// <summary>
/// Creates a unique filename to prevent filename collisions.

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.Core.Common.Models;
using Orchard.FileSystems.Media;
using Orchard.Localization;
@ -34,10 +35,13 @@ namespace Orchard.MediaLibrary.Services {
public Localizer T { get; set; }
public IEnumerable<string> GetMediaTypes() {
return _orchardServices.ContentManager.GetContentTypeDefinitions()
public IEnumerable<ContentTypeDefinition> GetMediaTypes() {
return _orchardServices
.ContentManager
.GetContentTypeDefinitions()
.Where(contentTypeDefinition => contentTypeDefinition.Settings.ContainsKey("Stereotype") && contentTypeDefinition.Settings["Stereotype"] == "Media")
.Select(contentTypeDefinition => contentTypeDefinition.Name);
.OrderBy(x => x.DisplayName)
.ToArray();
}
public IContentQuery<MediaPart, MediaPartRecord> GetMediaContentItems() {
@ -105,10 +109,14 @@ namespace Orchard.MediaLibrary.Services {
}
public MediaPart ImportMedia(Stream stream, string relativePath, string filename) {
return ImportMedia(stream, relativePath, filename, null);
}
public MediaPart ImportMedia(Stream stream, string relativePath, string filename, string contentType) {
var uniqueFilename = GetUniqueFilename(relativePath, filename);
UploadMediaFile(relativePath, uniqueFilename, stream);
return ImportMedia(relativePath, uniqueFilename);
return ImportMedia(relativePath, uniqueFilename, contentType);
}
public string GetUniqueFilename(string folderPath, string filename) {
@ -123,6 +131,10 @@ namespace Orchard.MediaLibrary.Services {
}
public MediaPart ImportMedia(string relativePath, string filename) {
return ImportMedia(relativePath, filename, null);
}
public MediaPart ImportMedia(string relativePath, string filename, string contentType) {
var mimeType = _mimeTypeProvider.GetMimeType(filename);
@ -134,21 +146,20 @@ namespace Orchard.MediaLibrary.Services {
var mediaFile = BuildMediaFile(relativePath, storageFile);
using (var stream = storageFile.OpenRead()) {
var mediaFactory = GetMediaFactory(stream, mimeType);
var mediaPart = mediaFactory.CreateMedia(stream, mediaFile.Name, mimeType);
var mediaFactory = GetMediaFactory(stream, mimeType, contentType);
var mediaPart = mediaFactory.CreateMedia(stream, mediaFile.Name, mimeType, contentType);
if (mediaPart != null) {
mediaPart.FolderPath = relativePath;
mediaPart.FileName = filename;
_orchardServices.ContentManager.Create(mediaPart);
}
return mediaPart;
}
}
public IMediaFactory GetMediaFactory(Stream stream, string mimeType) {
public IMediaFactory GetMediaFactory(Stream stream, string mimeType, string contentType) {
var requestMediaFactoryResults = _mediaFactorySelectors
.Select(x => x.GetMediaFactory(stream, mimeType))
.Select(x => x.GetMediaFactory(stream, mimeType, contentType))
.Where(x => x != null)
.OrderByDescending(x => x.Priority);

View File

@ -1,5 +1,7 @@
using Orchard.ContentManagement;
using System;
using Orchard.ContentManagement;
using Orchard.DisplayManagement.Descriptors;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Services {
public class Shapes : IShapeTableProvider {
@ -12,16 +14,30 @@ namespace Orchard.MediaLibrary.Services {
// Display type > content type > specific content > display type for a content type > display type for specific content
// BasicShapeTemplateHarvester.Adjust will then adjust the template name
var mediaPart = contentItem.As<MediaPart>();
// Media__[DisplayType] e.g. Media-Summary
displaying.ShapeMetadata.Alternates.Add("Media_" + EncodeAlternateElement(displaying.ShapeMetadata.DisplayType));
// Media__[ContentType] e.g. Media-BlogPost,
if (!String.IsNullOrEmpty(mediaPart.LogicalType)) {
// Media__[LogicalType] e.g. Media-Image,
displaying.ShapeMetadata.Alternates.Add("Media__" + EncodeAlternateElement(mediaPart.LogicalType));
// Media__[Id] e.g. Media-42,
displaying.ShapeMetadata.Alternates.Add("Media__" + contentItem.Id);
// Media_[DisplayType]__[LogicalType] e.g. Media-Image.Summary
displaying.ShapeMetadata.Alternates.Add("Media_" + displaying.ShapeMetadata.DisplayType + "__" + EncodeAlternateElement(mediaPart.LogicalType));
}
// Media__[ContentType] e.g. Media-ProductImage,
displaying.ShapeMetadata.Alternates.Add("Media__" + EncodeAlternateElement(contentItem.ContentType));
// Media__[Id] e.g. Media-42,
displaying.ShapeMetadata.Alternates.Add("Media__" + contentItem.Id);
// Media_[DisplayType]__[ContentType] e.g. Media-Image.Summary
// Media_[DisplayType]__[ContentType] e.g. Media-ProductImage.Summary
displaying.ShapeMetadata.Alternates.Add("Media_" + displaying.ShapeMetadata.DisplayType + "__" + EncodeAlternateElement(contentItem.ContentType));
// Media_[DisplayType]__[Id] e.g. Media-42.Summary

View File

@ -0,0 +1,6 @@
namespace Orchard.MediaLibrary.ViewModels {
public class ImportMediaViewModel {
public string FolderPath { get; set; }
public string Type { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.UI.Navigation;
namespace Orchard.MediaLibrary.ViewModels {
@ -6,5 +7,6 @@ namespace Orchard.MediaLibrary.ViewModels {
public IEnumerable<MenuItem> Menu { get; set; }
public IEnumerable<string> ImageSets { get; set; }
public string FolderPath { get; set; }
public IEnumerable<ContentTypeDefinition> MediaTypes { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.ViewModels {
@ -6,7 +7,7 @@ namespace Orchard.MediaLibrary.ViewModels {
public IEnumerable<FolderHierarchy> Folders { get; set; }
public string FolderPath { get; set; }
public bool DialogMode { get; set; }
public string[] MediaTypes { get; set; }
public IEnumerable<ContentTypeDefinition> MediaTypes { get; set; }
public dynamic CustomActionsShapes { get; set; }
public dynamic CustomNavigationShapes { get; set; }
}

View File

@ -6,5 +6,6 @@ namespace Orchard.MediaLibrary.ViewModels {
public string Url { get; set; }
public XDocument Content { get; set; }
public bool Success { get; set; }
public string Type { get; set; }
}
}

View File

@ -19,10 +19,20 @@
<div id="media-library-toolbar">
<div id="media-library-toolbar-actions">
<label for="filterMediaType">@T("Create")</label>
<select id="filterMediaType" name="FilteredMediaType">
@Html.SelectOption("", true, T("Any").ToString())
@foreach (var mediaType in Model.MediaTypes) {
@Html.SelectOption(mediaType.Name, false, mediaType.DisplayName)
}
</select>
<a href="@Url.Action("Index", "Admin", new { folderPath = Model.FolderPath})" class="button">@T("Close")</a>
</div>
@T("Media Folders") @foreach (var folder in Model.FolderPath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries)) {<text> &gt; </text>@folder}
</div>
@ -44,19 +54,43 @@
</div>
</div>
@using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
$(function () {
$("#media-library-main-navigation a").click(function () {
$(function() {
$("#media-library-main-navigation a").click(function() {
var self = $(this);
$("#media-library-main-list-frame").attr("src", self.attr("href"));
var href = replaceQueryString(self.attr("href"), "type", getMediaType());
$("#media-library-main-list-frame").attr("src", href);
return false;
});
})
$("#filterMediaType").change(function() {
var href = $("#media-library-main-list-frame").attr("src");
href = replaceQueryString(href, 'type', getMediaType());
$("#media-library-main-list-frame").attr("src", href);
});
// loads the first available link
$("#media-library-main-navigation a").first().click();
});
// returns the currently selected preferred media type
function getMediaType() {
return $('#filterMediaType').find("option:selected").attr('value');
}
// replaces a specific query parameter in the given url
function replaceQueryString(url, param, value) {
value = encodeURIComponent(value);
var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");
if (url.match(re))
return url.replace(re, '$1' + param + "=" + value + '$2');
else
return url + '&' + param + "=" + value;
}
//]]>
</script>
}

View File

@ -1,4 +1,4 @@
@using System.Text
@model Orchard.MediaLibrary.ViewModels.ImportMediaViewModel
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
@ -87,7 +87,8 @@
url: '@Url.Action("Upload", "ClientStorage")',
autoUpload: true,
formData: {
folderPath: '@HttpUtility.JavaScriptStringEncode(Model)',
folderPath: '@HttpUtility.JavaScriptStringEncode(Model.FolderPath)',
type: '@HttpUtility.JavaScriptStringEncode(Model.Type)',
__RequestVerificationToken: '@Html.AntiForgeryTokenValueOrchard()'
},
done: function (e, data) {

View File

@ -74,6 +74,7 @@
using (Html.BeginFormAntiForgeryPost(Url.Action("MediaPost"))) {
@Html.Hidden("url", Model.Url)
@Html.Hidden("folderPath", Model.FolderPath)
@Html.Hidden("type", Model.Type)
@Html.Hidden("document", Model.Content.ToString())
<button type="submit">@("Import")</button>

View File

@ -7,7 +7,7 @@
<select id="filterMediaType" name="FilteredMediaType" data-bind="selectedOptions: mediaType">
@Html.SelectOption("", true, T("Any (show all)").ToString())
@foreach (var mediaType in viewModel.MediaTypes) {
@Html.SelectOption(mediaType, false, mediaType)
@Html.SelectOption(mediaType.Name, false, mediaType.DisplayName)
}
</select>

View File

@ -1,4 +1,6 @@
@using System.Text
@model Orchard.MediaLibrary.ViewModels.ImportMediaViewModel
@using System.Text
@using Orchard.ContentManagement
@using Orchard.MediaLibrary.Models
@ -83,7 +85,8 @@
@Html.Hidden("antiforgerytoken", Html.AntiForgeryTokenValueOrchard())
@Html.Hidden("action", Url.Action("ImagePost"))
@Html.Hidden("folderPath", (string)Model)
@Html.Hidden("folderPath", Model.FolderPath)
@Html.Hidden("type", Model.Type)
@using(Script.Foot()) {
<script type="text/javascript">
@ -209,6 +212,7 @@
$("#button-import").on("click", function (e) {
var antiForgeryToken = $('[name=antiforgerytoken]').val();
var folderPath = $('[name=folderPath]').val();
var type = $('[name=type]').val();
var action = $('[name=action]').val();
viewModel.selection().forEach(function (item) {
@ -216,6 +220,7 @@
item.status('uploading');
$.post(action, {
folderPath: folderPath,
type: type,
url: url,
__RequestVerificationToken: antiForgeryToken,
})

View File

@ -157,7 +157,9 @@ namespace Upgrade.Controllers {
try {
//_orchardServices.Notifier.Information(T("Importing {0}.", mediaFile.MediaPath));
return _mediaLibraryService.ImportMedia(folderName, fileName);
var media = _mediaLibraryService.ImportMedia(folderName, fileName);
_orchardServices.ContentManager.Create(media);
return media;
}
catch(Exception e) {
_orchardServices.Notifier.Error(T("Error while importing {0}. Please check the logs", folderName + "/" + fileName));