|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * This software is licensed under the terms of the MIT license. |
| 7 | + * https://opensource.org/licenses/MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Pterodactyl\Repositories\Wings; |
| 11 | + |
| 12 | +use Webmozart\Assert\Assert; |
| 13 | +use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; |
| 14 | + |
| 15 | +class FileRepository extends BaseRepository implements FileRepositoryInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * {@inheritdoc} |
| 19 | + */ |
| 20 | + public function getFileStat($path) |
| 21 | + { |
| 22 | + Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.'); |
| 23 | + |
| 24 | + $file = pathinfo($path); |
| 25 | + $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; |
| 26 | + |
| 27 | + $response = $this->getHttpClient()->request('GET', sprintf( |
| 28 | + '/server/' . $this->getAccessServer() . '/file/stat/%s', |
| 29 | + rawurlencode($file['dirname'] . $file['basename']) |
| 30 | + )); |
| 31 | + |
| 32 | + return json_decode($response->getBody()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + public function getContent($path) |
| 39 | + { |
| 40 | + Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.'); |
| 41 | + |
| 42 | + $file = pathinfo($path); |
| 43 | + $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; |
| 44 | + |
| 45 | + $response = $this->getHttpClient()->request('GET', sprintf( |
| 46 | + '/server/' . $this->getAccessServer() . '/file/f/%s', |
| 47 | + rawurlencode($file['dirname'] . $file['basename']) |
| 48 | + )); |
| 49 | + |
| 50 | + return object_get(json_decode($response->getBody()), 'content'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public function putContent($path, $content) |
| 57 | + { |
| 58 | + Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.'); |
| 59 | + Assert::string($content, 'Second argument passed to putContent must be a string, received %s.'); |
| 60 | + |
| 61 | + $file = pathinfo($path); |
| 62 | + $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; |
| 63 | + |
| 64 | + return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/file/save', [ |
| 65 | + 'json' => [ |
| 66 | + 'path' => rawurlencode($file['dirname'] . $file['basename']), |
| 67 | + 'content' => $content, |
| 68 | + ], |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + public function getDirectory($path) |
| 76 | + { |
| 77 | + Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.'); |
| 78 | + |
| 79 | + $response = $this->getHttpClient()->request('GET', sprintf( |
| 80 | + '/server/' . $this->getAccessServer() . '/directory/%s', |
| 81 | + rawurlencode($path) |
| 82 | + )); |
| 83 | + |
| 84 | + $contents = json_decode($response->getBody()); |
| 85 | + $files = []; |
| 86 | + $folders = []; |
| 87 | + |
| 88 | + foreach ($contents as $value) { |
| 89 | + if ($value->directory) { |
| 90 | + array_push($folders, [ |
| 91 | + 'entry' => $value->name, |
| 92 | + 'directory' => trim($path, '/'), |
| 93 | + 'size' => null, |
| 94 | + 'date' => strtotime($value->modified), |
| 95 | + 'mime' => $value->mime, |
| 96 | + ]); |
| 97 | + } elseif ($value->file) { |
| 98 | + array_push($files, [ |
| 99 | + 'entry' => $value->name, |
| 100 | + 'directory' => trim($path, '/'), |
| 101 | + 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), |
| 102 | + 'size' => human_readable($value->size), |
| 103 | + 'date' => strtotime($value->modified), |
| 104 | + 'mime' => $value->mime, |
| 105 | + ]); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return [ |
| 110 | + 'files' => $files, |
| 111 | + 'folders' => $folders, |
| 112 | + ]; |
| 113 | + } |
| 114 | +} |
0 commit comments