Implementing obsolete Media Profiles image cleanup in AzureBlobStorageProvider too

Same logic as 999ecd0786
This commit is contained in:
Lombiq
2019-02-20 18:23:50 +01:00
parent 056233c9c5
commit 3404e0fe1b

View File

@@ -1,10 +1,11 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Web;
using Orchard.Azure.Services.Environment.Configuration;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.Media;
using System;
namespace Orchard.Azure.Services.FileSystems.Media {
@@ -20,8 +21,7 @@ namespace Orchard.Azure.Services.FileSystems.Media {
platformConfigurationAccessor.GetSetting(Constants.MediaStorageContainerNameSettingName, shellSettings.Name, null) ?? Constants.MediaStorageDefaultContainerName,
platformConfigurationAccessor.GetSetting(Constants.MediaStorageRootFolderPathSettingName, shellSettings.Name, null) ?? shellSettings.Name,
mimeTypeProvider,
platformConfigurationAccessor.GetSetting(Constants.MediaStoragePublicHostName, shellSettings.Name, null))
{
platformConfigurationAccessor.GetSetting(Constants.MediaStoragePublicHostName, shellSettings.Name, null)) {
}
public AzureBlobStorageProvider(string storageConnectionString, string containerName, string rootFolderPath, IMimeTypeProvider mimeTypeProvider, string publicHostName)
@@ -67,7 +67,7 @@ namespace Orchard.Azure.Services.FileSystems.Media {
EnsureInitialized();
var rootUri = new Uri(_absoluteRoot);
var uri = new Uri(url);
if((uri.Host == rootUri.Host || (!string.IsNullOrEmpty(_publicHostName) && uri.Host == _publicHostName)) && uri.AbsolutePath.StartsWith(rootUri.AbsolutePath)) {
if ((uri.Host == rootUri.Host || (!string.IsNullOrEmpty(_publicHostName) && uri.Host == _publicHostName)) && uri.AbsolutePath.StartsWith(rootUri.AbsolutePath)) {
return HttpUtility.UrlDecode(uri.PathAndQuery.Substring(Combine(rootUri.AbsolutePath, "/").Length));
}
@@ -77,5 +77,45 @@ namespace Orchard.Azure.Services.FileSystems.Media {
public string GetRelativePath(string path) {
return GetPublicUrl(path);
}
public new void DeleteFile(string path) {
DeleteFileProfiles(path);
base.DeleteFile(path);
}
/// <summary>
/// Cleans up files generated by Media Profiles for a specific file.
/// </summary>
/// <param name="path">The full path and name of a file.</param>
private void DeleteFileProfiles(string path) {
if (FolderExists("_Profiles")) {
// The children of the "_Profiles" folder correspond to specific Media Profiles.
var profileFolderPaths = ListFolders("_Profiles").Select(folder => folder.GetPath());
if (profileFolderPaths.Any()) {
var filenameWithExtension = GetFile(path)?.GetName() ?? "";
var publicUrl = GetPublicUrl(path);
var originalFullPath = publicUrl.Substring(0, publicUrl.Length - filenameWithExtension.Length);
// This identifies the child folder of the specific Profile Media folder,
// containing the files generated from the same path.
var pathHash = originalFullPath.GetHashCode().ToString("x").ToLowerInvariant();
string fullPath, fullName;
// Checking each Media Profile's folder.
foreach (var profileFolderPath in profileFolderPaths) {
fullPath = Combine(profileFolderPath, pathHash);
fullName = Combine(fullPath, filenameWithExtension);
// Deleting the file generated for the current Media Profile.
if (FileExists(fullName)) base.DeleteFile(fullName);
// If the current path's Media Profile folder hasn't got any more generated files,
// then it can be deleted too.
if (!ListFiles(fullPath).Any()) DeleteFolder(fullPath);
}
}
}
}
}
}