2015-12-08 12:53:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2013-09-13 18:11:20 +10:00
|
|
|
|
using System.Globalization;
|
2012-11-08 08:27:38 +05:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2013-09-13 18:11:20 +10:00
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2012-11-07 09:30:40 +05:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.IO
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A custom file system provider for media
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[FileSystemProvider("media")]
|
2013-01-15 01:19:51 +03:00
|
|
|
|
public class MediaFileSystem : FileSystemWrapper
|
2012-11-07 09:30:40 +05:00
|
|
|
|
{
|
2013-09-16 15:52:59 +10:00
|
|
|
|
private readonly IContentSection _contentConfig;
|
2013-09-13 18:11:20 +10:00
|
|
|
|
|
|
|
|
|
|
public MediaFileSystem(IFileSystem wrapped)
|
2013-09-25 19:23:41 +10:00
|
|
|
|
: this(wrapped, UmbracoConfig.For.UmbracoSettings().Content)
|
2012-11-07 09:30:40 +05:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2012-11-08 08:27:38 +05:00
|
|
|
|
|
2013-09-16 15:52:59 +10:00
|
|
|
|
public MediaFileSystem(IFileSystem wrapped, IContentSection contentConfig) : base(wrapped)
|
2013-09-13 18:11:20 +10:00
|
|
|
|
{
|
|
|
|
|
|
_contentConfig = contentConfig;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
[Obsolete("This low-level method should NOT exist.")]
|
|
|
|
|
|
public string GetRelativePath(int propertyId, string fileName)
|
2012-11-08 08:27:38 +05:00
|
|
|
|
{
|
2015-12-08 12:53:11 +01:00
|
|
|
|
var sep = _contentConfig.UploadAllowDirectories
|
2012-11-08 08:27:38 +05:00
|
|
|
|
? Path.DirectorySeparatorChar
|
|
|
|
|
|
: '-';
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
return propertyId.ToString(CultureInfo.InvariantCulture) + sep + fileName;
|
2012-11-08 08:27:38 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
[Obsolete("This low-level method should NOT exist.")]
|
2013-02-19 11:06:00 -01:00
|
|
|
|
public string GetRelativePath(string subfolder, string fileName)
|
|
|
|
|
|
{
|
2015-12-08 12:53:11 +01:00
|
|
|
|
var sep = _contentConfig.UploadAllowDirectories
|
2013-02-19 11:06:00 -01:00
|
|
|
|
? Path.DirectorySeparatorChar
|
|
|
|
|
|
: '-';
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
return subfolder + sep + fileName;
|
2013-02-19 11:06:00 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
// what's below is weird
|
|
|
|
|
|
// we are not deleting custom thumbnails
|
|
|
|
|
|
// MediaFileSystem is not just IFileSystem
|
|
|
|
|
|
// etc
|
|
|
|
|
|
|
2012-11-08 08:27:38 +05:00
|
|
|
|
public IEnumerable<string> GetThumbnails(string path)
|
|
|
|
|
|
{
|
2013-10-28 15:34:48 +11:00
|
|
|
|
var parentDirectory = Path.GetDirectoryName(path);
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
2012-11-08 08:27:38 +05:00
|
|
|
|
|
|
|
|
|
|
return GetFiles(parentDirectory)
|
2013-10-28 15:34:48 +11:00
|
|
|
|
.Where(x => x.StartsWith(path.TrimEnd(extension) + "_thumb") || x.StartsWith(path.TrimEnd(extension) + "_big-thumb"))
|
2012-11-08 08:27:38 +05:00
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteFile(string path, bool deleteThumbnails)
|
|
|
|
|
|
{
|
|
|
|
|
|
DeleteFile(path);
|
|
|
|
|
|
|
2013-10-28 15:34:48 +11:00
|
|
|
|
if (deleteThumbnails == false)
|
2012-11-08 08:27:38 +05:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
DeleteThumbnails(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteThumbnails(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
GetThumbnails(path)
|
|
|
|
|
|
.ForEach(DeleteFile);
|
|
|
|
|
|
}
|
2015-12-08 12:53:11 +01:00
|
|
|
|
|
|
|
|
|
|
public void CopyThumbnails(string sourcePath, string targetPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
var targetPathBase = Path.GetDirectoryName(targetPath) ?? "";
|
|
|
|
|
|
foreach (var sourceThumbPath in GetThumbnails(sourcePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
var sourceThumbFilename = Path.GetFileName(sourceThumbPath) ?? "";
|
|
|
|
|
|
var targetThumbPath = Path.Combine(targetPathBase, sourceThumbFilename);
|
|
|
|
|
|
this.CopyFile(sourceThumbPath, targetThumbPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-07 09:30:40 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|