From dedaa2125d4c3dc01ea1a98a7e61550eb8397d0e Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 28 Aug 2013 10:45:50 +1000 Subject: [PATCH] Fixes up a few issues found with media uploading --- src/Umbraco.Core/IO/UmbracoMediaFile.cs | 17 ++++++------- .../src/common/services/util.service.js | 2 +- src/Umbraco.Web/Editors/MediaController.cs | 24 ++++++++++++------- .../FileUploadPropertyEditor.cs | 7 ++++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Umbraco.Core/IO/UmbracoMediaFile.cs b/src/Umbraco.Core/IO/UmbracoMediaFile.cs index cf08c63168..dc8632d4c7 100644 --- a/src/Umbraco.Core/IO/UmbracoMediaFile.cs +++ b/src/Umbraco.Core/IO/UmbracoMediaFile.cs @@ -135,14 +135,15 @@ namespace Umbraco.Core.IO { EnsureFileSupportsResizing(); - var fs = _fs.OpenFile(Path); - var image = Image.FromStream(fs); - var fileWidth = image.Width; - var fileHeight = image.Height; - fs.Close(); - image.Dispose(); - - _size = new Size(fileWidth, fileHeight); + using (var fs = _fs.OpenFile(Path)) + { + using (var image = Image.FromStream(fs)) + { + var fileWidth = image.Width; + var fileHeight = image.Height; + _size = new Size(fileWidth, fileHeight); + } + } } return _size.Value; } diff --git a/src/Umbraco.Web.UI.Client/src/common/services/util.service.js b/src/Umbraco.Web.UI.Client/src/common/services/util.service.js index 8bca5edd1b..9041135a8e 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/util.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/util.service.js @@ -120,7 +120,7 @@ function imageHelper() { return imagePath.substr(0, imagePath.lastIndexOf('.')) + "_thumb" + ".jpg"; }, detectIfImageByExtension: function(imagePath) { - var lowered = imagePath; + var lowered = imagePath.toLowerCase(); var ext = lowered.substr(lowered.lastIndexOf(".") + 1); return ("," + Umbraco.Sys.ServerVariables.umbracoSettings.imageFileTypes + ",").indexOf("," + ext + ",") !== -1; } diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index ba8618652d..837091ed94 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -218,20 +218,28 @@ namespace Umbraco.Web.Editors } //short hand to use with the uploader in the media dialog - public HttpResponseMessage PostAddFile(int parentId) + public HttpResponseMessage PostAddFile() { var context = UmbracoContext.HttpContext; if(context.Request.Files.Count > 0) { - var postedFile = context.Request.Files[0]; - var name = postedFile.FileName; + if (context.Request.Form.Count > 0) + { + int parentId; + if (int.TryParse(context.Request.Form[0], out parentId)) + { + var file = context.Request.Files[0]; + var name = file.FileName; - var mediaService = base.ApplicationContext.Services.MediaService; - var f = mediaService.CreateMedia(name, parentId, Constants.Conventions.MediaTypes.Image); - f.SetValue(Constants.Conventions.Media.File, postedFile); - mediaService.Save(f); + var mediaService = base.ApplicationContext.Services.MediaService; + var f = mediaService.CreateMedia(name, parentId, Constants.Conventions.MediaTypes.Image); + f.SetValue(Constants.Conventions.Media.File, file); + mediaService.Save(f); - return new HttpResponseMessage(HttpStatusCode.OK); + return new HttpResponseMessage(HttpStatusCode.OK); + } + } + } return new HttpResponseMessage(HttpStatusCode.NotFound); diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 33a32e922c..4dc19dd85c 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Linq; using System.Xml; using Umbraco.Core; @@ -101,9 +102,11 @@ namespace Umbraco.Web.PropertyEditors private static void FillProperties(XmlNode uploadFieldConfigNode, IContentBase content, UmbracoMediaFile um) { + var size = um.SupportsResizing ? (Size?)um.GetDimensions() : null; + // only add dimensions to web images - UpdateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias", um.SupportsResizing ? um.GetDimensions().Width.ToString() : string.Empty); - UpdateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias", um.SupportsResizing ? um.GetDimensions().Height.ToString() : string.Empty); + UpdateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias", size.HasValue ? size.Value.Width.ToInvariantString() : string.Empty); + UpdateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias", size.HasValue ? size.Value.Height.ToInvariantString() : string.Empty); UpdateContentProperty(uploadFieldConfigNode, content, "lengthFieldAlias", um.Length); UpdateContentProperty(uploadFieldConfigNode, content, "extensionFieldAlias", um.Extension);