fix(vikon): rewrite SyncFilesAction - always download, add logging
This commit is contained in:
@@ -16,30 +16,31 @@ class SyncFilesAction
|
|||||||
public function run(int $moduleId, string $accessToken): string
|
public function run(int $moduleId, string $accessToken): string
|
||||||
{
|
{
|
||||||
$modulePath = $this->publicPath . '/' . $this->getModuleName($moduleId);
|
$modulePath = $this->publicPath . '/' . $this->getModuleName($moduleId);
|
||||||
|
$filesDir = $modulePath . '/files';
|
||||||
|
|
||||||
Log::info('Vikon FM: starting file sync', ['module' => $moduleId]);
|
Log::info('Vikon FM: starting sync', ['module' => $moduleId]);
|
||||||
|
|
||||||
$dirs = $this->getUsedDirNames($moduleId, $accessToken);
|
$dirs = $this->getUsedDirNames($moduleId, $accessToken);
|
||||||
$this->cleanupRemovedDirs($modulePath, $dirs);
|
Log::info('Vikon FM: directories from FM', ['dirs' => $dirs]);
|
||||||
|
|
||||||
$filesDir = $modulePath . '/files';
|
foreach ($dirs as $dir) {
|
||||||
if (!File::isDirectory($filesDir)) {
|
if (!File::isDirectory($filesDir . '/' . $dir)) {
|
||||||
File::makeDirectory($filesDir, 0755, true, true);
|
File::makeDirectory($filesDir . '/' . $dir, 0755, true, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$synced = 0;
|
$synced = 0;
|
||||||
|
|
||||||
$synced += $this->syncRootDir($moduleId, $accessToken, $filesDir, $dirs);
|
$synced += $this->syncRootDir($moduleId, $accessToken, $filesDir);
|
||||||
|
|
||||||
foreach ($dirs as $dir) {
|
foreach ($dirs as $dir) {
|
||||||
if ($dir === 'files') continue;
|
|
||||||
$synced += $this->syncSubDir($dir, $moduleId, $accessToken, $filesDir);
|
$synced += $this->syncSubDir($dir, $moduleId, $accessToken, $filesDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
$synced += $this->syncNewFiles($moduleId, $accessToken, $filesDir);
|
$synced += $this->syncNewFiles($moduleId, $accessToken, $filesDir);
|
||||||
|
|
||||||
Log::info('Vikon FM: sync complete', ['module' => $moduleId, 'synced' => $synced]);
|
Log::info('Vikon FM: sync done', ['module' => $moduleId, 'synced' => $synced]);
|
||||||
return "Синхронизировано файлов: {$synced}";
|
return "Синхронизировано: {$synced} файлов";
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getModuleName(int $moduleId): string
|
private function getModuleName(int $moduleId): string
|
||||||
@@ -61,28 +62,10 @@ class SyncFilesAction
|
|||||||
);
|
);
|
||||||
|
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
|
return $body['directories'] ?? [];
|
||||||
if (!isset($body['directories']) || !is_array($body['directories'])) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $body['directories'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function cleanupRemovedDirs(string $modulePath, array $knownDirs): void
|
private function syncRootDir(int $moduleId, string $accessToken, string $filesDir): int
|
||||||
{
|
|
||||||
$filesDir = $modulePath . '/files';
|
|
||||||
if (!File::isDirectory($filesDir)) return;
|
|
||||||
|
|
||||||
foreach (File::directories($filesDir) as $dir) {
|
|
||||||
$name = basename($dir);
|
|
||||||
if (!in_array($name, $knownDirs) && !is_link($dir)) {
|
|
||||||
File::deleteDirectory($dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function syncRootDir(int $moduleId, string $accessToken, string $filesDir, array $knownDirs): int
|
|
||||||
{
|
{
|
||||||
$response = $this->http->getWithToken(
|
$response = $this->http->getWithToken(
|
||||||
"sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}",
|
"sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}",
|
||||||
@@ -91,36 +74,22 @@ class SyncFilesAction
|
|||||||
);
|
);
|
||||||
|
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
if (!isset($body['files']) || !is_array($body['files'])) {
|
$files = $body['files'] ?? [];
|
||||||
|
|
||||||
|
if (empty($files)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$filesByIdentity = [];
|
|
||||||
foreach ($body['files'] as $file) {
|
|
||||||
$filesByIdentity[$file['n']] = $file['i'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$existingItems = [];
|
|
||||||
foreach (File::allFiles($filesDir) as $file) {
|
|
||||||
$name = $file->getFilename();
|
|
||||||
$relative = str_replace($filesDir . '/', '', $file->getPathname());
|
|
||||||
$existingItems[$relative] = $relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($existingItems as $relative) {
|
|
||||||
$name = basename($relative);
|
|
||||||
if (!isset($filesByIdentity[$name]) && !is_dir($filesDir . '/' . $name)) {
|
|
||||||
@unlink($filesDir . '/' . $relative);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$synced = 0;
|
$synced = 0;
|
||||||
foreach ($filesByIdentity as $fileName => $identity) {
|
foreach ($files as $file) {
|
||||||
$filePath = $filesDir . '/' . $fileName;
|
$name = $file['n'] ?? null;
|
||||||
if (!File::exists($filePath) || filesize($filePath) === 0) {
|
$identity = $file['i'] ?? null;
|
||||||
$this->downloadFile($identity, $moduleId, $filePath, $accessToken);
|
|
||||||
$synced++;
|
if (!$name || !$identity) continue;
|
||||||
}
|
|
||||||
|
$filePath = $filesDir . '/' . $name;
|
||||||
|
$this->downloadAndSave($identity, $moduleId, $filePath, $accessToken);
|
||||||
|
$synced++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $synced;
|
return $synced;
|
||||||
@@ -135,38 +104,27 @@ class SyncFilesAction
|
|||||||
);
|
);
|
||||||
|
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
if (!isset($body['files']) || !is_array($body['files'])) {
|
$files = $body['files'] ?? [];
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$filesByIdentity = [];
|
if (empty($files)) {
|
||||||
foreach ($body['files'] as $file) {
|
return 0;
|
||||||
$filesByIdentity[$file['n']] = $file['i'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$dirPath = $filesDir . '/' . $dir;
|
$dirPath = $filesDir . '/' . $dir;
|
||||||
if (!File::isDirectory($dirPath)) {
|
if (!File::isDirectory($dirPath)) {
|
||||||
File::makeDirectory($dirPath, 0775, true, true);
|
File::makeDirectory($dirPath, 0755, true, true);
|
||||||
}
|
|
||||||
|
|
||||||
$existingItems = [];
|
|
||||||
foreach (File::files($dirPath) as $file) {
|
|
||||||
$existingItems[$file->getFilename()] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($existingItems as $name => $_) {
|
|
||||||
if (!isset($filesByIdentity[$name])) {
|
|
||||||
@unlink($dirPath . '/' . $name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$synced = 0;
|
$synced = 0;
|
||||||
foreach ($filesByIdentity as $fileName => $identity) {
|
foreach ($files as $file) {
|
||||||
$filePath = $dirPath . '/' . $fileName;
|
$name = $file['n'] ?? null;
|
||||||
if (!File::exists($filePath) || filesize($filePath) === 0) {
|
$identity = $file['i'] ?? null;
|
||||||
$this->downloadFile($identity, $moduleId, $filePath, $accessToken);
|
|
||||||
$synced++;
|
if (!$name || !$identity) continue;
|
||||||
}
|
|
||||||
|
$filePath = $dirPath . '/' . $name;
|
||||||
|
$this->downloadAndSave($identity, $moduleId, $filePath, $accessToken);
|
||||||
|
$synced++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $synced;
|
return $synced;
|
||||||
@@ -185,7 +143,7 @@ class SyncFilesAction
|
|||||||
|
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
|
|
||||||
if (($body['file_name'] ?? null) === null && ($body['identity'] ?? null) === null) {
|
if (empty($body['file_name']) && empty($body['identity'])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,15 +152,14 @@ class SyncFilesAction
|
|||||||
$directory = $body['dir_name'] ?? null;
|
$directory = $body['dir_name'] ?? null;
|
||||||
|
|
||||||
$targetDir = $filesDir;
|
$targetDir = $filesDir;
|
||||||
if ($directory !== null) {
|
if ($directory) {
|
||||||
$targetDir = $filesDir . '/' . $directory;
|
$targetDir = $filesDir . '/' . $directory;
|
||||||
if (!File::isDirectory($targetDir)) {
|
if (!File::isDirectory($targetDir)) {
|
||||||
File::makeDirectory($targetDir, 0775, true, true);
|
File::makeDirectory($targetDir, 0775, true, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$filePath = $targetDir . '/' . $filename;
|
$this->downloadAndSave($identity, $moduleId, $targetDir . '/' . $filename, $accessToken);
|
||||||
$this->downloadFile($identity, $moduleId, $filePath, $accessToken);
|
|
||||||
|
|
||||||
$this->http->getWithToken(
|
$this->http->getWithToken(
|
||||||
"sync/markNewFileAsLoaded?identity={$identity}&moduleId={$moduleId}",
|
"sync/markNewFileAsLoaded?identity={$identity}&moduleId={$moduleId}",
|
||||||
@@ -216,30 +173,39 @@ class SyncFilesAction
|
|||||||
return $synced;
|
return $synced;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function downloadFile(string $identity, int $moduleId, string $targetPath, string $accessToken): void
|
private function downloadAndSave(string $identity, int $moduleId, string $targetPath, string $accessToken): void
|
||||||
{
|
{
|
||||||
$response = $this->http->getWithToken(
|
try {
|
||||||
"sync/getFileByIdentityInfo?identity={$identity}",
|
$infoResponse = $this->http->getWithToken(
|
||||||
$accessToken,
|
"sync/getFileByIdentityInfo?identity={$identity}",
|
||||||
'filemanager'
|
$accessToken,
|
||||||
);
|
'filemanager'
|
||||||
|
);
|
||||||
|
$info = $infoResponse->json();
|
||||||
|
|
||||||
$info = $response->json();
|
if (empty($info['identity'])) {
|
||||||
if (!isset($info['file_name'], $info['identity'])) {
|
Log::warning('Vikon FM: no identity in info response', ['identity' => $identity]);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->http->downloadWithToken(
|
||||||
|
"sync/downloadFileBinaryForSync?identity={$info['identity']}&moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
|
||||||
|
$dir = dirname($targetPath);
|
||||||
|
if (!File::isDirectory($dir)) {
|
||||||
|
File::makeDirectory($dir, 0755, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_put_contents($targetPath, $content);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning('Vikon FM: failed to download file', [
|
||||||
|
'identity' => $identity,
|
||||||
|
'target' => $targetPath,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$content = $this->http->downloadWithToken(
|
|
||||||
"sync/downloadFileBinaryForSync?identity={$info['identity']}&moduleId={$moduleId}",
|
|
||||||
$accessToken,
|
|
||||||
'filemanager'
|
|
||||||
);
|
|
||||||
|
|
||||||
$dir = dirname($targetPath);
|
|
||||||
if (!File::isDirectory($dir)) {
|
|
||||||
File::makeDirectory($dir, 0755, true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
file_put_contents($targetPath, $content);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user