feat(vikon): merge core update + FM sync into single Update button
This commit is contained in:
@@ -24,11 +24,22 @@ class UpdateCoreAction
|
|||||||
$modulePath = $this->basePath . '/' . $config['path'];
|
$modulePath = $this->basePath . '/' . $config['path'];
|
||||||
$tempPath = $this->storagePath . '/temp/' . $config['path'];
|
$tempPath = $this->storagePath . '/temp/' . $config['path'];
|
||||||
|
|
||||||
try {
|
Log::info('Vikon: starting full update', ['module' => $moduleId]);
|
||||||
Log::info('Vikon: downloading module core', ['module' => $moduleId]);
|
|
||||||
|
|
||||||
|
$this->downloadCore($moduleId, $modulePath, $tempPath, $accessToken);
|
||||||
|
$this->syncFromFM($moduleId, $modulePath, $accessToken);
|
||||||
|
|
||||||
|
File::put($modulePath . '/.vikon', date('Y-m-d H:i:s'));
|
||||||
|
|
||||||
|
Log::info('Vikon: full update complete', ['module' => $config['name']]);
|
||||||
|
return 'Модуль "' . $config['name'] . '" полностью обновлён.';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function downloadCore(int $moduleId, string $modulePath, string $tempPath, string $accessToken): void
|
||||||
|
{
|
||||||
if ($moduleId === 2) {
|
if ($moduleId === 2) {
|
||||||
return $this->initAbiturModule($modulePath, $accessToken);
|
$this->initAbiturModule($modulePath, $accessToken);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$zipContent = $this->http->downloadWithToken(
|
$zipContent = $this->http->downloadWithToken(
|
||||||
@@ -46,25 +57,168 @@ class UpdateCoreAction
|
|||||||
|
|
||||||
$blocked = $this->fs->validateFileTypes($tempPath);
|
$blocked = $this->fs->validateFileTypes($tempPath);
|
||||||
if (!empty($blocked)) {
|
if (!empty($blocked)) {
|
||||||
throw new \RuntimeException(
|
throw new \RuntimeException('Запрещённые файлы: ' . implode(', ', $blocked));
|
||||||
'Запрещённые файлы: ' . implode(', ', $blocked) . '. Обновление отклонено.'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File::delete($zipFile);
|
||||||
|
|
||||||
$extractedDirs = File::directories($tempPath);
|
$extractedDirs = File::directories($tempPath);
|
||||||
$syncSource = $extractedDirs[0] ?? $tempPath;
|
$syncSource = $extractedDirs[0] ?? $tempPath;
|
||||||
File::makeDirectory($modulePath, 0755, true, true);
|
File::makeDirectory($modulePath, 0755, true, true);
|
||||||
$this->syncFiles($syncSource, $modulePath);
|
$this->copyFiles($syncSource, $modulePath);
|
||||||
File::put($modulePath . '/.vikon', date('Y-m-d H:i:s'));
|
|
||||||
|
|
||||||
Log::info('Vikon: module updated', ['module' => $config['name']]);
|
Log::info('Vikon: core downloaded', ['module' => $moduleId]);
|
||||||
return 'Модуль "' . $config['name'] . '" обновлён.';
|
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
Log::error('Vikon update failed', ['module' => $moduleId, 'error' => $e->getMessage()]);
|
|
||||||
File::deleteDirectory($tempPath);
|
|
||||||
throw new \RuntimeException('Ошибка обновления: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function syncFromFM(int $moduleId, string $modulePath, string $accessToken): void
|
||||||
|
{
|
||||||
|
$filesDir = $modulePath . '/files';
|
||||||
|
if (!File::isDirectory($filesDir)) {
|
||||||
|
File::makeDirectory($filesDir, 0755, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$dirIds = $this->getUsedDirNames($moduleId, $accessToken);
|
||||||
|
Log::info('Vikon FM: dir identifiers', ['count' => count($dirIds), 'module' => $moduleId]);
|
||||||
|
|
||||||
|
$allIdentities = $this->getFileIdentitiesFromRoot($moduleId, $accessToken);
|
||||||
|
Log::info('Vikon FM: root identities', ['count' => count($allIdentities)]);
|
||||||
|
|
||||||
|
foreach ($dirIds as $dirId) {
|
||||||
|
$ids = $this->getFileIdentitiesFromSubDir($dirId, $moduleId, $accessToken);
|
||||||
|
$allIdentities = array_merge($allIdentities, $ids);
|
||||||
|
}
|
||||||
|
Log::info('Vikon FM: total identities', ['count' => count($allIdentities)]);
|
||||||
|
|
||||||
|
$synced = 0;
|
||||||
|
foreach ($allIdentities as $identity) {
|
||||||
|
if ($this->downloadByIdentity($identity, $moduleId, $filesDir, $accessToken)) {
|
||||||
|
$synced++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newSynced = $this->syncNewFiles($moduleId, $accessToken, $filesDir);
|
||||||
|
|
||||||
|
Log::info('Vikon FM: sync done', ['synced' => $synced, 'new' => $newSynced]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getUsedDirNames(int $moduleId, string $accessToken): array
|
||||||
|
{
|
||||||
|
$response = $this->http->getWithToken(
|
||||||
|
"sync/getUsedDirNamesByModule?moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
return $response->json()['directories'] ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFileIdentitiesFromRoot(int $moduleId, string $accessToken): array
|
||||||
|
{
|
||||||
|
$response = $this->http->getWithToken(
|
||||||
|
"sync/getFileNamesFromRootDirectoryByModule?moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
$files = $response->json()['files'] ?? [];
|
||||||
|
return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i'])));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFileIdentitiesFromSubDir(string $dirId, int $moduleId, string $accessToken): array
|
||||||
|
{
|
||||||
|
$response = $this->http->getWithToken(
|
||||||
|
"sync/getFileNamesFromSubDirectoryByModule?dir={$dirId}&moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
$files = $response->json()['files'] ?? [];
|
||||||
|
return array_map(fn($f) => $f['i'], array_filter($files, fn($f) => !empty($f['i'])));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function downloadByIdentity(string $identity, int $moduleId, string $filesDir, 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 = $dirName ? $filesDir . '/' . $dirName : $filesDir;
|
||||||
|
|
||||||
|
$content = $this->http->downloadWithToken(
|
||||||
|
"sync/downloadFileBinaryForSync?identity={$info['identity']}&moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!File::isDirectory($targetDir)) {
|
||||||
|
File::makeDirectory($targetDir, 0755, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_put_contents($targetDir . '/' . $info['file_name'], $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 $filesDir): int
|
||||||
|
{
|
||||||
|
$synced = 0;
|
||||||
|
while (true) {
|
||||||
|
$response = $this->http->getWithToken(
|
||||||
|
"sync/getNewFileInfoByModule?moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
$body = $response->json();
|
||||||
|
|
||||||
|
if (empty($body['file_name']) && empty($body['identity'])) break;
|
||||||
|
|
||||||
|
$identity = $body['identity'];
|
||||||
|
$filename = $body['file_name'];
|
||||||
|
$directory = $body['dir_name'] ?? null;
|
||||||
|
|
||||||
|
$targetDir = $directory ? $filesDir . '/' . $directory : $filesDir;
|
||||||
|
|
||||||
|
$content = $this->http->downloadWithToken(
|
||||||
|
"sync/downloadFileBinary?identity={$identity}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!File::isDirectory($targetDir)) {
|
||||||
|
File::makeDirectory($targetDir, 0755, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_put_contents($targetDir . '/' . $filename, $content);
|
||||||
|
|
||||||
|
$this->http->getWithToken(
|
||||||
|
"sync/markNewFileAsLoaded?identity={$identity}&moduleId={$moduleId}",
|
||||||
|
$accessToken,
|
||||||
|
'filemanager'
|
||||||
|
);
|
||||||
|
|
||||||
|
$synced++;
|
||||||
|
}
|
||||||
|
return $synced;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function initAbiturModule(string $modulePath, string $accessToken): void
|
||||||
|
{
|
||||||
|
$response = $this->http->getWithToken('pull_updates/generateEmptyModuleCore/2', $accessToken);
|
||||||
|
$body = $response->json();
|
||||||
|
|
||||||
|
if (!isset($body['success']) || $body['success'] !== true) {
|
||||||
|
throw new \RuntimeException('ABITUR init failed: ' . ($body['message'] ?? 'Unknown'));
|
||||||
|
}
|
||||||
|
|
||||||
|
File::makeDirectory($modulePath, 0755, true, true);
|
||||||
|
File::makeDirectory($modulePath . '/files', 0755, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractZip(string $zipPath, string $destination): void
|
private function extractZip(string $zipPath, string $destination): void
|
||||||
@@ -79,76 +233,33 @@ class UpdateCoreAction
|
|||||||
|
|
||||||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||||||
$name = $zip->getNameIndex($i);
|
$name = $zip->getNameIndex($i);
|
||||||
|
if (str_contains($name, '..')) { $zip->close(); throw new \RuntimeException("Zip Slip: {$name}"); }
|
||||||
if (str_contains($name, '..')) {
|
|
||||||
$zip->close();
|
|
||||||
throw new \RuntimeException("Zip Slip: {$name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||||
if (in_array($ext, $blocked, true)) {
|
if (in_array($ext, $blocked, true)) { $zip->close(); throw new \RuntimeException("Blocked: {$name}"); }
|
||||||
$zip->close();
|
|
||||||
throw new \RuntimeException("Blocked executable: {$name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
$full = realpath($realDest . '/' . $name);
|
$full = realpath($realDest . '/' . $name);
|
||||||
if ($full !== false && !str_starts_with($full, $realDest)) {
|
if ($full !== false && !str_starts_with($full, $realDest)) { $zip->close(); throw new \RuntimeException("Escape: {$name}"); }
|
||||||
$zip->close();
|
|
||||||
throw new \RuntimeException("Path escape: {$name}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip->extractTo($destination);
|
$zip->extractTo($destination);
|
||||||
$zip->close();
|
$zip->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function syncFiles(string $source, string $target): void
|
private function copyFiles(string $source, string $target): void
|
||||||
{
|
{
|
||||||
File::makeDirectory($target, 0755, true, true);
|
File::makeDirectory($target, 0755, true, true);
|
||||||
|
|
||||||
foreach (File::files($source) as $file) {
|
foreach (File::files($source) as $file) {
|
||||||
$name = $file->getFilename();
|
rename($file->getPathname(), $target . '/' . $file->getFilename());
|
||||||
$targetPath = $target . '/' . $name;
|
|
||||||
rename($file->getPathname(), $targetPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (File::directories($source) as $dir) {
|
foreach (File::directories($source) as $dir) {
|
||||||
$name = basename($dir);
|
$name = basename($dir);
|
||||||
$targetPath = $target . '/' . $name;
|
$targetPath = $target . '/' . $name;
|
||||||
|
|
||||||
if (!File::isDirectory($targetPath)) {
|
if (!File::isDirectory($targetPath)) {
|
||||||
rename($dir, $targetPath);
|
rename($dir, $targetPath);
|
||||||
} else {
|
} else {
|
||||||
$this->syncFiles($dir, $targetPath);
|
$this->copyFiles($dir, $targetPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function initAbiturModule(string $modulePath, string $accessToken): string
|
|
||||||
{
|
|
||||||
$response = $this->http->getWithToken(
|
|
||||||
'pull_updates/generateEmptyModuleCore/2',
|
|
||||||
$accessToken
|
|
||||||
);
|
|
||||||
$body = $response->json();
|
|
||||||
|
|
||||||
if (!isset($body['success']) || $body['success'] !== true) {
|
|
||||||
throw new \RuntimeException('ABITUR init failed: ' . ($body['message'] ?? 'Unknown error'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File::isDirectory($modulePath)) {
|
|
||||||
File::makeDirectory($modulePath, 0755, true, true);
|
|
||||||
}
|
|
||||||
if (!File::isDirectory($modulePath . '/files')) {
|
|
||||||
File::makeDirectory($modulePath . '/files', 0755, true, true);
|
|
||||||
}
|
|
||||||
File::put($modulePath . '/.vikon', date('Y-m-d H:i:s'));
|
|
||||||
|
|
||||||
$tempPath = $this->storagePath . '/temp/abitur';
|
|
||||||
File::makeDirectory($tempPath, 0755, true, true);
|
|
||||||
File::put($tempPath . '/api_response.json', json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
||||||
|
|
||||||
Log::info('Vikon: ABITUR module initialized');
|
|
||||||
return 'Модуль "Абитуриент" инициализирован.';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,10 +79,6 @@
|
|||||||
class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-hover disabled:opacity-50">
|
class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-hover disabled:opacity-50">
|
||||||
{{ updating ? 'Обновление...' : 'Обновить' }}
|
{{ updating ? 'Обновление...' : 'Обновить' }}
|
||||||
</button>
|
</button>
|
||||||
<button @click="syncModuleFiles(id)" :disabled="syncing || !accessInfo.has_access"
|
|
||||||
class="px-4 py-2 bg-surface border border-layer-line rounded-lg hover:bg-muted-hover disabled:opacity-50">
|
|
||||||
{{ syncing ? 'Синхронизация...' : 'Синхронизировать файлы' }}
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -139,7 +135,6 @@ const currentVersion = ref(props.current_version);
|
|||||||
const checkingVersion = ref(false);
|
const checkingVersion = ref(false);
|
||||||
const checkingAccess = ref(false);
|
const checkingAccess = ref(false);
|
||||||
const updating = ref(false);
|
const updating = ref(false);
|
||||||
const syncing = ref(false);
|
|
||||||
const progress = ref(0);
|
const progress = ref(0);
|
||||||
const updateError = ref(null);
|
const updateError = ref(null);
|
||||||
const versionInfo = ref({ current_version: props.current_version, has_update: false, latest_version: null });
|
const versionInfo = ref({ current_version: props.current_version, has_update: false, latest_version: null });
|
||||||
@@ -247,24 +242,4 @@ async function logout() {
|
|||||||
console.error('Logout failed:', e);
|
console.error('Logout failed:', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function syncModuleFiles(moduleId) {
|
|
||||||
if (!confirm('Синхронизировать файлы модуля?')) return;
|
|
||||||
|
|
||||||
syncing.value = true;
|
|
||||||
updateError.value = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await axios.post(route('dashboard.vikon-updates.sync-files'), { module_id: moduleId });
|
|
||||||
syncing.value = false;
|
|
||||||
if (res.data.success) {
|
|
||||||
alert(res.data.message);
|
|
||||||
} else {
|
|
||||||
updateError.value = res.data.message;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
syncing.value = false;
|
|
||||||
updateError.value = e.response?.data?.message || 'Ошибка синхронизации';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user