Skip to content

Commit 0361bb6

Browse files
committed
Fix download and update
1 parent 64045b1 commit 0361bb6

4 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/Actions/DownloadLatestReleaseAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function execute()
2626
$url = $releaseDetails['url'];
2727

2828
$response = Http::accept('application/octet-stream')->withToken($this->key)->get($url);
29+
2930
if ($response->successful() === true) {
3031
$disk = Install::getDisk();
3132
$disk->put($zipPath, $response->body());

src/Actions/GetLatestReleaseDetailsAction.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AdminUI\AdminUIInstaller\Facades\Json;
66
use Illuminate\Support\Facades\Http;
7+
use Illuminate\Support\Facades\Log;
78

89
class GetLatestReleaseDetailsAction
910
{
@@ -27,6 +28,7 @@ public function execute()
2728
}
2829
});
2930

31+
3032
$releaseDetails = $response->json();
3133
Json::setField('releaseDetails', $releaseDetails);
3234

src/Controllers/UpdateController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function update(
132132
UpdateVersionEntryAction $versionAction
133133
) {
134134
$log = [];
135+
$log[] = "Running preliminary composer update";
136+
// $composerAction->execute();
137+
135138
$isMaintenance = App::isDownForMaintenance() === true;
136139
$validated = $request->validate([
137140
'url' => ['required', 'url'],
@@ -140,9 +143,12 @@ public function update(
140143
]);
141144

142145
try {
146+
$log[] = "Running cleanup action";
143147
$cleanupAction->execute();
148+
$log[] = "Downloading AdminUI package";
144149
$downloadAction->execute();
145150
$isValid = $validateDownloadAction->execute(checksum: $validated['shasum']);
151+
$log[] = "Package valid: " . $isValid;
146152
} catch (\Exception $err) {
147153
return $this->sendFailed($err->getMessage(), $log);
148154
}

src/Services/JsonService.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace AdminUI\AdminUIInstaller\Services;
44

5+
use Illuminate\Contracts\Filesystem\Filesystem;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Facades\Storage;
7-
use Illuminate\Contracts\Filesystem\Filesystem;
88

99
class JsonService
1010
{
1111
protected string $file;
12+
1213
protected Filesystem $disk;
1314

15+
protected ?array $cache = null;
16+
1417
public function __construct()
1518
{
1619
$this->disk = Storage::disk('local');
@@ -20,6 +23,7 @@ public function __construct()
2023
if (file_exists($oldPath)) {
2124
$contents = file_get_contents($oldPath);
2225
$this->disk->put('adminui-installer/status.json', $contents);
26+
unlink($oldPath);
2327
}
2428
$this->checkStatusFile();
2529
}
@@ -40,23 +44,40 @@ private function getDefault()
4044

4145
public function get(): array
4246
{
47+
if ($this->cache !== null) {
48+
return $this->cache;
49+
}
50+
4351
try {
4452
$string = $this->disk->get($this->file);
4553
} catch (\Exception $e) {
46-
return $this->getDefault();
54+
return $this->cache = $this->getDefault();
4755
}
4856
if (empty($string)) {
49-
return $this->getDefault();
57+
return $this->cache = $this->getDefault();
5058
}
5159

52-
return json_decode($string, true);
60+
return $this->cache = json_decode($string, true);
5361
}
5462

5563
public function set(array|object $json): void
5664
{
57-
$string = json_encode($json, JSON_PRETTY_PRINT);
58-
$this->checkStatusFile();
59-
$this->disk->put($this->file, $string);
65+
$array = (array) $json;
66+
67+
$this->cache = $array;
68+
69+
$path = $this->disk->path($this->file);
70+
71+
$fp = fopen($path, 'c+');
72+
73+
if (flock($fp, LOCK_EX)) {
74+
ftruncate($fp, 0);
75+
fwrite($fp, json_encode($array, JSON_PRETTY_PRINT));
76+
fflush($fp);
77+
flock($fp, LOCK_UN);
78+
}
79+
80+
fclose($fp);
6081
}
6182

6283
public function getField(string $field): mixed
@@ -69,7 +90,7 @@ public function getField(string $field): mixed
6990
public function setField(string $field, mixed $data): void
7091
{
7192
$array = $this->get();
72-
$array[$field] = $data;
93+
$array = Arr::set($array, $field, $data);
7394
$this->set($array);
7495
}
7596
}

0 commit comments

Comments
 (0)