fix(vikon): root files to module root, 3-letter dirs to files/{code}/

- 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/
This commit is contained in:
F4ilji
2026-07-04 22:40:30 +05:00
parent a7d0628a13
commit a33d93e163
2 changed files with 102 additions and 80 deletions
@@ -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}",
@@ -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;
}