/** * Move the uploaded photo to the storage and store it in the database. * All upload actions should use this function to prevent "ghost" files * or database entries * * @param string $path the temporary path of the uploaded photo * @param \Photo\Model\Album $targetAlbum the album to save the photo in * @param boolean $move whether to move the photo instead of copying it * * @return \Photo\Model\Photo|boolean */ public function storeUploadedPhoto($path, $targetAlbum, $move = false) { if (!$this->isAllowed('photo', 'add')) { throw new \User\Permissions\NotAllowedException($this->getTranslator()->translate('Not allowed to add photos.')); } $config = $this->getConfig(); $storagePath = $this->generateStoragePath($path); //check if photo exists already in the database $photo = $this->getPhotoMapper()->getPhotoByData($storagePath, $targetAlbum); //if the returned object is null, then the photo doesn't exist if (is_null($photo)) { $photo = new PhotoModel(); $photo->setAlbum($targetAlbum); $photo = $this->getMetadataService()->populateMetaData($photo, $path); $photo->setPath($storagePath); $mapper = $this->getPhotoMapper(); $mapper->getConnection()->beginTransaction(); try { /* * Create and set the storage paths for thumbnails. */ $photo->setLargeThumbPath($this->createThumbnail($path, $config['large_thumb_size']['width'], $config['large_thumb_size']['height'])); $photo->setSmallThumbPath($this->createThumbnail($path, $config['small_thumb_size']['width'], $config['small_thumb_size']['height'])); if ($move) { rename($path, $config['upload_dir'] . '/' . $storagePath); } else { copy($path, $config['upload_dir'] . '/' . $storagePath); } $mapper->persist($photo); $mapper->flush(); $mapper->getConnection()->commit(); } catch (Exception $e) { // Rollback if anything went wrong $mapper->getConnection()->rollBack(); $this->getPhotoService()->deletePhotoFiles($photo); return false; } } return $photo; }
/** * Add a photo to an album. * * @param \Photo\Model\Photo $photo */ public function addPhoto($photo) { $photo->setAlbum($this); $this->photos[] = $photo; }