From a33d93e163b91090207fc70e0dfface7f12175a2 Mon Sep 17 00:00:00 2001 From: F4ilji Date: Sat, 4 Jul 2026 22:40:30 +0500 Subject: [PATCH] fix(vikon): root files to module root, 3-letter dirs to files/{code}/ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - UpdateCoreAction: split syncDirFiles into syncRootDir (→module root) and syncSubDir (→files/{dirId}) - SyncFilesAction: same split, matching original PHP vikon_core behavior - index.html, file_stubs/ etc go to module root - 3-letter FM codes (eib, eid, rir) go inside files/ --- .../Actions/SyncFilesAction.php | 118 +++++++++--------- .../Actions/UpdateCoreAction.php | 64 +++++++--- 2 files changed, 102 insertions(+), 80 deletions(-) diff --git a/app/Containers/VikonIntegration/Actions/SyncFilesAction.php b/app/Containers/VikonIntegration/Actions/SyncFilesAction.php index 9a5f68d..c539e00 100644 --- a/app/Containers/VikonIntegration/Actions/SyncFilesAction.php +++ b/app/Containers/VikonIntegration/Actions/SyncFilesAction.php @@ -16,28 +16,21 @@ class SyncFilesAction public function run(int $moduleId, string $accessToken): string { $modulePath = $this->publicPath . '/' . $this->getModuleName($moduleId); + $filesDir = $modulePath . '/files'; + File::makeDirectory($modulePath, 0755, true, true); + File::makeDirectory($filesDir, 0755, true, true); Log::info('Vikon FM: starting sync', ['module' => $moduleId]); $dirIds = $this->getUsedDirNames($moduleId, $accessToken); - Log::info('Vikon FM: got dir identifiers', ['count' => count($dirIds)]); - - $identities = []; - - $rootFiles = $this->getFileIdentitiesFromRoot($moduleId, $accessToken); - $identities = array_merge($identities, $rootFiles); - Log::info('Vikon FM: root file identities', ['count' => count($rootFiles)]); - - foreach ($dirIds as $dirId) { - $dirFiles = $this->getFileIdentitiesFromSubDir($dirId, $moduleId, $accessToken); - $identities = array_merge($identities, $dirFiles); - } - Log::info('Vikon FM: total file identities', ['count' => count($identities)]); + Log::info('Vikon FM: dir identifiers', ['count' => count($dirIds)]); $synced = 0; - foreach ($identities as $identity) { - $result = $this->downloadByIdentity($identity, $moduleId, $modulePath, $accessToken); - if ($result) $synced++; + + $synced += $this->syncRootDir($moduleId, $modulePath, $accessToken); + + foreach ($dirIds as $dirId) { + $synced += $this->syncSubDir($dirId, $moduleId, $filesDir, $accessToken); } $newSynced = $this->syncNewFiles($moduleId, $accessToken, $modulePath); @@ -68,69 +61,71 @@ class SyncFilesAction return $body['directories'] ?? []; } - private function getFileIdentitiesFromRoot(int $moduleId, string $accessToken): array + private function syncRootDir(int $moduleId, string $modulePath, string $accessToken): int { $response = $this->http->getWithToken( "sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}", $accessToken, 'filemanager' ); - $body = $response->json(); - $files = $body['files'] ?? []; - return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i']))); + $files = $response->json()['files'] ?? []; + if (empty($files)) return 0; + + $synced = 0; + foreach ($files as $file) { + $name = $file['n'] ?? null; + $identity = $file['i'] ?? null; + if (!$name || !$identity) continue; + + try { + $content = $this->http->downloadWithToken( + "sync/downloadFileBinaryForSync?identity={$identity}&moduleId={$moduleId}", + $accessToken, + 'filemanager' + ); + file_put_contents($modulePath . '/' . $name, $content); + $synced++; + } catch (\Throwable $e) { + Log::warning('Vikon FM: root download failed', ['identity' => $identity, 'error' => $e->getMessage()]); + } + } + return $synced; } - private function getFileIdentitiesFromSubDir(string $dirId, int $moduleId, string $accessToken): array + private function syncSubDir(string $dirId, int $moduleId, string $filesDir, string $accessToken): int { $response = $this->http->getWithToken( "sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}", $accessToken, 'filemanager' ); - $body = $response->json(); - $files = $body['files'] ?? []; - return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i']))); - } + $files = $response->json()['files'] ?? []; + if (empty($files)) return 0; - private function downloadByIdentity(string $identity, int $moduleId, string $modulePath, string $accessToken): bool - { - try { - $infoResp = $this->http->getWithToken( - "sync/getFileByIdentityInfo?identity={$identity}", - $accessToken, - 'filemanager' - ); - $info = $infoResp->json(); + $targetDir = $filesDir . '/' . $dirId; + File::makeDirectory($targetDir, 0755, true, true); - if (empty($info['identity']) || empty($info['file_name'])) { - return false; + $synced = 0; + foreach ($files as $file) { + $name = $file['n'] ?? null; + $identity = $file['i'] ?? null; + if (!$name || !$identity) continue; + + try { + $content = $this->http->downloadWithToken( + "sync/downloadFileBinaryForSync?identity={$identity}&moduleId={$moduleId}", + $accessToken, + 'filemanager' + ); + file_put_contents($targetDir . '/' . $name, $content); + $synced++; + } catch (\Throwable $e) { + Log::warning('Vikon FM: sub download failed', ['identity' => $identity, 'error' => $e->getMessage()]); } - - $dirName = $info['dir_name'] ?? null; - $targetDir = $modulePath; - if ($dirName) { - $targetDir = $modulePath . '/' . $dirName; - } - - $content = $this->http->downloadWithToken( - "sync/downloadFileBinaryForSync?identity={$info['identity']}&moduleId={$moduleId}", - $accessToken, - 'filemanager' - ); - - if (!File::isDirectory($targetDir)) { - File::makeDirectory($targetDir, 0755, true, true); - } - - $targetPath = $targetDir . '/' . $info['file_name']; - file_put_contents($targetPath, $content); - return true; - } catch (\Throwable $e) { - Log::warning('Vikon FM: download failed', ['identity' => $identity, 'error' => $e->getMessage()]); - return false; } + return $synced; } private function syncNewFiles(int $moduleId, string $accessToken, string $modulePath): int @@ -153,7 +148,10 @@ class SyncFilesAction $filename = $body['file_name']; $directory = $body['dir_name'] ?? null; - $targetDir = $directory ? $modulePath . '/' . $directory : $modulePath; + $targetDir = $modulePath; + if ($directory) { + $targetDir = $modulePath . '/' . $directory; + } $content = $this->http->downloadWithToken( "sync/downloadFileBinary?identity={$identity}", diff --git a/app/Containers/VikonIntegration/Actions/UpdateCoreAction.php b/app/Containers/VikonIntegration/Actions/UpdateCoreAction.php index 4801312..14ca2de 100644 --- a/app/Containers/VikonIntegration/Actions/UpdateCoreAction.php +++ b/app/Containers/VikonIntegration/Actions/UpdateCoreAction.php @@ -72,17 +72,19 @@ class UpdateCoreAction private function syncFromFM(int $moduleId, string $modulePath, string $accessToken): void { + $filesDir = $modulePath . '/files'; File::makeDirectory($modulePath, 0755, true, true); + File::makeDirectory($filesDir, 0755, true, true); $dirIds = $this->getUsedDirNames($moduleId, $accessToken); Log::info('Vikon FM: dir identifiers', ['count' => count($dirIds)]); $synced = 0; - $synced += $this->syncDirFiles('root', $moduleId, $modulePath, $accessToken); + $synced += $this->syncRootDir($moduleId, $modulePath, $accessToken); foreach ($dirIds as $dirId) { - $synced += $this->syncDirFiles($dirId, $moduleId, $modulePath, $accessToken); + $synced += $this->syncSubDir($dirId, $moduleId, $filesDir, $accessToken); } $newSynced = $this->syncNewFiles($moduleId, $accessToken, $modulePath); @@ -90,27 +92,50 @@ class UpdateCoreAction Log::info('Vikon FM: sync done', ['synced' => $synced, 'new' => $newSynced]); } - private function syncDirFiles(string $dirId, int $moduleId, string $modulePath, string $accessToken): int + private function syncRootDir(int $moduleId, string $modulePath, string $accessToken): int { - if ($dirId === 'root') { - $response = $this->http->getWithToken( - "sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}", - $accessToken, - 'filemanager' - ); - $targetDir = $modulePath; - } else { - $response = $this->http->getWithToken( - "sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}", - $accessToken, - 'filemanager' - ); - $targetDir = $modulePath . '/' . $dirId; - } + $response = $this->http->getWithToken( + "sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}", + $accessToken, + 'filemanager' + ); $files = $response->json()['files'] ?? []; if (empty($files)) return 0; + $synced = 0; + foreach ($files as $file) { + $name = $file['n'] ?? null; + $identity = $file['i'] ?? null; + if (!$name || !$identity) continue; + + try { + $content = $this->http->downloadWithToken( + "sync/downloadFileBinaryForSync?identity={$identity}&moduleId={$moduleId}", + $accessToken, + 'filemanager' + ); + file_put_contents($modulePath . '/' . $name, $content); + $synced++; + } catch (\Throwable $e) { + Log::warning('Vikon FM: root download failed', ['identity' => $identity, 'error' => $e->getMessage()]); + } + } + return $synced; + } + + private function syncSubDir(string $dirId, int $moduleId, string $filesDir, string $accessToken): int + { + $response = $this->http->getWithToken( + "sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}", + $accessToken, + 'filemanager' + ); + + $files = $response->json()['files'] ?? []; + if (empty($files)) return 0; + + $targetDir = $filesDir . '/' . $dirId; File::makeDirectory($targetDir, 0755, true, true); $synced = 0; @@ -128,10 +153,9 @@ class UpdateCoreAction file_put_contents($targetDir . '/' . $name, $content); $synced++; } catch (\Throwable $e) { - Log::warning('Vikon FM: download failed', ['identity' => $identity, 'error' => $e->getMessage()]); + Log::warning('Vikon FM: sub download failed', ['identity' => $identity, 'error' => $e->getMessage()]); } } - return $synced; }