diff --git a/RMuseum/Services/Implementation/ArtifactService.cs b/RMuseum/Services/Implementation/ArtifactService.cs index bcf8e1b7..36676c59 100644 --- a/RMuseum/Services/Implementation/ArtifactService.cs +++ b/RMuseum/Services/Implementation/ArtifactService.cs @@ -1430,13 +1430,94 @@ namespace RMuseum.Services.Implementation await context.SaveChangesAsync();//redundant } - return new RServiceResult(true); + } catch (Exception exp) { await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); return new RServiceResult(false, exp.ToString()); } + + try + { + if (bool.Parse(Configuration.GetSection("BackupFTPServer")["UploadEnabled"])) + { + var ftpClient = new AsyncFtpClient + ( + Configuration.GetSection("BackupFTPServer")["Host"], + Configuration.GetSection("BackupFTPServer")["Username"], + Configuration.GetSection("BackupFTPServer")["Password"] + ); + + + + if (!skipUpload) + { + ftpClient.ValidateCertificate += FtpClient_ValidateCertificate; + await ftpClient.AutoConnect(); + ftpClient.Config.RetryAttempts = 3; + } + + + foreach (var imageSizeString in new string[] { "orig", "norm", "thumb" }) + { + var localFilePath = _pictureFileService.GetImagePath(book.CoverImage, imageSizeString).Result; + if (imageSizeString == "norm") + { + book.CoverImage.ExternalNormalSizeImageUrl = $"{Configuration.GetSection("BackupFTPServer")["RootUrl"]}/{book.CoverImage.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + context.Update(book.CoverImage); + } + if (!skipUpload) + { + await jobProgressServiceEF.UpdateJob(job.Id, 0, localFilePath); + + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/{book.CoverImage.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + } + + } + + + foreach (var item in book.Items) + { + foreach (var image in item.Images) + { + foreach (var imageSizeString in new string[] { "orig", "norm", "thumb" }) + { + var localFilePath = _pictureFileService.GetImagePath(image, imageSizeString).Result; + if (imageSizeString == "norm") + { + image.ExternalNormalSizeImageUrl = $"{Configuration.GetSection("BackupFTPServer")["RootUrl"]}/{image.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + context.Update(image); + } + if (!skipUpload) + { + await jobProgressServiceEF.UpdateJob(job.Id, 0, localFilePath); + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/{book.CoverImage.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + } + + } + } + } + + if (!skipUpload) + { + await ftpClient.Disconnect(); + } + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + await context.SaveChangesAsync();//redundant + } + + + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + return new RServiceResult(false, exp.ToString()); + } + + return new RServiceResult(true); } @@ -3472,6 +3553,30 @@ namespace RMuseum.Services.Implementation await ftpClient.Disconnect(); } + if (bool.Parse(Configuration.GetSection("BackupFTPServer")["UploadEnabled"])) + { + var ftpClient = new AsyncFtpClient + ( + Configuration.GetSection("BackupFTPServer")["Host"], + Configuration.GetSection("BackupFTPServer")["Username"], + Configuration.GetSection("BackupFTPServer")["Password"] + ); + ftpClient.ValidateCertificate += FtpClient_ValidateCertificate; + await ftpClient.AutoConnect(); + ftpClient.Config.RetryAttempts = 3; + foreach (var imageSizeString in new string[] { "orig", "norm", "thumb" }) + { + var localFilePath = _pictureFileService.GetImagePath(picture.Result, imageSizeString).Result; + if (imageSizeString == "norm") + { + picture.Result.ExternalNormalSizeImageUrl = $"{Configuration.GetSection("BackupFTPServer")["RootUrl"]}/Pinterest/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + } + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/Pinterest/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath); + } + await ftpClient.Disconnect(); + } + RArtifactMasterRecord book = new RArtifactMasterRecord(titleInToc, titleInToc) {