コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * update
  *
  * @param Data $data
  *
  * @return  bool
  *
  * @throws ValidFailException
  */
 protected function saveAuthor($data)
 {
     $authorMapper = new DataMapper('authors');
     if (!$data->name) {
         throw new ValidFailException('Name should not be empty.');
     }
     if (!$data->image) {
         unset($data->image);
     }
     $isNew = !$data->id;
     $author = new Data();
     if ($isNew) {
         $data->uuid = $data->uuid ?: Uuid::v4();
     } else {
         $author = $authorMapper->findOne($data->id);
         $author->bind($data);
     }
     $author->blog = Blog::get()->id;
     $authorMapper->saveOne($author, 'id');
     $this->setRedirect(Router::buildHttp('admin:authors'), 'Save success', 'success');
     return true;
 }
コード例 #3
0
 /**
  * Method to test v5().
  *
  * @return void
  *
  * @covers \Windwalker\Uuid\Uuid::v5
  */
 public function testV5()
 {
     $this->assertEquals('42966b0e-da16-5a1c-8fbf-487dcac94fe8', Uuid::v5(Uuid::NAMESPACE_DNS, 'foo.bar.com'));
     $this->assertEquals('c4fc7437-a5c8-53d3-ac95-36bbc71fc931', Uuid::v5(Uuid::NAMESPACE_URL, 'http://foo.bar.com/foobar/'));
     $this->assertEquals('5d9c8d79-c265-5398-b135-2a97382eb7df', Uuid::v5(Uuid::NAMESPACE_X500, 'cn=SMS Taiwan,ou=Company,o=Healthy,c=TW'));
 }