From 3ea7dc6f9109d69d55d846d0282125ed3bdea75a Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 21 Jul 2023 17:46:45 +0330 Subject: [PATCH] #399 basic code for importing local PDF files --- RMuseum/RMuseum.csproj | 2 +- .../GanjoorService-Mundex.cs | 2 +- .../Implementation/PDFLibraryService.cs | 68 ++++++++++++------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/RMuseum/RMuseum.csproj b/RMuseum/RMuseum.csproj index 84b4b111..2b26cfed 100644 --- a/RMuseum/RMuseum.csproj +++ b/RMuseum/RMuseum.csproj @@ -27,7 +27,7 @@ - + diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Mundex.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Mundex.cs index 701859dc..f665b30c 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Mundex.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Mundex.cs @@ -99,7 +99,7 @@ namespace RMuseum.Services.Implementation { using (Stream imageStream = await imageResult.Content.ReadAsStreamAsync()) { - RServiceResult image = await imageFileService.Add(null, imageStream, $"{beepId}.jpg", Path.Combine(configuration.GetSection("PictureFileService")["StoragePath"], "SingerImages")); + RServiceResult image = await imageFileService.Add(null, imageStream, $"{beepId}.jpg", Path.Combine(configuration.GetSection("PictureFileService")["StoragePath"], "SingerImages"), true); if (string.IsNullOrEmpty(image.ExceptionString)) { image = await imageFileService.Store(image.Result); diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index e5de2d10..e1613126 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -139,17 +139,35 @@ namespace RMuseum.Services.Implementation if (pages.Count == 0) { - using (RMuseumDbContext importJobUpdaterDb = new RMuseumDbContext(new DbContextOptions())) + job.EndTime = DateTime.Now; + job.Status = ImportJobStatus.Failed; + job.Exception = "Pages.Count == 0"; + context.Update(job); + await context.SaveChangesAsync(); + return; + } + + 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)) { job.EndTime = DateTime.Now; job.Status = ImportJobStatus.Failed; - job.Exception = "Pages.Count == 0"; - importJobUpdaterDb.Update(job); - await importJobUpdaterDb.SaveChangesAsync(); + job.Exception = $"pdfStorageResult.ExceptionString): {pdfStorageResult.ExceptionString}"; + + job.EndTime = DateTime.Now; + context.Update(job); + await context.SaveChangesAsync(); + return; } - return; + pdfStorageResult.Result.ContentType = "application/pdf"; + pdfBook.PDFFile = pdfStorageResult.Result; } + await context.PDFBooks.AddAsync(pdfBook); await context.SaveChangesAsync(); @@ -291,19 +309,7 @@ namespace RMuseum.Services.Implementation if (page.PageNumber == 1) { - RImage copy = new RImage() - { - OriginalFileName = picture.Result.OriginalFileName, - ContentType = picture.Result.ContentType, - DataTime = picture.Result.DataTime, - FileSizeInBytes = picture.Result.FileSizeInBytes, - FolderName = picture.Result.FolderName, - ImageHeight = picture.Result.ImageHeight, - ImageWidth = picture.Result.ImageWidth, - LastModified = picture.Result.LastModified, - StoredFileName = picture.Result.StoredFileName, - }; - pdfBook.CoverImage = copy; + pdfBook.CoverImage = picture.Result.DuplicateExcludingId(picture.Result); } } } @@ -346,18 +352,29 @@ namespace RMuseum.Services.Implementation ftpClient.Config.RetryAttempts = 3; } - - + var localPDFFilePath = _imageFileService.GetImagePath(book.PDFFile).Result; + var remotePDFFilePath = $"{Configuration.GetSection("ExternalFTPServer")["RootPath"]}/images/{book.StorageFolderName}/{Path.GetFileName(localPDFFilePath)}"; if (!skipUpload) { - var localFilePath = _imageFileService.GetImagePath(book.CoverImage).Result; - await jobProgressServiceEF.UpdateJob(job.Id, 0, localFilePath); - - var remoteFilePath = $"{Configuration.GetSection("ExternalFTPServer")["RootPath"]}/images/{book.CoverImage.FolderName}/{Path.GetFileName(localFilePath)}"; - await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + await jobProgressServiceEF.UpdateJob(job.Id, 0, localPDFFilePath); + await ftpClient.UploadFile(localPDFFilePath, remotePDFFilePath, createRemoteDir: true); } + var localCoverImageFilePath = _imageFileService.GetImagePath(book.CoverImage).Result; + var remoteCoverImageFilePath = $"{Configuration.GetSection("ExternalFTPServer")["RootPath"]}/images/{book.StorageFolderName}/{Path.GetFileName(localCoverImageFilePath)}"; + + if (!skipUpload) + { + await jobProgressServiceEF.UpdateJob(job.Id, 0, localCoverImageFilePath); + await ftpClient.UploadFile(localCoverImageFilePath, remoteCoverImageFilePath, createRemoteDir: true); + } + + book.ExternalPDFFileUrl = remotePDFFilePath; + book.ExtenalCoverImageUrl = remoteCoverImageFilePath; + + context.Update(book); + foreach (var item in book.Pages) { @@ -377,7 +394,6 @@ namespace RMuseum.Services.Implementation await ftpClient.Disconnect(); } await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); - await context.SaveChangesAsync();//redundant } return new RServiceResult(true);