diff --git a/RMuseum/Controllers/ArtifactController.cs b/RMuseum/Controllers/ArtifactController.cs index 5c1dbadd..9c8b0cb9 100644 --- a/RMuseum/Controllers/ArtifactController.cs +++ b/RMuseum/Controllers/ArtifactController.cs @@ -1974,6 +1974,32 @@ namespace RMuseum.Controllers return BadRequest(exp.ToString()); } } + + /// + /// start removing original images + /// + /// + + [HttpDelete("originalimages/remove")] + [Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Unauthorized)] + public IActionResult StartRemovingOriginalImages() + { + try + { + var res = _artifactService.StartRemovingOriginalImages(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(); + } + catch (Exception exp) + { + return BadRequest(exp.ToString()); + } + } + /// /// readonly mode /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 3e0c94a4..6a76bf01 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -596,6 +596,12 @@ + + + start removing original images + + + readonly mode @@ -9377,6 +9383,12 @@ + + + start removing original images + + + donation service @@ -10404,6 +10416,9 @@ IArtifactService implementation + + IArtifactService implementation + @@ -10511,6 +10526,12 @@ golestan-walters-01 + + + start removing original images + + + from http://pudl.princeton.edu/ diff --git a/RMuseum/Services/IArtifactService.cs b/RMuseum/Services/IArtifactService.cs index 004f475d..a636cc37 100644 --- a/RMuseum/Services/IArtifactService.cs +++ b/RMuseum/Services/IArtifactService.cs @@ -557,5 +557,11 @@ namespace RMuseum.Services /// Task> AcceptPublicNoteReport(Guid id); + /// + /// start removing original images + /// + /// + RServiceResult StartRemovingOriginalImages(); + } } diff --git a/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-RemoveOriginalImages.cs b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-RemoveOriginalImages.cs new file mode 100644 index 00000000..b99294cc --- /dev/null +++ b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-RemoveOriginalImages.cs @@ -0,0 +1,86 @@ +using Microsoft.EntityFrameworkCore; +using RMuseum.DbContext; +using RSecurityBackend.Models.Generic; +using RSecurityBackend.Services.Implementation; +using System; +using System.Linq; +using System.IO; + +namespace RMuseum.Services.Implementation +{ + /// + /// IArtifactService implementation + /// + public partial class ArtifactService : IArtifactService + { + /// + /// start removing original images + /// + /// + public RServiceResult StartRemovingOriginalImages() + { + try + { + _backgroundTaskQueue.QueueBackgroundWorkItem + ( + async token => + { + using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so _context might be already been freed/collected by GC + { + LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context); + var job = (await jobProgressServiceEF.NewJob("RemovingOriginalImages", "Removing")).Result; + + try + { + var srcPath = Configuration.GetSection("PictureFileService")["StoragePath"]; + var trashPath = Configuration.GetSection("PictureFileService")["TrashStoragePath"]; + + var images = await context.PictureFiles.Where(p => p.StoredFileName != null && p.SrcUrl != null).ToListAsync(); + + int progress = 0; + + for (int i = 0; i < images.Count; i++) + { + var image = images[i]; + string targetDir = Path.Combine(trashPath, image.FolderName); + if (!Directory.Exists(targetDir)) + { + Directory.CreateDirectory(targetDir); + Directory.CreateDirectory(Path.Combine(targetDir, "orig")); + } + string srcFileName = Path.Combine(Path.Combine(srcPath, image.FolderName), image.StoredFileName); + if(File.Exists(srcFileName)) + { + string targetFileName = Path.Combine(Path.Combine(trashPath, image.FolderName), image.StoredFileName); + File.Move(srcFileName, targetFileName, true); + image.StoredFileName = null; + context.Update(image); + + if( (i * 100 / images.Count) > progress) + { + progress = i * 100 / images.Count; + await jobProgressServiceEF.UpdateJob(job.Id, progress); + } + } + } + + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + } + } + ); + + + return new RServiceResult(true); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + } +} \ No newline at end of file diff --git a/RMuseum/appsettings.json b/RMuseum/appsettings.json index 5ce70c68..07c9aa59 100644 --- a/RMuseum/appsettings.json +++ b/RMuseum/appsettings.json @@ -15,7 +15,8 @@ "NormalImageMaxHeight": "1350", "ThumbnailImageWidth": "300", "ThumbnailMaxHeight": "500", - "ImageInfoCacheEnabled": "False" + "ImageInfoCacheEnabled": "False", + "TrashStoragePath": "C:\\museum-trash" }, "Logging": { "LogLevel": {