2014-04-01 07:14:19 +08:00
|
|
|
|
using System;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
2024-04-19 21:48:17 +08:00
|
|
|
|
using Orchard.FileSystems.Media;
|
2014-04-01 07:14:19 +08:00
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.MediaLibrary.Models;
|
|
|
|
|
using Orchard.MediaLibrary.Services;
|
|
|
|
|
using Orchard.Security;
|
|
|
|
|
using Orchard.UI.Notify;
|
|
|
|
|
|
2024-04-19 21:48:17 +08:00
|
|
|
|
namespace Orchard.MediaLibrary.MediaFileName {
|
2014-04-01 07:14:19 +08:00
|
|
|
|
public class MediaFileNameDriver : ContentPartDriver<MediaPart> {
|
|
|
|
|
private readonly IAuthenticationService _authenticationService;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
private readonly IMediaLibraryService _mediaLibraryService;
|
|
|
|
|
private readonly INotifier _notifier;
|
|
|
|
|
|
|
|
|
|
public MediaFileNameDriver(IAuthorizationService authorizationService, IAuthenticationService authenticationService, IMediaLibraryService mediaLibraryService, INotifier notifier) {
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
_authenticationService = authenticationService;
|
|
|
|
|
_mediaLibraryService = mediaLibraryService;
|
|
|
|
|
_notifier = notifier;
|
|
|
|
|
|
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
|
|
|
|
protected override string Prefix {
|
|
|
|
|
get { return "MediaFileName"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(MediaPart part, dynamic shapeHelper) {
|
|
|
|
|
return Editor(part, null, shapeHelper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(MediaPart part, IUpdateModel updater, dynamic shapeHelper) {
|
|
|
|
|
return ContentShape(
|
|
|
|
|
"Parts_Media_Edit_FileName",
|
|
|
|
|
() => {
|
|
|
|
|
var currentUser = _authenticationService.GetAuthenticatedUser();
|
2017-06-23 03:27:29 +08:00
|
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.EditMediaContent, currentUser, part)) {
|
2014-04-01 07:14:19 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var settings = part.TypeDefinition.Settings.GetModel<MediaFileNameEditorSettings>();
|
|
|
|
|
if (!settings.ShowFileNameEditor) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MediaFileNameEditorViewModel model = shapeHelper.Parts_Media_Edit_FileName(typeof(MediaFileNameEditorViewModel));
|
|
|
|
|
|
|
|
|
|
if (part.FileName != null) {
|
|
|
|
|
model.FileName = part.FileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
var priorFileName = model.FileName;
|
|
|
|
|
if (updater.TryUpdateModel(model, Prefix, null, null)) {
|
|
|
|
|
if (model.FileName != null && !model.FileName.Equals(priorFileName, StringComparison.OrdinalIgnoreCase)) {
|
2024-04-19 21:48:17 +08:00
|
|
|
|
var fieldName = "MediaFileNameEditorSettings.FileName";
|
|
|
|
|
|
2014-04-01 07:14:19 +08:00
|
|
|
|
try {
|
|
|
|
|
_mediaLibraryService.RenameFile(part.FolderPath, priorFileName, model.FileName);
|
|
|
|
|
part.FileName = model.FileName;
|
2019-08-23 01:39:09 +08:00
|
|
|
|
|
2016-03-23 04:19:50 +08:00
|
|
|
|
_notifier.Add(NotifyType.Success, T("File '{0}' was renamed to '{1}'", priorFileName, model.FileName));
|
2014-04-01 07:14:19 +08:00
|
|
|
|
}
|
2019-08-23 01:39:09 +08:00
|
|
|
|
catch (OrchardException) {
|
2024-04-19 21:48:17 +08:00
|
|
|
|
updater.AddModelError(fieldName, T("Unable to rename file. Invalid Windows file path."));
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidNameCharacterException) {
|
|
|
|
|
updater.AddModelError(fieldName, T("The file name contains invalid character(s)."));
|
2019-08-23 01:39:09 +08:00
|
|
|
|
}
|
2024-04-19 21:48:17 +08:00
|
|
|
|
catch (Exception exception) {
|
|
|
|
|
updater.AddModelError(fieldName, T("Unable to rename file: {0}", exception.Message));
|
2014-04-01 07:14:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-23 04:19:50 +08:00
|
|
|
|
|
2014-04-01 07:14:19 +08:00
|
|
|
|
return model;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|