2013-04-19 01:59:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.Core.Contents.Controllers;
|
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.Logging;
|
|
|
|
|
using Orchard.MediaLibrary.Models;
|
|
|
|
|
using Orchard.MediaLibrary.Services;
|
|
|
|
|
using Orchard.MediaLibrary.ViewModels;
|
|
|
|
|
using Orchard.UI.Admin;
|
|
|
|
|
using Orchard.UI.Notify;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.MediaLibrary.Controllers {
|
|
|
|
|
[Admin]
|
|
|
|
|
public class FolderController : Controller {
|
|
|
|
|
private readonly IMediaLibraryService _mediaLibraryService;
|
|
|
|
|
|
2013-07-03 09:10:43 +08:00
|
|
|
|
public FolderController(
|
|
|
|
|
IOrchardServices services,
|
|
|
|
|
IMediaLibraryService mediaManagerService
|
|
|
|
|
) {
|
2013-04-19 01:59:15 +08:00
|
|
|
|
_mediaLibraryService = mediaManagerService;
|
|
|
|
|
|
|
|
|
|
Services = services;
|
|
|
|
|
Logger = NullLogger.Instance;
|
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IOrchardServices Services { get; set; }
|
|
|
|
|
public ILogger Logger { get; set; }
|
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
2013-07-03 09:10:43 +08:00
|
|
|
|
public ActionResult Create(string folderPath) {
|
2013-04-19 01:59:15 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't create media folder")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new MediaManagerFolderCreateViewModel {
|
2013-07-03 09:10:43 +08:00
|
|
|
|
Hierarchy = _mediaLibraryService.GetMediaFolders(folderPath),
|
|
|
|
|
FolderPath = folderPath
|
2013-04-19 01:59:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Create")]
|
|
|
|
|
public ActionResult Create() {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't create media folder")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new MediaManagerFolderCreateViewModel();
|
|
|
|
|
UpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
try {
|
2013-07-03 09:10:43 +08:00
|
|
|
|
_mediaLibraryService.CreateFolder(viewModel.FolderPath, viewModel.Name);
|
2013-04-19 01:59:15 +08:00
|
|
|
|
Services.Notifier.Information(T("Media folder created"));
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException argumentException) {
|
|
|
|
|
Services.Notifier.Error(T("Creating Folder failed: {0}", argumentException.Message));
|
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Index", "Admin", new { area = "Orchard.MediaLibrary" });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-03 09:10:43 +08:00
|
|
|
|
public ActionResult Edit(string folderPath) {
|
2013-04-19 01:59:15 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't edit media folder")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
2013-07-03 09:10:43 +08:00
|
|
|
|
|
2013-04-19 01:59:15 +08:00
|
|
|
|
var viewModel = new MediaManagerFolderEditViewModel {
|
2013-07-03 09:10:43 +08:00
|
|
|
|
Hierarchy = _mediaLibraryService.GetMediaFolders(folderPath),
|
|
|
|
|
FolderPath = folderPath,
|
|
|
|
|
Name = folderPath.Split('/').LastOrDefault()
|
2013-04-19 01:59:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Edit")]
|
|
|
|
|
[FormValueRequired("submit.Save")]
|
|
|
|
|
public ActionResult Edit() {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't edit media folder")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new MediaManagerFolderEditViewModel();
|
|
|
|
|
UpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
try {
|
2013-07-03 09:10:43 +08:00
|
|
|
|
_mediaLibraryService.RenameFolder(viewModel.FolderPath, viewModel.Name);
|
2013-04-19 01:59:15 +08:00
|
|
|
|
Services.Notifier.Information(T("Media folder renamed"));
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException argumentException) {
|
|
|
|
|
Services.Notifier.Error(T("Editing Folder failed: {0}", argumentException.Message));
|
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Index", "Admin", new { area = "Orchard.MediaLibrary" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Edit")]
|
|
|
|
|
[FormValueRequired("submit.Delete")]
|
|
|
|
|
public ActionResult Delete() {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't delete media folder")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new MediaManagerFolderEditViewModel();
|
|
|
|
|
UpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
try {
|
2013-07-03 09:10:43 +08:00
|
|
|
|
_mediaLibraryService.DeleteFolder(viewModel.FolderPath);
|
2013-04-19 01:59:15 +08:00
|
|
|
|
Services.Notifier.Information(T("Media folder deleted"));
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException argumentException) {
|
|
|
|
|
Services.Notifier.Error(T("Deleting Folder failed: {0}", argumentException.Message));
|
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Index", "Admin", new { area = "Orchard.MediaLibrary" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2013-07-03 09:10:43 +08:00
|
|
|
|
public ActionResult Move(string folderPath, int[] mediaItemIds) {
|
2013-04-19 01:59:15 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Couldn't move media items")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2013-07-03 09:10:43 +08:00
|
|
|
|
foreach (var media in Services.ContentManager.Query().ForPart<MediaPart>().ForContentItems(mediaItemIds).List()) {
|
|
|
|
|
var uniqueFilename = _mediaLibraryService.GetUniqueFilename(folderPath, media.FileName);
|
|
|
|
|
_mediaLibraryService.MoveFile(media.FolderPath, media.FileName, folderPath, uniqueFilename);
|
|
|
|
|
media.FolderPath = folderPath;
|
|
|
|
|
media.FileName = uniqueFilename;
|
2013-04-19 01:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Json(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|