|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\FileBrowser;
|
4 | 4 |
|
5 |
| -class FileBrowser extends \App\SupportedApps |
| 5 | +class FileBrowser extends \App\SupportedApps implements \App\EnhancedApps |
6 | 6 | {
|
| 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 | + } |
7 | 101 | }
|
0 commit comments