Fix to replace media when original file has been deleted from filesystem (#8555)

* Added statuses management instead of notifier when catching file not found exception

# Conflicts:
#	src/Orchard.Web/Modules/Orchard.MediaLibrary/Controllers/ClientStorageController.cs

* Visual studio automatic code formatting.
This commit is contained in:
Andrea Piovanelli
2022-04-29 15:33:23 +02:00
committed by GitHub
parent aed260544d
commit aff9b2542c

View File

@@ -178,9 +178,15 @@ namespace Orchard.MediaLibrary.Controllers {
.Where(x => x.FolderPath == replaceMedia.FolderPath && x.FileName == replaceMedia.FileName)
.Count();
if (mediaItemsUsingTheFile == 1) { // if the file is referenced only by the deleted media content, the file too can be removed.
_mediaLibraryService.DeleteFile(replaceMedia.FolderPath, replaceMedia.FileName);
}
else {
try {
_mediaLibraryService.DeleteFile(replaceMedia.FolderPath, replaceMedia.FileName);
} catch (ArgumentException) { // File not found by FileSystemStorageProvider is thrown as ArgumentException.
statuses.Add(new {
error = T("Error when deleting file to replace: file {0} does not exist in folder {1}. Media has been updated anyway.", replaceMedia.FileName, replaceMedia.FolderPath).Text,
progress = 1.0
});
}
} else {
// it changes the media file name
replaceMedia.FileName = filename;
}