From e2274b78929be4af557bbbfbf8ca5c148370597f Mon Sep 17 00:00:00 2001 From: "Matt@MBP13-PC" Date: Tue, 14 Aug 2012 09:11:49 -0100 Subject: [PATCH] Hooked up IFileSystem to the file uploader / IFile --- src/Umbraco.Core/IO/AbstractFileSystem.cs | 52 ++++++ src/Umbraco.Core/IO/IFileSystem.cs | 33 ++-- src/Umbraco.Core/IO/PhysicalFileSystem.cs | 55 +++--- src/Umbraco.Core/Properties/AssemblyInfo.cs | 1 + src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../IO/AbstractFileSystemTests.cs | 29 ++++ .../umbraco.presentation/default.aspx.cs | 2 + src/umbraco.cms/businesslogic/Files/IFile.cs | 3 + .../businesslogic/Files/UmbracoFile.cs | 163 +++++++++--------- .../businesslogic/datatype/FileHandlerData.cs | 4 +- src/umbraco.cms/umbraco.cms.csproj | 4 + .../umbraco.editorControls.csproj | 4 + .../uploadfield/uploadField.cs | 59 ++++--- 13 files changed, 258 insertions(+), 152 deletions(-) create mode 100644 src/Umbraco.Core/IO/AbstractFileSystem.cs diff --git a/src/Umbraco.Core/IO/AbstractFileSystem.cs b/src/Umbraco.Core/IO/AbstractFileSystem.cs new file mode 100644 index 0000000000..3e49742c03 --- /dev/null +++ b/src/Umbraco.Core/IO/AbstractFileSystem.cs @@ -0,0 +1,52 @@ +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 3a2c426754..0bb11527ea 100644 --- a/src/Umbraco.Core/IO/IFileSystem.cs +++ b/src/Umbraco.Core/IO/IFileSystem.cs @@ -6,32 +6,37 @@ namespace Umbraco.Core.IO { internal interface IFileSystem { + IEnumerable GetDirectories(string path); + void DeleteDirectory(string path); void DeleteDirectory(string path, bool recursive); - IEnumerable GetFiles(string path); - - IEnumerable GetFiles(string path, string filter); - - IEnumerable GetDirectories(string path); - - string GetFullPath(string path); - - string GetUrl(string path); - - void DeleteFile(string path); - - bool FileExists(string path); - bool DirectoryExists(string path); + void AddFile(string path, Stream stream); void AddFile(string path, Stream stream, bool overrideIfExists); + IEnumerable GetFiles(string path); + + IEnumerable GetFiles(string path, string filter); + Stream OpenFile(string path); + void DeleteFile(string path); + + bool FileExists(string path); + + string GetRelativePath(string fullPathOrUrl); + + string GetFullPath(string path); + + string GetUrl(string path); + + long GetSize(string path); + DateTimeOffset GetLastModified(string path); DateTimeOffset GetCreated(string path); diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index 30398c5e04..5b2cbd7db5 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 : IFileSystem + internal class PhysicalFileSystem : AbstractFileSystem { private readonly string _rootPath; private readonly string _rootUrl; @@ -33,14 +33,14 @@ namespace Umbraco.Core.IO _rootUrl = rootUrl; } - public IEnumerable GetDirectories(string path) + public override IEnumerable GetDirectories(string path) { path = EnsureTrailingSeparator(GetFullPath(path)); try { if (Directory.Exists(path)) - return Directory.EnumerateDirectories(path).Select(MakeRelativePath); + return Directory.EnumerateDirectories(path).Select(GetRelativePath); } catch (UnauthorizedAccessException ex) { } @@ -50,12 +50,12 @@ namespace Umbraco.Core.IO return Enumerable.Empty(); } - public virtual void DeleteDirectory(string path) + public override void DeleteDirectory(string path) { DeleteDirectory(path, false); } - public void DeleteDirectory(string path, bool recursive) + public override void DeleteDirectory(string path, bool recursive) { if (!DirectoryExists(path)) return; @@ -68,17 +68,17 @@ namespace Umbraco.Core.IO { } } - public bool DirectoryExists(string path) + public override bool DirectoryExists(string path) { return Directory.Exists(GetFullPath(path)); } - public void AddFile(string path, Stream stream) + public override void AddFile(string path, Stream stream) { AddFile(path, stream, true); } - public void AddFile(string path, Stream stream, bool overrideIfExists) + public override void AddFile(string path, Stream stream, bool overrideIfExists) { if (FileExists(path) && !overrideIfExists) throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", @@ -90,19 +90,19 @@ namespace Umbraco.Core.IO stream.CopyTo(destination); } - public IEnumerable GetFiles(string path) + public override IEnumerable GetFiles(string path) { return GetFiles(path, "*.*"); } - public IEnumerable GetFiles(string path, string filter) + public override IEnumerable GetFiles(string path, string filter) { path = EnsureTrailingSeparator(GetFullPath(path)); try { if (Directory.Exists(path)) - return Directory.EnumerateFiles(path, filter).Select(MakeRelativePath); + return Directory.EnumerateFiles(path, filter).Select(GetRelativePath); } catch (UnauthorizedAccessException ex) { } @@ -112,12 +112,12 @@ namespace Umbraco.Core.IO return Enumerable.Empty(); } - public Stream OpenFile(string path) + public override Stream OpenFile(string path) { return File.OpenRead(GetFullPath(path)); } - public void DeleteFile(string path) + public override void DeleteFile(string path) { if (!FileExists(path)) return; @@ -130,33 +130,44 @@ namespace Umbraco.Core.IO { } } - public bool FileExists(string path) + public override bool FileExists(string path) { return File.Exists(GetFullPath(path)); } - public string GetFullPath(string path) + public override string GetRelativePath(string fullPathOrUrl) + { + var relativePath = fullPathOrUrl + .TrimStart(_rootUrl) + .Replace('/', Path.DirectorySeparatorChar) + .TrimStart(_rootPath) + .TrimStart(Path.DirectorySeparatorChar); + + return relativePath; + } + + public override string GetFullPath(string path) { return !path.StartsWith(_rootPath) ? Path.Combine(_rootPath, path) : path; } - public string GetUrl(string path) + public override string GetUrl(string path) { return _rootUrl.TrimEnd("/") + "/" + path .TrimStart(Path.DirectorySeparatorChar) .Replace(Path.DirectorySeparatorChar, '/'); } - public DateTimeOffset GetLastModified(string path) + public override DateTimeOffset GetLastModified(string path) { return DirectoryExists(path) ? new DirectoryInfo(GetFullPath(path)).LastWriteTimeUtc : new FileInfo(GetFullPath(path)).LastWriteTimeUtc; } - public DateTimeOffset GetCreated(string path) + public override DateTimeOffset GetCreated(string path) { return DirectoryExists(path) ? Directory.GetCreationTimeUtc(GetFullPath(path)) @@ -165,14 +176,6 @@ namespace Umbraco.Core.IO #region Helper Methods - protected string MakeRelativePath(string fullPath) - { - return fullPath.Substring(_rootPath.Length).TrimStart(new char[1] - { - Path.DirectorySeparatorChar - }); - } - protected virtual void EnsureDirectory(string path) { path = GetFullPath(path); diff --git a/src/Umbraco.Core/Properties/AssemblyInfo.cs b/src/Umbraco.Core/Properties/AssemblyInfo.cs index fd937747fc..363d4e0f63 100644 --- a/src/Umbraco.Core/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Core/Properties/AssemblyInfo.cs @@ -21,4 +21,5 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("cms")] [assembly: InternalsVisibleTo("umbraco")] [assembly: InternalsVisibleTo("businesslogic")] +[assembly: InternalsVisibleTo("umbraco.editorControls")] [assembly: InternalsVisibleTo("Umbraco.Tests")] \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 8ae92f2038..d5621bf47f 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -51,6 +51,7 @@ + diff --git a/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs b/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs index debcd8492b..5379dcd059 100644 --- a/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs +++ b/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs @@ -136,6 +136,35 @@ namespace Umbraco.Tests.IO _fileSystem.DeleteFile("test.txt"); } + [Test] + public void Can_Convert_Full_Path_And_Url_To_Relative_Path() + { + _fileSystem.AddFile("test.txt", CreateStream()); + + var url = _fileSystem.GetUrl("test.txt"); + var fullPath = _fileSystem.GetFullPath("test.txt"); + + Assert.AreNotEqual("test.txt", url); + Assert.AreNotEqual("test.txt", fullPath); + + Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(url)); + Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(fullPath)); + + _fileSystem.DeleteFile("test.txt"); + } + + [Test] + public void Can_Get_Size() + { + var stream = CreateStream(); + var streamLength = stream.Length; + _fileSystem.AddFile("test.txt", stream); + + Assert.AreEqual(streamLength, _fileSystem.GetSize("test.txt")); + + _fileSystem.DeleteFile("test.txt"); + } + #region Helper Methods protected Stream CreateStream(string contents = null) diff --git a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs index b18e10c58a..f320b9ec02 100644 --- a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs @@ -4,6 +4,8 @@ using System.Web.UI; using System.IO; using System.Xml; using System.Text.RegularExpressions; +using Umbraco.Core.Configuration; +using Umbraco.Core.IO; using umbraco.presentation; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic; diff --git a/src/umbraco.cms/businesslogic/Files/IFile.cs b/src/umbraco.cms/businesslogic/Files/IFile.cs index 7bfb4490f5..ccf858fb02 100644 --- a/src/umbraco.cms/businesslogic/Files/IFile.cs +++ b/src/umbraco.cms/businesslogic/Files/IFile.cs @@ -9,7 +9,10 @@ namespace umbraco.cms.businesslogic.Files { string Filename { get; } string Extension { get; } + [Obsolete("LocalName is obsolete, please use URL instead", false)] string LocalName { get; } + string Path { get; } + string Url { get; } bool SupportsResizing { get; } string GetFriendlyName(); System.Tuple GetDimensions(); diff --git a/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs b/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs index ec1d7666b4..a8f65839a2 100644 --- a/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs +++ b/src/umbraco.cms/businesslogic/Files/UmbracoFile.cs @@ -6,109 +6,89 @@ using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; +using Umbraco.Core.IO; using Encoder = System.Text.Encoder; namespace umbraco.cms.businesslogic.Files { public class UmbracoFile : IFile { - private string _fullFilePath; + private string _path; private string _fileName; - private string _directoryName; private string _extension; - private string _localName; + private string _url; private long _length; + private IFileSystem _fs; + + #region Constructors + public UmbracoFile() { - + _fs = FileSystemProviderManager.Current.GetFileSystemProvider(FileSystemProvider.Media); } - public UmbracoFile(string fullFilePath) + public UmbracoFile(string path) { - _fullFilePath = fullFilePath; + _fs = FileSystemProviderManager.Current.GetFileSystemProvider(FileSystemProvider.Media); + + _path = path; + initialize(); } + + #endregion - public static UmbracoFile Save(HttpPostedFile file, string fullFileName) + #region Static Methods + + //MB: Do we really need all these overloads? looking through the code, only one of them is actually used + + public static UmbracoFile Save(HttpPostedFile file, string path) { - byte[] fileData = null; - using (var binaryReader = new BinaryReader(file.InputStream)) - { - fileData = binaryReader.ReadBytes(file.ContentLength); - } - - return Save(fileData, fullFileName); + return Save(file.InputStream, path); } - public static UmbracoFile Save(HttpPostedFileBase file, string fullFileName) + public static UmbracoFile Save(HttpPostedFileBase file, string path) { - byte[] fileData = null; - using (var binaryReader = new BinaryReader(file.InputStream)) - { - fileData = binaryReader.ReadBytes(file.ContentLength); - } - - return Save(fileData, fullFileName); + return Save(file.InputStream, path); } - public static UmbracoFile Save(Stream inputStream, string fullFileName){ - - byte[] fileData = null; - using (var binaryReader = new BinaryReader(inputStream)) - { - fileData = binaryReader.ReadBytes((int)inputStream.Length); - } + public static UmbracoFile Save(Stream inputStream, string path) + { + var fs = FileSystemProviderManager.Current.GetFileSystemProvider(FileSystemProvider.Media); + fs.AddFile(path, inputStream); - return Save(fileData, fullFileName); + return new UmbracoFile(path); } - public static UmbracoFile Save(byte[] file, string fullFileName) + public static UmbracoFile Save(byte[] file, string relativePath) { - string fullFilePath = IO.IOHelper.MapPath(fullFileName); - - // create directories - DirectoryInfo di = new DirectoryInfo(IO.IOHelper.MapPath(fullFilePath.Substring(0, fullFilePath.LastIndexOf(Path.DirectorySeparatorChar)))); - if (!di.Exists) - { - var currentDir = IO.IOHelper.MapPath(IO.SystemDirectories.Root); - var rootDir = IO.IOHelper.MapPath(IO.SystemDirectories.Root); - foreach (var dir in di.FullName.Substring(rootDir.Length).Split(Path.DirectorySeparatorChar)) - { - currentDir = Path.Combine(currentDir, dir); - if (!new DirectoryInfo(currentDir).Exists) - { - Directory.CreateDirectory(currentDir); - } - } - } - - File.WriteAllBytes(fullFilePath, file); - return new UmbracoFile(fullFilePath); + return Save(new MemoryStream(file), relativePath); } public static UmbracoFile Save(HttpPostedFile file) { - string tempDir = Path.Combine(IO.SystemDirectories.Media, "uploads", Guid.NewGuid().ToString()); - return Save(file, tempDir); - } - //filebase overload... - public static UmbracoFile Save(HttpPostedFileBase file) - { - string tempDir = Path.Combine(IO.SystemDirectories.Media, "uploads", Guid.NewGuid().ToString()); + string tempDir = System.IO.Path.Combine(IO.SystemDirectories.Media, "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()); + return Save(file, tempDir); + } + + #endregion + private void initialize() { - var fi = new FileInfo(_fullFilePath); - _fileName = fi.Name; - _length = fi.Length; - _directoryName = fi.DirectoryName; - _extension = fi.Extension.Substring(1).ToLowerInvariant(); - _localName = - "/" + fi.FullName.Substring(IO.IOHelper.MapPath(IO.SystemDirectories.Root).Length).Replace( - Path.DirectorySeparatorChar.ToString(), "/"); + _fileName = System.IO.Path.GetFileName(_path); + _length = _fs.GetSize(_path); + _extension = System.IO.Path.GetExtension(_path) != null + ? System.IO.Path.GetExtension(_path).Substring(1).ToLowerInvariant() + : ""; + _url = _fs.GetUrl(_path); } #region IFile Members @@ -124,9 +104,20 @@ namespace umbraco.cms.businesslogic.Files get { return _extension; } } + [Obsolete("LocalName is obsolete, please use Url instead", false)] public string LocalName { - get { return _localName; } + get { return Url; } + } + + public string Path + { + get { return _path; } + } + + public string Url + { + get { return _url; } } public long Length @@ -155,11 +146,8 @@ namespace umbraco.cms.businesslogic.Files { throwNotAnImageException(); - - FileStream fs = new FileStream(_fullFilePath, - FileMode.Open, FileAccess.Read, FileShare.Read); - - Image image = Image.FromStream(fs); + var fs = _fs.OpenFile(_path); + var image = Image.FromStream(fs); var fileWidth = image.Width; var fileHeight = image.Height; fs.Close(); @@ -172,42 +160,43 @@ namespace umbraco.cms.businesslogic.Files { throwNotAnImageException(); - string fileNameThumb = DoResize(width, height, 0, String.Empty); + var fileNameThumb = DoResize(width, height, 0, String.Empty); - return fileNameThumb.Substring(IO.IOHelper.MapPath(IO.SystemDirectories.Root).Length-1); + return _fs.GetUrl(fileNameThumb); } public string Resize(int maxWidthHeight, string fileNameAddition) { throwNotAnImageException(); - string fileNameThumb = DoResize(GetDimensions().Item1, GetDimensions().Item2, maxWidthHeight, fileNameAddition); + var fileNameThumb = DoResize(GetDimensions().Item1, GetDimensions().Item2, maxWidthHeight, fileNameAddition); - return fileNameThumb.Substring(IO.IOHelper.MapPath(IO.SystemDirectories.Root).Length); + return _fs.GetUrl(fileNameThumb); } private string DoResize(int width, int height, int maxWidthHeight, string fileNameAddition) { - - FileStream fs = new FileStream(_fullFilePath, - FileMode.Open, FileAccess.Read, FileShare.Read); - Image image = Image.FromStream(fs); + var fs = _fs.OpenFile(_path); + var image = Image.FromStream(fs); fs.Close(); string fileNameThumb = String.IsNullOrEmpty(fileNameAddition) ? - string.Format("{0}_UMBRACOSYSTHUMBNAIL.jpg", _fullFilePath.Substring(0, _fullFilePath.LastIndexOf("."))) : - string.Format("{0}_{1}.jpg", _fullFilePath.Substring(0, _fullFilePath.LastIndexOf(".")), fileNameAddition); + string.Format("{0}_UMBRACOSYSTHUMBNAIL.jpg", _path.Substring(0, _path.LastIndexOf("."))) : + string.Format("{0}_{1}.jpg", _path.Substring(0, _path.LastIndexOf(".")), fileNameAddition); + fileNameThumb = generateThumbnail( image, maxWidthHeight, width, height, - _fullFilePath, + _path, _extension, fileNameThumb, maxWidthHeight == 0 ).FileName; + image.Dispose(); + return fileNameThumb; } @@ -269,7 +258,13 @@ namespace umbraco.cms.businesslogic.Files // Save the new image using the dimensions of the image string newFileName = thumbnailFileName.Replace("UMBRACOSYSTHUMBNAIL", string.Format("{0}x{1}", widthTh, heightTh)); - bp.Save(newFileName, codec, ep); + var ms = new MemoryStream(); + bp.Save(ms, codec, ep); + ms.Seek(0, 0); + + _fs.AddFile(newFileName, ms); + + ms.Close(); bp.Dispose(); g.Dispose(); diff --git a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs index dfb21afdad..f399f9600b 100644 --- a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs +++ b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs @@ -58,7 +58,7 @@ namespace umbraco.cms.businesslogic.datatype ? Path.Combine(PropertyId.ToString(), name) : PropertyId + "-" + name; - fileName = Path.Combine(SystemDirectories.Media, fileName); + //fileName = Path.Combine(SystemDirectories.Media, fileName); um = UmbracoFile.Save(fileStream, fileName); if (um.SupportsResizing) @@ -111,7 +111,7 @@ namespace umbraco.cms.businesslogic.datatype } } - base.Value = um.LocalName; + base.Value = um.Url; } else { diff --git a/src/umbraco.cms/umbraco.cms.csproj b/src/umbraco.cms/umbraco.cms.csproj index 3ff2f6eac3..068836f738 100644 --- a/src/umbraco.cms/umbraco.cms.csproj +++ b/src/umbraco.cms/umbraco.cms.csproj @@ -147,6 +147,10 @@ {E469A9CE-1BEC-423F-AC44-713CD72457EA} {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + {31785BC3-256C-4613-B2F5-A1B0BDDED8C1} + Umbraco.Core + {C7CB79F0-1C97-4B33-BFA7-00731B579AE2} umbraco.datalayer diff --git a/src/umbraco.editorControls/umbraco.editorControls.csproj b/src/umbraco.editorControls/umbraco.editorControls.csproj index b4636781e3..8b1570ed2c 100644 --- a/src/umbraco.editorControls/umbraco.editorControls.csproj +++ b/src/umbraco.editorControls/umbraco.editorControls.csproj @@ -102,6 +102,10 @@ AllRules.ruleset + + {31785bc3-256c-4613-b2f5-a1b0bdded8c1} + Umbraco.Core + {651E1350-91B6-44B7-BD60-7207006D7003} Umbraco.Web diff --git a/src/umbraco.editorControls/uploadfield/uploadField.cs b/src/umbraco.editorControls/uploadfield/uploadField.cs index 4b57d3e78c..dbd0d6f46e 100644 --- a/src/umbraco.editorControls/uploadfield/uploadField.cs +++ b/src/umbraco.editorControls/uploadfield/uploadField.cs @@ -4,6 +4,7 @@ using System.Text.RegularExpressions; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; +using Umbraco.Core.IO; using umbraco.interfaces; using umbraco.IO; using Content = umbraco.cms.businesslogic.Content; @@ -18,8 +19,11 @@ namespace umbraco.editorControls private readonly String _thumbnails; private String _text; + private IFileSystem _fs; + public uploadField(IData Data, string ThumbnailSizes) { + _fs = FileSystemProviderManager.Current.GetFileSystemProvider(FileSystemProvider.Media); _data = (cms.businesslogic.datatype.DefaultData) Data; _thumbnails = ThumbnailSizes; } @@ -107,8 +111,6 @@ namespace umbraco.editorControls // we update additional properties post image upload if (_data.Value != DBNull.Value && !string.IsNullOrEmpty(_data.Value.ToString())) { - string fullFilePath = IOHelper.MapPath(_data.Value.ToString()); - Content content = Content.GetContentFromVersion(_data.Version); // update extension in UI @@ -177,27 +179,29 @@ namespace umbraco.editorControls Text = _data.Value.ToString(); } - private void deleteFile(string file) + private void deleteFile(string fileUrl) { - if (file.Length > 0) + if (fileUrl.Length > 0) { - // delete old file - if (File.Exists(IOHelper.MapPath(file))) - File.Delete(IOHelper.MapPath(file)); + var relativeFilePath = _fs.GetRelativePath(fileUrl); - string extension = (file.Substring(file.LastIndexOf(".") + 1, file.Length - file.LastIndexOf(".") - 1)); + // delete old file + if (_fs.FileExists(relativeFilePath)) + _fs.DeleteFile(relativeFilePath); + + string extension = (relativeFilePath.Substring(relativeFilePath.LastIndexOf(".") + 1, relativeFilePath.Length - relativeFilePath.LastIndexOf(".") - 1)); extension = extension.ToLower(); //check for thumbnails if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + extension + ",") > -1) { //delete thumbnails - string thumbnailfile = file.Replace("." + extension, "_thumb"); + string relativeThumbFilePath = relativeFilePath.Replace("." + extension, "_thumb"); try { - if (File.Exists(IOHelper.MapPath(thumbnailfile + _thumbnailext))) - File.Delete(IOHelper.MapPath(thumbnailfile + _thumbnailext)); + if (_fs.FileExists(relativeThumbFilePath + _thumbnailext)) + _fs.DeleteFile(relativeThumbFilePath + _thumbnailext); } catch { @@ -210,12 +214,12 @@ namespace umbraco.editorControls { if (thumb != "") { - string thumbnailextra = thumbnailfile + "_" + thumb + _thumbnailext; + string relativeExtraThumbFilePath = relativeThumbFilePath + "_" + thumb + _thumbnailext; try { - if (File.Exists(IOHelper.MapPath(thumbnailextra))) - File.Delete(IOHelper.MapPath(thumbnailextra)); + if (_fs.FileExists(relativeExtraThumbFilePath)) + _fs.DeleteFile(relativeExtraThumbFilePath); } catch { @@ -269,17 +273,18 @@ namespace umbraco.editorControls { if (!string.IsNullOrEmpty(Text)) { - string ext = _text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1); - string fileNameThumb = _text.Replace("." + ext, "_thumb.jpg"); - bool hasThumb = false; + var relativeFilePath = _fs.GetRelativePath(_text); + var ext = relativeFilePath.Substring(relativeFilePath.LastIndexOf(".") + 1, relativeFilePath.Length - relativeFilePath.LastIndexOf(".") - 1); + var relativeThumbFilePath = relativeFilePath.Replace("." + ext, "_thumb.jpg"); + var hasThumb = false; try { - hasThumb = File.Exists(IOHelper.MapPath(IOHelper.FindFile(fileNameThumb))); + hasThumb = _fs.FileExists(relativeThumbFilePath); // 4.8.0 added support for png thumbnails (but for legacy it might have been jpg - hence the check before) if (!hasThumb && (ext == "gif" || ext == "png")) { - fileNameThumb = _text.Replace("." + ext, "_thumb.png"); - hasThumb = File.Exists(IOHelper.MapPath(IOHelper.FindFile(fileNameThumb))); + relativeThumbFilePath = relativeFilePath.Replace("." + ext, "_thumb.png"); + hasThumb = _fs.FileExists(relativeThumbFilePath); } } catch @@ -287,17 +292,19 @@ namespace umbraco.editorControls } if (hasThumb) { - var thumb = new Image(); - thumb.ImageUrl = fileNameThumb; - thumb.BorderStyle = BorderStyle.None; + var thumb = new Image + { + ImageUrl = _fs.GetUrl(relativeThumbFilePath), + BorderStyle = BorderStyle.None + }; - output.WriteLine(""); + output.WriteLine(""); thumb.RenderControl(output); output.WriteLine("
"); } else - output.WriteLine("" + - IOHelper.FindFile(Text) + "
"); + output.WriteLine("" + + _fs.GetUrl(relativeFilePath) + "
"); output.WriteLine("