/** * Returns a listing of files in a given directory for a server. * * @param \Illuminate\Http\Request $request * @param string $uuid` * @return \Illuminate\Contracts\View\View */ public function postDirectoryList(Request $request, $uuid) { $server = Models\Server::getByUUID($uuid); $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); $this->authorize('list-files', $server); $prevDir = ['header' => $this->directory !== '/' ? $this->directory : '']; if ($this->directory !== '/') { $prevDir['first'] = true; } // Determine if we should show back links in the file browser. // This code is strange, and could probably be rewritten much better. $goBack = explode('/', rtrim($this->directory, '/')); if (isset($goBack[2]) && !empty($goBack[2])) { $prevDir['show'] = true; $prevDir['link'] = '/' . trim(str_replace(end($goBack), '', $this->directory), '/'); $prevDir['link_show'] = trim($prevDir['link'], '/'); } $controller = new Repositories\Daemon\FileRepository($uuid); try { $directoryContents = $controller->returnDirectoryListing($this->directory); } catch (DisplayException $ex) { return response($ex->getMessage(), 500); } catch (\Exception $ex) { Log::error($ex); return response('An error occured while attempting to load the requested directory, please try again.', 500); } return view('server.files.list', ['server' => $server, 'files' => $directoryContents->files, 'folders' => $directoryContents->folders, 'extensions' => Repositories\HelperRepository::editableFiles(), 'directory' => $prevDir]); }
/** * Returns a listing of all files and folders within a specified Scales directory. * * @param string $directory * @return object */ public function returnDirectoryListing($directory) { if (empty($directory)) { throw new Exception('A valid directory must be specified in order to list its contents.'); } $res = $this->client->request('GET', '/server/directory/' . rawurlencode($directory), ['headers' => $this->headers]); $json = json_decode($res->getBody()); if ($res->getStatusCode() !== 200) { throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody()); } // Iterate through results $files = []; $folders = []; foreach ($json as &$value) { if ($value->directory === true) { // @TODO Handle Symlinks $folders = array_merge($folders, [['entry' => $value->name, 'directory' => trim($directory, '/'), 'size' => null, 'date' => strtotime($value->modified)]]); } else { if ($value->file === true) { $files = array_merge($files, [['entry' => $value->name, 'directory' => trim($directory, '/'), 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), 'size' => HelperRepository::bytesToHuman($value->size), 'date' => strtotime($value->modified)]]); } } } return (object) ['files' => $files, 'folders' => $folders]; }