check SyncService
This commit is contained in:
parent
ea82f5968e
commit
693046bf5b
|
|
@ -8,6 +8,7 @@
|
|||
use App\Models\SyncLog;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class SyncService
|
||||
|
|
@ -143,6 +144,7 @@ private function transferFilesViaFtp(array $changes, string $side, array &$repor
|
|||
$ftp = $this->connectFtp();
|
||||
if (! $ftp) {
|
||||
$report[] = 'خطا در اتصال FTP به سیستم مقابل';
|
||||
Log::error('FTP connection failed in transferFilesViaFtp');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -169,26 +171,84 @@ private function transferFilesViaFtp(array $changes, string $side, array &$repor
|
|||
|
||||
if ($side === 'c2s') {
|
||||
if (! file_exists($localFile)) {
|
||||
$errorMsg = "فایل [{$filePath}] در سیستم محلی یافت نشد";
|
||||
$report[] = $errorMsg . ' ✗';
|
||||
Log::warning('FTP upload skipped - local file not found', [
|
||||
'file' => $filePath,
|
||||
'local_path' => $localFile,
|
||||
]);
|
||||
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) ✓";
|
||||
Log::info('FTP file uploaded', [
|
||||
'file' => $filePath,
|
||||
'size' => filesize($localFile),
|
||||
]);
|
||||
} 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 {
|
||||
if (file_exists($localFile)) {
|
||||
Log::debug('FTP download skipped - file already exists', [
|
||||
'file' => $filePath,
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$localFolder = dirname($localFile);
|
||||
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) ✓";
|
||||
Log::info('FTP file downloaded', [
|
||||
'file' => $filePath,
|
||||
'size' => filesize($localFile),
|
||||
]);
|
||||
} 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);
|
||||
Log::info('FTP connection closed');
|
||||
}
|
||||
|
||||
private function connectFtp(): mixed
|
||||
{
|
||||
if (! function_exists('ftp_connect')) {
|
||||
Log::error('FTP extension is not available');
|
||||
return null;
|
||||
}
|
||||
|
||||
$ftp = @ftp_connect($this->peerIp, $this->ftpPort, 30);
|
||||
$ftp = ftp_connect($this->peerIp, $this->ftpPort, 30);
|
||||
if (! $ftp) {
|
||||
Log::error('FTP connection failed', [
|
||||
'host' => $this->peerIp,
|
||||
'port' => $this->ftpPort,
|
||||
'error' => error_get_last(),
|
||||
]);
|
||||
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);
|
||||
return null;
|
||||
}
|
||||
|
||||
ftp_pasv($ftp, true);
|
||||
Log::info('FTP connection established', [
|
||||
'host' => $this->peerIp,
|
||||
'port' => $this->ftpPort,
|
||||
]);
|
||||
|
||||
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
|
||||
{
|
||||
$fileFields = ['photos_before', 'photos_after', 'videos', 'audio_files'];
|
||||
|
|
|
|||
Loading…
Reference in New Issue