refactoring

This commit is contained in:
Milos Stojanovic
2019-06-14 12:46:45 +02:00
parent 667389ab1a
commit f6185c92ab
14 changed files with 1019 additions and 34 deletions

View File

@@ -33,7 +33,7 @@ class App
$container->get($key)->init(isset($service['config']) ? $service['config'] : []); $container->get($key)->init(isset($service['config']) ? $service['config'] : []);
} }
$response->send($request); $response->send();
$this->container = $container; $this->container = $container;
} }

View File

@@ -46,7 +46,7 @@ class DownloadController
public function download($path_encoded, Request $request, Response $response, StreamedResponse $streamedResponse) public function download($path_encoded, Request $request, Response $response, StreamedResponse $streamedResponse)
{ {
try { try {
$file = $this->storage->readStream(base64_decode($path_encoded)); $file = $this->storage->readStream((string) base64_decode($path_encoded));
} catch (\Exception $e) { } catch (\Exception $e) {
return $response->redirect('/'); return $response->redirect('/');
} }
@@ -101,7 +101,7 @@ class DownloadController
public function batchDownloadStart(Request $request, StreamedResponse $streamedResponse, TmpfsInterface $tmpfs) public function batchDownloadStart(Request $request, StreamedResponse $streamedResponse, TmpfsInterface $tmpfs)
{ {
$uniqid = preg_replace('/[^0-9a-zA-Z_]/', '', $request->input('uniqid')); $uniqid = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('uniqid'));
$streamedResponse->setCallback(function () use ($tmpfs, $uniqid) { $streamedResponse->setCallback(function () use ($tmpfs, $uniqid) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart

View File

@@ -42,7 +42,7 @@ class UploadController
public function chunkCheck(Request $request, Response $response) public function chunkCheck(Request $request, Response $response)
{ {
$file_name = $request->input('resumableFilename', 'file'); $file_name = $request->input('resumableFilename', 'file');
$identifier = preg_replace('/[^0-9a-zA-Z_]/', '', $request->input('resumableIdentifier')); $identifier = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('resumableIdentifier'));
$chunk_number = (int) $request->input('resumableChunkNumber'); $chunk_number = (int) $request->input('resumableChunkNumber');
$chunk_file = 'multipart_'.$identifier.$file_name.'.part'.$chunk_number; $chunk_file = 'multipart_'.$identifier.$file_name.'.part'.$chunk_number;
@@ -61,7 +61,7 @@ class UploadController
$chunk_number = (int) $request->input('resumableChunkNumber'); $chunk_number = (int) $request->input('resumableChunkNumber');
$total_chunks = (int) $request->input('resumableTotalChunks'); $total_chunks = (int) $request->input('resumableTotalChunks');
$total_size = (int) $request->input('resumableTotalSize'); $total_size = (int) $request->input('resumableTotalSize');
$identifier = preg_replace('/[^0-9a-zA-Z_]/', '', $request->input('resumableIdentifier')); $identifier = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('resumableIdentifier'));
$file = $request->files->get('file'); $file = $request->files->get('file');

View File

@@ -21,7 +21,7 @@ class Request extends SymfonyRequest
// then look into JSON content, fallback to default // then look into JSON content, fallback to default
if ($value === null) { if ($value === null) {
$content = json_decode($this->getContent()); $content = json_decode((string) $this->getContent());
$value = isset($content->{$key}) ? $content->{$key} : $default; $value = isset($content->{$key}) ? $content->{$key} : $default;
} }
@@ -33,7 +33,7 @@ class Request extends SymfonyRequest
$params = []; $params = [];
// first look into JSON content // first look into JSON content
$content = json_decode($this->getContent()); $content = json_decode((string) $this->getContent());
if (! empty($content)) { if (! empty($content)) {
foreach ($content as $key => $param) { foreach ($content as $key => $param) {
$params[$key] = $param; $params[$key] = $param;

View File

@@ -96,7 +96,7 @@ class ZipArchiver implements Service, ArchiverInterface
foreach ($contents as $item) { foreach ($contents as $item) {
$stream = $archive->readStream($item['path']); $stream = $archive->readStream($item['path']);
if ($item['type'] == 'dir') { if ($item['type'] == 'dir') {
$storage->createDir($destination, $item['path'], $stream); $storage->createDir($destination, $item['path']);
} }
if ($item['type'] == 'file') { if ($item['type'] == 'file') {
$storage->store($destination.'/'.$item['dirname'], $item['basename'], $stream); $storage->store($destination.'/'.$item['dirname'], $item['basename'], $stream);

View File

@@ -29,6 +29,8 @@ class Database implements Service, AuthInterface
protected $session; protected $session;
protected $conn;
public function __construct(Session $session) public function __construct(Session $session)
{ {
$this->session = $session; $this->session = $session;
@@ -174,11 +176,11 @@ class Database implements Service, AuthInterface
{ {
$new = new User(); $new = new User();
$new->setRole($user->role); $new->setRole(isset($user->role) ? $user->role : 'guest');
$new->setHomedir($user->homedir); $new->setHomedir(isset($user->homedir) ? $user->homedir : '/');
$new->setPermissions($user->permissions, true); $new->setPermissions(isset($user->permissions) ? $user->permissions : '', true);
$new->setUsername($user->username); $new->setUsername(isset($user->username) ? $user->username : '');
$new->setName($user->name); $new->setName(isset($user->name) ? $user->name : 'Guest');
return $new; return $new;
} }

View File

@@ -189,7 +189,7 @@ class JsonFile implements Service, AuthInterface
protected function getUsers(): array protected function getUsers(): array
{ {
$users = json_decode(file_get_contents($this->file), true); $users = json_decode((string) file_get_contents($this->file), true);
return is_array($users) ? $users : []; return is_array($users) ? $users : [];
} }

View File

@@ -32,7 +32,7 @@ class MonoLogger implements Service, LoggerInterface
$handler->registerFatalHandler(); $handler->registerFatalHandler();
} }
public function log(string $message, string $level = Logger::INFO) public function log(string $message, int $level = Logger::INFO)
{ {
$this->logger->log($level, $message); $this->logger->log($level, $message);
} }

View File

@@ -12,5 +12,5 @@ namespace Filegator\Services\Logger;
interface LoggerInterface interface LoggerInterface
{ {
public function log(string $message, string $level); public function log(string $message, int $level);
} }

View File

@@ -33,6 +33,8 @@ class Router implements Service
protected $container; protected $container;
protected $user;
public function __construct(Request $request, AuthInterface $auth, Container $container) public function __construct(Request $request, AuthInterface $auth, Container $container)
{ {
$this->request = $request; $this->request = $request;

View File

@@ -40,22 +40,22 @@ class SessionStorage implements Service, SessionStorageInterface
public function save() public function save()
{ {
$this->getSession()->save(); return $this->getSession() !== null ? $this->getSession()->save() : false;
} }
public function set(string $key, $data) public function set(string $key, $data)
{ {
return $this->getSession()->set($key, $data); return $this->getSession() !== null ? $this->getSession()->set($key, $data) : false;
} }
public function get(string $key, $default = null) public function get(string $key, $default = null)
{ {
return $this->getSession() ? $this->getSession()->get($key, $default) : $default; return $this->getSession() !== null ? $this->getSession()->get($key, $default) : $default;
} }
public function invalidate() public function invalidate()
{ {
if (! $this->getSession()->isStarted()) { if ($this->getSession() !== null || ! $this->getSession()->isStarted()) {
$this->getSession()->start(); $this->getSession()->start();
} }

View File

@@ -54,7 +54,7 @@ class Tmpfs implements Service, TmpfsInterface
{ {
$filename = $this->sanitizeFilename($filename); $filename = $this->sanitizeFilename($filename);
return file_get_contents($this->getPath().$filename); return (string) file_get_contents($this->getPath().$filename);
} }
public function readStream(string $filename): array public function readStream(string $filename): array
@@ -79,14 +79,16 @@ class Tmpfs implements Service, TmpfsInterface
public function findAll($pattern): array public function findAll($pattern): array
{ {
$files = []; $files = [];
$matches = glob($this->getPath().$pattern);
foreach (glob($this->getPath().$pattern) as $filename) { if (! empty($matches)) {
if (is_file($filename)) { foreach ($matches as $filename) {
$files[] = [ if (is_file($filename)) {
'name' => basename($filename), $files[] = [
'size' => filesize($filename), 'name' => basename($filename),
'time' => filemtime($filename), 'size' => filesize($filename),
]; 'time' => filemtime($filename),
];
}
} }
} }
@@ -117,7 +119,7 @@ class Tmpfs implements Service, TmpfsInterface
private function sanitizeFilename($filename) private function sanitizeFilename($filename)
{ {
$filename = preg_replace( $filename = (string) preg_replace(
'~ '~
[<>:"/\\|?*]| # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words [<>:"/\\|?*]| # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
[\x00-\x1F]| # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx [\x00-\x1F]| # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
@@ -125,12 +127,19 @@ class Tmpfs implements Service, TmpfsInterface
[;\\\{}^\~`] # other non-safe [;\\\{}^\~`] # other non-safe
~x', ~x',
'-', '-',
$filename (string) $filename
); );
// maximize filename length to 255 bytes http://serverfault.com/a/9548/44086 // maximize filename length to 255 bytes http://serverfault.com/a/9548/44086
$ext = pathinfo($filename, PATHINFO_EXTENSION); $ext = pathinfo($filename, PATHINFO_EXTENSION);
return mb_strcut(pathinfo($filename, PATHINFO_FILENAME), 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename)).($ext ? '.'.$ext : ''); return mb_strcut(
pathinfo($filename, PATHINFO_FILENAME),
0,
255 - ($ext ? strlen($ext) + 1 : 0),
(string) mb_detect_encoding($filename)
).(
$ext ? '.'.$ext : ''
);
} }
} }

View File

@@ -34,6 +34,7 @@
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7.5", "phpunit/phpunit": "^7.5",
"symfony/var-dumper": "^4.2", "symfony/var-dumper": "^4.2",
"league/flysystem-memory": "^1.0" "league/flysystem-memory": "^1.0",
"phpstan/phpstan": "^0.11.8"
} }
} }

973
composer.lock generated

File diff suppressed because it is too large Load Diff