Updated Document code to use IFileSystem when copying

Added some helper extention methods for IFileSystem
This commit is contained in:
Matt@MBP13-PC
2012-08-20 11:42:18 -01:00
parent 3e2510cbda
commit 085f4a4fcf
3 changed files with 30 additions and 41 deletions

View File

@@ -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));
}
}
}

View File

@@ -18,6 +18,16 @@ namespace Umbraco.Core.IO
return propertyId.ToString() + seperator + fileName;
}
internal static IEnumerable<string> 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);
}
}

View File

@@ -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
/// <param name="RelateToOrignal"></param>
public Document Copy(int CopyTo, User u, bool RelateToOrignal)
{
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<IMediaFileSystem>();
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
{