From 54e826bc964142d214f24d41c78873d3626f60c9 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 26 Nov 2013 13:13:43 +1100 Subject: [PATCH] Fixes: U4-3696 Upload datatype only updates on first save --- .../src/common/services/filemanager.service.js | 15 ++++----------- .../common/services/umbrequesthelper.service.js | 4 ++-- src/Umbraco.Web.UI.Client/src/init.js | 8 ++++++-- .../fileupload/fileupload.controller.js | 6 +++--- .../unit/common/services/file-manager.spec.js | 10 +++++----- src/Umbraco.Web/Editors/ContentControllerBase.cs | 2 +- .../Models/ContentEditing/ContentItemFile.cs | 4 ++-- .../WebApi/Binders/ContentItemBaseBinder.cs | 14 +++----------- 8 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/filemanager.service.js b/src/Umbraco.Web.UI.Client/src/common/services/filemanager.service.js index 3e09032f7d..4057623675 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/filemanager.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/filemanager.service.js @@ -8,17 +8,10 @@ * that need to attach files. * When a route changes successfully, we ensure that the collection is cleared. */ -function fileManager($rootScope) { +function fileManager() { var fileCollection = []; - //Whenever a route changes - clear the curent file collection, the file collection is only relavent - // when working in an editor and submitting data to the server. - //This ensures that memory remains clear of any files and that the editors don't have to manually clear the files. - $rootScope.$on('$routeChangeSuccess', function (event, current, previous) { - fileCollection = []; - }); - return { /** * @ngdoc function @@ -30,14 +23,14 @@ function fileManager($rootScope) { * Attaches files to the current manager for the current editor for a particular property, if an empty array is set * for the files collection that effectively clears the files for the specified editor. */ - setFiles: function(propertyId, files) { + setFiles: function(propertyAlias, files) { //this will clear the files for the current property and then add the new ones for the current property fileCollection = _.reject(fileCollection, function (item) { - return item.id === propertyId; + return item.alias === propertyAlias; }); for (var i = 0; i < files.length; i++) { //save the file object to the files collection - fileCollection.push({ id: propertyId, file: files[i] }); + fileCollection.push({ alias: propertyAlias, file: files[i] }); } }, diff --git a/src/Umbraco.Web.UI.Client/src/common/services/umbrequesthelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/umbrequesthelper.service.js index 1398deca20..dfc970422c 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/umbrequesthelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/umbrequesthelper.service.js @@ -200,9 +200,9 @@ function umbRequestHelper($http, $q, umbDataFormatter, angularHelper, dialogServ function (data, formData) { //now add all of the assigned files for (var f in args.files) { - //each item has a property id and the file object, we'll ensure that the id is suffixed to the key + //each item has a property alias and the file object, we'll ensure that the alias is suffixed to the key // so we know which property it belongs to on the server side - formData.append("file_" + args.files[f].id, args.files[f].file); + formData.append("file_" + args.files[f].alias, args.files[f].file); } }, diff --git a/src/Umbraco.Web.UI.Client/src/init.js b/src/Umbraco.Web.UI.Client/src/init.js index 34fc1b5888..0bb455d0ec 100644 --- a/src/Umbraco.Web.UI.Client/src/init.js +++ b/src/Umbraco.Web.UI.Client/src/init.js @@ -1,6 +1,6 @@ /** Executed when the application starts */ -app.run(['userService', '$log', '$rootScope', '$location', 'navigationService', 'appState', 'editorState', - function(userService, $log, $rootScope, $location, navigationService, appState, editorState) { +app.run(['userService', '$log', '$rootScope', '$location', 'navigationService', 'appState', 'editorState', 'fileManager', + function (userService, $log, $rootScope, $location, navigationService, appState, editorState, fileManager) { var firstRun = true; @@ -26,6 +26,10 @@ app.run(['userService', '$log', '$rootScope', '$location', 'navigationService', //reset the editorState on each successful route chage editorState.reset(); + //reset the file manager on each route change, the file collection is only relavent + // when working in an editor and submitting data to the server. + //This ensures that memory remains clear of any files and that the editors don't have to manually clear the files. + fileManager.clearFiles(); }); /** When the route change is rejected - based on checkAuth - we'll prevent the rejected route from executing including 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 cd3b24da4e..f1049f2a41 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 @@ -18,7 +18,7 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag /** Clears the file collections when content is saving (if we need to clear) or after saved */ function clearFiles() { //clear the files collection (we don't want to upload any!) - fileManager.setFiles($scope.id, []); + fileManager.setFiles($scope.model.alias, []); //clear the current files $scope.files = []; } @@ -85,13 +85,13 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag $scope.$on("filesSelected", function (event, args) { $scope.$apply(function () { //set the files collection - fileManager.setFiles($scope.model.id, args.files); + fileManager.setFiles($scope.model.alias, args.files); //clear the current files $scope.files = []; var newVal = ""; for (var i = 0; i < args.files.length; i++) { //save the file object to the scope's files collection - $scope.files.push({ id: $scope.model.id, file: args.files[i] }); + $scope.files.push({ alias: $scope.model.alias, file: args.files[i] }); newVal += args.files[i].name + ","; } //set clear files to false, this will reset the model too diff --git a/src/Umbraco.Web.UI.Client/test/unit/common/services/file-manager.spec.js b/src/Umbraco.Web.UI.Client/test/unit/common/services/file-manager.spec.js index a7e9243ade..60d57d347c 100644 --- a/src/Umbraco.Web.UI.Client/test/unit/common/services/file-manager.spec.js +++ b/src/Umbraco.Web.UI.Client/test/unit/common/services/file-manager.spec.js @@ -10,20 +10,20 @@ describe('file manager tests', function () { describe('file management', function () { it('adding a file adds to the collection', function () { - fileManager.setFiles(123, ["testFile"]); + fileManager.setFiles('testProp', ["testFile"]); expect(fileManager.getFiles().length).toBe(1); }); it('adding a file with the same property id replaces the existing one', function () { - fileManager.setFiles(123, ["testFile"]); - fileManager.setFiles(123, ["testFile2"]); + fileManager.setFiles('testProp', ["testFile"]); + fileManager.setFiles('testProp', ["testFile2"]); expect(fileManager.getFiles().length).toBe(1); expect(fileManager.getFiles()[0].file).toBe("testFile2"); }); it('clears all files', function () { - fileManager.setFiles(123, ["testFile"]); - fileManager.setFiles(234, ["testFile"]); + fileManager.setFiles('testProp1', ["testFile"]); + fileManager.setFiles('testProp2', ["testFile"]); expect(fileManager.getFiles().length).toBe(2); fileManager.clearFiles(); expect(fileManager.getFiles().length).toBe(0); diff --git a/src/Umbraco.Web/Editors/ContentControllerBase.cs b/src/Umbraco.Web/Editors/ContentControllerBase.cs index 77125cc4b9..7e97940d08 100644 --- a/src/Umbraco.Web/Editors/ContentControllerBase.cs +++ b/src/Umbraco.Web/Editors/ContentControllerBase.cs @@ -95,7 +95,7 @@ namespace Umbraco.Web.Editors //create the property data to send to the property editor var d = new Dictionary(); //add the files if any - var files = contentItem.UploadedFiles.Where(x => x.PropertyId == p.Id).ToArray(); + var files = contentItem.UploadedFiles.Where(x => x.PropertyAlias == p.Alias).ToArray(); if (files.Any()) { d.Add("files", files); diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemFile.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemFile.cs index 666d6a5553..2800d410f4 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemFile.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemFile.cs @@ -6,9 +6,9 @@ public class ContentItemFile { /// - /// The property id associated with the file + /// The property alias associated with the file /// - public int PropertyId { get; set; } + public string PropertyAlias { get; set; } /// /// The original file name diff --git a/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs b/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs index df9a76cda8..a25ce7d18c 100644 --- a/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs +++ b/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs @@ -144,22 +144,14 @@ namespace Umbraco.Web.WebApi.Binders ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited" }); } - int propertyId; - if (int.TryParse(parts[1], out propertyId) == false) - { - throw new HttpResponseException( - new HttpResponseMessage(HttpStatusCode.BadRequest) - { - ReasonPhrase = "The request was not formatted correctly the file name's 2nd part must be an integer" - }); - } - + var propAlias = parts[1]; + var fileName = file.Headers.ContentDisposition.FileName.Trim(new char[] {'\"'}); model.UploadedFiles.Add(new ContentItemFile { TempFilePath = file.LocalFileName, - PropertyId = propertyId, + PropertyAlias = propAlias, FileName = fileName }); }