/**
* Handle Image Upload
*
* @throws \Exception
*
* @return ApiFileService
*/
private function handleImageUpload()
{
try {
$config = new FlowConfig();
$request = new FlowRequest();
$basic = new Basic();
$config->setTempDir(storage_path() . '/tmp');
$config->setDeleteChunksOnSave(false);
$totalSize = $request->getTotalSize();
$uploadFile = $request->getFile();
$fileName = md5($request->getFileName());
$extension = explode('.', $request->getFileName())[1];
$extraNumber = 1;
if ($totalSize && $totalSize > 1024 * 1024 * 4) {
throw new \Exception('File size exceeds 4MB', 400);
}
while ($this->isNameDuplicated($fileName . '.' . $extension, $this->location)) {
$fileName = $fileName . $extraNumber;
++$extraNumber;
}
$fileName = $fileName . '.' . $extension;
if ($basic->save($this->directory . $this->location . $fileName, $config, $request)) {
$file = $this->handleImageSave($fileName, $this->location);
return $file;
}
throw new \Exception('unable to save file', 500);
} catch (\Exception $e) {
throw new \Exception($e->getMessage(), $e->getCode());
}
}