/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $id = $this->input->get('id'); $author = Author::getAuthor($id); $user = User::get(); $blog = Blog::get(); try { if (!Author::isAdmin($blog, $user)) { throw new ValidFailException('You cannot edit this author.'); } $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $uuid = $author->uuid ?: Uuid::v4(); $src = Thumb::createThumb($src); $dest = sprintf('author/%s/%s.%s', sha1($uuid), md5($uuid), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['uuid'] = $uuid; if ($author->id) { $author->image = $return['filename']; (new DataMapper('authors'))->updateOne($author); } $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $user = User::get(); try { $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $src = Thumb::createThumb($src); $dest = sprintf('user/%s/%s.%s', sha1('user-profile-' . $user->id), md5('user-profile-' . $user->id), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $user->image = $return['filename']; User::save($user); $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }