mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 09:39:57 +00:00
Stream uploads to file (#2477)
* Stream uploads to file * Adapt code style / use up to date reasonable chunk size --------- Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
parent
4d8f08eddd
commit
d2a878e98e
@ -109,9 +109,27 @@ class FilesApiController extends BaseApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$fileName = $this->checkFileName($args['fileName']);
|
$fileName = $this->checkFileName($args['fileName']);
|
||||||
$data = $request->getBody()->getContents();
|
|
||||||
|
|
||||||
file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data);
|
$fileHandle = fopen($this->getFilesService()->GetFilePath($args['group'], $fileName), 'xb');
|
||||||
|
if($fileHandle === false)
|
||||||
|
{
|
||||||
|
throw new \Exception("Error while creating file $fileName");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the file to disk in chunks of 1 MB
|
||||||
|
$requestBody = $request->getBody();
|
||||||
|
while ($data = $requestBody->read(1048576))
|
||||||
|
{
|
||||||
|
if (fwrite($fileHandle, $data) === false)
|
||||||
|
{
|
||||||
|
throw new \Exception("Error while writing file $fileName");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fclose($fileHandle) === false)
|
||||||
|
{
|
||||||
|
throw new \Exception("Error while closing file $fileName");
|
||||||
|
}
|
||||||
|
|
||||||
return $this->EmptyApiResponse($response);
|
return $this->EmptyApiResponse($response);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user