Ejemplo n.º 1
0
        protected override DriverResult Editor(MediaPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            return(ContentShape(
                       "Parts_Media_Edit_FileName",
                       () => {
                var currentUser = _authenticationService.GetAuthenticatedUser();
                if (!_authorizationService.TryCheckAccess(Permissions.ManageMediaContent, currentUser, part))
                {
                    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))
                        {
                            try {
                                _mediaLibraryService.RenameFile(part.FolderPath, priorFileName, model.FileName);
                                part.FileName = model.FileName;

                                _notifier.Add(NotifyType.Success, T("File '{0}' was renamed to '{1}'", priorFileName, model.FileName));
                            }
                            catch (Exception) {
                                updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file"));
                            }
                        }
                    }
                }

                return model;
            }));
        }