mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 09:39:57 +00:00
Prepare file upload API (references #58)
This commit is contained in:
parent
596dc9e36d
commit
9c2c2c1fa2
38
controllers/FilesApiController.php
Normal file
38
controllers/FilesApiController.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Grocy\Controllers;
|
||||||
|
|
||||||
|
use \Grocy\Services\FilesService;
|
||||||
|
|
||||||
|
class FilesApiController extends BaseApiController
|
||||||
|
{
|
||||||
|
public function __construct(\Slim\Container $container)
|
||||||
|
{
|
||||||
|
parent::__construct($container);
|
||||||
|
$this->FilesService = new FilesService();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected $FilesService;
|
||||||
|
|
||||||
|
public function Upload(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (isset($request->getQueryParams()['file_name']) && !empty($request->getQueryParams()['file_name']) && IsValidFileName($request->getQueryParams()['file_name']))
|
||||||
|
{
|
||||||
|
$fileName = $request->getQueryParams()['file_name'];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new \Exception('file_name query parameter missing or contains an invalid filename');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $request->getBody()->getContents();
|
||||||
|
file_put_contents($this->FilesService->GetFilePath($args['group'], $fileName), $data);
|
||||||
|
}
|
||||||
|
catch (\Exception $ex)
|
||||||
|
{
|
||||||
|
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -390,6 +390,66 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/files/upload/{group}": {
|
||||||
|
"post": {
|
||||||
|
"description": "Uploads a single file to /data/storage/{group}/{file_name}",
|
||||||
|
"tags": [
|
||||||
|
"Files"
|
||||||
|
],
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"in": "path",
|
||||||
|
"name": "group",
|
||||||
|
"required": true,
|
||||||
|
"description": "The file group",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"in": "query",
|
||||||
|
"name": "file_name",
|
||||||
|
"required": true,
|
||||||
|
"description": "The file name (including extension)",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/octet-stream": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "binary"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A VoidApiActionResponse object",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/VoidApiActionResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "A VoidApiActionResponse object",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/ErrorExampleVoidApiActionResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/users/get": {
|
"/users/get": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Returns all users",
|
"description": "Returns all users",
|
||||||
|
@ -178,3 +178,13 @@ function Pluralize($number, $singularForm, $pluralForm)
|
|||||||
}
|
}
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function IsValidFileName($fileName)
|
||||||
|
{
|
||||||
|
if(preg_match('#^[a-z0-9]+\.[a-z]+?$#i', $fileName))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@ -82,6 +82,9 @@ $app->group('/api', function()
|
|||||||
// System
|
// System
|
||||||
$this->get('/system/get-db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime');
|
$this->get('/system/get-db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime');
|
||||||
|
|
||||||
|
// Files
|
||||||
|
$this->post('/files/upload/{group}', '\Grocy\Controllers\FilesApiController:Upload');
|
||||||
|
|
||||||
// Users
|
// Users
|
||||||
$this->get('/users/get', '\Grocy\Controllers\UsersApiController:GetUsers');
|
$this->get('/users/get', '\Grocy\Controllers\UsersApiController:GetUsers');
|
||||||
$this->post('/users/create', '\Grocy\Controllers\UsersApiController:CreateUser');
|
$this->post('/users/create', '\Grocy\Controllers\UsersApiController:CreateUser');
|
||||||
|
31
services/FilesService.php
Normal file
31
services/FilesService.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Grocy\Services;
|
||||||
|
|
||||||
|
class FilesService extends BaseService
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->StoragePath = GROCY_DATAPATH . '/storage';
|
||||||
|
|
||||||
|
if (!file_exists($this->StoragePath))
|
||||||
|
{
|
||||||
|
mkdir($this->StoragePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private $StoragePath;
|
||||||
|
|
||||||
|
public function GetFilePath($group, $fileName)
|
||||||
|
{
|
||||||
|
$groupFolderPath = $this->StoragePath . '/' . $group;
|
||||||
|
if (!file_exists($groupFolderPath))
|
||||||
|
{
|
||||||
|
mkdir($groupFolderPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $groupFolderPath . '/' . $fileName;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user