From a6d7318aed88890f36b2b8a2f94b14a478de8029 Mon Sep 17 00:00:00 2001 From: Ruben Date: Mon, 12 Sep 2016 17:27:06 +0100 Subject: [PATCH] Image handling --- src/Api/ScoreControllerProvider.php | 59 +++++++++++++++++++++++++++-- src/Models/Game.php | 5 ++- src/Models/Hit.php | 17 +++++++-- www/index.php | 2 + 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index bc3dbd5..cdbd648 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -95,16 +95,16 @@ class ScoreControllerProvider implements ControllerProviderInterface // attributes $hit->getAttributes()->setGlasses($data_hit['glasses']); - foreach($hit->getAttributes()::$ATTRIBUTES as $attribute) { + foreach($hit->getAttributes()->ATTRIBUTES as $attribute) { $hit->getAttributes()->setAttribute($attribute, $data_hit[$attribute]); } // emotions - foreach($hit->getEmotions()::$EMOTIONS as $emotion) { + foreach($hit->getEmotions()->EMOTIONS as $emotion) { $hit->getEmotions()->setEmotion($emotion, $data_hit['emotions'][$emotion]); } //expressions - foreach($hit->getExpressions()::$EXPRESSIONS as $expression) { + foreach($hit->getExpressions()->EXPRESSIONS as $expression) { $hit->getExpressions()->setExpression($expression, $data_hit['expressions'][$expression]); } @@ -137,6 +137,59 @@ class ScoreControllerProvider implements ControllerProviderInterface return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200); }); + + + /** + * Expects a full game + hits in the request + * add images as post multipart data. + * filename: hit_id-imagetype.jpg (ie. 12341-brows.jpg) + */ + $controllers->post('/images', function (Request $request, Application $app) { + $token = $app['security.token_storage']->getToken(); + /* @var $user Models\User */ + $user = $token->getUser(); + if($request->files->count() < 1) { + return new JsonResponse(['success' => false, 'message' => "no files given"], 400); + } + + $valid_parts = ['brows','mouth_left','mouth_right','nose']; + foreach($valid_parts as $part) { + $path = $app['file.path'] . "/hits/$part"; + if(!is_dir($path)) { + mkdir($path, 0777, true); + } + } + + foreach($request->files->all() as $file) { + /* @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */ + $filename = $file->getClientOriginalName(); + $ext = $file->getClientOriginalExtension(); + if($ext != 'jpg' || $file->getMimeType() != 'image/jpeg') { + return new JsonResponse(['success' => false, 'message' => "Image not image/jpeg"], 400); + } + + + // name without .jpg + $id_string = substr($filename, 0, -4); + + list($hit_id, $part) = \explode('-', $id_string); + + if(!in_array($part, $valid_parts)) { + return new JsonResponse(['success' => false, 'message' => "unknown face part."], 400); + } + + /* @var $hit Models\Hit */ + $hit = $app['entity.manager']->getRepository(Models\Hit::class)->find($hit_id); + if(empty($hit) || $hit->getGame()->getUser()->getId() != $user->getId()) { + return new JsonResponse(['success' => false, 'message' => "Hit not found."], 400); + } + + $file->move($app['file.path'] . "/hits/{$part}/", "{$hit->getId()}.jpg" ); + + } + + return new JsonResponse(['success' => true, 'message' => "images saved."], 200); + }); return $controllers; } diff --git a/src/Models/Game.php b/src/Models/Game.php index 6ce991b..c92b351 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -99,7 +99,7 @@ class Game /** * Gets the value of user. * - * @return mixed + * @return User */ public function getUser() { @@ -252,7 +252,8 @@ class Game if($diffExpression) { /* @var $diffExpressionText string */ - $diffExpressionText = $betterHit->getExpressions()::$EXPRESSIONS_2ND_PERSON[$diffExpression]; +// $diffExpressionText = $betterHit->getExpressions()::$EXPRESSIONS_2ND_PERSON[$diffExpression]; + $diffExpressionText = $betterHit->getExpressions()->get2ndPersonName($diffExpression); $text .= " To show more empathy, try to " . $diffExpressionText ." " . $diff . "% " . $diffText ."."; } diff --git a/src/Models/Hit.php b/src/Models/Hit.php index ea24b4c..9b50572 100644 --- a/src/Models/Hit.php +++ b/src/Models/Hit.php @@ -101,7 +101,7 @@ class Hit /** * Gets the value of game. - * @return mixed + * @return Game */ public function getGame() { @@ -120,7 +120,7 @@ class Hit /** * Gets the value of attributes. * - * @return mixed + * @return Attributes */ public function getAttributes() { @@ -130,7 +130,7 @@ class Hit /** * Gets the value of expressions. * - * @return mixed + * @return Expressions */ public function getExpressions() { @@ -802,6 +802,17 @@ class Expressions { } return $diffs; } + + /** + * @param string $expression + * @return string + */ + public function get2ndPersonName(string $expression) { + if(!isSet(static::$EXPRESSIONS_2ND_PERSON[$expression])) { + return ""; + } + return static::$EXPRESSIONS_2ND_PERSON[$expression]; + } } /** @ORM\Embeddable */ diff --git a/www/index.php b/www/index.php index 64dc9d4..4d9a90c 100644 --- a/www/index.php +++ b/www/index.php @@ -27,6 +27,8 @@ $app['entity.manager'] = function() { return EmotionHero\Application::getInstance()->getEm(); }; +$app['file.path'] = realpath(__DIR__ . "/../files"); + $app['security.jwt'] = [