diff --git a/src/Umbraco.Core/IO/AbstractFileSystem.cs b/src/Umbraco.Core/IO/AbstractFileSystem.cs deleted file mode 100644 index 3e49742c03..0000000000 --- a/src/Umbraco.Core/IO/AbstractFileSystem.cs +++ /dev/null @@ -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 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 GetFiles(string path); - - public abstract IEnumerable 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); - } -} diff --git a/src/Umbraco.Core/IO/IFileSystem.cs b/src/Umbraco.Core/IO/IFileSystem.cs index a2621965ce..2c11d290d0 100644 --- a/src/Umbraco.Core/IO/IFileSystem.cs +++ b/src/Umbraco.Core/IO/IFileSystem.cs @@ -36,8 +36,6 @@ namespace Umbraco.Core.IO string GetUrl(string path); - long GetSize(string path); - DateTimeOffset GetLastModified(string path); DateTimeOffset GetCreated(string path); diff --git a/src/Umbraco.Core/IO/IFileSystemExtensions.cs b/src/Umbraco.Core/IO/IFileSystemExtensions.cs new file mode 100644 index 0000000000..548017a0a6 --- /dev/null +++ b/src/Umbraco.Core/IO/IFileSystemExtensions.cs @@ -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) + { + + } + } +} diff --git a/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs b/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs new file mode 100644 index 0000000000..4ff8393c66 --- /dev/null +++ b/src/Umbraco.Core/IO/IMediaFileSystemExtensions.cs @@ -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); + } + } +} diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index de21e94622..3ea721dfdc 100644 --- a/src/Umbraco.Core/IO/PhysicalFileSystem.cs +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -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 GetDirectories(string path) + public IEnumerable GetDirectories(string path) { path = EnsureTrailingSeparator(GetFullPath(path)); @@ -47,12 +47,12 @@ namespace Umbraco.Core.IO return Enumerable.Empty(); } - 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 GetFiles(string path) + public IEnumerable GetFiles(string path) { return GetFiles(path, "*.*"); } - public override IEnumerable GetFiles(string path, string filter) + public IEnumerable GetFiles(string path, string filter) { path = EnsureTrailingSeparator(GetFullPath(path)); @@ -109,12 +109,12 @@ namespace Umbraco.Core.IO return Enumerable.Empty(); } - 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)) diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 28c28b1b40..5b76e00d58 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -31,6 +31,9 @@ 4 + + ..\Umbraco.Web\bin\businesslogic.dll + @@ -52,12 +55,13 @@ - + + diff --git a/src/umbraco.cms/businesslogic/Content.cs b/src/umbraco.cms/businesslogic/Content.cs index 00a17ea0b0..f404a06772 100644 --- a/src/umbraco.cms/businesslogic/Content.cs +++ b/src/umbraco.cms/businesslogic/Content.cs @@ -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); } } } diff --git a/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs b/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs index 70500793f4..b42f55dcb8 100644 --- a/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs +++ b/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs @@ -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); } diff --git a/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs index ba1005d32c..fa3c5ed6ae 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs @@ -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(); diff --git a/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs index d14aebc2b5..ed75956ef4 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs @@ -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 { diff --git a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs index bf916336fc..8b745653a8 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs @@ -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(); + } + 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;