Delete obsolete profile images (#8157)

Fixes ##8156

Delete profiles media's files when deleting media's files.
Delete empty profiles folder.
This commit is contained in:
LorenzoFrediani-Laser 2019-01-31 21:17:11 +01:00 committed by Sébastien Ros
parent 9321082d44
commit 999ecd0786

View File

@ -158,7 +158,7 @@ namespace Orchard.FileSystems.Media {
catch (Exception ex) {
if (ex.IsFatal()) {
throw;
}
}
throw new ArgumentException(T("The folder could not be created at path: {0}. {1}", path, ex).ToString());
}
}
@ -186,7 +186,7 @@ namespace Orchard.FileSystems.Media {
CreateFolder(path);
}
catch {
return false;
return false;
}
return true;
@ -240,17 +240,48 @@ namespace Orchard.FileSystems.Media {
}
/// <summary>
/// Deletes a file in the storage provider.
/// Deletes a file and profile files in the storage provider.
/// </summary>
/// <param name="path">The relative path to the file to be deleted.</param>
/// <exception cref="ArgumentException">If the file doesn't exist.</exception>
public void DeleteFile(string path) {
FileInfo fileInfo = new FileInfo(MapStorage(path));
if (!fileInfo.Exists) {
throw new ArgumentException(T("File {0} does not exist", path).ToString());
}
fileInfo.Delete();
lock (String.Intern(path)) {
var ListProfileFileInfo = ListProfiles(path);
foreach (var profileFileInfo in ListProfileFileInfo) {
if (profileFileInfo.Exists) {
profileFileInfo.Delete();
}
if (profileFileInfo.Directory.Exists && !(profileFileInfo.Directory.EnumerateFiles().Any() || profileFileInfo.Directory.EnumerateDirectories().Any())) {
profileFileInfo.Directory.Delete();
}
}
}
}
/// <summary>
/// Get the List of Profile's files in the storage provider.
/// </summary>
/// <param name="path">The relative path to the file to be deleted.</param>
/// <returns></returns>
private IEnumerable<FileInfo> ListProfiles(string path) {
var filenameWithExtension = Path.GetFileName(path) ?? "";
var urlpath = GetPublicUrl(path);
var fileLocation = urlpath.Substring(0, urlpath.Length - filenameWithExtension.Length);
var hashpath = fileLocation.GetHashCode().ToString("x").ToLowerInvariant();
DirectoryInfo directoryInfo = new DirectoryInfo(MapStorage("_Profiles"));
if (!directoryInfo.Exists) {
return null;
}
var fileinfos = directoryInfo.GetFiles(filenameWithExtension, SearchOption.AllDirectories);
return fileinfos.Where(x => x.Directory.Name.Equals(hashpath));
}
/// <summary>