mirror of
https://github.com/grocy/grocy.git
synced 2025-08-18 03:25:50 +00:00
Applied PHP formatting rules
This commit is contained in:
@@ -2,24 +2,30 @@
|
||||
|
||||
namespace Grocy\Controllers;
|
||||
|
||||
use \Grocy\Services\FilesService;
|
||||
use Grocy\Services\FilesService;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
|
||||
class FilesApiController extends BaseApiController
|
||||
{
|
||||
public function __construct(\DI\Container $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
public function UploadFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
public function DeleteFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
try
|
||||
{
|
||||
$fileName = $this->checkFileName($args['fileName']);
|
||||
if (IsValidFileName(base64_decode($args['fileName'])))
|
||||
{
|
||||
$fileName = base64_decode($args['fileName']);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
|
||||
$data = $request->getBody()->getContents();
|
||||
file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data);
|
||||
$filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
|
||||
|
||||
if (file_exists($filePath))
|
||||
{
|
||||
unlink($filePath);
|
||||
}
|
||||
|
||||
return $this->EmptyApiResponse($response);
|
||||
}
|
||||
@@ -27,6 +33,7 @@ class FilesApiController extends BaseApiController
|
||||
{
|
||||
return $this->GenericErrorResponse($response, $ex->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function ServeFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
@@ -46,13 +53,15 @@ class FilesApiController extends BaseApiController
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpNotFoundException($request, 'File not found');
|
||||
throw new HttpNotFoundException($request, 'File not found');
|
||||
}
|
||||
|
||||
}
|
||||
catch (\Exception $ex)
|
||||
{
|
||||
throw new HttpNotFoundException($request, $ex->getMessage(), $ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function ShowFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
@@ -75,31 +84,23 @@ class FilesApiController extends BaseApiController
|
||||
{
|
||||
throw new HttpNotFoundException($request, 'File not found');
|
||||
}
|
||||
|
||||
}
|
||||
catch (\Exception $ex)
|
||||
{
|
||||
throw new HttpNotFoundException($request, $ex->getMessage(), $ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function DeleteFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
public function UploadFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsValidFileName(base64_decode($args['fileName'])))
|
||||
{
|
||||
$fileName = base64_decode($args['fileName']);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
$fileName = $this->checkFileName($args['fileName']);
|
||||
|
||||
$filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
|
||||
if (file_exists($filePath))
|
||||
{
|
||||
unlink($filePath);
|
||||
}
|
||||
$data = $request->getBody()->getContents();
|
||||
file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data);
|
||||
|
||||
return $this->EmptyApiResponse($response);
|
||||
}
|
||||
@@ -107,6 +108,31 @@ class FilesApiController extends BaseApiController
|
||||
{
|
||||
return $this->GenericErrorResponse($response, $ex->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function __construct(\DI\Container $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName base64-encoded file-name
|
||||
* @return false|string the decoded file-name
|
||||
* @throws \Exception if the file-name is invalid.
|
||||
*/
|
||||
protected function checkFileName(string $fileName)
|
||||
{
|
||||
if (IsValidFileName(base64_decode($fileName)))
|
||||
{
|
||||
$fileName = base64_decode($fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,40 +144,36 @@ class FilesApiController extends BaseApiController
|
||||
protected function getFilePath(string $group, string $fileName, array $queryParams = [])
|
||||
{
|
||||
$forceServeAs = null;
|
||||
if (isset($queryParams['force_serve_as']) && !empty($queryParams['force_serve_as'])) {
|
||||
|
||||
if (isset($queryParams['force_serve_as']) && !empty($queryParams['force_serve_as']))
|
||||
{
|
||||
$forceServeAs = $queryParams['force_serve_as'];
|
||||
}
|
||||
|
||||
if ($forceServeAs == FilesService::FILE_SERVE_TYPE_PICTURE) {
|
||||
if ($forceServeAs == FilesService::FILE_SERVE_TYPE_PICTURE)
|
||||
{
|
||||
$bestFitHeight = null;
|
||||
if (isset($queryParams['best_fit_height']) && !empty($queryParams['best_fit_height']) && is_numeric($queryParams['best_fit_height'])) {
|
||||
|
||||
if (isset($queryParams['best_fit_height']) && !empty($queryParams['best_fit_height']) && is_numeric($queryParams['best_fit_height']))
|
||||
{
|
||||
$bestFitHeight = $queryParams['best_fit_height'];
|
||||
}
|
||||
|
||||
$bestFitWidth = null;
|
||||
if (isset($queryParams['best_fit_width']) && !empty($queryParams['best_fit_width']) && is_numeric($queryParams['best_fit_width'])) {
|
||||
|
||||
if (isset($queryParams['best_fit_width']) && !empty($queryParams['best_fit_width']) && is_numeric($queryParams['best_fit_width']))
|
||||
{
|
||||
$bestFitWidth = $queryParams['best_fit_width'];
|
||||
}
|
||||
|
||||
$filePath = $this->getFilesService()->DownscaleImage($group, $fileName, $bestFitHeight, $bestFitWidth);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
$filePath = $this->getFilesService()->GetFilePath($group, $fileName);
|
||||
}
|
||||
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName base64-encoded file-name
|
||||
* @return false|string the decoded file-name
|
||||
* @throws \Exception if the file-name is invalid.
|
||||
*/
|
||||
protected function checkFileName(string $fileName)
|
||||
{
|
||||
if (IsValidFileName(base64_decode($fileName))) {
|
||||
$fileName = base64_decode($fileName);
|
||||
} else {
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
return $fileName;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user