#494 backup ftp server

This commit is contained in:
Hamid Reza Mohammadi 2026-07-10 16:26:42 +03:30
parent 220203a97c
commit 5fc1238069

View File

@ -1430,13 +1430,94 @@ namespace RMuseum.Services.Implementation
await context.SaveChangesAsync();//redundant
}
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
return new RServiceResult<bool>(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<bool>(false, exp.ToString());
}
return new RServiceResult<bool>(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)
{