AzuraCast/src/Controller/Admin/InstallShoutcastController.php

95 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
namespace App\Controller\Admin;
2020-07-09 02:32:34 +00:00
use App\Config;
use App\Environment;
2019-05-08 22:23:26 +00:00
use App\Form\Form;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Radio\Frontend\SHOUTcast;
2019-09-04 18:00:51 +00:00
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;
use Symfony\Component\Process\Process;
class InstallShoutcastController
{
protected array $form_config;
public function __construct(
2021-04-23 05:24:12 +00:00
protected SHOUTcast $adapter,
Config $config
) {
$this->form_config = $config->get('forms/install_shoutcast');
}
public function __invoke(
ServerRequest $request,
Response $response,
Environment $environment
): ResponseInterface {
$form_config = $this->form_config;
$version = $this->adapter->getVersion();
if (null !== $version) {
$form_config['groups'][0]['elements']['current_version'][1]['markup'] = '<p class="text-success">' . __(
'SHOUTcast version "%s" is currently installed.',
$version
) . '</p>';
}
2019-05-08 22:23:26 +00:00
$form = new Form($form_config, []);
2021-07-19 05:53:45 +00:00
if ($form->isValid($request)) {
2019-09-04 18:00:51 +00:00
try {
$sc_base_dir = $environment->getParentDirectory() . '/servers/shoutcast2';
2021-07-19 05:53:45 +00:00
$values = $form->getValues();
2021-07-19 05:53:45 +00:00
$import_file = $values['binary'] ?? null;
if ($import_file instanceof UploadedFileInterface) {
2019-09-04 18:00:51 +00:00
$sc_tgz_path = $sc_base_dir . '/sc_serv.tar.gz';
2021-06-08 06:40:49 +00:00
if (is_file($sc_tgz_path)) {
unlink($sc_tgz_path);
}
$import_file->moveTo($sc_tgz_path);
$process = new Process(
[
'tar',
'xvzf',
$sc_tgz_path,
],
$sc_base_dir
);
$process->mustRun();
unlink($sc_tgz_path);
}
return $response->withRedirect($request->getUri()->getPath());
2019-09-04 18:00:51 +00:00
} catch (Exception $e) {
$form
->getField('binary')
2019-09-04 18:00:51 +00:00
->addError(get_class($e) . ': ' . $e->getMessage());
}
}
return $request->getView()->renderToResponse(
$response,
'system/form_page',
[
'form' => $form,
'render_mode' => 'edit',
'title' => __('Install SHOUTcast'),
]
);
}
}