From 1405b5373a51665caa119a0b557ae1b470ecf811 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 10 Jul 2026 16:39:54 +0330 Subject: [PATCH] #494 backup ftp server --- .../Implementation/PictureFileService.cs | 65 +++++++++++++++++++ .../PoetPhotoSuggestionService.cs | 26 +++++++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/RMuseum/Services/Implementation/PictureFileService.cs b/RMuseum/Services/Implementation/PictureFileService.cs index 5779ee74..e7ee16e2 100644 --- a/RMuseum/Services/Implementation/PictureFileService.cs +++ b/RMuseum/Services/Implementation/PictureFileService.cs @@ -277,6 +277,28 @@ 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 = GetImagePath(rPictureFile, imageSizeString).Result; + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/{rPictureFile.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + } + + await ftpClient.Disconnect(); + } + return result; } } @@ -359,6 +381,30 @@ namespace RMuseum.Services.Implementation } + 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 = GetImagePath(rPictureFile, imageSizeString).Result; + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/{rPictureFile.FolderName}/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + } + + await ftpClient.Disconnect(); + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + + } + return result; } @@ -672,6 +718,25 @@ 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; + + var localFilePath = _simpleImageStorage.GetImagePath(image).Result; + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/CroppedImages/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath, createRemoteDir: true); + + await ftpClient.Disconnect(); + } return new RServiceResult(image); } return res; diff --git a/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs b/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs index a3796fe6..29513039 100644 --- a/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs +++ b/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs @@ -268,7 +268,31 @@ namespace RMuseum.Services.Implementation } await ftpClient.Disconnect(); } - + + if (model.Published && 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(dbModel.Picture, imageSizeString).Result; + if (imageSizeString == "norm") + { + dbModel.Picture.ExternalNormalSizeImageUrl = $"{Configuration.GetSection("BackupFTPServer")["RootUrl"]}/PoetsPhotoSuggestions/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + } + var remoteFilePath = $"{Configuration.GetSection("BackupFTPServer")["RootPath"]}/images/PoetsPhotoSuggestions/{imageSizeString}/{Path.GetFileName(localFilePath)}"; + await ftpClient.UploadFile(localFilePath, remoteFilePath); + } + await ftpClient.Disconnect(); + } + bool publishIsChanged = model.Published != dbModel.Published; if(publishIsChanged) {