From c5710277a5d248e751c7670065e923dd87b03b4a Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 22 Mar 2013 12:13:06 -0100 Subject: [PATCH 1/3] Redoing the fix for U4-1875 --- src/Umbraco.Core/Models/EntityBase/Entity.cs | 9 ++++++ src/Umbraco.Core/Services/ContentService.cs | 18 +++++++----- src/Umbraco.Core/Services/MediaService.cs | 28 +++++++++++-------- src/umbraco.cms/businesslogic/media/Media.cs | 5 ++-- src/umbraco.cms/businesslogic/web/Document.cs | 5 ++-- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Core/Models/EntityBase/Entity.cs b/src/Umbraco.Core/Models/EntityBase/Entity.cs index 6c081ea0b4..8df5be5809 100644 --- a/src/Umbraco.Core/Models/EntityBase/Entity.cs +++ b/src/Umbraco.Core/Models/EntityBase/Entity.cs @@ -66,6 +66,15 @@ namespace Umbraco.Core.Models.EntityBase [DataMember] public DateTime UpdateDate { get; set; } + /// + /// Gets or sets the WasCancelled flag, which is used to track + /// whether some action against an entity was cancelled through some event. + /// This only exists so we have a way to check if an event was cancelled through + /// the new api, which also needs to take effect in the legacy api. + /// + [IgnoreDataMember] + internal bool WasCancelled { get; set; } + /// /// Property changed event /// diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index ee435f0ffa..18f7de78b0 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -64,13 +64,15 @@ namespace Umbraco.Core.Services /// public IContent CreateContent(string name, int parentId, string contentTypeAlias, int userId = 0) { - IContentType contentType = FindContentTypeByAlias(contentTypeAlias); - IContent content = null; + var contentType = FindContentTypeByAlias(contentTypeAlias); + var content = new Content(name, parentId, contentType); ; if (Creating.IsRaisedEventCancelled(new NewEventArgs(content, contentTypeAlias, parentId), this)) - return content; + { + content.WasCancelled = true; + return content; + } - content = new Content(name, parentId, contentType); content.CreatorId = userId; content.WriterId = userId; @@ -92,13 +94,15 @@ namespace Umbraco.Core.Services /// public IContent CreateContent(string name, IContent parent, string contentTypeAlias, int userId = 0) { - IContentType contentType = FindContentTypeByAlias(contentTypeAlias); - IContent content = null; + var contentType = FindContentTypeByAlias(contentTypeAlias); + var content = new Content(name, parent, contentType); if (Creating.IsRaisedEventCancelled(new NewEventArgs(content, contentTypeAlias, parent), this)) + { + content.WasCancelled = true; return content; + } - content = new Content(name, parent, contentType); content.CreatorId = userId; content.WriterId = userId; diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 88aab5d5e1..71b7f421b4 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -46,11 +46,8 @@ namespace Umbraco.Core.Services /// public IMedia CreateMedia(string name, int parentId, string mediaTypeAlias, int userId = 0) { - Models.Media media = null; - IMediaType mediaType = null; - if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parentId), this)) - return media; - + IMediaType mediaType; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) { @@ -68,7 +65,13 @@ namespace Umbraco.Core.Services mediaTypeAlias)); } - media = new Models.Media(name, parentId, mediaType); + var media = new Models.Media(name, parentId, mediaType); + + if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parentId), this)) + { + media.WasCancelled = true; + return media; + } media.CreatorId = userId; @@ -90,11 +93,7 @@ namespace Umbraco.Core.Services /// public IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0) { - Models.Media media = null; - IMediaType mediaType = null; - - if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parent), this)) - return media; + IMediaType mediaType; var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) @@ -113,7 +112,12 @@ namespace Umbraco.Core.Services mediaTypeAlias)); } - media = new Models.Media(name, parent, mediaType); + var media = new Models.Media(name, parent, mediaType); + if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parent), this)) + { + media.WasCancelled = true; + return media; + } media.CreatorId = userId; diff --git a/src/umbraco.cms/businesslogic/media/Media.cs b/src/umbraco.cms/businesslogic/media/Media.cs index aed7476d5b..f2c0c84847 100644 --- a/src/umbraco.cms/businesslogic/media/Media.cs +++ b/src/umbraco.cms/businesslogic/media/Media.cs @@ -1,6 +1,7 @@ using System; using Umbraco.Core; using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using umbraco.BusinessLogic; using umbraco.DataLayer; using System.Collections; @@ -85,8 +86,8 @@ namespace umbraco.cms.businesslogic.media } var media = ApplicationContext.Current.Services.MediaService.CreateMedia(Name, ParentId, dct.Alias, u.Id); - //The media object will only be null if the 'Creating' event has been cancelled - if (media == null) + //The media object will only have the 'WasCancelled' flag set to 'True' if the 'Creating' event has been cancelled + if (((Entity)media).WasCancelled) return null; ApplicationContext.Current.Services.MediaService.Save(media); diff --git a/src/umbraco.cms/businesslogic/web/Document.cs b/src/umbraco.cms/businesslogic/web/Document.cs index a470d31fb7..6c9f039976 100644 --- a/src/umbraco.cms/businesslogic/web/Document.cs +++ b/src/umbraco.cms/businesslogic/web/Document.cs @@ -7,6 +7,7 @@ using System.Xml; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Logging; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence.Caching; using Umbraco.Core.Services; using umbraco.BusinessLogic; @@ -288,8 +289,8 @@ namespace umbraco.cms.businesslogic.web //Create a new IContent object based on the passed in DocumentType's alias, set the name and save it IContent content = ApplicationContext.Current.Services.ContentService.CreateContent(Name, ParentId, dct.Alias, u.Id); - //The content object will only be null if the 'Creating' event has been cancelled, so we return null. - if (content == null) + //The content object will only have the 'WasCancelled' flag set to 'True' if the 'Creating' event has been cancelled, so we return null. + if (((Entity)content).WasCancelled) return null; //don't raise events here (false), they will get raised with the d.Save() call. From ba0e72a1c1c9adb576f9b3d958d06e754db7de2f Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 22 Mar 2013 12:30:20 -0100 Subject: [PATCH 2/3] Fixes U4-1937 by calling save when media is updated with new properties. Also uses the incremented folder count instead of the property id. --- src/umbraco.webservices/media/mediaService.cs | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/umbraco.webservices/media/mediaService.cs b/src/umbraco.webservices/media/mediaService.cs index f1cb1dddd0..4623a48e26 100644 --- a/src/umbraco.webservices/media/mediaService.cs +++ b/src/umbraco.webservices/media/mediaService.cs @@ -1,16 +1,14 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.IO; -using System.Reflection; using System.Web.Services; using Umbraco.Core.Media; -using umbraco.IO; using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.property; using Umbraco.Core.IO; using System.Xml; -using System.Web; using System.Text.RegularExpressions; using System.Linq; using System.Web.Script.Services; @@ -41,12 +39,11 @@ namespace umbraco.webservices.media [WebMethod] public void update(mediaCarrier carrier, string username, string password) { - Authenticate(username, password); if (carrier == null) throw new Exception("No carrier specified"); - Media m = new Media(carrier.Id); + var m = new Media(carrier.Id); if (carrier.MediaProperties != null) { @@ -68,7 +65,6 @@ namespace umbraco.webservices.media [WebMethod] public int create(mediaCarrier carrier, string username, string password) { - Authenticate(username, password); if (carrier == null) throw new Exception("No carrier specified"); @@ -76,12 +72,10 @@ namespace umbraco.webservices.media if (carrier.TypeId == 0) throw new Exception("Type must be specified"); if (carrier.Text == null || carrier.Text.Length == 0) carrier.Text = "unnamed"; - umbraco.BusinessLogic.User user = GetUser(username, password); + BusinessLogic.User user = GetUser(username, password); - - MediaType mt = new MediaType(carrier.TypeId); - - Media m = Media.MakeNew(carrier.Text, mt, user, carrier.ParentId); + var mt = new MediaType(carrier.TypeId); + var m = Media.MakeNew(carrier.Text, mt, user, carrier.ParentId); if (carrier.MediaProperties != null) { @@ -134,10 +128,10 @@ namespace umbraco.webservices.media filename = filename.Replace(@"\", global::Umbraco.Core.IO.IOHelper.DirSepChar.ToString()); filename = filename.Substring(filename.LastIndexOf(global::Umbraco.Core.IO.IOHelper.DirSepChar) + 1, filename.Length - filename.LastIndexOf(global::Umbraco.Core.IO.IOHelper.DirSepChar) - 1).ToLower(); - Media m = new Media(id); + var m = new Media(id); - //TODO Fix this as the Id of the umbracoFile-property is no longer used - var path = _fs.GetRelativePath(m.getProperty("umbracoFile").Id, filename); + var numberedFolder = MediaSubfolderCounter.Current.Increment(); + var path = _fs.GetRelativePath(numberedFolder.ToString(CultureInfo.InvariantCulture), filename); var stream = new MemoryStream(); stream.Write(contents, 0, contents.Length); @@ -149,7 +143,7 @@ namespace umbraco.webservices.media m.getProperty("umbracoExtension").Value = Path.GetExtension(filename).Substring(1); m.getProperty("umbracoBytes").Value = _fs.GetSize(path); - + m.Save(); } [WebMethod] @@ -157,10 +151,8 @@ namespace umbraco.webservices.media { Authenticate(username, password); - Media m = new Media(id); - return createCarrier(m); } @@ -169,7 +161,7 @@ namespace umbraco.webservices.media { Authenticate(username, password); - List carriers = new List(); + var carriers = new List(); Media[] mediaList; if (parentId < 1) @@ -256,19 +248,18 @@ namespace umbraco.webservices.media private mediaCarrier createCarrier(Media m) { - mediaCarrier carrier = new mediaCarrier(); - carrier.Id = m.Id; - carrier.Text = m.Text; - - carrier.TypeAlias = m.ContentType.Alias; - carrier.TypeId = m.ContentType.Id; - - carrier.CreateDateTime = m.CreateDateTime; - carrier.HasChildren = m.HasChildren; - carrier.Level = m.Level; - - carrier.Path = m.Path; - carrier.SortOrder = m.sortOrder; + var carrier = new mediaCarrier + { + Id = m.Id, + Text = m.Text, + TypeAlias = m.ContentType.Alias, + TypeId = m.ContentType.Id, + CreateDateTime = m.CreateDateTime, + HasChildren = m.HasChildren, + Level = m.Level, + Path = m.Path, + SortOrder = m.sortOrder + }; try { @@ -281,10 +272,9 @@ namespace umbraco.webservices.media foreach (Property p in m.GenericProperties) { + var carrierprop = new mediaProperty(); - mediaProperty carrierprop = new mediaProperty(); - - if (p.Value == System.DBNull.Value) + if (p.Value == DBNull.Value) { carrierprop.PropertyValue = ""; } From fdefe993b715a552802618760e0ea569ca91f5b7 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 22 Mar 2013 12:54:46 -0100 Subject: [PATCH 3/3] Fixes U4-1990 --- .../media/UmbracoMediaFactory.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs index 52ad7beb84..8f2d841fa5 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs @@ -85,15 +85,20 @@ namespace umbraco.cms.businesslogic.media if (childMedia.ContentType.Alias == MediaTypeAlias) { var prop = childMedia.getProperty("umbracoFile"); - if (prop != null) + if (prop != null && prop.Value != null) { - var destFilePath = FileSystem.GetRelativePath(prop.Id, fileName); - var destFileUrl = FileSystem.GetUrl(destFilePath); - - if (prop.Value.ToString() == destFileUrl) + int subfolderId; + var subfolder = prop.Value.ToString().Replace(FileSystem.GetUrl("/"), "").Split('/')[0]; + if (int.TryParse(subfolder, out subfolderId)) { - existingMedia = childMedia; - return true; + var destFilePath = FileSystem.GetRelativePath(subfolderId, fileName); + var destFileUrl = FileSystem.GetUrl(destFilePath); + + if (prop.Value.ToString() == destFileUrl) + { + existingMedia = childMedia; + return true; + } } } }