feat(admin): remove old assets
This commit is contained in:
@@ -94,6 +94,7 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
|
||||
if ($hasConfig) {
|
||||
$this->updateConfigFromDefaults();
|
||||
$this->cleanUpOldAssets();
|
||||
$this->config();
|
||||
} else {
|
||||
$this->init();
|
||||
@@ -132,7 +133,6 @@ class ControllerExtensionModuleTgshop extends Controller
|
||||
foreach ($data['settings'] as $configs) {
|
||||
foreach ($configs as $key => $config) {
|
||||
if ($config['type'] === 'image') {
|
||||
|
||||
if (isset($this->request->post[$key]) && is_file(DIR_IMAGE . $this->request->post[$key])) {
|
||||
$data[$key] = $this->model_tool_image->resize($this->request->post[$key], 100, 100);
|
||||
} elseif ($this->config->get($key) && is_file(DIR_IMAGE . $this->config->get($key))) {
|
||||
@@ -550,4 +550,64 @@ HTML,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanUpOldAssets(): void
|
||||
{
|
||||
$spaPath = rtrim(DIR_IMAGE, '/') . '/catalog/tgshopspa';
|
||||
$assetsPath = $spaPath . '/assets';
|
||||
$manifestPath = $spaPath . '/manifest.json';
|
||||
if (! file_exists($manifestPath)) {
|
||||
$this->log->write('[TELECART] Файл manifest.json не найден — очистка пропущена.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$contents = json_decode(file_get_contents($manifestPath), true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
$entry = $contents['index.html'] ?? null;
|
||||
if (! $entry) {
|
||||
throw new RuntimeException('[TELECART] Некорректный manifest.json — отсутствует ключ index.html.');
|
||||
}
|
||||
|
||||
$keep = [$entry['file']];
|
||||
if (! empty($entry['css'])) {
|
||||
foreach ($entry['css'] as $css) {
|
||||
$keep[] = $css;
|
||||
}
|
||||
}
|
||||
|
||||
$deletedFiles = 0;
|
||||
$keptFiles = 0;
|
||||
|
||||
foreach (glob($assetsPath . '/*') as $file) {
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
if (! in_array($ext, ['js', 'css'])) {
|
||||
continue;
|
||||
}
|
||||
$relative = 'assets/' . basename($file);
|
||||
if (in_array($relative, $keep, true)) {
|
||||
$keptFiles++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
$deletedFiles++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->log->write(
|
||||
sprintf(
|
||||
'[TELECART] Очистка assets завершена. Удалено: %d, оставлено: %d',
|
||||
$deletedFiles,
|
||||
$keptFiles
|
||||
)
|
||||
);
|
||||
} catch (JsonException $e) {
|
||||
$this->log->write('[TELECART] Ошибка декодирования файла manifest.json: ' . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
$this->log->write('[TELECART] Ошибка удаления старых assets: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user