From 790cb0204b864c35ef242f33a137bfe2b971e9c1 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 3 Apr 2014 17:51:25 -0400 Subject: [PATCH] #20599: Moving FolderController's Move Logic Work Item: 20599 --- .../Controllers/FolderController.cs | 12 +- .../Services/MediaLibraryService.cs | 115 ++++++++++-------- 2 files changed, 69 insertions(+), 58 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Controllers/FolderController.cs b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Controllers/FolderController.cs index e7b33cd33..b08662cf8 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Controllers/FolderController.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Controllers/FolderController.cs @@ -88,18 +88,10 @@ namespace Orchard.MediaLibrary.Controllers { try { _mediaLibraryService.RenameFolder(viewModel.FolderPath, viewModel.Name); - - var segments = viewModel.FolderPath.Split(new char[] {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries).ToArray(); - var newFolderPath = String.Join(Path.DirectorySeparatorChar.ToString(), segments.Take(segments.Length - 1).Union(new [] { viewModel.Name })); - - foreach (var media in Services.ContentManager.Query().ForPart().Where(m => m.FolderPath.StartsWith(viewModel.FolderPath)).List()) { - media.FolderPath = newFolderPath + media.FolderPath.Substring(viewModel.FolderPath.Length); - } Services.Notifier.Information(T("Media folder renamed")); } - catch (ArgumentException argumentException) { - Services.Notifier.Error(T("Editing Folder failed: {0}", argumentException.Message)); - Services.TransactionManager.Cancel(); + catch (Exception exception) { + Services.Notifier.Error(T("Editing Folder failed: {0}", exception.Message)); return View(viewModel); } diff --git a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/MediaLibraryService.cs b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/MediaLibraryService.cs index d470fa7b4..eb1da642b 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/MediaLibraryService.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/MediaLibraryService.cs @@ -49,65 +49,70 @@ namespace Orchard.MediaLibrary.Services { } public IEnumerable GetMediaContentItems(string folderPath, int skip, int count, string order, string mediaType, VersionOptions versionOptions = null) { - var query = _orchardServices.ContentManager.Query(versionOptions); - - if (!String.IsNullOrEmpty(mediaType)) { - query = query.ForType(new[] { mediaType }); - } - - if (!String.IsNullOrEmpty(folderPath)) { - query = query.Join().Where(m => m.FolderPath == folderPath); - } - - switch(order) { - case "title": - return query.Join() - .OrderBy(x => x.Title) - .Slice(skip, count) - .ToArray(); - - case "modified": - return query.Join() - .OrderByDescending(x => x.ModifiedUtc) - .Slice(skip, count) - .ToArray(); - - case "published": - return query.Join() - .OrderByDescending(x => x.PublishedUtc) - .Slice(skip, count) - .ToArray(); - - default: - return query.Join() - .OrderByDescending(x => x.CreatedUtc) - .Slice(skip, count) - .ToArray(); - } + return BuildGetMediaContentItemsQuery(_orchardServices.ContentManager, folderPath, order: order, mediaType: mediaType, versionOptions: versionOptions) + .Slice(skip, count); } public IEnumerable GetMediaContentItems(int skip, int count, string order, string mediaType, VersionOptions versionOptions = null) { - return GetMediaContentItems(null, skip, count, order, mediaType); + return GetMediaContentItems(null, skip, count, order, mediaType, versionOptions); } public int GetMediaContentItemsCount(string folderPath, string mediaType, VersionOptions versionOptions = null) { - var query = _orchardServices.ContentManager.Query(versionOptions); - - if (!String.IsNullOrEmpty(mediaType)) { - query = query.ForType(new[] { mediaType }); - } - - if (!String.IsNullOrEmpty(folderPath)) { - query = query.Join().Where(m => m.FolderPath == folderPath); - } - - return query.Count(); + return BuildGetMediaContentItemsQuery(_orchardServices.ContentManager, folderPath, mediaType: mediaType, versionOptions: versionOptions) + .Count(); } public int GetMediaContentItemsCount(string mediaType, VersionOptions versionOptions = null) { return GetMediaContentItemsCount(null, mediaType, versionOptions); } + private static IContentQuery BuildGetMediaContentItemsQuery( + IContentManager contentManager, string folderPath = null, bool recursive = false, string order = null, string mediaType = null, VersionOptions versionOptions = null) { + + var query = contentManager.Query(versionOptions); + + if (!String.IsNullOrEmpty(mediaType)) { + query = query.ForType(new[] { mediaType }); + } + + if (!String.IsNullOrEmpty(folderPath)) { + if (recursive) { + query = query.Join().Where(m => m.FolderPath.StartsWith(folderPath)); + } + else { + query = query.Join().Where(m => m.FolderPath == folderPath); + } + } + + switch (order) { + case "title": + query = query.Join() + .OrderBy(x => x.Title) + .Join(); + break; + + case "modified": + query = query.Join() + .OrderByDescending(x => x.ModifiedUtc) + .Join(); + break; + + case "published": + query = query.Join() + .OrderByDescending(x => x.PublishedUtc) + .Join(); + break; + + default: + query = query.Join() + .OrderByDescending(x => x.CreatedUtc) + .Join(); + break; + } + + return query; + } + public MediaPart ImportMedia(Stream stream, string relativePath, string filename) { return ImportMedia(stream, relativePath, filename, null); } @@ -267,7 +272,21 @@ namespace Orchard.MediaLibrary.Services { Argument.ThrowIfNullOrEmpty(folderPath, "folderPath"); Argument.ThrowIfNullOrEmpty(newFolderName, "newFolderName"); - _storageProvider.RenameFolder(folderPath, _storageProvider.Combine(Path.GetDirectoryName(folderPath), newFolderName)); + try { + var segments = folderPath.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).ToArray(); + var newFolderPath = String.Join(Path.DirectorySeparatorChar.ToString(), segments.Take(segments.Length - 1).Union(new[] { newFolderName })); + + var mediaParts = BuildGetMediaContentItemsQuery(_orchardServices.ContentManager, folderPath, true).List(); + foreach (var mediaPart in mediaParts) { + mediaPart.FolderPath = newFolderPath + mediaPart.FolderPath.Substring(folderPath.Length); + } + + _storageProvider.RenameFolder(folderPath, _storageProvider.Combine(Path.GetDirectoryName(folderPath), newFolderName)); + } + catch (Exception) { + _orchardServices.TransactionManager.Cancel(); + throw; + } } ///