diff --git a/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs b/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs index ba8e6b975..a1716767a 100644 --- a/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs @@ -9,6 +9,7 @@ using System.Web; using LearningHub.Nhs.Models.Enums; using LearningHub.Nhs.Models.Resource; + using LearningHub.Nhs.Models.Resource.Blocks; using LearningHub.Nhs.Models.Resource.Contribute; using LearningHub.Nhs.Models.Resource.Files; using LearningHub.Nhs.WebUI.Interfaces; @@ -490,7 +491,9 @@ public async Task SaveWeblinkDetailAsync([FromBody] WebLinkViewMod [Route("SaveCaseDetail")] public async Task SaveCaseDetailAsync([FromBody] CaseViewModel request) { + var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId); int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request); + this.RemoveDeletedCaseFiles(existingResourceState?.CaseDetails, request); return this.Ok(resourceVersionId); } @@ -623,5 +626,103 @@ private async Task UserCanEditCatalogue(int catalogueId) { return await this.catalogueService.CanCurrentUserEditCatalogue(catalogueId); } + + private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewModel newResource) + { + try + { + var filePaths = new List(); + if (existingResource != null && existingResource.BlockCollection != null) + { + var allBlocks = existingResource.BlockCollection.Blocks.ToList(); + var newBlocks = newResource.BlockCollection.Blocks.ToList(); + if (allBlocks.Any()) + { + var existingAttachements = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null).ToList(); + if (existingAttachements.Any()) + { + foreach (var oldblock in existingAttachements) + { + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId); + if (entry == null) + { + filePaths.Add(oldblock.MediaBlock.Attachment?.File?.FilePath); + } + } + } + + var existingVideos = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null).ToList(); + if (existingVideos.Any()) + { + foreach (var oldblock in existingVideos) + { + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId); + if (entry == null) + { + filePaths.Add(oldblock.MediaBlock.Video?.VideoFile?.File?.FilePath); + if (oldblock.MediaBlock?.Video?.VideoFile?.TranscriptFile?.File?.FilePath != null) + { + filePaths.Add(oldblock.MediaBlock.Video?.VideoFile?.TranscriptFile?.File?.FilePath); + } + } + } + } + + var existingImages = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null).ToList(); + if (existingImages.Any()) + { + foreach (var oldblock in existingImages) + { + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId); + if (entry == null) + { + filePaths.Add(oldblock?.MediaBlock?.Image?.File?.FilePath); + } + } + } + + var existingImageCarousel = allBlocks.Where(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null).ToList(); + if (existingImageCarousel.Any()) + { + foreach (var imageBlock in existingImageCarousel) + { + foreach (var oldblock in imageBlock?.ImageCarouselBlock?.ImageBlockCollection?.Blocks) + { + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId).Any()); + if (entry == null) + { + filePaths.Add(oldblock.MediaBlock?.Image?.File?.FilePath); + } + } + } + } + + var existingWholeSlideImages = allBlocks.Where(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Any()).ToList(); + if (existingWholeSlideImages.Any()) + { + foreach (var wsi in existingWholeSlideImages) + { + foreach (var oldblock in wsi?.WholeSlideImageBlock?.WholeSlideImageBlockItems) + { + var entry = newBlocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId).Any()); + if (entry == null) + { + filePaths.Add(oldblock.WholeSlideImage?.File?.FilePath); + } + } + } + } + } + } + + if (filePaths != null && filePaths.Any()) + { + _ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, filePaths); }); + } + } + catch (Exception ex) + { + } + } } } diff --git a/LearningHub.Nhs.WebUI/Services/FileService.cs b/LearningHub.Nhs.WebUI/Services/FileService.cs index d9a95f440..a2120a74d 100644 --- a/LearningHub.Nhs.WebUI/Services/FileService.cs +++ b/LearningHub.Nhs.WebUI/Services/FileService.cs @@ -7,11 +7,9 @@ using System.Threading.Tasks; using Azure.Storage.Files.Shares; using Azure.Storage.Files.Shares.Models; - using LearningHub.Nhs.Models.Enums; using LearningHub.Nhs.Models.Resource; using LearningHub.Nhs.WebUI.Configuration; using LearningHub.Nhs.WebUI.Interfaces; - using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Options; @@ -89,7 +87,7 @@ private ShareClient InputArchiveShareClient this.archiveShareClient = new ShareClient(this.settings.AzureSourceArchiveStorageConnectionString, this.settings.AzureFileStorageResourceShareName, options); - if (!this.shareClient.Exists()) + if (!this.archiveShareClient.Exists()) { throw new Exception($"Unable to access azure file storage resource {this.settings.AzureFileStorageResourceShareName}"); } @@ -130,6 +128,7 @@ public async Task DeleteChunkDirectory(string directoryRef, int chunks) public async Task DownloadFileAsync(string filePath, string fileName) { var directory = this.ShareClient.GetDirectoryClient(filePath); + var sourceDirectory = this.InputArchiveShareClient.GetDirectoryClient(filePath); if (await directory.ExistsAsync()) { @@ -140,6 +139,15 @@ public async Task DownloadFileAsync(string filePath, stri return await file.DownloadAsync(); } } + else if (await sourceDirectory.ExistsAsync()) + { + var file = sourceDirectory.GetFileClient(fileName); + + if (await file.ExistsAsync()) + { + return await file.DownloadAsync(); + } + } return null; } diff --git a/WebAPI/LearningHub.Nhs.Services/ResourceService.cs b/WebAPI/LearningHub.Nhs.Services/ResourceService.cs index f7d855edc..9ff98ef4a 100644 --- a/WebAPI/LearningHub.Nhs.Services/ResourceService.cs +++ b/WebAPI/LearningHub.Nhs.Services/ResourceService.cs @@ -1471,6 +1471,73 @@ public async Task> GetObsoleteResourceFile(int resourceVersionId, b } } } + else if (resource.ResourceTypeEnum == ResourceTypeEnum.Case) + { + if (deletedResource) + { + if (resource != null) + { + var allBlocks = resource.CaseDetails.BlockCollection.Blocks.ToList(); + if (allBlocks.Any()) + { + var existingAttachements = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null).ToList(); + if (existingAttachements.Any()) + { + foreach (var oldblock in existingAttachements) + { + retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath); + } + } + + var existingVideos = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null).ToList(); + if (existingVideos.Any()) + { + foreach (var oldblock in existingVideos) + { + retVal.Add(oldblock.MediaBlock.Video?.VideoFile?.File?.FilePath); + if (oldblock.MediaBlock?.Video?.VideoFile.TranscriptFile?.File.FilePath != null) + { + retVal.Add(oldblock.MediaBlock.Video?.VideoFile?.TranscriptFile?.File?.FilePath); + } + } + } + + var existingImages = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null).ToList(); + if (existingImages.Any()) + { + foreach (var oldblock in existingImages) + { + retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath); + } + } + + var existingImageCarousel = allBlocks.Where(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null).ToList(); + if (existingImageCarousel.Any()) + { + foreach (var imageBlock in existingImageCarousel) + { + foreach (var oldblock in imageBlock.ImageCarouselBlock.ImageBlockCollection.Blocks) + { + retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath); + } + } + } + + var existingWholeSlideImages = allBlocks.Where(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Any()).ToList(); + if (existingWholeSlideImages.Any()) + { + foreach (var wsi in existingWholeSlideImages) + { + foreach (var oldblock in wsi.WholeSlideImageBlock.WholeSlideImageBlockItems) + { + retVal.Add(oldblock.WholeSlideImage?.File?.FilePath); + } + } + } + } + } + } + } } return retVal;