fix(vikon): use module root as FM sync base path instead of files/ subdir

Root files (index.html, file_stubs/) and non-3-letter directories now go
to module root where VIKON expects them, not into files/ subdirectory.
dir_name from FM API is relative to module root.
This commit is contained in:
F4ilji
2026-07-04 22:31:10 +05:00
parent bf3bcc4eca
commit a7d0628a13
2 changed files with 84 additions and 115 deletions
@@ -16,31 +16,35 @@ 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 sync', ['module' => $moduleId]); Log::info('Vikon FM: starting sync', ['module' => $moduleId]);
$dirs = $this->getUsedDirNames($moduleId, $accessToken); $dirIds = $this->getUsedDirNames($moduleId, $accessToken);
Log::info('Vikon FM: directories from FM', ['dirs' => $dirs]); Log::info('Vikon FM: got dir identifiers', ['count' => count($dirIds)]);
foreach ($dirs as $dir) { $identities = [];
if (!File::isDirectory($filesDir . '/' . $dir)) {
File::makeDirectory($filesDir . '/' . $dir, 0755, true, true); $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)]);
$synced = 0; $synced = 0;
foreach ($identities as $identity) {
$synced += $this->syncRootDir($moduleId, $accessToken, $filesDir); $result = $this->downloadByIdentity($identity, $moduleId, $modulePath, $accessToken);
if ($result) $synced++;
foreach ($dirs as $dir) {
$synced += $this->syncSubDir($dir, $moduleId, $accessToken, $filesDir);
} }
$synced += $this->syncNewFiles($moduleId, $accessToken, $filesDir); $newSynced = $this->syncNewFiles($moduleId, $accessToken, $modulePath);
Log::info('Vikon FM: sync done', ['module' => $moduleId, 'synced' => $synced]); $total = $synced + $newSynced;
return "Синхронизировано: {$synced} файлов"; Log::info('Vikon FM: sync complete', ['downloaded' => $synced, 'new' => $newSynced]);
return "Синхронизировано: {$total} файлов";
} }
private function getModuleName(int $moduleId): string private function getModuleName(int $moduleId): string
@@ -60,77 +64,76 @@ class SyncFilesAction
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$body = $response->json(); $body = $response->json();
return $body['directories'] ?? []; return $body['directories'] ?? [];
} }
private function syncRootDir(int $moduleId, string $accessToken, string $filesDir): int private function getFileIdentitiesFromRoot(int $moduleId, string $accessToken): array
{ {
$response = $this->http->getWithToken( $response = $this->http->getWithToken(
"sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}", "sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}",
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$body = $response->json(); $body = $response->json();
$files = $body['files'] ?? []; $files = $body['files'] ?? [];
if (empty($files)) { return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i'])));
return 0;
}
$synced = 0;
foreach ($files as $file) {
$name = $file['n'] ?? null;
$identity = $file['i'] ?? null;
if (!$name || !$identity) continue;
$filePath = $filesDir . '/' . $name;
$this->downloadAndSave($identity, $moduleId, $filePath, $accessToken);
$synced++;
}
return $synced;
} }
private function syncSubDir(string $dir, int $moduleId, string $accessToken, string $filesDir): int private function getFileIdentitiesFromSubDir(string $dirId, int $moduleId, string $accessToken): array
{ {
$response = $this->http->getWithToken( $response = $this->http->getWithToken(
"sync/getFileNamesFromSubDirectoryByModule?dir={$dir}&moduleId={$moduleId}", "sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}",
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$body = $response->json(); $body = $response->json();
$files = $body['files'] ?? []; $files = $body['files'] ?? [];
if (empty($files)) { return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i'])));
return 0;
}
$dirPath = $filesDir . '/' . $dir;
if (!File::isDirectory($dirPath)) {
File::makeDirectory($dirPath, 0755, true, true);
}
$synced = 0;
foreach ($files as $file) {
$name = $file['n'] ?? null;
$identity = $file['i'] ?? null;
if (!$name || !$identity) continue;
$filePath = $dirPath . '/' . $name;
$this->downloadAndSave($identity, $moduleId, $filePath, $accessToken);
$synced++;
}
return $synced;
} }
private function syncNewFiles(int $moduleId, string $accessToken, string $filesDir): int 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();
if (empty($info['identity']) || empty($info['file_name'])) {
return false;
}
$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;
}
}
private function syncNewFiles(int $moduleId, string $accessToken, string $modulePath): int
{ {
$synced = 0; $synced = 0;
@@ -140,7 +143,6 @@ class SyncFilesAction
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$body = $response->json(); $body = $response->json();
if (empty($body['file_name']) && empty($body['identity'])) { if (empty($body['file_name']) && empty($body['identity'])) {
@@ -151,15 +153,19 @@ class SyncFilesAction
$filename = $body['file_name']; $filename = $body['file_name'];
$directory = $body['dir_name'] ?? null; $directory = $body['dir_name'] ?? null;
$targetDir = $filesDir; $targetDir = $directory ? $modulePath . '/' . $directory : $modulePath;
if ($directory) {
$targetDir = $filesDir . '/' . $directory; $content = $this->http->downloadWithToken(
if (!File::isDirectory($targetDir)) { "sync/downloadFileBinary?identity={$identity}",
File::makeDirectory($targetDir, 0775, true, true); $accessToken,
} 'filemanager'
);
if (!File::isDirectory($targetDir)) {
File::makeDirectory($targetDir, 0755, true, true);
} }
$this->downloadAndSave($identity, $moduleId, $targetDir . '/' . $filename, $accessToken); file_put_contents($targetDir . '/' . $filename, $content);
$this->http->getWithToken( $this->http->getWithToken(
"sync/markNewFileAsLoaded?identity={$identity}&moduleId={$moduleId}", "sync/markNewFileAsLoaded?identity={$identity}&moduleId={$moduleId}",
@@ -172,40 +178,4 @@ class SyncFilesAction
return $synced; return $synced;
} }
private function downloadAndSave(string $identity, int $moduleId, string $targetPath, string $accessToken): void
{
try {
$infoResponse = $this->http->getWithToken(
"sync/getFileByIdentityInfo?identity={$identity}",
$accessToken,
'filemanager'
);
$info = $infoResponse->json();
if (empty($info['identity'])) {
Log::warning('Vikon FM: no identity in info response', ['identity' => $identity]);
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(),
]);
}
}
} }
@@ -72,26 +72,25 @@ class UpdateCoreAction
private function syncFromFM(int $moduleId, string $modulePath, string $accessToken): void 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); $dirIds = $this->getUsedDirNames($moduleId, $accessToken);
Log::info('Vikon FM: dir identifiers', ['count' => count($dirIds)]); Log::info('Vikon FM: dir identifiers', ['count' => count($dirIds)]);
$synced = 0; $synced = 0;
$synced += $this->syncDirFiles('root', $moduleId, $filesDir, $accessToken); $synced += $this->syncDirFiles('root', $moduleId, $modulePath, $accessToken);
foreach ($dirIds as $dirId) { foreach ($dirIds as $dirId) {
$synced += $this->syncDirFiles($dirId, $moduleId, $filesDir, $accessToken); $synced += $this->syncDirFiles($dirId, $moduleId, $modulePath, $accessToken);
} }
$newSynced = $this->syncNewFiles($moduleId, $accessToken, $filesDir); $newSynced = $this->syncNewFiles($moduleId, $accessToken, $modulePath);
Log::info('Vikon FM: sync done', ['synced' => $synced, 'new' => $newSynced]); Log::info('Vikon FM: sync done', ['synced' => $synced, 'new' => $newSynced]);
} }
private function syncDirFiles(string $dirId, int $moduleId, string $filesDir, string $accessToken): int private function syncDirFiles(string $dirId, int $moduleId, string $modulePath, string $accessToken): int
{ {
if ($dirId === 'root') { if ($dirId === 'root') {
$response = $this->http->getWithToken( $response = $this->http->getWithToken(
@@ -99,14 +98,14 @@ class UpdateCoreAction
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$targetDir = $filesDir; $targetDir = $modulePath;
} else { } else {
$response = $this->http->getWithToken( $response = $this->http->getWithToken(
"sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}", "sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}",
$accessToken, $accessToken,
'filemanager' 'filemanager'
); );
$targetDir = $filesDir . '/' . $dirId; $targetDir = $modulePath . '/' . $dirId;
} }
$files = $response->json()['files'] ?? []; $files = $response->json()['files'] ?? [];
@@ -136,7 +135,7 @@ class UpdateCoreAction
return $synced; return $synced;
} }
private function syncNewFiles(int $moduleId, string $accessToken, string $filesDir): int private function syncNewFiles(int $moduleId, string $accessToken, string $modulePath): int
{ {
$synced = 0; $synced = 0;
while (true) { while (true) {
@@ -153,7 +152,7 @@ class UpdateCoreAction
$filename = $body['file_name']; $filename = $body['file_name'];
$directory = $body['dir_name'] ?? null; $directory = $body['dir_name'] ?? null;
$targetDir = $directory ? $filesDir . '/' . $directory : $filesDir; $targetDir = $directory ? $modulePath . '/' . $directory : $modulePath;
try { try {
$content = $this->http->downloadWithToken( $content = $this->http->downloadWithToken(