Skip to content

Commit 5e5c110

Browse files
authored
Make FileBrowser Enhanced App (#740)
* Make FileBrowser Enhanced App * Remove initial values for FileBrowser data * Dynamic unit for FileBrowser * Fix lint error
1 parent 404f948 commit 5e5c110

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

FileBrowser/FileBrowser.php

+95-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,100 @@
22

33
namespace App\SupportedApps\FileBrowser;
44

5-
class FileBrowser extends \App\SupportedApps
5+
class FileBrowser extends \App\SupportedApps implements \App\EnhancedApps
66
{
7+
public $config;
8+
9+
//protected $login_first = true; // Uncomment if api requests need to be authed first
10+
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
11+
12+
public function __construct()
13+
{
14+
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
15+
}
16+
17+
private function getToken()
18+
{
19+
$username = $this->config->username;
20+
$password = $this->config->password;
21+
$authHeader = $this->config->authHeader;
22+
23+
$attrs = [
24+
"headers" => [
25+
"Content-Type" => "application/json"
26+
],
27+
"body" => json_encode([
28+
"username" => $username,
29+
"password" => $password
30+
])
31+
];
32+
if ($authHeader !== null) {
33+
$attrs["headers"][$authHeader] = $username;
34+
}
35+
36+
$res = parent::execute($this->url("api/login"), $attrs, null, "POST");
37+
38+
switch ($res->getStatusCode()) {
39+
case 200:
40+
return $res->getBody()->getContents();
41+
case 403:
42+
throw new \Exception("Invalid username/password");
43+
default:
44+
throw new \Exception("Could not connect to FileBrowser");
45+
}
46+
}
47+
48+
private function formatBytes($bytes, $precision = 2) {
49+
$units = array('B', 'KB', 'MB', 'GB', 'TB');
50+
$bytes = max($bytes, 0);
51+
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
52+
$pow = min($pow, count($units) - 1);
53+
54+
$bytes /= pow(1024, $pow);
55+
56+
$value = round($bytes, $precision);
57+
$unit = $units[$pow];
58+
59+
return [
60+
"value" => $value,
61+
"unit" => $unit
62+
];
63+
}
64+
65+
public function test()
66+
{
67+
try {
68+
$token = $this->getToken();
69+
echo "Successfully communicated with the API";
70+
} catch (Exception $err) {
71+
echo $err->getMessage();
72+
}
73+
}
74+
75+
public function livestats()
76+
{
77+
$status = "inactive";
78+
79+
$token = $this->getToken();
80+
$attrs = [
81+
"headers" => [
82+
"X-AUTH" => $token
83+
],
84+
];
85+
$res = parent::execute($this->url("api/usage"), $attrs);
86+
$details = json_decode($res->getBody());
87+
88+
if ($details != null) {
89+
$data["used"] = $this->formatBytes($details->used);
90+
$data["total"] = $this->formatBytes($details->total);
91+
}
92+
return parent::getLiveStats($status, $data);
93+
}
94+
95+
public function url($endpoint)
96+
{
97+
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
98+
99+
return $api_url;
100+
}
7101
}

FileBrowser/app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"website": "https://github.com/filebrowser/filebrowser",
55
"license": "Apache License 2.0",
66
"description": "filebrowser provides a file managing interface within a specified directory and it can be used to upload, delete, preview, rename and edit your files. It allows the creation of multiple users and each user can have its own directory. It can be used as a standalone app or as a middleware.",
7-
"enhanced": false,
7+
"enhanced": true,
88
"tile_background": "dark",
99
"icon": "filebrowser.svg"
1010
}

FileBrowser/config.blade.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items" style="flex-direction:column">
3+
<div style="display:flex;flex-direction:row;">
4+
<div class="input">
5+
<label>{{ strtoupper(__('app.url')) }}</label>
6+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
7+
</div>
8+
</div>
9+
<div style="display:flex;flex-direction:row;">
10+
<div class="input">
11+
<label>{{ __('app.apps.username') }}</label>
12+
{!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, ['placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item']) !!}
13+
</div>
14+
<div class="input">
15+
<label>{{ __('app.apps.password') }}</label>
16+
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
17+
</div>
18+
</div>
19+
<div style="display:flex;flex-direction:row;">
20+
<div class="input">
21+
<label>Proxy Header (<a href="https://filebrowser.org/configuration/authentication-method#proxy-header" target="_blank">help?</a>)</label>
22+
{!! Form::text('config[authHeader]', isset($item) ? $item->getconfig()->authHeader : null, ['placeholder' => 'X-My-Header', 'data-config' => 'authHeader', 'class' => 'form-control config-item']) !!}
23+
<small>Also fill {{ __('app.apps.username') }}</small>
24+
</div>
25+
</div>
26+
<div class="input">
27+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
28+
</div>
29+
</div>

FileBrowser/livestats.blade.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<ul class="livestats">
2+
<li>
3+
<span class="title">Used</span>
4+
<strong>{!! $used["value"] !!}<span>{!! $used["unit"] !!}</span></strong>
5+
</li>
6+
<li>
7+
<span class="title">Total</span>
8+
<strong>{!! $total["value"] !!}<span>{!! $total["unit"] !!}</span></strong>
9+
</li>
10+
</ul>

0 commit comments

Comments
 (0)