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:
bbx0 2024-02-23 17:56:32 +01:00 committed by GitHub
parent 4d8f08eddd
commit d2a878e98e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -109,9 +109,27 @@ class FilesApiController extends BaseApiController
}
$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);
}