diff --git a/src/Umbraco.Core/IO/FileSystemExtensions.cs b/src/Umbraco.Core/IO/FileSystemExtensions.cs index 6edc52eeb1..f513deb9f5 100644 --- a/src/Umbraco.Core/IO/FileSystemExtensions.cs +++ b/src/Umbraco.Core/IO/FileSystemExtensions.cs @@ -18,9 +18,9 @@ namespace Umbraco.Core.IO public static void CopyFile(this IFileSystem fs, string path, string newPath) { - using (var fileStream = fs.OpenFile(path)) + using (var stream = fs.OpenFile(path)) { - fs.AddFile(newPath, fileStream); + fs.AddFile(newPath, stream); } } diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index bdba541ea3..8e40d4c711 100644 --- a/src/Umbraco.Core/IO/PhysicalFileSystem.cs +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -90,10 +90,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 74bf7bd899..6137800239 100644 --- a/src/Umbraco.Core/IO/UmbracoMediaFile.cs +++ b/src/Umbraco.Core/IO/UmbracoMediaFile.cs @@ -153,13 +153,14 @@ namespace Umbraco.Core.IO { EnsureFileSupportsResizing(); - using (var fs = _fs.OpenFile(Path)) - { - using (var image = Image.FromStream(fs)) + 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); + var fileWidth = image.Width; + var fileHeight = image.Height; + _size = new Size(fileWidth, fileHeight); + } } } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs index 4952b4eccf..a57809ac07 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/channels/UmbracoMetaWeblogAPI.cs @@ -15,6 +15,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; @@ -74,15 +75,15 @@ 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 (UmbracoConfig.For.UmbracoSettings().Content.TidyEditorContent) 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) @@ -97,7 +98,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 != "") { @@ -384,17 +385,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 (UmbracoConfig.For.UmbracoSettings().Content.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) @@ -486,24 +487,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; @@ -553,7 +559,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 6d142bf90e..a51648a529 100644 --- a/src/umbraco.editorControls/imagecropper/ImageInfo.cs +++ b/src/umbraco.editorControls/imagecropper/ImageInfo.cs @@ -33,16 +33,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 2c24740338..69f9fb3607 100644 --- a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs +++ b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs @@ -34,18 +34,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) @@ -62,13 +59,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) @@ -108,20 +105,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; }