コード例 #1
0
 /**
  * Method to test v4().
  *
  * @return void
  *
  * @covers \Windwalker\Uuid\Uuid::v4
  */
 public function testV4()
 {
     $seed = 123456789;
     $expect = '76f0030c-88bd-4fdc-a09c-47d8483b2296';
     mt_srand($seed);
     $this->assertEquals($expect, Uuid::v4());
     mt_srand($seed);
     $this->assertEquals($expect, Uuid::v4());
 }
コード例 #2
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;
 }
コード例 #3
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;
 }