protected override void ScopeExitCompleted() { // processing only the last instance of each event... // this is probably far from perfect, because if eg a content is saved in a list // and then as a single content, the two events will probably not be de-duplicated, // but it's better than nothing foreach (var e in GetEvents(EventDefinitionFilter.LastIn)) { e.RaiseEvent(); // separating concerns means that this should probably not be here, // but then where should it be (without making things too complicated)? var delete = e.Args as IDeletingMediaFilesEventArgs; if (delete != null && delete.MediaFilesToDelete.Count > 0) { _mediaFileManager.DeleteMediaFiles(delete.MediaFilesToDelete); } } }
public void Can_Delete_MediaFiles() { MediaFileManager mediaFileManager = GetRequiredService <MediaFileManager>(); var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("test")); string virtualPath = mediaFileManager.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid()); mediaFileManager.FileSystem.AddFile(virtualPath, memoryStream); // ~/media/1234/file.txt exists IHostingEnvironment hostingEnvironment = GetRequiredService <IHostingEnvironment>(); string physPath = hostingEnvironment.MapPathWebRoot(Path.Combine("media", virtualPath)); Assert.IsTrue(File.Exists(physPath)); // ~/media/1234/file.txt is gone mediaFileManager.DeleteMediaFiles(new[] { virtualPath }); Assert.IsFalse(File.Exists(physPath)); IMediaPathScheme scheme = GetRequiredService <IMediaPathScheme>(); if (scheme is UniqueMediaPathScheme) { // ~/media/1234 is *not* gone physPath = Path.GetDirectoryName(physPath); Assert.IsTrue(Directory.Exists(physPath)); } else { // ~/media/1234 is gone physPath = Path.GetDirectoryName(physPath); Assert.IsFalse(Directory.Exists(physPath)); } // ~/media exists physPath = Path.GetDirectoryName(physPath); Assert.IsTrue(Directory.Exists(physPath)); }
private void DeleteContainedFiles(IEnumerable <IContentBase> deletedEntities) { var filePathsToDelete = ContainedFilePaths(deletedEntities); _mediaFileManager.DeleteMediaFiles(filePathsToDelete); }