From adac5a8c2a661f25646359e5bfa6b02082f269ea Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Wed, 8 Jun 2022 19:27:59 +0430 Subject: [PATCH] docs: #331 some refactoring and preparing to implement this feature: append a folder of images to an existing artifact --- .../ArtifactService-ImportServerFolder.cs | 189 +++++++++--------- 1 file changed, 98 insertions(+), 91 deletions(-) diff --git a/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-ImportServerFolder.cs b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-ImportServerFolder.cs index ab28ac8e..b5abcdac 100644 --- a/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-ImportServerFolder.cs +++ b/RMuseum/Services/Implementation/ArtifactService-Partials/ArtifactService-ImportServerFolder.cs @@ -115,97 +115,7 @@ namespace RMuseum.Services.Implementation await importJobUpdaterDb.SaveChangesAsync(); } - List pages = new List(); - string[] fileNames = Directory.GetFiles(job.ResourceNumber, "*.jpg"); - int order = 0; - foreach (string fileName in fileNames) - { - using (RMuseumDbContext importJobUpdaterDb = new RMuseumDbContext(new DbContextOptions())) - { - job.ProgressPercent = order * 100 / (decimal)fileNames.Length; - importJobUpdaterDb.Update(job); - await importJobUpdaterDb.SaveChangesAsync(); - } - - order++; - - RArtifactItemRecord page = new RArtifactItemRecord() - { - Name = $"تصویر {order}", - NameInEnglish = $"Image {order} of {book.NameInEnglish}", - Description = "", - DescriptionInEnglish = "", - Order = order, - FriendlyUrl = $"p{$"{order}".PadLeft(4, '0')}", - LastModified = DateTime.Now - }; - - - page.Tags = new RTagValue[] { }; - - if ( - File.Exists - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "orig"), $"{order}".PadLeft(4, '0') + ".jpg") - ) - - ) - { - File.Delete - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "orig"), $"{order}".PadLeft(4, '0') + ".jpg") - ); - } - if ( - - File.Exists - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "norm"), $"{order}".PadLeft(4, '0') + ".jpg") - ) - - ) - { - File.Delete - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "norm"), $"{order}".PadLeft(4, '0') + ".jpg") - ); - } - if ( - - File.Exists - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "thumb"), $"{order}".PadLeft(4, '0') + ".jpg") - ) - ) - { - File.Delete - ( - Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, friendlyUrl), "thumb"), $"{order}".PadLeft(4, '0') + ".jpg") - ); - } - using (FileStream imageStream = new FileStream(fileName, FileMode.Open)) - { - RServiceResult picture = await _pictureFileService.Add(page.Name, page.Description, 1, null, job.SrcUrl, imageStream, $"{order}".PadLeft(4, '0') + ".jpg", friendlyUrl); - if (picture.Result == null) - { - throw new Exception($"_pictureFileService.Add : {picture.ExceptionString}"); - } - - page.Images = new RPictureFile[] { picture.Result }; - - page.CoverImageIndex = 0; - - if (book.CoverItemIndex == (order - 1)) - { - book.CoverImage = RPictureFile.Duplicate(picture.Result); - } - - } - - - pages.Add(page); - } - + List pages = await _ImportAndReturnServerFolderJobImages(book, job, 0); book.Tags = meta.ToArray(); @@ -261,5 +171,102 @@ namespace RMuseum.Services.Implementation return new RServiceResult(false, exp.ToString()); } } + + + private async Task> _ImportAndReturnServerFolderJobImages(RArtifactMasterRecord book, ImportJob job, int order) + { + List pages = new List(); + string[] fileNames = Directory.GetFiles(job.ResourceNumber, "*.jpg"); + ; + foreach (string fileName in fileNames) + { + using (RMuseumDbContext importJobUpdaterDb = new RMuseumDbContext(new DbContextOptions())) + { + job.ProgressPercent = order * 100 / (decimal)fileNames.Length; + importJobUpdaterDb.Update(job); + await importJobUpdaterDb.SaveChangesAsync(); + } + + order++; + + RArtifactItemRecord page = new RArtifactItemRecord() + { + Name = $"تصویر {order}", + NameInEnglish = $"Image {order} of {book.NameInEnglish}", + Description = "", + DescriptionInEnglish = "", + Order = order, + FriendlyUrl = $"p{$"{order}".PadLeft(4, '0')}", + LastModified = DateTime.Now + }; + + + page.Tags = new RTagValue[] { }; + + if ( + File.Exists + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "orig"), $"{order}".PadLeft(4, '0') + ".jpg") + ) + + ) + { + File.Delete + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "orig"), $"{order}".PadLeft(4, '0') + ".jpg") + ); + } + if ( + + File.Exists + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "norm"), $"{order}".PadLeft(4, '0') + ".jpg") + ) + + ) + { + File.Delete + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "norm"), $"{order}".PadLeft(4, '0') + ".jpg") + ); + } + if ( + + File.Exists + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "thumb"), $"{order}".PadLeft(4, '0') + ".jpg") + ) + ) + { + File.Delete + ( + Path.Combine(Path.Combine(Path.Combine(_pictureFileService.ImageStoragePath, book.FriendlyUrl), "thumb"), $"{order}".PadLeft(4, '0') + ".jpg") + ); + } + using (FileStream imageStream = new FileStream(fileName, FileMode.Open)) + { + RServiceResult picture = await _pictureFileService.Add(page.Name, page.Description, 1, null, job.SrcUrl, imageStream, $"{order}".PadLeft(4, '0') + ".jpg", book.FriendlyUrl); + if (picture.Result == null) + { + throw new Exception($"_pictureFileService.Add : {picture.ExceptionString}"); + } + + page.Images = new RPictureFile[] { picture.Result }; + + page.CoverImageIndex = 0; + + if (book.CoverItemIndex == (order - 1)) + { + book.CoverImage = RPictureFile.Duplicate(picture.Result); + } + + } + + + pages.Add(page); + } + + return pages; + } } }