Post-merge fixing & refactoring (tests ok)

This commit is contained in:
Stephan
2016-11-03 10:39:21 +01:00
parent b26b415096
commit b29bc66026
26 changed files with 203 additions and 224 deletions

View File

@@ -37,20 +37,6 @@ namespace Umbraco.Core.IO
throw new ArgumentException("Retries must be greater than zero");
}
// GetSize has been added to IFileSystem2 but not IFileSystem
// this is implementing GetSize for IFileSystem, the old way
public static long GetSize(this IFileSystem fs, string path)
{
// if we reach this point, fs is *not* IFileSystem2
// so it's not FileSystemWrapper nor shadow nor anything we know
// so... fall back to the old & inefficient method
using (var file = fs.OpenFile(path))
{
return file.Length;
}
}
public static void CopyFile(this IFileSystem fs, string path, string newPath)
{
using (var stream = fs.OpenFile(path))

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Core.IO
///
/// This abstract class just wraps the 'real' IFileSystem object passed in to its constructor.
/// </remarks>
public abstract class FileSystemWrapper : IFileSystem2
public abstract class FileSystemWrapper : IFileSystem
{
protected FileSystemWrapper(IFileSystem wrapped)
{
@@ -104,10 +104,9 @@ namespace Umbraco.Core.IO
}
// explicitely implementing - not breaking
long IFileSystem2.GetSize(string path)
long IFileSystem.GetSize(string path)
{
var wrapped2 = Wrapped as IFileSystem2;
return wrapped2 == null ? Wrapped.GetSize(path) : wrapped2.GetSize(path);
return Wrapped.GetSize(path);
}
}
}

View File

@@ -27,7 +27,7 @@ namespace Umbraco.Core.IO
#region Singleton & Constructor
internal FileSystems(ILogger logger)
public FileSystems(ILogger logger)
{
_config = (FileSystemProvidersSection) ConfigurationManager.GetSection("umbracoConfiguration/FileSystemProviders");
_logger = logger;
@@ -59,13 +59,13 @@ namespace Umbraco.Core.IO
#region Well-Known FileSystems
public IFileSystem2 MacroPartialsFileSystem { get; }
public IFileSystem2 PartialViewsFileSystem { get; }
public IFileSystem2 StylesheetsFileSystem { get; }
public IFileSystem2 ScriptsFileSystem { get; }
public IFileSystem2 XsltFileSystem { get; }
public IFileSystem2 MasterPagesFileSystem { get; }
public IFileSystem2 MvcViewsFileSystem { get; }
public IFileSystem MacroPartialsFileSystem { get; }
public IFileSystem PartialViewsFileSystem { get; }
public IFileSystem StylesheetsFileSystem { get; }
public IFileSystem ScriptsFileSystem { get; }
public IFileSystem XsltFileSystem { get; }
public IFileSystem MasterPagesFileSystem { get; }
public IFileSystem MvcViewsFileSystem { get; }
public MediaFileSystem MediaFileSystem { get; }
#endregion

View File

@@ -4,9 +4,6 @@ using System.IO;
namespace Umbraco.Core.IO
{
//TODO: There is no way to create a directory here without creating a file in a directory and then deleting it
//TODO: Should probably implement a rename?
public interface IFileSystem
{
IEnumerable<string> GetDirectories(string path);
@@ -40,11 +37,14 @@ namespace Umbraco.Core.IO
DateTimeOffset GetLastModified(string path);
DateTimeOffset GetCreated(string path);
}
// this should be part of IFileSystem but we don't want to change the interface
public interface IFileSystem2 : IFileSystem
{
long GetSize(string path);
// TODO: implement these
//
//void CreateDirectory(string path);
//
//// move or rename, directory or file
//void Move(string source, string target);
}
}

View File

@@ -8,10 +8,12 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LightInject;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.DI;
using Umbraco.Core.Exceptions;
using Umbraco.Core.Logging;
using Umbraco.Core.Media;
using Umbraco.Core.Media.Exif;
using Umbraco.Core.Models;
@@ -25,12 +27,7 @@ namespace Umbraco.Core.IO
[FileSystemProvider("media")]
public class MediaFileSystem : FileSystemWrapper
{
private readonly IContentSection _contentConfig;
private readonly ImageHelper _imageHelper;
private readonly UploadAutoFillProperties _uploadAutoFillProperties;
private readonly IDataTypeService _dataTypeService;
private readonly object _folderCounterLock = new object();
private readonly object _folderCounterLock = new object();
private long _folderCounter;
private bool _folderCounterInitialized;
@@ -40,49 +37,31 @@ namespace Umbraco.Core.IO
{ 500, "big-thumb" }
};
// FIXME
// explain the two ctors
// fix the whole mediaHelper, imageHelper, autofill props mess
// also
// what shannon says: this is an "enhanced filesystem" so it's OK to use it with extra stuff!
public MediaFileSystem(IFileSystem2 wrapped)
: this(wrapped, UmbracoConfig.For.UmbracoSettings().Content)
{ }
public MediaFileSystem(IFileSystem2 wrapped, IContentSection contentConfig) : base(wrapped)
public MediaFileSystem(IFileSystem wrapped)
: base(wrapped)
{
_contentConfig = contentConfig;
_imageHelper = new ImageHelper(contentConfig); // FIXME inject or KILL
// due to how FileSystems is written at the moment, the ctor cannot be used to inject
// dependencies, so we have to rely on property injection for anything we might need
Current.Container.InjectProperties(this);
UploadAutoFillProperties = new UploadAutoFillProperties(this, Logger, ContentConfig);
}
// note - this is currently experimental / being developed
[Inject]
internal IContentSection ContentConfig { get; set; }
[Inject]
internal ILogger Logger { get; set; }
[Inject]
internal IDataTypeService DataTypeService { get; set; }
internal UploadAutoFillProperties UploadAutoFillProperties { get; }
// note - this is currently experimental / being developed
//public static bool UseTheNewMediaPathScheme { get; set; }
public const bool UseTheNewMediaPathScheme = false;
// none of the methods below are used in Core anymore
// fixme FIX THEM WTF WTF WTF?!
[Obsolete("This low-level method should NOT exist.")]
public string GetRelativePath(int propertyId, string fileName)
{
var sep = _contentConfig.UploadAllowDirectories
? Path.DirectorySeparatorChar
: '-';
return propertyId.ToString(CultureInfo.InvariantCulture) + sep + fileName;
}
[Obsolete("This low-level method should NOT exist.", false)]
public string GetRelativePath(string subfolder, string fileName)
{
var sep = _contentConfig.UploadAllowDirectories
? Path.DirectorySeparatorChar
: '-';
return subfolder + sep + fileName;
}
/// <summary>
/// Deletes all files passed in.
/// </summary>
@@ -109,7 +88,7 @@ namespace Umbraco.Core.IO
var parentDirectory = Path.GetDirectoryName(relativeFilePath);
// don't want to delete the media folder if not using directories.
if (_contentConfig.UploadAllowDirectories && parentDirectory != rootRelativePath)
if (ContentConfig.UploadAllowDirectories && parentDirectory != rootRelativePath)
{
//issue U4-771: if there is a parent directory the recursive parameter should be true
DeleteDirectory(parentDirectory, string.IsNullOrEmpty(parentDirectory) == false);
@@ -242,7 +221,7 @@ namespace Umbraco.Core.IO
_folderCounter = folderNumber;
}
// note: not multi-domains ie LB safe as another domain could create directories
// note: not multi-domains ie LB safe as another domain could create directories
// while we read and parse them - don't fix, move to new scheme eventually
_folderCounterInitialized = true;
@@ -352,7 +331,7 @@ namespace Umbraco.Core.IO
var svalue = property.Value as string;
var oldpath = svalue == null ? null : GetRelativePath(svalue); // FIXME DELETE?
if (string.IsNullOrWhiteSpace(oldpath) == false && oldpath != filepath)
DeleteFile(oldpath);
DeleteFile(oldpath, true);
property.Value = GetUrl(filepath);
using (var filestream = OpenFile(filepath))
{
@@ -366,7 +345,7 @@ namespace Umbraco.Core.IO
{
// check if file is an image (and supports resizing and thumbnails etc)
var extension = Path.GetExtension(filepath);
var isImage = _imageHelper.IsImageFile(extension);
var isImage = IsImageFile(extension);
// specific stuff for images (thumbnails etc)
if (isImage)
@@ -375,13 +354,13 @@ namespace Umbraco.Core.IO
{
// use one image for all
GenerateThumbnails(image, filepath, property.PropertyType);
_uploadAutoFillProperties.Populate(content, property.Alias, filepath, filestream, image);
UploadAutoFillProperties.Populate(content, property.Alias, filepath, filestream, image);
}
}
else
{
// will use filepath for extension, and filestream for length
_uploadAutoFillProperties.Populate(content, property.Alias, filepath, filestream);
UploadAutoFillProperties.Populate(content, property.Alias, filepath, filestream);
}
}
@@ -398,7 +377,7 @@ namespace Umbraco.Core.IO
{
if (extension == null) return false;
extension = extension.TrimStart('.');
return _contentConfig.ImageFileTypes.InvariantContains(extension);
return ContentConfig.ImageFileTypes.InvariantContains(extension);
}
/// <summary>
@@ -442,7 +421,7 @@ namespace Umbraco.Core.IO
return new Size(fileWidth, fileHeight);
}
}
#endregion
#region Manage thumbnails
@@ -461,7 +440,7 @@ namespace Umbraco.Core.IO
public void DeleteFile(string path, bool deleteThumbnails)
{
DeleteFile(path);
base.DeleteFile(path);
if (deleteThumbnails == false)
return;
@@ -472,7 +451,7 @@ namespace Umbraco.Core.IO
public void DeleteThumbnails(string path)
{
GetThumbnails(path)
.ForEach(DeleteFile);
.ForEach(x => base.DeleteFile(x));
}
public void CopyThumbnails(string sourcePath, string targetPath)
@@ -543,7 +522,7 @@ namespace Umbraco.Core.IO
// that can be defined in the prevalue for the property data type. otherwise,
// just use the default sizes.
var sizes = propertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias
? _dataTypeService
? DataTypeService
.GetPreValuesByDataTypeId(propertyType.DataTypeDefinitionId)
.FirstOrDefault()
: string.Empty;

View File

@@ -8,7 +8,7 @@ using Umbraco.Core.Logging;
namespace Umbraco.Core.IO
{
public class PhysicalFileSystem : IFileSystem2
public class PhysicalFileSystem : IFileSystem
{
// the rooted, filesystem path, using directory separator chars, NOT ending with a separator
// eg "c:" or "c:\path\to\site" or "\\server\path"

View File

@@ -5,21 +5,18 @@ using System.Linq;
namespace Umbraco.Core.IO
{
public class ShadowFileSystem : IFileSystem2
public class ShadowFileSystem : IFileSystem
{
private readonly IFileSystem _fs;
private readonly IFileSystem2 _sfs;
private readonly IFileSystem _sfs;
public ShadowFileSystem(IFileSystem fs, IFileSystem2 sfs)
public ShadowFileSystem(IFileSystem fs, IFileSystem sfs)
{
_fs = fs;
_sfs = sfs;
}
public IFileSystem Inner
{
get { return _fs; }
}
public IFileSystem Inner => _fs;
public void Complete()
{
@@ -65,7 +62,7 @@ namespace Umbraco.Core.IO
private Dictionary<string, ShadowNode> _nodes;
private Dictionary<string, ShadowNode> Nodes { get { return _nodes ?? (_nodes = new Dictionary<string, ShadowNode>()); } }
private Dictionary<string, ShadowNode> Nodes => _nodes ?? (_nodes = new Dictionary<string, ShadowNode>());
private class ShadowNode
{
@@ -75,11 +72,11 @@ namespace Umbraco.Core.IO
IsDir = isdir;
}
public bool IsDelete { get; private set; }
public bool IsDir { get; private set; }
public bool IsDelete { get; }
public bool IsDir { get; }
public bool IsExist { get { return IsDelete == false; } }
public bool IsFile { get { return IsDir == false; } }
public bool IsExist => IsDelete == false;
public bool IsFile => IsDir == false;
}
private static string NormPath(string path)
@@ -273,12 +270,8 @@ namespace Umbraco.Core.IO
{
ShadowNode sf;
if (Nodes.TryGetValue(NormPath(path), out sf) == false)
{
// the inner filesystem (_fs) can be IFileSystem2... or just IFileSystem
// figure it out and use the most effective GetSize method
var fs2 = _fs as IFileSystem2;
return fs2 == null ? _fs.GetSize(path) : fs2.GetSize(path);
}
return _fs.GetSize(path);
if (sf.IsDelete || sf.IsDir) throw new InvalidOperationException("Invalid path.");
return _sfs.GetSize(path);
}

View File

@@ -35,10 +35,10 @@ namespace Umbraco.Core.IO
private ShadowFileSystemsScope(Guid id, ShadowWrapper[] wrappers, ILogger logger)
{
_logger = logger;
_logger.Debug<ShadowFileSystemsScope>("Shadow " + id + ".");
_id = id;
_wrappers = wrappers;
_logger = logger;
foreach (var wrapper in _wrappers)
wrapper.Shadow(id);
}

View File

@@ -2,11 +2,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
namespace Umbraco.Core.IO
{
public class ShadowWrapper : IFileSystem2
public class ShadowWrapper : IFileSystem
{
private readonly IFileSystem _innerFileSystem;
private readonly string _shadowPath;
@@ -148,12 +147,7 @@ namespace Umbraco.Core.IO
public long GetSize(string path)
{
var filesystem = FileSystem; // will be either a ShadowFileSystem OR the actual underlying IFileSystem
// and the underlying filesystem can be IFileSystem2... or just IFileSystem
// figure it out and use the most effective GetSize method
var filesystem2 = filesystem as IFileSystem2;
return filesystem2 == null ? filesystem.GetSize(path) : filesystem2.GetSize(path);
return FileSystem.GetSize(path);
}
}
}