diff --git a/RMuseum/Models/PDFLibrary/ViewModels/NewPDFBookViewModel.cs b/RMuseum/Models/PDFLibrary/ViewModels/NewPDFBookViewModel.cs index 2d45407a..6996fadf 100644 --- a/RMuseum/Models/PDFLibrary/ViewModels/NewPDFBookViewModel.cs +++ b/RMuseum/Models/PDFLibrary/ViewModels/NewPDFBookViewModel.cs @@ -1,7 +1,7 @@ namespace RMuseum.Models.PDFLibrary.ViewModels { /// - /// new PDFBook view model + /// New PDFBook view model /// public class NewPDFBookViewModel { diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index b166feed..8a4e1526 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -12105,7 +12105,7 @@ - new PDFBook view model + New PDFBook view model diff --git a/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs b/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs index d3a16395..716357ac 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs @@ -22,152 +22,158 @@ namespace RMuseum.Services.Implementation { private async Task> ImportLocalPDFFileAsync(RMuseumDbContext context, int bookId, int? volumeId, int volumeOrder, string filePath, string srcUrl, bool skipUpload) { - if (volumeId == 0) volumeId = null; - string fileChecksum = PoemAudio.ComputeCheckSum(filePath); - if ( - ( - await context.ImportJobs - .Where(j => j.JobType == JobType.Pdf && j.SrcContent == fileChecksum && !(j.Status == ImportJobStatus.Failed || j.Status == ImportJobStatus.Aborted)) - .SingleOrDefaultAsync() - ) - != - null - ) - { - return new RServiceResult(null, $"Job is already scheduled or running for importing pdf file {filePath} (duplicated checksum: {fileChecksum})"); - } - if (!string.IsNullOrEmpty(srcUrl)) + try { + if (volumeId == 0) volumeId = null; + string fileChecksum = PoemAudio.ComputeCheckSum(filePath); if ( - (await context.PDFBooks.Where(a => a.OriginalSourceUrl == srcUrl).SingleOrDefaultAsync()) + ( + await context.ImportJobs + .Where(j => j.JobType == JobType.Pdf && j.SrcContent == fileChecksum && !(j.Status == ImportJobStatus.Failed || j.Status == ImportJobStatus.Aborted)) + .SingleOrDefaultAsync() + ) != null ) { - return new RServiceResult(null, $"duplicated srcUrl '{srcUrl}'"); + return new RServiceResult(null, $"Job is already scheduled or running for importing pdf file {filePath} (duplicated checksum: {fileChecksum})"); } - } - if ( - (await context.PDFBooks.Where(a => a.FileMD5CheckSum == fileChecksum).SingleOrDefaultAsync()) - != - null - ) - { - return new RServiceResult(null, $"duplicated pdf with checksum '{fileChecksum}'"); - } - ImportJob job = new ImportJob() - { - JobType = JobType.Pdf, - SrcContent = fileChecksum, - ResourceNumber = filePath, - FriendlyUrl = "", - SrcUrl = srcUrl, - QueueTime = DateTime.Now, - ProgressPercent = 0, - Status = ImportJobStatus.NotStarted - }; - await context.ImportJobs.AddAsync - ( - job - ); - await context.SaveChangesAsync(); - try - { - var folderNumber = await context.PDFBooks.CountAsync(); - while (Directory.Exists(Path.Combine(_imageFileService.ImageStoragePath, $"pdfbook-{folderNumber}"))) + if (!string.IsNullOrEmpty(srcUrl)) { - folderNumber++; + if ( + (await context.PDFBooks.Where(a => a.OriginalSourceUrl == srcUrl).SingleOrDefaultAsync()) + != + null + ) + { + return new RServiceResult(null, $"duplicated srcUrl '{srcUrl}'"); + } } - Directory.CreateDirectory(Path.Combine(_imageFileService.ImageStoragePath, $"pdfbook-{folderNumber}")); - PDFBook pdfBook = new PDFBook() + if ( + (await context.PDFBooks.Where(a => a.FileMD5CheckSum == fileChecksum).SingleOrDefaultAsync()) + != + null + ) { - Status = PublishStatus.Draft, - DateTime = DateTime.Now, - LastModified = DateTime.Now, - FileMD5CheckSum = fileChecksum, - OriginalSourceUrl = srcUrl, - OriginalFileName = Path.GetFileName(filePath), - StorageFolderName = $"pdfbook-{folderNumber}", - BookId = bookId, - VolumeOrder = volumeOrder, - MultiVolumePDFCollectionId = volumeId, + return new RServiceResult(null, $"duplicated pdf with checksum '{fileChecksum}'"); + } + ImportJob job = new ImportJob() + { + JobType = JobType.Pdf, + SrcContent = fileChecksum, + ResourceNumber = filePath, + FriendlyUrl = "", + SrcUrl = srcUrl, + QueueTime = DateTime.Now, + ProgressPercent = 0, + Status = ImportJobStatus.NotStarted }; - job.FriendlyUrl = pdfBook.StorageFolderName; - List meta = new List(); - using (RMuseumDbContext importJobUpdaterDb = new RMuseumDbContext(new DbContextOptions())) + await context.ImportJobs.AddAsync + ( + job + ); + await context.SaveChangesAsync(); + try { - job.StartTime = DateTime.Now; - job.Status = ImportJobStatus.Running; - job.SrcContent = ""; - importJobUpdaterDb.Update(job); - await importJobUpdaterDb.SaveChangesAsync(); - } - List pages = await _ImportAndReturnPDFJobImages(pdfBook, job, 0); - pdfBook.Tags = meta.ToArray(); - pdfBook.Pages = pages.ToArray(); - pdfBook.PageCount = pages.Count; - if (pages.Count == 0) - { - job.EndTime = DateTime.Now; - job.Status = ImportJobStatus.Failed; - job.Exception = "Pages.Count == 0"; - context.Update(job); - await context.SaveChangesAsync(); - return new RServiceResult(null, job.Exception); - } - string pdfFilePath = Path.Combine(Path.Combine(_imageFileService.ImageStoragePath, pdfBook.StorageFolderName), pdfBook.OriginalFileName); - using (FileStream fs = new FileStream(pdfFilePath, FileMode.Open)) - { - var pdfStorageResult = await _imageFileService.Add(null, fs, pdfBook.OriginalFileName, pdfBook.StorageFolderName, false); - if (!string.IsNullOrEmpty(pdfStorageResult.ExceptionString)) + var folderNumber = 1 + await context.PDFBooks.CountAsync(); + while (Directory.Exists(Path.Combine(_imageFileService.ImageStoragePath, $"pdfbook-{folderNumber}"))) + { + folderNumber++; + } + Directory.CreateDirectory(Path.Combine(_imageFileService.ImageStoragePath, $"pdfbook-{folderNumber}")); + PDFBook pdfBook = new PDFBook() + { + Status = PublishStatus.Draft, + DateTime = DateTime.Now, + LastModified = DateTime.Now, + FileMD5CheckSum = fileChecksum, + OriginalSourceUrl = srcUrl, + OriginalFileName = Path.GetFileName(filePath), + StorageFolderName = $"pdfbook-{folderNumber}", + BookId = bookId, + VolumeOrder = volumeOrder, + MultiVolumePDFCollectionId = volumeId, + }; + job.FriendlyUrl = pdfBook.StorageFolderName; + List meta = new List(); + using (RMuseumDbContext importJobUpdaterDb = new RMuseumDbContext(new DbContextOptions())) + { + job.StartTime = DateTime.Now; + job.Status = ImportJobStatus.Running; + job.SrcContent = ""; + importJobUpdaterDb.Update(job); + await importJobUpdaterDb.SaveChangesAsync(); + } + List pages = await _ImportAndReturnPDFJobImages(pdfBook, job, 0); + pdfBook.Tags = meta.ToArray(); + pdfBook.Pages = pages.ToArray(); + pdfBook.PageCount = pages.Count; + if (pages.Count == 0) { job.EndTime = DateTime.Now; job.Status = ImportJobStatus.Failed; - job.Exception = $"pdfStorageResult.ExceptionString: {pdfStorageResult.ExceptionString}"; + job.Exception = "Pages.Count == 0"; + context.Update(job); + await context.SaveChangesAsync(); + return new RServiceResult(null, job.Exception); + } + using (FileStream fs = new FileStream(filePath, FileMode.Open)) + { + var pdfStorageResult = await _imageFileService.Add(null, fs, pdfBook.OriginalFileName, pdfBook.StorageFolderName, false); + if (!string.IsNullOrEmpty(pdfStorageResult.ExceptionString)) + { + job.EndTime = DateTime.Now; + job.Status = ImportJobStatus.Failed; + job.Exception = $"pdfStorageResult.ExceptionString: {pdfStorageResult.ExceptionString}"; + job.EndTime = DateTime.Now; + context.Update(job); + await context.SaveChangesAsync(); + return new RServiceResult(null, job.Exception); + } + pdfStorageResult.Result.ContentType = "application/pdf"; + pdfBook.PDFFile = pdfStorageResult.Result; + } + await context.PDFBooks.AddAsync(pdfBook); + await context.SaveChangesAsync(); + var book = await context.Books.Where(b => b.Id == bookId).SingleAsync(); + if (book.CoverImageId == null) + { + book.CoverImage = pdfBook.CoverImage.DuplicateExcludingId(pdfBook.CoverImage); + context.Update(book); + await context.SaveChangesAsync(); + } + var resFTPUpload = await _UploadPDFBookToExternalServer(pdfBook, context, skipUpload); + if (!string.IsNullOrEmpty(resFTPUpload.ExceptionString)) + { + job.EndTime = DateTime.Now; + job.Status = ImportJobStatus.Failed; + job.Exception = $"UploadArtifactToExternalServer: {resFTPUpload.ExceptionString}"; job.EndTime = DateTime.Now; context.Update(job); await context.SaveChangesAsync(); return new RServiceResult(null, job.Exception); } - pdfStorageResult.Result.ContentType = "application/pdf"; - pdfBook.PDFFile = pdfStorageResult.Result; - } - await context.PDFBooks.AddAsync(pdfBook); - await context.SaveChangesAsync(); - var book = await context.Books.Where(b => b.Id == bookId).SingleAsync(); - if (book.CoverImageId == null) - { - book.CoverImage = pdfBook.CoverImage.DuplicateExcludingId(pdfBook.CoverImage); - context.Update(book); + job.ProgressPercent = 100; + job.Status = ImportJobStatus.Succeeded; + job.EndTime = DateTime.Now; + context.Update(job); await context.SaveChangesAsync(); + return new RServiceResult(pdfBook); } - var resFTPUpload = await _UploadPDFBookToExternalServer(pdfBook, context, skipUpload); - if (!string.IsNullOrEmpty(resFTPUpload.ExceptionString)) + catch (Exception exp) { job.EndTime = DateTime.Now; job.Status = ImportJobStatus.Failed; - job.Exception = $"UploadArtifactToExternalServer: {resFTPUpload.ExceptionString}"; + job.Exception = exp.ToString(); job.EndTime = DateTime.Now; context.Update(job); await context.SaveChangesAsync(); return new RServiceResult(null, job.Exception); } - job.ProgressPercent = 100; - job.Status = ImportJobStatus.Succeeded; - job.EndTime = DateTime.Now; - context.Update(job); - await context.SaveChangesAsync(); - return new RServiceResult(pdfBook); } - catch (Exception exp) + catch(Exception e) { - job.EndTime = DateTime.Now; - job.Status = ImportJobStatus.Failed; - job.Exception = exp.ToString(); - job.EndTime = DateTime.Now; - context.Update(job); - await context.SaveChangesAsync(); - return new RServiceResult(null, job.Exception); + return new RServiceResult(null, e.ToString()); } } private async Task> _ImportAndReturnPDFJobImages(PDFBook pdfBook, ImportJob job, int order) @@ -230,7 +236,7 @@ namespace RMuseum.Services.Implementation Image thumbnail = new Bitmap(thumbnailImageWidth, thumbnailImageHeight); using (Graphics gThumbnail = Graphics.FromImage(thumbnail)) { - gThumbnail.DrawImage(img, 0, 0, imageWidth, imageHeight); + gThumbnail.DrawImage(img, 0, 0, thumbnailImageWidth, thumbnailImageHeight); } using (MemoryStream msThumbnail = new MemoryStream()) { diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index 10721a03..89bdd31c 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -53,6 +53,10 @@ namespace RMuseum.Services.Implementation /// public RServiceResult StartImportingLocalPDF(NewPDFBookViewModel model) { + if(model == null) + { + return new RServiceResult(false, "model == null"); + } _backgroundTaskQueue.QueueBackgroundWorkItem ( async token =>