Removed AbstractFileSystem in favour of extension methods

Updated UmbracoFile to use IFileSystem
Updated UmbracoMediaFactories to use IFileSystem
This commit is contained in:
Matt@MBP13-PC
2012-08-20 10:46:32 -01:00
parent 7afbc8e7bf
commit 2775d9bedc
11 changed files with 118 additions and 138 deletions

View File

@@ -1,52 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Umbraco.Core.IO
{
public abstract class AbstractFileSystem : IFileSystem
{
public abstract IEnumerable<string> GetDirectories(string path);
public abstract void DeleteDirectory(string path);
public abstract void DeleteDirectory(string path, bool recursive);
public abstract bool DirectoryExists(string path);
public abstract void AddFile(string path, Stream stream);
public abstract void AddFile(string path, Stream stream, bool overrideIfExists);
public abstract IEnumerable<string> GetFiles(string path);
public abstract IEnumerable<string> GetFiles(string path, string filter);
public abstract Stream OpenFile(string path);
public abstract void DeleteFile(string path);
public abstract bool FileExists(string path);
public abstract string GetRelativePath(string fullPathOrUrl);
public abstract string GetFullPath(string path);
public abstract string GetUrl(string path);
public virtual long GetSize(string path)
{
var s = OpenFile(path);
var size = s.Length;
s.Close();
return size;
}
public abstract DateTimeOffset GetLastModified(string path);
public abstract DateTimeOffset GetCreated(string path);
}
}

View File

@@ -36,8 +36,6 @@ namespace Umbraco.Core.IO
string GetUrl(string path);
long GetSize(string path);
DateTimeOffset GetLastModified(string path);
DateTimeOffset GetCreated(string path);

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Umbraco.Core.IO
{
public static class IFileSystemExtensions
{
internal static long GetSize(this IFileSystem fs, string path)
{
var s = fs.OpenFile(path);
var size = s.Length;
s.Close();
return size;
}
internal static void CopyFile(string path, string newPath)
{
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using umbraco;
namespace Umbraco.Core.IO
{
public static class IMediaFileSystemExtensions
{
internal static string GetRelativePath(this IMediaFileSystem fs, int propertyId, string fileName)
{
var seperator = UmbracoSettings.UploadAllowDirectories
? Path.DirectorySeparatorChar
: '-';
return propertyId.ToString() + seperator + fileName;
}
internal static void DeleteFile(this IMediaFileSystem fs, string path, bool deleteThumbnails)
{
fs.DeleteFile(path);
if(!deleteThumbnails)
return;
var parentDirectory = System.IO.Path.GetDirectoryName(path);
var extension = System.IO.Path.GetExtension(path);
fs.GetFiles(parentDirectory)
.Where(x => x.StartsWith(path.TrimEnd(extension)))
.ToList()
.ForEach(fs.DeleteFile);
}
}
}

View File

@@ -7,7 +7,7 @@ using System.Web;
namespace Umbraco.Core.IO
{
internal class PhysicalFileSystem : AbstractFileSystem
internal class PhysicalFileSystem : IFileSystem
{
private readonly string _rootPath;
private readonly string _rootUrl;
@@ -30,7 +30,7 @@ namespace Umbraco.Core.IO
_rootUrl = rootUrl;
}
public override IEnumerable<string> GetDirectories(string path)
public IEnumerable<string> GetDirectories(string path)
{
path = EnsureTrailingSeparator(GetFullPath(path));
@@ -47,12 +47,12 @@ namespace Umbraco.Core.IO
return Enumerable.Empty<string>();
}
public override void DeleteDirectory(string path)
public void DeleteDirectory(string path)
{
DeleteDirectory(path, false);
}
public override void DeleteDirectory(string path, bool recursive)
public void DeleteDirectory(string path, bool recursive)
{
if (!DirectoryExists(path))
return;
@@ -65,17 +65,17 @@ namespace Umbraco.Core.IO
{ }
}
public override bool DirectoryExists(string path)
public bool DirectoryExists(string path)
{
return Directory.Exists(GetFullPath(path));
}
public override void AddFile(string path, Stream stream)
public void AddFile(string path, Stream stream)
{
AddFile(path, stream, true);
}
public override void AddFile(string path, Stream stream, bool overrideIfExists)
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",
@@ -87,12 +87,12 @@ namespace Umbraco.Core.IO
stream.CopyTo(destination);
}
public override IEnumerable<string> GetFiles(string path)
public IEnumerable<string> GetFiles(string path)
{
return GetFiles(path, "*.*");
}
public override IEnumerable<string> GetFiles(string path, string filter)
public IEnumerable<string> GetFiles(string path, string filter)
{
path = EnsureTrailingSeparator(GetFullPath(path));
@@ -109,12 +109,12 @@ namespace Umbraco.Core.IO
return Enumerable.Empty<string>();
}
public override Stream OpenFile(string path)
public Stream OpenFile(string path)
{
return File.OpenRead(GetFullPath(path));
}
public override void DeleteFile(string path)
public void DeleteFile(string path)
{
if (!FileExists(path))
return;
@@ -127,12 +127,12 @@ namespace Umbraco.Core.IO
{ }
}
public override bool FileExists(string path)
public bool FileExists(string path)
{
return File.Exists(GetFullPath(path));
}
public override string GetRelativePath(string fullPathOrUrl)
public string GetRelativePath(string fullPathOrUrl)
{
var relativePath = fullPathOrUrl
.TrimStart(_rootUrl)
@@ -143,28 +143,28 @@ namespace Umbraco.Core.IO
return relativePath;
}
public override string GetFullPath(string path)
public string GetFullPath(string path)
{
return !path.StartsWith(_rootPath)
? Path.Combine(_rootPath, path)
: path;
}
public override string GetUrl(string path)
public string GetUrl(string path)
{
return _rootUrl.TrimEnd("/") + "/" + path
.TrimStart(Path.DirectorySeparatorChar)
.Replace(Path.DirectorySeparatorChar, '/');
}
public override DateTimeOffset GetLastModified(string path)
public DateTimeOffset GetLastModified(string path)
{
return DirectoryExists(path)
? new DirectoryInfo(GetFullPath(path)).LastWriteTimeUtc
: new FileInfo(GetFullPath(path)).LastWriteTimeUtc;
}
public override DateTimeOffset GetCreated(string path)
public DateTimeOffset GetCreated(string path)
{
return DirectoryExists(path)
? Directory.GetCreationTimeUtc(GetFullPath(path))

View File

@@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="businesslogic">
<HintPath>..\Umbraco.Web\bin\businesslogic.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
@@ -52,12 +55,13 @@
<Compile Include="DynamicWrapper.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="IfExtensions.cs" />
<Compile Include="IO\AbstractFileSystem.cs" />
<Compile Include="IO\FileSystemProvider.cs" />
<Compile Include="IO\FileSystemProviderAttribute.cs" />
<Compile Include="IO\FileSystemProviderManager.cs" />
<Compile Include="IO\IFileSystem.cs" />
<Compile Include="IO\IFileSystemExtensions.cs" />
<Compile Include="IO\IMediaFileSystem.cs" />
<Compile Include="IO\IMediaFileSystemExtensions.cs" />
<Compile Include="IO\PhysicalFileSystem.cs" />
<Compile Include="IThumbnailProvider.cs" />
<Compile Include="ObjectExtensions.cs" />

View File

@@ -596,7 +596,6 @@ namespace umbraco.cms.businesslogic
{
var relativeFilePath = fs.GetRelativePath(p.Value.ToString());
var parentDirectory = System.IO.Path.GetDirectoryName(relativeFilePath);
var extension = System.IO.Path.GetExtension(relativeFilePath);
// don't want to delete the media folder if not using directories.
if (UmbracoSettings.UploadAllowDirectories && parentDirectory != fs.GetRelativePath("/"))
@@ -605,10 +604,7 @@ namespace umbraco.cms.businesslogic
}
else
{
fs.GetFiles(parentDirectory)
.Where(x => x.StartsWith(relativeFilePath.TrimEnd(extension)))
.ToList()
.ForEach(fs.DeleteFile);
fs.DeleteFile(relativeFilePath, true);
}
}
}

View File

@@ -68,14 +68,14 @@ namespace umbraco.cms.businesslogic.Files
public static UmbracoFile Save(HttpPostedFile file)
{
string tempDir = System.IO.Path.Combine(IO.SystemDirectories.Media, "uploads", Guid.NewGuid().ToString());
var tempDir = System.IO.Path.Combine("uploads", Guid.NewGuid().ToString());
return Save(file, tempDir);
}
//filebase overload...
public static UmbracoFile Save(HttpPostedFileBase file)
{
string tempDir = System.IO.Path.Combine(IO.SystemDirectories.Media, "uploads", Guid.NewGuid().ToString());
var tempDir = System.IO.Path.Combine("uploads", Guid.NewGuid().ToString());
return Save(file, tempDir);
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Web;
using umbraco.BusinessLogic;
using Umbraco.Core.IO;
namespace umbraco.cms.businesslogic.media
{
@@ -26,13 +27,11 @@ namespace umbraco.cms.businesslogic.media
var propertyId = media.getProperty("umbracoFile").Id;
// Get paths
var destFileName = ConstructDestFileName(propertyId, uploadedFile.FileName);
var destPath = ConstructDestPath(propertyId);
var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
var ext = VirtualPathUtility.GetExtension(destFileName).Substring(1);
var destFilePath = _fileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
var ext = Path.GetExtension(destFilePath).Substring(1);
var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
//var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
//var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
// Set media properties
media.getProperty("umbracoFile").Value = destFilePath;
@@ -44,15 +43,7 @@ namespace umbraco.cms.businesslogic.media
if (media.getProperty("umbracoExtensio") != null)
media.getProperty("umbracoExtensio").Value = ext;
// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(absoluteDestPath);
// Save file
uploadedFile.SaveAs(absoluteDestFilePath);
// Close stream
uploadedFile.InputStream.Close();
_fileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);
// Save media
media.Save();

View File

@@ -10,6 +10,7 @@ using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
using Encoder = System.Text.Encoder;
using Umbraco.Core.IO;
namespace umbraco.cms.businesslogic.media
{
@@ -36,13 +37,8 @@ namespace umbraco.cms.businesslogic.media
var propertyId = media.getProperty("umbracoFile").Id;
// Get paths
var destFileName = ConstructDestFileName(propertyId, postedFile.FileName);
var destPath = ConstructDestPath(propertyId);
var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
var ext = VirtualPathUtility.GetExtension(destFileName).Substring(1);
var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
var destFilePath = _fileSystem.GetRelativePath(propertyId, postedFile.FileName);
var ext = Path.GetExtension(destFilePath).Substring(1);
// Set media properties
media.getProperty("umbracoFile").Value = destFilePath;
@@ -56,12 +52,8 @@ namespace umbraco.cms.businesslogic.media
if (media.getProperty("umbracoExtensio") != null)
media.getProperty("umbracoExtensio").Value = ext;
// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(absoluteDestPath);
// Generate thumbnail
var thumbDestFilePath = Path.Combine(absoluteDestPath, Path.GetFileNameWithoutExtension(destFileName) + "_thumb");
var thumbDestFilePath = Path.Combine(Path.GetDirectoryName(destFilePath), Path.GetFileNameWithoutExtension(destFilePath) + "_thumb");
GenerateThumbnail(image, 100, fileWidth, fileHeight, thumbDestFilePath + ".jpg");
// Generate additional thumbnails based on PreValues set in DataTypeDefinition uploadField
@@ -69,17 +61,13 @@ namespace umbraco.cms.businesslogic.media
image.Dispose();
// Save file
postedFile.SaveAs(absoluteDestFilePath);
// Close stream
postedFile.InputStream.Close();
_fileSystem.AddFile(destFilePath, postedFile.InputStream, postedFile.ReplaceExisting);
// Save media
media.Save();
}
private static void GenerateAdditionalThumbnails(Image image, int fileWidth, int fileHeight, string destFilePath)
private void GenerateAdditionalThumbnails(Image image, int fileWidth, int fileHeight, string destFilePath)
{
var uploadFieldDataTypeId = new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c");
@@ -113,7 +101,7 @@ namespace umbraco.cms.businesslogic.media
}
}
private static void GenerateThumbnail(Image image, int maxWidthHeight, int fileWidth, int fileHeight, string thumbnailFileName)
private void GenerateThumbnail(Image image, int maxWidthHeight, int fileWidth, int fileHeight, string thumbnailFileName)
{
// Generate thumbnailee
var fx = (float)fileWidth / maxWidthHeight;
@@ -157,7 +145,13 @@ namespace umbraco.cms.businesslogic.media
// Save the new image
if (codec != null)
{
bp.Save(thumbnailFileName, codec, ep);
var ms = new MemoryStream();
bp.Save(ms, codec, ep);
ms.Seek(0, 0);
_fileSystem.AddFile(thumbnailFileName, ms);
ms.Close();
}
else
{

View File

@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using Umbraco.Core.IO;
using umbraco.BusinessLogic;
namespace umbraco.cms.businesslogic.media
@@ -14,6 +16,13 @@ namespace umbraco.cms.businesslogic.media
public virtual int Priority { get { return 1000; } }
public abstract string MediaTypeAlias { get; }
internal readonly IMediaFileSystem _fileSystem;
protected UmbracoMediaFactory()
{
_fileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<IMediaFileSystem>();
}
public virtual bool CanHandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
{
try
@@ -70,26 +79,6 @@ namespace umbraco.cms.businesslogic.media
#region Helper Methods
public string ConstructDestPath(int propertyId)
{
if (UmbracoSettings.UploadAllowDirectories)
{
var path = VirtualPathUtility.Combine(VirtualPathUtility.AppendTrailingSlash(IO.SystemDirectories.Media), propertyId.ToString());
return VirtualPathUtility.ToAbsolute(VirtualPathUtility.AppendTrailingSlash(path));
}
return VirtualPathUtility.ToAbsolute(VirtualPathUtility.AppendTrailingSlash(IO.SystemDirectories.Media));
}
public string ConstructDestFileName(int propertyId, string filename)
{
if (UmbracoSettings.UploadAllowDirectories)
return filename;
return propertyId + "-" + filename;
}
public bool TryFindExistingMedia(int parentNodeId, string fileName, out Media existingMedia)
{
var children = parentNodeId == -1 ? Media.GetRootMedias() : new Media(parentNodeId).Children;
@@ -100,11 +89,10 @@ namespace umbraco.cms.businesslogic.media
var prop = childMedia.getProperty("umbracoFile");
if (prop != null)
{
var destFileName = ConstructDestFileName(prop.Id, fileName);
var destPath = ConstructDestPath(prop.Id);
var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
var destFilePath = _fileSystem.GetRelativePath(prop.Id, fileName);
var destFileUrl = _fileSystem.GetUrl(destFilePath);
if (prop.Value.ToString() == destFilePath)
if (prop.Value.ToString() == destFileUrl)
{
existingMedia = childMedia;
return true;