diff --git a/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js index e70f030557..11bae4e027 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js @@ -542,9 +542,6 @@ function contentEditingHelper(fileManager, $q, $location, $routeParams, notifica changed.push(origProp); } - } - for (var p in allOrigProps) { - } } diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js index a5ebc92c5f..17959b9950 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js @@ -10,7 +10,7 @@ * The controller for the file upload property editor. * */ - function fileUploadController($scope) { + function fileUploadController($scope, fileManager) { $scope.fileChanged = onFileChanged; //declare a special method which will be called whenever the value has changed from the server @@ -22,11 +22,6 @@ */ function onFileChanged(value) { $scope.model.value = value; - - //if the value is empty, then tell the server to clear the files - if (!$scope.model.value) { - $scope.model.value = { clearFiles: true }; - } } /** diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyValueEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyValueEditor.cs index b98de3f574..be7b3ff2f8 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyValueEditor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json.Linq; +using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models.Editors; using Umbraco.Core.PropertyEditors; @@ -42,34 +43,14 @@ namespace Umbraco.Web.PropertyEditors /// public override object FromEditor(ContentPropertyData editorValue, object currentValue) { - currentValue = currentValue ?? string.Empty; + var currentPath = currentValue as string; + if (!currentPath.IsNullOrWhiteSpace()) + currentPath = _mediaFileSystem.GetRelativePath(currentPath); - // at that point, - // currentValue is either empty or "/media/path/to/img.jpg" - // editorValue.Value is { "clearFiles": true } or { "selectedFiles": "img1.jpg,img2.jpg" } - // comparing them makes little sense - - // check the editorValue value to see whether we need to clear files - var editorJsonValue = editorValue.Value as JObject; - var clears = editorJsonValue != null && editorJsonValue["clearFiles"] != null && editorJsonValue["clearFiles"].Value(); - var uploads = editorValue.Files != null && editorValue.Files.Length > 0; - - // nothing = no changes, return what we have already (leave existing files intact) - if (clears == false && uploads == false) - return currentValue; - - // get the current file paths - var currentPaths = currentValue.ToString() - .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) - .Select(x => _mediaFileSystem.GetRelativePath(x)) // get the fs-relative path - .ToArray(); - - // if clearing, remove these files and return - if (clears) + string editorFile = null; + if (editorValue.Value != null) { - foreach (var pathToRemove in currentPaths) - _mediaFileSystem.DeleteFile(pathToRemove); - return string.Empty; // no more files + editorFile = editorValue.Value as string; } // ensure we have the required guids @@ -78,46 +59,58 @@ namespace Umbraco.Web.PropertyEditors var puid = editorValue.PropertyTypeKey; if (puid == Guid.Empty) throw new Exception("Invalid property type key."); - // process the files - var files = editorValue.Files; - if (files == null) throw new Exception("Invalid files."); + var uploads = editorValue.Files; + if (uploads == null) throw new Exception("Invalid files."); + var file = uploads.Length > 0 ? uploads[0] : null; - var newPaths = new List(); - const int maxLength = 1; // we only process ONE file - for (var i = 0; i < maxLength /*files.Length*/; i++) + if (file == null) // not uploading a file { - var file = files[i]; - - // skip invalid files - if (UploadFileTypeValidator.ValidateFileExtension(file.FileName) == false) - continue; - - // get the filepath - // in case we are using the old path scheme, try to re-use numbers (bah...) - var reuse = i < currentPaths.Length ? currentPaths[i] : null; // this would be WRONG with many files - var filepath = _mediaFileSystem.GetMediaPath(file.FileName, reuse, cuid, puid); // fs-relative path - - using (var filestream = File.OpenRead(file.TempFilePath)) + // if editorFile is empty then either there was nothing to begin with, + // or it has been cleared and we need to remove the file - else the + // value is unchanged. + if (string.IsNullOrWhiteSpace(editorFile) && string.IsNullOrWhiteSpace(currentPath) == false) { - _mediaFileSystem.AddFile(filepath, filestream, true); // must overwrite! - - // all related properties (auto-fill) are managed by FileUploadPropertyEditor - // when the content is saved (through event handlers) - - newPaths.Add(filepath); + _mediaFileSystem.DeleteFile(currentPath); + return null; // clear } + + return currentValue; // unchanged } + // process the file + var filepath = editorFile == null ? null : ProcessFile(editorValue, file, currentPath, cuid, puid); + // remove all temp files - foreach (var file in files) - File.Delete(file.TempFilePath); + foreach (var f in uploads) + File.Delete(f.TempFilePath); - // remove files that are not there anymore - foreach (var pathToRemove in currentPaths.Except(newPaths)) - _mediaFileSystem.DeleteFile(pathToRemove); + // remove current file if replaced + if (currentPath != filepath && string.IsNullOrWhiteSpace(currentPath) == false) + _mediaFileSystem.DeleteFile(currentPath); + // update json and return + if (editorFile == null) return null; + return filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath); + + } - return string.Join(",", newPaths.Select(x => _mediaFileSystem.GetUrl(x))); + private string ProcessFile(ContentPropertyData editorValue, ContentPropertyFile file, string currentPath, Guid cuid, Guid puid) + { + // process the file + // no file, invalid file, reject change + if (UploadFileTypeValidator.ValidateFileExtension(file.FileName) == false) + return null; + + // get the filepath + // in case we are using the old path scheme, try to re-use numbers (bah...) + var filepath = _mediaFileSystem.GetMediaPath(file.FileName, currentPath, cuid, puid); // fs-relative path + + using (var filestream = File.OpenRead(file.TempFilePath)) + { + _mediaFileSystem.AddFile(filepath, filestream, true); // must overwrite! + } + + return filepath; } } }