Skip to content

Commit 82a607a

Browse files
committed
feat(api): added more logic for update API (#3313)
1 parent 00615f1 commit 82a607a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/UpdateController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ public function updateCheck(): JsonResponse
116116
$branch = $this->configuration->get('upgrade.releaseEnvironment');
117117

118118
try {
119-
$api = new Api($this->configuration, new System());
120-
$versions = $api->getVersions();
119+
$versions = $this->container->get('phpmyfaq.admin.api')->getVersions();
121120
$this->configuration->set('upgrade.dateLastChecked', $dateLastChecked);
122121

123122
if (version_compare($versions['installed'], $versions[$branch], '<')) {

phpmyfaq/src/phpMyFAQ/Controller/Api/UpdateController.php

+46
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919

2020
namespace phpMyFAQ\Controller\Api;
2121

22+
use DateTime;
23+
use DateTimeInterface;
2224
use OpenApi\Attributes as OA;
2325
use phpMyFAQ\Controller\AbstractController;
2426
use phpMyFAQ\Enums\PermissionType;
27+
use phpMyFAQ\Translation;
2528
use Symfony\Component\HttpFoundation\Response;
2629
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
30+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
31+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2732

2833
class UpdateController extends AbstractController
2934
{
@@ -36,6 +41,9 @@ public function __construct()
3641
}
3742
}
3843

44+
/**
45+
* @throws \Exception
46+
*/
3947
#[OA\Post(path: '/api/v3.1/update', operationId: 'triggerUpdate', tags: ['Endpoints with Authentication'])]
4048
#[OA\Header(
4149
header: 'x-pmf-token',
@@ -55,6 +63,44 @@ public function index(): Response
5563
{
5664
$this->userHasPermission(PermissionType::CONFIGURATION_EDIT);
5765

66+
$upgrade = $this->container->get('phpmyfaq.setup.upgrade');
67+
$branch = $this->configuration->get('upgrade.releaseEnvironment');
68+
69+
// Check if the maintenance mode is enabled
70+
if (!$upgrade->isMaintenanceEnabled()) {
71+
return $this->json(['error' => Translation::get('msgNotInMaintenanceMode')], Response::HTTP_CONFLICT);
72+
}
73+
74+
// Fetch latest version
75+
try {
76+
$versions = $this->container->get('phpmyfaq.admin.api')->getVersions();
77+
78+
if (version_compare($versions['installed'], $versions[$branch], '<')) {
79+
$versionNumber = $versions[$branch];
80+
}
81+
} catch (TransportExceptionInterface | DecodingExceptionInterface $e) {
82+
return $this->json(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
83+
}
84+
85+
// Fetch a package version
86+
$pathToPackage = $upgrade->downloadPackage($versionNumber);
87+
88+
if ($pathToPackage === false) {
89+
return $this->json(['error' => Translation::get('downloadFailure')], Response::HTTP_BAD_GATEWAY);
90+
}
91+
92+
if (!$upgrade->isNightly()) {
93+
$result = $upgrade->verifyPackage($pathToPackage, $versionNumber);
94+
if ($result === false) {
95+
return $this->json(['error' => Translation::get('verificationFailure')], Response::HTTP_BAD_GATEWAY);
96+
}
97+
}
98+
99+
$this->configuration->set('upgrade.lastDownloadedPackage', urlencode($pathToPackage));
100+
101+
// Extract package
102+
// WORK IN PROGESS
103+
58104
return $this->json($this->configuration->getVersion());
59105
}
60106
}

0 commit comments

Comments
 (0)