예제 #1
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $key = Key::whereId($id)->first();
     if (empty($key)) {
         return $this->saveResponse(false);
     }
     return $this->saveResponse($key->delete());
 }
예제 #2
0
 /**
  * Handle the command.
  *
  * @param  CreateKeyCommand  $command
  * @return void
  */
 public function handle(CreateKeyCommand $command)
 {
     $key = Key::create(['colour' => $command->colour, 'title' => $command->title, 'description' => $command->description]);
     if (!empty($key)) {
         return $key;
     }
     return false;
 }
예제 #3
0
 /**
  * Handle the command.
  *
  * @param  UpdateKeyCommand  $command
  * @return void
  */
 public function handle(UpdateKeyCommand $command)
 {
     $key = Key::whereId($command->id)->first();
     if (!empty($key)) {
         $key->colour = $command->colour;
         $key->title = $command->title;
         $key->description = $command->description;
         if ($key->save()) {
             return $key;
         }
     }
     return false;
 }
예제 #4
0
 public function getVerify($key = null)
 {
     if (empty($key)) {
         return response()->json(array("error" => "No API key provided."));
     }
     $key = Key::where('api_key', '=', $key)->first();
     if (empty($key)) {
         return response()->json(array("error" => "Invalid key provided."));
     } else {
         return response()->json(array("valid" => "Key validated.", "name" => $key->name, "created_at" => $key->created_at));
     }
 }
예제 #5
0
 public function actionUpload($key = null)
 {
     $readyPicsCount = Picture::find()->where(['state' => 'ready'])->count();
     $lastPictures = Picture::find()->where(['state' => 'ready'])->orderBy('updated ASC')->limit(50)->offset($readyPicsCount - 50)->all();
     $avgPictureTime = 0;
     if (count($lastPictures) > 1) {
         $summ = 0;
         for ($i = 1; $i < count($lastPictures); $i++) {
             $diff = strtotime($lastPictures[$i]->updated) - strtotime($lastPictures[$i - 1]->updated);
             $summ += $diff;
         }
         $avgPictureTime = intval($summ / (count($lastPictures) - 1));
     }
     $readyTime = $avgPictureTime * Picture::find()->where(['state' => 'new'])->count();
     $myPictureDP = new ActiveDataProvider(['query' => Picture::find()->where(['state' => 'new', 'ip' => Yii::$app->getRequest()->getUserIP()]), 'sort' => false]);
     $lastPending = Picture::find()->where(['state' => 'pending'])->orderBy('id DESC')->one();
     if ($lastPending == null) {
         $lastPending = Picture::find()->where(['state' => 'ready'])->orderBy('id DESC')->one();
     }
     $pendingPicsCount = Picture::find()->where(['state' => 'new'])->count();
     $algorithms = [];
     $algos = Algorithm::find()->orderBy('count DESC')->all();
     foreach ($algos as $algo) {
         if (count($algorithms) == 0) {
             $algorithms[$algo->getPrimaryKey()] = $algo->name . ' (default, ' . $algo->count . ' pics)';
         } else {
             $algorithms[$algo->getPrimaryKey()] = $algo->name . ' (' . $algo->count . ' pics)';
         }
     }
     $viewData = ['readyTime' => $readyTime, 'myPictureDP' => $myPictureDP, 'avgPictureTime' => $avgPictureTime, 'lastPendingId' => $lastPending->id, 'pendingPicsCount' => $pendingPicsCount, 'algorithms' => $algorithms, 'algos' => $algos];
     $priority = 0;
     if ($key != null) {
         $keyModel = Key::find()->where(['value' => $key])->andWhere('used < count')->one();
         if ($keyModel == null) {
             throw new HttpException(404, "Bad key");
         }
         $priority = $keyModel->priority;
         $viewData['key'] = $keyModel;
     }
     $model = new UploadForm();
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'image');
         $model->image = $image;
         if ($model->validate() && $model->check()) {
             $size = getimagesize($image->tempName);
             list($width, $height, $type) = $size;
             if ($type == IMAGETYPE_JPEG) {
                 $img = imagecreatefromjpeg($image->tempName);
             } else {
                 if ($type == IMAGETYPE_PNG) {
                     $img = imagecreatefrompng($image->tempName);
                 } else {
                     throw new HttpException(400, 'Bad image');
                 }
             }
             $srcName = Helper::gen_uuid() . '.jpg';
             $filename = \Yii::$app->basePath . '/web/images/' . $srcName;
             $k = 650;
             if (!($width <= $k && $height <= $k)) {
                 $minSide = (int) (min($width, $height) * $k / max($width, $height));
                 list($newWidth, $newHeight) = $width > $height ? [$k, $minSide] : [$minSide, $k];
                 $newImage = imagecreatetruecolor($newWidth, $newHeight);
                 imagecopyresampled($newImage, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
                 imagejpeg($newImage, $filename, 100);
             } else {
                 imagejpeg($img, $filename, 100);
             }
             $hash = sha1(file_get_contents($filename));
             $count = Picture::find()->where(['hash' => $hash, 'algorithmId' => $model->algoId])->all();
             if (count($count) > 0) {
                 unlink($filename);
                 $first = $count[0];
                 Yii::$app->getSession()->setFlash('success', 'This image was already submitted.');
                 return $this->redirect('/picture/' . $first->getPrimaryKey());
             }
             $algo = Algorithm::find()->where(['id' => $model->algoId])->one();
             $picture = new Picture();
             $picture->email = $model->email;
             $picture->ip = \Yii::$app->getRequest()->getUserIP();
             $picture->source = $srcName;
             $picture->output = null;
             $picture->state = 'new';
             $picture->hash = $hash;
             $picture->status = 0;
             $picture->priority = $priority;
             $picture->algorithm = $algo->name;
             $picture->algorithmId = $model->algoId;
             $picture->save();
             $algo->count += 1;
             $algo->save();
             if (!empty($keyModel)) {
                 $keyModel->used += 1;
                 $keyModel->save();
                 \Yii::$app->getSession()->setFlash('success', 'Your image were successfully uploaded. Converted image will be ready <b>ASAP</b> and sent on your email. Thank you!');
             } else {
                 \Yii::$app->getSession()->setFlash('success', 'Your image were successfully uploaded. Converted image will be ready after ~' . Helper::formatHourAndMin($readyTime) . ' and sent on your email. Thank you!');
             }
             return $this->redirect('/picture/' . $picture->getPrimaryKey());
         }
     }
     $viewData['model'] = $model;
     return $this->render('upload', $viewData);
 }
예제 #6
0
 public static function keysActivity($fromTime, $toTime = null)
 {
     $query = Key::find()->select([new Expression('COUNT(*) as count'), new Expression('strftime("%Y-%m-%d %H:%M:00", `at`) as `date`')])->groupBy(new Expression('strftime("%Y-%m-%d %H:%M", `at`) '));
     self::whereFromTo($query, $fromTime, $toTime, 'at');
     $data = $query->createCommand()->queryAll();
     array_walk($data, function (&$a) {
         $a['count'] = (int) $a['count'];
     });
     $data = ArrayHelper::map($data, 'date', 'count');
     $timezone = new \DateTimeZone(\Yii::$app->timeZone);
     $from = (new \DateTime('@' . $fromTime))->setTimezone($timezone);
     $to = (new \DateTime('@' . $toTime))->setTimezone($timezone);
     $interval = new \DateInterval('PT1M');
     $period = new \DatePeriod($from, $interval, $to);
     $out = [];
     foreach ($period as $min) {
         $date = $min->format('Y-m-d H:i:00');
         if (isset($data[$date])) {
             $out[] = ['date' => $min->setTimezone($timezone)->format('c'), 'count' => (int) $data[$date]];
         } else {
             $out[] = ['date' => $min->setTimezone($timezone)->format('c'), 'count' => 0];
         }
     }
     return $out;
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     DB::table('keys')->delete();
     $testmodpack = Key::create(array('name' => 'TestKey', 'api_key' => 'sfIvEcNueZtwKsTAIYOIYng1iuPAgavJsfIvEcNueZtwKsTAIYOIYng1iuPAgavJ'));
 }