Files
Umbraco-CMS/src/Umbraco.Core/IO/MediaFileSystem.cs
T
Shannon 2b7a9389b4 Fixes: U4-3254 Property level validation not working when no field level validators are used
U4-2683 Ensure all uploaded files are cleaned up when they change for the FileUploadValueEditor
U4-3252 File uploader needs to adhere to DisallowedUploadFiles config
U4-2774 FileUploadValueEditor needs to remove old files
2013-10-28 15:34:48 +11:00

73 lines
1.9 KiB
C#

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Core.IO
{
/// <summary>
/// A custom file system provider for media
/// </summary>
[FileSystemProvider("media")]
public class MediaFileSystem : FileSystemWrapper
{
private readonly IContentSection _contentConfig;
public MediaFileSystem(IFileSystem wrapped)
: this(wrapped, UmbracoConfig.For.UmbracoSettings().Content)
{
}
public MediaFileSystem(IFileSystem wrapped, IContentSection contentConfig) : base(wrapped)
{
_contentConfig = contentConfig;
}
public string GetRelativePath(int propertyId, string fileName)
{
var seperator = _contentConfig.UploadAllowDirectories
? Path.DirectorySeparatorChar
: '-';
return propertyId.ToString(CultureInfo.InvariantCulture) + seperator + fileName;
}
public string GetRelativePath(string subfolder, string fileName)
{
var seperator = _contentConfig.UploadAllowDirectories
? Path.DirectorySeparatorChar
: '-';
return subfolder + seperator + fileName;
}
public IEnumerable<string> GetThumbnails(string path)
{
var parentDirectory = Path.GetDirectoryName(path);
var extension = Path.GetExtension(path);
return GetFiles(parentDirectory)
.Where(x => x.StartsWith(path.TrimEnd(extension) + "_thumb") || x.StartsWith(path.TrimEnd(extension) + "_big-thumb"))
.ToList();
}
public void DeleteFile(string path, bool deleteThumbnails)
{
DeleteFile(path);
if (deleteThumbnails == false)
return;
DeleteThumbnails(path);
}
public void DeleteThumbnails(string path)
{
GetThumbnails(path)
.ForEach(DeleteFile);
}
}
}