mirror of
https://github.com/grocy/grocy.git
synced 2025-08-15 10:14:39 +00:00
Add user-field-type "file" (#977)
* Add user-field-type "file" * Add userfield-type "picture" * Also limit image height on userfieldsform * Prevent empty userfields (cause warnings in tables after deleting a file) * Show files in dialogs Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
committed by
GitHub
parent
318db53818
commit
07beee93a9
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace Grocy\Controllers;
|
||||
|
||||
use Grocy\Controllers\Users\User;
|
||||
use \Grocy\Services\FilesService;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
|
||||
class FilesApiController extends BaseApiController
|
||||
{
|
||||
@@ -16,14 +16,7 @@ class FilesApiController extends BaseApiController
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsValidFileName(base64_decode($args['fileName'])))
|
||||
{
|
||||
$fileName = base64_decode($args['fileName']);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
$fileName = $this->checkFileName($args['fileName']);
|
||||
|
||||
$data = $request->getBody()->getContents();
|
||||
file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data);
|
||||
@@ -40,41 +33,9 @@ class FilesApiController extends BaseApiController
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsValidFileName(base64_decode($args['fileName'])))
|
||||
{
|
||||
$fileName = base64_decode($args['fileName']);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('Invalid filename');
|
||||
}
|
||||
$fileName = $this->checkFileName($args['fileName']);
|
||||
|
||||
$forceServeAs = null;
|
||||
if (isset($request->getQueryParams()['force_serve_as']) && !empty($request->getQueryParams()['force_serve_as']))
|
||||
{
|
||||
$forceServeAs = $request->getQueryParams()['force_serve_as'];
|
||||
}
|
||||
|
||||
if ($forceServeAs == FilesService::FILE_SERVE_TYPE_PICTURE)
|
||||
{
|
||||
$bestFitHeight = null;
|
||||
if (isset($request->getQueryParams()['best_fit_height']) && !empty($request->getQueryParams()['best_fit_height']) && is_numeric($request->getQueryParams()['best_fit_height']))
|
||||
{
|
||||
$bestFitHeight = $request->getQueryParams()['best_fit_height'];
|
||||
}
|
||||
|
||||
$bestFitWidth = null;
|
||||
if (isset($request->getQueryParams()['best_fit_width']) && !empty($request->getQueryParams()['best_fit_width']) && is_numeric($request->getQueryParams()['best_fit_width']))
|
||||
{
|
||||
$bestFitWidth = $request->getQueryParams()['best_fit_width'];
|
||||
}
|
||||
|
||||
$filePath = $this->getFilesService()->DownscaleImage($args['group'], $fileName, $bestFitHeight, $bestFitWidth);
|
||||
}
|
||||
else
|
||||
{
|
||||
$filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
|
||||
}
|
||||
$filePath = $this->getFilePath($args['group'], $fileName, $request->getQueryParams());
|
||||
|
||||
if (file_exists($filePath))
|
||||
{
|
||||
@@ -85,12 +46,39 @@ class FilesApiController extends BaseApiController
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->GenericErrorResponse($response, 'File not found', 404);
|
||||
throw new HttpNotFoundException($request, 'File not found');
|
||||
}
|
||||
}
|
||||
catch (\Exception $ex)
|
||||
{
|
||||
return $this->GenericErrorResponse($response, $ex->getMessage());
|
||||
throw new HttpNotFoundException($request, $ex->getMessage(), $ex);
|
||||
}
|
||||
}
|
||||
|
||||
public function ShowFile(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
try
|
||||
{
|
||||
$fileInfo = explode('_', $args['fileName']);
|
||||
$fileName = $this->checkFileName($fileInfo[1]);
|
||||
|
||||
$filePath = $this->getFilePath($args['group'], base64_decode($fileInfo[0]), $request->getQueryParams());
|
||||
|
||||
if (file_exists($filePath))
|
||||
{
|
||||
$response->write(file_get_contents($filePath));
|
||||
$response = $response->withHeader('Cache-Control', 'max-age=2592000');
|
||||
$response = $response->withHeader('Content-Type', mime_content_type($filePath));
|
||||
return $response->withHeader('Content-Disposition', 'inline; filename="' . $fileName . '"');
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpNotFoundException($request, 'File not found');
|
||||
}
|
||||
}
|
||||
catch (\Exception $ex)
|
||||
{
|
||||
throw new HttpNotFoundException($request, $ex->getMessage(), $ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,4 +108,50 @@ class FilesApiController extends BaseApiController
|
||||
return $this->GenericErrorResponse($response, $ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $group The group the requested files belongs to.
|
||||
* @param string $fileName The name of the requested file.
|
||||
* @param array $queryParams Parameter, e.g. for scaling. Optional.
|
||||
* @return string
|
||||
*/
|
||||
protected function getFilePath(string $group, string $fileName, array $queryParams = [])
|
||||
{
|
||||
$forceServeAs = null;
|
||||
if (isset($queryParams['force_serve_as']) && !empty($queryParams['force_serve_as'])) {
|
||||
$forceServeAs = $queryParams['force_serve_as'];
|
||||
}
|
||||
|
||||
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'])) {
|
||||
$bestFitHeight = $queryParams['best_fit_height'];
|
||||
}
|
||||
|
||||
$bestFitWidth = null;
|
||||
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 {
|
||||
$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