Esempio n. 1
0
 /**
  * @api            {post} /images Create an Image
  * @apiGroup       Images
  * @apiDescription Store an image and return its metadata including a to view it.
  *     <br/>The image will be made anonymous if a `sessionKey` is given and the user's `defaultAnonymous`
  *     value is `true`.
  *     <br/>The image will be password protected if a `sessionKey` is given and the user's `defaultPassword`
  *     value is set.
  *     <br/>If no `sessionKey` is given in the request, an `imageKey` property will be returned in the response
  *     it is a 64 character string that can be used to manipulate the image in future requests.
  *     If a sessionKey was given, use that or another sessionKey for that user to manipulate it.
  * @apiParam {string} base64 Base64 encoded image to upload.
  * @apiParam {string} [sessionKey] A session key for a user the image should be attributed to.
  * @apiSuccessExample {json} Success Response
  *     {
  *       "success": true,
  *       "image": {
  *         // See Get Image Info
  *       },
  *       "imageKey": "2c9933ce51d082ec61a4a55e21cb9befabdca6a65635930fdd0891dbb78f68f2"
  *     }
  *
  * @param FileManager    $fileManager
  * @param PictureFactory $pictureFactory
  * @param PasswordHasher $passwordHasher
  *
  * @throws Exception
  * @return Response
  */
 public function store(FileManager $fileManager, PictureFactory $pictureFactory, PasswordHasher $passwordHasher)
 {
     $this->validate($this->request, ['base64' => 'required_without:file', 'file' => 'required_without:base64', 'sessionKey' => 'required_with:albumId']);
     $albumId = null;
     // Validating albumId later because we want to ensure the session is setup first
     if ($albumId = $this->request->input('albumId')) {
         $this->validate($this->request, ['albumId' => 'exists:albums,albumId,userId,' . $this->user->getId()]);
     }
     if ($this->request->has('base64')) {
         $picture = $pictureFactory->createFromBase64String($this->request->input('base64'));
     } elseif ($this->request->hasFile('file')) {
         $picture = $pictureFactory->createFromUploadedFile($this->request->file('file'));
     }
     if (empty($picture)) {
         throw new HttpException(400, "Unable to create picture from input.");
     }
     $userId = null;
     $anonymous = false;
     $password = null;
     if ($this->user) {
         $userId = $this->user->userId;
         $anonymous = $this->user->defaultAnonymous;
         $password = $this->user->defaultPassword;
     }
     $image = new Image(['albumId' => $albumId, 'ip' => $this->request->getClientIp(), 'userId' => $userId, 'anonymous' => $anonymous, 'password' => $password]);
     if (!$this->user) {
         $key = $image->generateKey($passwordHasher);
     } else {
         $key = null;
     }
     $imageFile = $fileManager->savePicture($picture);
     $image->setImageFile($imageFile);
     $image->save();
     /** @var Image $image */
     $image = $image->fresh();
     $response = ['image' => $image, 'success' => true];
     if ($key) {
         $response['imageKey'] = $key;
     }
     return $this->response($response);
 }