From 874f464a2b51000920e2323f93bea116546e9b88 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 10 Jul 2014 11:45:57 +1000 Subject: [PATCH] Fixes: U4-5190 Overwrite Media File in 6.2.1 - process cannot access along with a bunch of other undisposed disposables :/ --- src/Umbraco.Core/IO/FileSystemExtensions.cs | 5 +- src/Umbraco.Core/IO/PhysicalFileSystem.cs | 10 +++- src/Umbraco.Core/IO/UmbracoMediaFile.cs | 15 +++-- .../umbraco/channels/UmbracoMetaWeblogAPI.cs | 54 ++++++++++-------- .../imagecropper/ImageInfo.cs | 18 +++--- .../imagecropper/ImageManipulation.cs | 57 ++++++++++--------- 6 files changed, 88 insertions(+), 71 deletions(-) diff --git a/src/Umbraco.Core/IO/FileSystemExtensions.cs b/src/Umbraco.Core/IO/FileSystemExtensions.cs index a4a40d37ba..bc89916677 100644 --- a/src/Umbraco.Core/IO/FileSystemExtensions.cs +++ b/src/Umbraco.Core/IO/FileSystemExtensions.cs @@ -19,7 +19,10 @@ namespace Umbraco.Core.IO public static void CopyFile(this IFileSystem fs, string path, string newPath) { - fs.AddFile(newPath, fs.OpenFile(path)); + using (var stream = fs.OpenFile(path)) + { + fs.AddFile(newPath, stream); + } } public static string GetExtension(this IFileSystem fs, string path) diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index 74d7f27671..6571721362 100644 --- a/src/Umbraco.Core/IO/PhysicalFileSystem.cs +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -93,10 +93,16 @@ namespace Umbraco.Core.IO public void AddFile(string path, Stream stream, bool overrideIfExists) { - if (FileExists(path) && !overrideIfExists) throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path)); - + var exists = FileExists(path); + if (exists && overrideIfExists == false) throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path)); + EnsureDirectory(Path.GetDirectoryName(path)); + if (exists) + { + DeleteFile(path); + } + if (stream.CanSeek) stream.Seek(0, 0); diff --git a/src/Umbraco.Core/IO/UmbracoMediaFile.cs b/src/Umbraco.Core/IO/UmbracoMediaFile.cs index e657d9507a..2c8021388a 100644 --- a/src/Umbraco.Core/IO/UmbracoMediaFile.cs +++ b/src/Umbraco.Core/IO/UmbracoMediaFile.cs @@ -135,14 +135,13 @@ 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/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs index 00e1fc721f..166a8719cf 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs @@ -14,6 +14,7 @@ using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.property; using umbraco.cms.businesslogic.propertytype; using umbraco.cms.businesslogic.web; +using Umbraco.Core.Logging; using Umbraco.Core.Security; using umbraco.presentation.channels.businesslogic; using Post = CookComputing.MetaWeblog.Post; @@ -73,14 +74,14 @@ namespace umbraco.presentation.channels // Excerpt if (userChannel.FieldExcerptAlias != null && userChannel.FieldExcerptAlias != "") - doc.getProperty(userChannel.FieldExcerptAlias).Value = removeLeftUrl(post.mt_excerpt); + doc.getProperty(userChannel.FieldExcerptAlias).Value = RemoveLeftUrl(post.mt_excerpt); if (UmbracoSettings.TidyEditorContent) - doc.getProperty(userChannel.FieldDescriptionAlias).Value = library.Tidy(removeLeftUrl(post.description), false); + doc.getProperty(userChannel.FieldDescriptionAlias).Value = library.Tidy(RemoveLeftUrl(post.description), false); else - doc.getProperty(userChannel.FieldDescriptionAlias).Value = removeLeftUrl(post.description); + doc.getProperty(userChannel.FieldDescriptionAlias).Value = RemoveLeftUrl(post.description); - updateCategories(doc, post, userChannel); + UpdateCategories(doc, post, userChannel); if (publish) @@ -95,7 +96,7 @@ namespace umbraco.presentation.channels } } - private void updateCategories(Document doc, Post post, Channel userChannel) + private static void UpdateCategories(Document doc, Post post, Channel userChannel) { if (userChannel.FieldCategoriesAlias != null && userChannel.FieldCategoriesAlias != "") { @@ -382,17 +383,17 @@ namespace umbraco.presentation.channels // Excerpt if (userChannel.FieldExcerptAlias != null && userChannel.FieldExcerptAlias != "") - doc.getProperty(userChannel.FieldExcerptAlias).Value = removeLeftUrl(post.mt_excerpt); + doc.getProperty(userChannel.FieldExcerptAlias).Value = RemoveLeftUrl(post.mt_excerpt); // Description if (UmbracoSettings.TidyEditorContent) - doc.getProperty(userChannel.FieldDescriptionAlias).Value = library.Tidy(removeLeftUrl(post.description), false); + doc.getProperty(userChannel.FieldDescriptionAlias).Value = library.Tidy(RemoveLeftUrl(post.description), false); else - doc.getProperty(userChannel.FieldDescriptionAlias).Value = removeLeftUrl(post.description); + doc.getProperty(userChannel.FieldDescriptionAlias).Value = RemoveLeftUrl(post.description); // Categories - updateCategories(doc, post, userChannel); + UpdateCategories(doc, post, userChannel); // check release date if (post.dateCreated.Year > 0001) @@ -484,24 +485,29 @@ namespace umbraco.presentation.channels int fileWidth; int fileHeight; - var stream = _fs.OpenFile(relativeFilePath); + using (var stream = _fs.OpenFile(relativeFilePath)) + { + Image image = Image.FromStream(stream); + fileWidth = image.Width; + fileHeight = image.Height; + stream.Close(); + try + { + m.getProperty(Constants.Conventions.Media.Width).Value = fileWidth.ToString(); + m.getProperty(Constants.Conventions.Media.Height).Value = fileHeight.ToString(); + } + catch (Exception ex) + { + LogHelper.Error("An error occurred reading the media stream", ex); + } + } - Image image = Image.FromStream(stream); - fileWidth = image.Width; - fileHeight = image.Height; - stream.Close(); - try - { - m.getProperty(Constants.Conventions.Media.Width).Value = fileWidth.ToString(); - m.getProperty(Constants.Conventions.Media.Height).Value = fileHeight.ToString(); - } - catch - { - } + } } - catch + catch (Exception ex) { + LogHelper.Error("An error occurred in newMediaObjectLogic", ex); } return fileUrl; @@ -551,7 +557,7 @@ namespace umbraco.presentation.channels throw new ArgumentException(string.Format("No data found for user with username: '{0}'", username)); } - private string removeLeftUrl(string text) + private static string RemoveLeftUrl(string text) { return text.Replace(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), ""); diff --git a/src/umbraco.editorControls/imagecropper/ImageInfo.cs b/src/umbraco.editorControls/imagecropper/ImageInfo.cs index addd629880..e0ee17cd4d 100644 --- a/src/umbraco.editorControls/imagecropper/ImageInfo.cs +++ b/src/umbraco.editorControls/imagecropper/ImageInfo.cs @@ -32,16 +32,18 @@ namespace umbraco.editorControls.imagecropper //This get's the IFileSystem's path based on the URL (i.e. /media/blah/blah.jpg ) Path = _fs.GetRelativePath(relativePath); - - image = Image.FromStream(_fs.OpenFile(Path)); - var fileName = _fs.GetFileName(Path); - Name = fileName.Substring(0, fileName.LastIndexOf('.')); + using (var stream = _fs.OpenFile(Path)) + using (image = Image.FromStream(stream)) + { + var fileName = _fs.GetFileName(Path); + Name = fileName.Substring(0, fileName.LastIndexOf('.')); - DateStamp = _fs.GetLastModified(Path).Date; - Width = image.Width; - Height = image.Height; - Aspect = (float)Width / Height; + DateStamp = _fs.GetLastModified(Path).Date; + Width = image.Width; + Height = image.Height; + Aspect = (float)Width / Height; + } } catch (Exception) diff --git a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs index 0e54b4aca1..c278f2d98a 100644 --- a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs +++ b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs @@ -33,18 +33,15 @@ namespace umbraco.editorControls.imagecropper //DirectoryInfo di = new DirectoryInfo(path); //if (!di.Exists) di.Create(); - Image image = Image.FromStream(_fs.OpenFile(sourceFile)); - - using (Image croppedImage = CropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight))) + using (var stream = _fs.OpenFile(sourceFile)) + using (var image = Image.FromStream(stream)) + using (var croppedImage = CropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight))) + using (var resizedImage = ResizeImage(croppedImage, new Size(sizeWidth, sizeHeight))) + using (var b = new Bitmap(resizedImage)) { - using (Image resizedImage = ResizeImage(croppedImage, new Size(sizeWidth, sizeHeight))) - { - using (Bitmap b = new Bitmap(resizedImage)) - { - SaveJpeg(String.Format("{0}/{1}.jpg", path, name), b, quality); - } - } + SaveJpeg(String.Format("{0}/{1}.jpg", path, name), b, quality); } + } private static void SaveJpeg(string path, Bitmap img, long quality) @@ -61,13 +58,13 @@ namespace umbraco.editorControls.imagecropper EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; - var fileStream = new MemoryStream(); + using (var fileStream = new MemoryStream()) + { + img.Save(fileStream, jpegCodec, encoderParams); + fileStream.Position = 0; + _fs.AddFile(path, fileStream, true); + } - img.Save(fileStream, jpegCodec, encoderParams); - fileStream.Position = 0; - _fs.AddFile(path, fileStream, true); - //just to be sure - fileStream.Dispose(); } private static ImageCodecInfo GetEncoderInfo(string mimeType) @@ -107,20 +104,24 @@ namespace umbraco.editorControls.imagecropper Bitmap b = new Bitmap(destWidth, destHeight); - ImageAttributes ia = new ImageAttributes(); - ia.SetWrapMode(WrapMode.TileFlipXY); + using (var ia = new ImageAttributes()) + { + ia.SetWrapMode(WrapMode.TileFlipXY); - Graphics g = Graphics.FromImage(b); - g.PixelOffsetMode = PixelOffsetMode.HighQuality; - g.Clear(Color.White); - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - g.CompositingQuality = CompositingQuality.HighQuality; - g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawImage(imgToResize, new Rectangle(0, 0, destWidth, destHeight), 0, 0, imgToResize.Width, - imgToResize.Height, GraphicsUnit.Pixel, ia); + using (Graphics g = Graphics.FromImage(b)) + { + g.PixelOffsetMode = PixelOffsetMode.HighQuality; + g.Clear(Color.White); + g.InterpolationMode = InterpolationMode.HighQualityBicubic; + g.CompositingQuality = CompositingQuality.HighQuality; + g.SmoothingMode = SmoothingMode.HighQuality; + g.DrawImage(imgToResize, new Rectangle(0, 0, destWidth, destHeight), 0, 0, imgToResize.Width, + imgToResize.Height, GraphicsUnit.Pixel, ia); - ia.Dispose(); - g.Dispose(); + ia.Dispose(); + } + } + return b; }