Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -490,7 +491,9 @@ public async Task<ActionResult> SaveWeblinkDetailAsync([FromBody] WebLinkViewMod
[Route("SaveCaseDetail")]
public async Task<ActionResult> 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);
}

Expand Down Expand Up @@ -623,5 +626,103 @@ private async Task<bool> UserCanEditCatalogue(int catalogueId)
{
return await this.catalogueService.CanCurrentUserEditCatalogue(catalogueId);
}

private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewModel newResource)
{
try
{
var filePaths = new List<string>();
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)
{
}
}
}
}
14 changes: 11 additions & 3 deletions LearningHub.Nhs.WebUI/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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}");
}
Expand Down Expand Up @@ -130,6 +128,7 @@ public async Task DeleteChunkDirectory(string directoryRef, int chunks)
public async Task<ShareFileDownloadInfo> DownloadFileAsync(string filePath, string fileName)
{
var directory = this.ShareClient.GetDirectoryClient(filePath);
var sourceDirectory = this.InputArchiveShareClient.GetDirectoryClient(filePath);

if (await directory.ExistsAsync())
{
Expand All @@ -140,6 +139,15 @@ public async Task<ShareFileDownloadInfo> 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;
}
Expand Down
67 changes: 67 additions & 0 deletions WebAPI/LearningHub.Nhs.Services/ResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,73 @@ public async Task<List<string>> 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;
Expand Down