forked from linuxserver/Heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
219 lines (188 loc) · 5.6 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace App;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/**
* App\Application
*
* @property string $appid
* @property string $name
* @property string|null $sha
* @property string|null $icon
* @property string|null $website
* @property string|null $license
* @property string|null $description
* @property int $enhanced
* @property string $tile_background
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string|null $class
* @method static Builder|Application newModelQuery()
* @method static Builder|Application newQuery()
* @method static Builder|Application query()
* @method static Builder|Application whereAppid($value)
* @method static Builder|Application whereClass($value)
* @method static Builder|Application whereCreatedAt($value)
* @method static Builder|Application whereDescription($value)
* @method static Builder|Application whereEnhanced($value)
* @method static Builder|Application whereIcon($value)
* @method static Builder|Application whereLicense($value)
* @method static Builder|Application whereName($value)
* @method static Builder|Application whereSha($value)
* @method static Builder|Application whereTileBackground($value)
* @method static Builder|Application whereUpdatedAt($value)
* @method static Builder|Application whereWebsite($value)
*/
class Application extends Model
{
/**
* @var bool
*/
public $incrementing = false;
/**
* @var string
*/
protected $primaryKey = 'appid';
/**
* @return mixed
*/
public function icon()
{
if (! file_exists(storage_path('app/public/'.$this->icon))) {
$img_src = app_path('SupportedApps/'.$this->name.'/'.str_replace('icons/', '', $this->icon));
$img_dest = storage_path('app/public/'.$this->icon);
//die("i: ".$img_src);
@copy($img_src, $img_dest);
}
return $this->icon;
}
/**
* @return string
*/
public function iconView(): string
{
return asset('storage/'.$this->icon);
}
/**
* @return string
*/
public function defaultColour(): string
{
// check if light or dark
if ($this->tile_background == 'light') {
return '#fafbfc';
}
return '#161b1f';
}
/**
* @return string
*/
public function class(): string
{
$name = $this->name;
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
return '\App\SupportedApps\\'.$name.'\\'.$name;
}
/**
* @param $name
* @return string
*/
public static function classFromName($name): string
{
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
$class = '\App\SupportedApps\\'.$name.'\\'.$name;
return $class;
}
/**
* @return Collection
*/
public static function apps(): Collection
{
$json = json_decode(file_get_contents(storage_path('app/supportedapps.json'))) ?? [];
$apps = collect($json->apps);
return $apps->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE);
}
/**
* @return array
*/
public static function autocomplete(): array
{
$apps = self::apps();
$list = [];
foreach ($apps as $app) {
$list[] = (object) [
'label' => $app->name,
'value' => $app->appid,
];
}
return $list;
}
/**
* @param $appid
* @return mixed|null
* @throws GuzzleException
*/
public static function getApp($appid)
{
Log::debug("Get app triggered for: $appid");
$localapp = self::where('appid', $appid)->first();
$app = self::single($appid);
$application = ($localapp) ? $localapp : new self;
// Files missing? || app not in db || old sha version
if (! file_exists(app_path('SupportedApps/'.className($app->name))) ||
! $localapp ||
$localapp->sha !== $app->sha
) {
$gotFiles = SupportedApps::getFiles($app);
if ($gotFiles) {
$app = SupportedApps::saveApp($app, $application);
}
}
return $app;
}
/**
* @param $appid
* @return mixed|null
*/
public static function single($appid)
{
$apps = self::apps();
$app = $apps->where('appid', $appid)->first();
if ($app === null) {
// Try in db for Private App
$appModel = self::where('appid', $appid)->first();
if ($appModel) {
$app = json_decode($appModel->toJson());
}
}
if ($app === null) {
return null;
}
$classname = preg_replace('/[^\p{L}\p{N}]/u', '', $app->name);
$app->class = '\App\SupportedApps\\'.$classname.'\\'.$classname;
return $app;
}
/**
* @return array
*/
public static function applist(): array
{
$list = [];
$list['null'] = 'None';
$apps = self::apps();
foreach ($apps as $app) {
$list[$app->appid] = $app->name;
}
// Check for private apps in the db
$appsListFromDB = self::all(['appid', 'name']);
foreach ($appsListFromDB as $app) {
// Already existing keys are overwritten,
// only private apps should be added at the end
$list[$app->appid] = $app->name;
}
return $list;
}
}