check SyncService

This commit is contained in:
SajjadMahmoody 2026-04-18 02:44:30 +03:30
parent ea82f5968e
commit 693046bf5b
1 changed files with 118 additions and 8 deletions

View File

@ -8,6 +8,7 @@
use App\Models\SyncLog; use App\Models\SyncLog;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class SyncService class SyncService
@ -143,6 +144,7 @@ private function transferFilesViaFtp(array $changes, string $side, array &$repor
$ftp = $this->connectFtp(); $ftp = $this->connectFtp();
if (! $ftp) { if (! $ftp) {
$report[] = 'خطا در اتصال FTP به سیستم مقابل'; $report[] = 'خطا در اتصال FTP به سیستم مقابل';
Log::error('FTP connection failed in transferFilesViaFtp');
return; return;
} }
@ -169,26 +171,84 @@ private function transferFilesViaFtp(array $changes, string $side, array &$repor
if ($side === 'c2s') { if ($side === 'c2s') {
if (! file_exists($localFile)) { if (! file_exists($localFile)) {
$errorMsg = "فایل [{$filePath}] در سیستم محلی یافت نشد";
$report[] = $errorMsg . ' ✗';
Log::warning('FTP upload skipped - local file not found', [
'file' => $filePath,
'local_path' => $localFile,
]);
continue; continue;
} }
@ftp_mkdir($ftp, $remoteFolder);
if (@ftp_put($ftp, $remoteFile, $localFile, FTP_BINARY)) { if (! $this->createFtpDirectory($ftp, $remoteFolder)) {
$errorMsg = "ایجاد پوشه [{$remoteFolder}] برای فایل [{$filePath}] شکست خورد";
$report[] = $errorMsg . ' ✗';
Log::error('FTP mkdir failed', [
'file' => $filePath,
'remote_folder' => $remoteFolder,
]);
continue;
}
if (ftp_put($ftp, $remoteFile, $localFile, FTP_BINARY)) {
$report[] = "فایل [{$filePath}] ارسال شد (FTP) ✓"; $report[] = "فایل [{$filePath}] ارسال شد (FTP) ✓";
Log::info('FTP file uploaded', [
'file' => $filePath,
'size' => filesize($localFile),
]);
} else { } else {
$report[] = "فایل [{$filePath}] ارسال نشد (FTP) ✗"; $errorDetail = error_get_last();
$errorMsg = "فایل [{$filePath}] ارسال نشد (FTP)";
if ($errorDetail) {
$errorMsg .= " - " . $errorDetail['message'];
}
$report[] = $errorMsg . ' ✗';
Log::error('FTP upload failed', [
'file' => $filePath,
'local_file' => $localFile,
'remote_file' => $remoteFile,
'error' => $errorDetail,
]);
} }
} else { } else {
if (file_exists($localFile)) { if (file_exists($localFile)) {
Log::debug('FTP download skipped - file already exists', [
'file' => $filePath,
]);
continue; continue;
} }
$localFolder = dirname($localFile); $localFolder = dirname($localFile);
if (! is_dir($localFolder)) { if (! is_dir($localFolder)) {
mkdir($localFolder, 0755, true); if (! mkdir($localFolder, 0755, true)) {
$report[] = "ایجاد پوشه [{$localFolder}] شکست خورد ✗";
Log::error('Local mkdir failed', [
'folder' => $localFolder,
'error' => error_get_last(),
]);
continue;
} }
if (@ftp_get($ftp, $localFile, $remoteFile, FTP_BINARY)) { }
if (ftp_get($ftp, $localFile, $remoteFile, FTP_BINARY)) {
$report[] = "فایل [{$filePath}] دریافت شد (FTP) ✓"; $report[] = "فایل [{$filePath}] دریافت شد (FTP) ✓";
Log::info('FTP file downloaded', [
'file' => $filePath,
'size' => filesize($localFile),
]);
} else { } else {
$report[] = "فایل [{$filePath}] دریافت نشد (FTP) ✗"; $errorDetail = error_get_last();
$errorMsg = "فایل [{$filePath}] دریافت نشد (FTP)";
if ($errorDetail) {
$errorMsg .= " - " . $errorDetail['message'];
}
$report[] = $errorMsg . ' ✗';
Log::error('FTP download failed', [
'file' => $filePath,
'local_file' => $localFile,
'remote_file' => $remoteFile,
'error' => $errorDetail,
]);
} }
} }
} }
@ -196,29 +256,79 @@ private function transferFilesViaFtp(array $changes, string $side, array &$repor
} }
ftp_close($ftp); ftp_close($ftp);
Log::info('FTP connection closed');
} }
private function connectFtp(): mixed private function connectFtp(): mixed
{ {
if (! function_exists('ftp_connect')) { if (! function_exists('ftp_connect')) {
Log::error('FTP extension is not available');
return null; return null;
} }
$ftp = @ftp_connect($this->peerIp, $this->ftpPort, 30); $ftp = ftp_connect($this->peerIp, $this->ftpPort, 30);
if (! $ftp) { if (! $ftp) {
Log::error('FTP connection failed', [
'host' => $this->peerIp,
'port' => $this->ftpPort,
'error' => error_get_last(),
]);
return null; return null;
} }
if (! @ftp_login($ftp, $this->ftpUser, $this->ftpPass)) { if (! ftp_login($ftp, $this->ftpUser, $this->ftpPass)) {
Log::error('FTP login failed', [
'host' => $this->peerIp,
'port' => $this->ftpPort,
'user' => $this->ftpUser,
'error' => error_get_last(),
]);
ftp_close($ftp); ftp_close($ftp);
return null; return null;
} }
ftp_pasv($ftp, true); ftp_pasv($ftp, true);
Log::info('FTP connection established', [
'host' => $this->peerIp,
'port' => $this->ftpPort,
]);
return $ftp; return $ftp;
} }
private function createFtpDirectory($ftp, string $path): bool
{
$parts = explode('/', trim($path, '/'));
$currentPath = '';
foreach ($parts as $part) {
if (empty($part)) {
continue;
}
$currentPath .= '/' . $part;
$originalDir = ftp_pwd($ftp);
if (@ftp_chdir($ftp, $currentPath)) {
@ftp_chdir($ftp, $originalDir);
continue;
}
if (! @ftp_mkdir($ftp, $currentPath)) {
Log::error('FTP mkdir failed', [
'path' => $currentPath,
'full_path' => $path,
'error' => error_get_last(),
]);
return false;
}
Log::debug('FTP directory created', ['path' => $currentPath]);
}
return true;
}
private function pushFilesViaHttp(array $changes, string $baseUrl, array &$report): void private function pushFilesViaHttp(array $changes, string $baseUrl, array &$report): void
{ {
$fileFields = ['photos_before', 'photos_after', 'videos', 'audio_files']; $fileFields = ['photos_before', 'photos_after', 'videos', 'audio_files'];