示例#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');
     $type = $this->input->get('type', 'post');
     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');
         }
         $dest = $this->getDest($name, $type);
         $s3 = new \S3($this->app->get('amazon.access_key'), $this->app->get('amazon.secret_key'));
         $result = $s3::putObject(\S3::inputFile($src, false), 'windspeaker', $dest, \S3::ACL_PUBLIC_READ);
         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;
     $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()
 {
     try {
         $id = $this->input->get('id');
         if ($id) {
             $author = Author::getAuthor($id);
             $author->image = "0";
             (new DataMapper('authors'))->updateOne($author, 'id');
         }
     } catch (\Exception $e) {
         $response = new Response();
         $response->setBody(json_encode(['error' => $e->getMessage()]));
         $response->setMimeType('text/json');
         $response->respond();
         exit;
     }
     $return = new Registry();
     $return['success'] = true;
     $return['image'] = UserHelper::getGavatar('*****@*****.**', 400);
     $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');
     $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;
 }
示例#4
0
    /**
     * Execute the controller.
     *
     * @return  mixed Return executed result.
     *
     * @throws  \LogicException
     * @throws  \RuntimeException
     */
    public function execute()
    {
        $query = $this->input->getString('query');
        $query = trim($query);
        if (!$query) {
            return;
        }
        $queries = explode(' ', $query);
        $mapper = new DataMapper('users');
        $conditions = [];
        $q = Ioc::getDatabase()->getQuery(true);
        foreach ($queries as $query) {
            if (!trim($query)) {
                continue;
            }
            $query = $q->quote('%' . $query . '%');
            $conditions[] = 'username LIKE ' . $query;
            $conditions[] = 'fullname LIKE ' . $query;
            $conditions[] = 'email LIKE ' . $query;
        }
        $conditions = new QueryElement('()', $conditions, ' OR ');
        $users = $mapper->find([(string) $conditions], 'username', 0, 10);
        $suggestions = [];
        $tmpl = <<<VALUE
<div>
\t<img width="48" height="48" class="find-author-avatar pull-left" src="%s" alt=""/>
\t<div class="find-author-name">%s <small>%s</small></div>
\t<small>%s</small>
\t<div class="clearfix"></div>
</div>
VALUE;
        foreach ($users as $user) {
            $suggestions[] = ['value' => sprintf($tmpl, UserHelper::getAvatar($user->id), $user->fullname, $user->username, $user->email), 'data' => $user->username];
        }
        $response = new Response();
        $response->setBody(json_encode(['suggestions' => $suggestions]));
        $response->setMimeType('text/json');
        $response->respond();
        exit;
        return true;
    }
 /**
  * Execute the controller.
  *
  * @return  mixed Return executed result.
  *
  * @throws  \LogicException
  * @throws  \RuntimeException
  */
 public function execute()
 {
     $user = User::get();
     try {
         $user->image = "0";
         User::save($user);
     } catch (\Exception $e) {
         $response = new Response();
         $response->setBody(json_encode(['error' => $e->getMessage()]));
         $response->setMimeType('text/json');
         $response->respond();
         exit;
     }
     $return = new Registry();
     $return['success'] = true;
     $return['image'] = UserHelper::getAvatar($user->id, 650);
     $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;
 }
示例#7
0
 /**
  * respond
  *
  * @param array $data
  * @param int   $code
  *
  * @return  void
  */
 protected function respond($data, $code = 200)
 {
     if ($this->quiet) {
         return;
     }
     $response = new Response();
     $response->setBody(json_encode($data));
     $response->setMimeType('application/json');
     $response->setHeader('Status', $code);
     $response->respond();
     exit;
     return;
 }