From 067268a96e472a888aeeae10762b0967b4127a99 Mon Sep 17 00:00:00 2001 From: stefana99 Date: Mon, 16 Dec 2013 13:35:18 +0000 Subject: [PATCH 1/4] Changes to imageCropper, supports new mediafilesystem --- .../imagecropper/ImageInfo.cs | 70 +++++++++---------- .../imagecropper/ImageManipulation.cs | 40 +++++++---- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/src/umbraco.editorControls/imagecropper/ImageInfo.cs b/src/umbraco.editorControls/imagecropper/ImageInfo.cs index 9207a71ed8..b02d63256c 100644 --- a/src/umbraco.editorControls/imagecropper/ImageInfo.cs +++ b/src/umbraco.editorControls/imagecropper/ImageInfo.cs @@ -1,9 +1,11 @@ using System; using System.Drawing; using System.IO; +using System.Net; using System.Web; -using umbraco.editorControls.imagecropper; using Umbraco.Core.IO; +//using Umbraco.Core.IO; + namespace umbraco.editorControls.imagecropper { @@ -18,47 +20,39 @@ namespace umbraco.editorControls.imagecropper public string Path { get; set; } public string RelativePath { get; set; } + private readonly MediaFileSystem _fs; + public ImageInfo(string relativePath) { - RelativePath = relativePath; - Path = IOHelper.MapPath(relativePath); - if (File.Exists(Path)) + _fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + + try { - string fileName = Path.Substring(Path.LastIndexOf('\\') + 1); - Name = fileName.Substring(0, fileName.LastIndexOf('.')); - - byte[] buffer = null; - - using (FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read)) + if (relativePath.Contains("//")) { - buffer = new byte[fs.Length]; - fs.Read(buffer, 0, (int) fs.Length); - fs.Close(); + Path = _fs.GetFullPath(relativePath); + } + else + { + Path = IOHelper.MapPath(relativePath); } - try - { - image = Image.FromStream(new MemoryStream(buffer)); - - Width = image.Width; - Height = image.Height; - Aspect = (float) Width/Height; - DateStamp = File.GetLastWriteTime(Path); - } - catch (Exception) - { - Width = 0; - Height = 0; - Aspect = 0; - } + image = Image.FromStream(_fs.OpenFile(Path)); + Name = _fs.GetFileName(Path); + DateStamp = _fs.GetLastModified(Path).Date; + Width = image.Width; + Height = image.Height; + Aspect = (float)Width / Height; + RelativePath = relativePath; } - else + catch (Exception) { Width = 0; Height = 0; Aspect = 0; } + } public bool Exists @@ -66,10 +60,10 @@ namespace umbraco.editorControls.imagecropper get { return Width > 0 && Height > 0; } } - public string Directory - { - get { return Path.Substring(0, Path.LastIndexOf('\\')); } - } + //public string Directory + //{ + // get { return Path.Substring(0, Path.LastIndexOf('\\')); } + //} public void GenerateThumbnails(SaveData saveData, Config config) { @@ -77,18 +71,20 @@ namespace umbraco.editorControls.imagecropper { for (int i = 0; i < config.presets.Count; i++) { - Crop crop = (Crop) saveData.data[i]; - Preset preset = (Preset) config.presets[i]; + Crop crop = (Crop)saveData.data[i]; + Preset preset = (Preset)config.presets[i]; // Crop rectangle bigger than actual image - if(crop.X2 - crop.X > Width || crop.Y2 - crop.Y > Height) + if (crop.X2 - crop.X > Width || crop.Y2 - crop.Y > Height) { crop = preset.Fit(this); } + var tmpName = Name.Substring(0, Name.LastIndexOf('.')); + ImageTransform.Execute( Path, - String.Format("{0}_{1}", Name, preset.Name), + String.Format("{0}_{1}", tmpName, preset.Name), crop.X, crop.Y, crop.X2 - crop.X, diff --git a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs index 9835f7b2cb..0e54b4aca1 100644 --- a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs +++ b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs @@ -2,34 +2,38 @@ using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing; +using Umbraco.Core.IO; using System.IO; + namespace umbraco.editorControls.imagecropper { public class ImageTransform { + private static readonly MediaFileSystem _fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + public static void Execute(string sourceFile, string name, int cropX, int cropY, int cropWidth, int cropHeight, int sizeWidth, int sizeHeight, long quality) { - if (!File.Exists(sourceFile)) return; - string path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\')); + if (!_fs.FileExists(sourceFile)) return; + + + string path = string.Empty; + + //http or local filesystem + if (sourceFile.Contains("/")) + path = sourceFile.Substring(0, sourceFile.LastIndexOf('/')); + else + path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\')); // TODO: Make configurable and move to imageInfo //if(File.Exists(String.Format(@"{0}\{1}.jpg", path, name))) return; - byte[] buffer = null; + //Do we need this check as we are always working with images that are already in a folder?? + //DirectoryInfo di = new DirectoryInfo(path); + //if (!di.Exists) di.Create(); - using (FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) - { - buffer = new byte[fs.Length]; - fs.Read(buffer, 0, (int)fs.Length); - fs.Close(); - } - - Image image = Image.FromStream(new MemoryStream(buffer)); - - 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))) { @@ -57,7 +61,13 @@ namespace umbraco.editorControls.imagecropper EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; - img.Save(path, jpegCodec, encoderParams); + var fileStream = new MemoryStream(); + + 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) From 987644379e06def25e01ea70f496836ddf1928e0 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Tue, 18 Feb 2014 15:07:47 +0000 Subject: [PATCH 2/4] Fix U4-4119 - Export Fails for Generic Properties Adds null checking for PropertyGroupID on export so that properties with no group (tab) don't throw an exception. --- src/Umbraco.Core/Services/PackagingService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index 816865d807..1c2ae6678b 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -341,7 +341,9 @@ namespace Umbraco.Core.Services foreach (var propertyType in contentType.PropertyTypes) { var definition = _dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId); - var propertyGroup = contentType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value); + var propertyGroup = propertyType.PropertyGroupId == null + ? null + : contentType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value); var genericProperty = new XElement("GenericProperty", new XElement("Name", propertyType.Name), new XElement("Alias", propertyType.Alias), From 366697f4eb8cf242eba742831999d27aaf97b82c Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Tue, 18 Feb 2014 15:10:11 +0000 Subject: [PATCH 3/4] PackageService: Fixes Single import for DataTypes Importing a file with a single data type failed, because the DataType element is at the root when exported. --- src/Umbraco.Core/Services/PackagingService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index 1c2ae6678b..4979c941ac 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -733,7 +733,7 @@ namespace Umbraco.Core.Services var dataTypes = new Dictionary(); var dataTypeElements = name.Equals("DataTypes") ? (from doc in element.Elements("DataType") select doc).ToList() - : new List { element.Element("DataType") }; + : new List { element }; foreach (var dataTypeElement in dataTypeElements) { From ded82611a7c328c64ed7707e3b5c9c8802dba7a4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 19 Mar 2014 15:09:50 +1100 Subject: [PATCH 4/4] Finalizes: U4-4472 Image cropper should use IFileSystem - currently uses standard IO --- .../imagecropper/ImageInfo.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/umbraco.editorControls/imagecropper/ImageInfo.cs b/src/umbraco.editorControls/imagecropper/ImageInfo.cs index b02d63256c..b2fe94365e 100644 --- a/src/umbraco.editorControls/imagecropper/ImageInfo.cs +++ b/src/umbraco.editorControls/imagecropper/ImageInfo.cs @@ -28,14 +28,10 @@ namespace umbraco.editorControls.imagecropper try { - if (relativePath.Contains("//")) - { - Path = _fs.GetFullPath(relativePath); - } - else - { - Path = IOHelper.MapPath(relativePath); - } + RelativePath = relativePath; + + //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)); Name = _fs.GetFileName(Path); @@ -43,8 +39,7 @@ namespace umbraco.editorControls.imagecropper Width = image.Width; Height = image.Height; Aspect = (float)Width / Height; - - RelativePath = relativePath; + } catch (Exception) {