From 085f4a4fcff6d71af07666e744d54c83815aa190 Mon Sep 17 00:00:00 2001 From: "Matt@MBP13-PC" Date: Mon, 20 Aug 2012 11:42:18 -0100 Subject: [PATCH] Updated Document code to use IFileSystem when copying Added some helper extention methods for IFileSystem --- src/Umbraco.Core/IO/IFileSystemExtensions.cs | 4 +- .../IO/IMediaFileSystemExtensions.cs | 20 ++++++-- src/umbraco.cms/businesslogic/web/Document.cs | 47 +++++-------------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/Umbraco.Core/IO/IFileSystemExtensions.cs b/src/Umbraco.Core/IO/IFileSystemExtensions.cs index 548017a0a6..516049a146 100644 --- a/src/Umbraco.Core/IO/IFileSystemExtensions.cs +++ b/src/Umbraco.Core/IO/IFileSystemExtensions.cs @@ -16,9 +16,9 @@ namespace Umbraco.Core.IO return size; } - internal static void CopyFile(string path, string newPath) + internal static void CopyFile(this IFileSystem fs, string path, string newPath) { - + fs.AddFile(newPath, fs.OpenFile(path)); } } } diff --git a/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs b/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs index 4ff8393c66..5201923dbf 100644 --- a/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs +++ b/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs @@ -18,6 +18,16 @@ namespace Umbraco.Core.IO return propertyId.ToString() + seperator + fileName; } + internal static IEnumerable GetThumbnails(this IMediaFileSystem fs, string path) + { + var parentDirectory = System.IO.Path.GetDirectoryName(path); + var extension = System.IO.Path.GetExtension(path); + + return fs.GetFiles(parentDirectory) + .Where(x => x.StartsWith(path.TrimEnd(extension) + "_thumb")) + .ToList(); + } + internal static void DeleteFile(this IMediaFileSystem fs, string path, bool deleteThumbnails) { fs.DeleteFile(path); @@ -25,12 +35,12 @@ namespace Umbraco.Core.IO if(!deleteThumbnails) return; - var parentDirectory = System.IO.Path.GetDirectoryName(path); - var extension = System.IO.Path.GetExtension(path); + fs.DeleteThumbnails(path); + } - fs.GetFiles(parentDirectory) - .Where(x => x.StartsWith(path.TrimEnd(extension))) - .ToList() + internal static void DeleteThumbnails(this IMediaFileSystem fs, string path) + { + fs.GetThumbnails(path) .ForEach(fs.DeleteFile); } } diff --git a/src/umbraco.cms/businesslogic/web/Document.cs b/src/umbraco.cms/businesslogic/web/Document.cs index 79352d52cd..5dc4995e05 100644 --- a/src/umbraco.cms/businesslogic/web/Document.cs +++ b/src/umbraco.cms/businesslogic/web/Document.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; +using Umbraco.Core.IO; using umbraco.BusinessLogic; using umbraco.BusinessLogic.Actions; using umbraco.cms.businesslogic.property; @@ -15,6 +16,7 @@ using umbraco.interfaces; using umbraco.cms.businesslogic.datatype.controls; using System.IO; using System.Diagnostics; +using Umbraco.Core; namespace umbraco.cms.businesslogic.web { @@ -1283,6 +1285,8 @@ namespace umbraco.cms.businesslogic.web /// public Document Copy(int CopyTo, User u, bool RelateToOrignal) { + var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + CopyEventArgs e = new CopyEventArgs(); e.CopyTo = CopyTo; FireBeforeCopy(e); @@ -1312,49 +1316,24 @@ namespace umbraco.cms.businesslogic.web if (p.PropertyType.DataTypeDefinition.DataType.Id == uploadField.Id && p.Value.ToString() != "" - && File.Exists(IOHelper.MapPath(p.Value.ToString()))) + && fs.FileExists(fs.GetRelativePath(p.Value.ToString()))) { + var currentPath = fs.GetRelativePath(p.Value.ToString()); - int propId = newDoc.getProperty(p.PropertyType.Alias).Id; + var propId = newDoc.getProperty(p.PropertyType.Alias).Id; + var newPath = fs.GetRelativePath(propId, System.IO.Path.GetFileName(currentPath)); - System.IO.Directory.CreateDirectory(IOHelper.MapPath(SystemDirectories.Media + "/" + propId.ToString())); + fs.CopyFile(currentPath, newPath); - string fileCopy = IOHelper.MapPath( - SystemDirectories.Media + "/" + - propId.ToString() + "/" + - new FileInfo(IOHelper.MapPath(p.Value.ToString())).Name); - - File.Copy(IOHelper.MapPath(p.Value.ToString()), fileCopy); - - string relFilePath = - SystemDirectories.Media + "/" + - propId.ToString() + "/" + - new FileInfo(IOHelper.MapPath(p.Value.ToString())).Name; - - if (SystemDirectories.Root == string.Empty) - relFilePath = relFilePath.TrimStart('~'); - - newDoc.getProperty(p.PropertyType.Alias).Value = relFilePath; + newDoc.getProperty(p.PropertyType.Alias).Value = fs.GetUrl(newPath); //copy thumbs - FileInfo origFile = new FileInfo(IOHelper.MapPath(p.Value.ToString())); - - foreach (FileInfo thumb in origFile.Directory.GetFiles("*_thumb*")) + foreach (var thumbPath in fs.GetThumbnails(currentPath)) { - if (!File.Exists(IOHelper.MapPath( - SystemDirectories.Media + "/" + - propId.ToString() + "/" + - thumb.Name))) - { - thumb.CopyTo(IOHelper.MapPath( - SystemDirectories.Media + "/" + - propId.ToString() + "/" + - thumb.Name)); - } + var newThumbPath = fs.GetRelativePath(propId, System.IO.Path.GetFileName(thumbPath)); + fs.CopyFile(thumbPath, newThumbPath); } - - } else {