diff --git a/app/AlbumSources/IAlbumSource.php b/app/AlbumSources/IAlbumSource.php index 65eb25a..dc3c5de 100644 --- a/app/AlbumSources/IAlbumSource.php +++ b/app/AlbumSources/IAlbumSource.php @@ -45,6 +45,14 @@ interface IAlbumSource */ function getUrlToPhoto(Photo $photo, $thumbnail = null); + /** + * Saves the original photo to its permanent location. + * @param Photo $photo Photo the original relates to. + * @param $tempFilename Filename containing the original image. + * @return mixed + */ + function saveOriginal(Photo $photo, $tempFilename); + /** * Saves a generated thumbnail to its permanent location. * @param Photo $photo Photo the thumbnail relates to. diff --git a/app/AlbumSources/LocalFilesystemSource.php b/app/AlbumSources/LocalFilesystemSource.php index 4eb5ea1..180b9ee 100644 --- a/app/AlbumSources/LocalFilesystemSource.php +++ b/app/AlbumSources/LocalFilesystemSource.php @@ -62,10 +62,15 @@ class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource return $photoUrl; } - public function saveThumbnail(Photo $photo, $thumbnailInfo, $tempFilename) + public function saveOriginal(Photo $photo, $tempFilename) + { + $this->saveThumbnail($photo, $tempFilename, $this->getOriginalsFolder()); + } + + public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail) { $fileInfo = new File($tempFilename); - $fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum(), $thumbnailInfo['name']), $photo->storage_file_name); + $fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum(), $thumbnail), $photo->storage_file_name); } public function saveUploadedPhoto(File $uploadedFile, $overrideFilename = null) diff --git a/app/AlbumSources/OpenStackSource.php b/app/AlbumSources/OpenStackSource.php index 7e33023..db338fe 100644 --- a/app/AlbumSources/OpenStackSource.php +++ b/app/AlbumSources/OpenStackSource.php @@ -2,6 +2,7 @@ namespace App\AlbumSources; use App\Photo; +use OpenCloud\OpenStack; use Symfony\Component\HttpFoundation\File\File; /** @@ -14,7 +15,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * Deletes an entire album's media contents. * @return void */ - function deleteAlbumContents() + public function deleteAlbumContents() { // TODO: Implement deleteAlbumContents() method. } @@ -25,7 +26,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * @param string $thumbnail Thumbnail to delete (or null to delete the original.) * @return void */ - function deleteThumbnail(Photo $photo, $thumbnail = null) + public function deleteThumbnail(Photo $photo, $thumbnail = null) { // TODO: Implement deleteThumbnail() method. } @@ -34,7 +35,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * Gets the name of this album source. * @return string */ - function getName() + public function getName() { return 'global.album_sources.openstack'; } @@ -45,7 +46,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * @param string $thumbnail Thumbnail to get the image to. * @return string */ - function getPathToPhoto(Photo $photo, $thumbnail = null) + public function getPathToPhoto(Photo $photo, $thumbnail = null) { // TODO: Implement getPathToPhoto() method. } @@ -56,21 +57,36 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * @param string $thumbnail Thumbnail to get the image to. * @return string */ - function getUrlToPhoto(Photo $photo, $thumbnail = null) + public function getUrlToPhoto(Photo $photo, $thumbnail = null) { - // TODO: Implement getUrlToPhoto() method. + return sprintf( + '%s/%s/%s/%s', + 'https://photos-dev-898b0644.cdn.memsites.com', + $this->album->url_alias, + is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail, + $photo->storage_file_name + ); + } + + public function saveOriginal(Photo $photo, $tempFilename) + { + $this->saveThumbnail($photo, $tempFilename, $this->getOriginalsFolder()); } /** * Saves a generated thumbnail to its permanent location. * @param Photo $photo Photo the thumbnail relates to. - * @param string $thumbnailInfo Information about the thumbnail. * @param string $tempFilename Filename containing the thumbnail. + * @param string $thumbnail Name of the thumbnail. * @return mixed */ - function saveThumbnail(Photo $photo, $thumbnailInfo, $tempFilename) + public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail) { - // TODO: Implement saveThumbnail() method. + $container = $this->getContainer(); + $container->uploadObject( + sprintf('%s/%s/%s', $this->album->url_alias, $thumbnail, $photo->storage_file_name), + fopen($tempFilename, 'r+') + ); } /** @@ -79,8 +95,36 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource * @param string $overrideFilename Specific file name to use, or null to randomly generate one. * @return File */ - function saveUploadedPhoto(File $uploadedFile, $overrideFilename = null) + public function saveUploadedPhoto(File $uploadedFile, $overrideFilename = null) { // TODO: Implement saveUploadedPhoto() method. } + + private function getClient() + { + return new OpenStack($this->configuration->auth_url, [ + 'username' => $this->configuration->username, + 'password' => decrypt($this->configuration->password), + 'tenantName' => $this->configuration->tenant_name, + ]); + } + + private function getContainer() + { + return $this->getStorageService()->getContainer($this->configuration->container_name); + } + + private function getOriginalsFolder() + { + return '_originals'; + } + + private function getStorageService() + { + return $this->getClient()->objectStoreService( + $this->configuration->service_name, + $this->configuration->service_region, + 'publicURL' + ); + } } \ No newline at end of file diff --git a/app/Services/PhotoService.php b/app/Services/PhotoService.php index e78e44d..3470c43 100644 --- a/app/Services/PhotoService.php +++ b/app/Services/PhotoService.php @@ -51,9 +51,6 @@ class PhotoService public function analyse($queueToken) { - /** @var Album $album */ - $album = $this->photo->album; - $photoFile = join(DIRECTORY_SEPARATOR, [ FileHelper::getQueuePath($queueToken), $this->photo->storage_file_name @@ -119,6 +116,9 @@ class PhotoService $this->photo->is_analysed = true; $this->photo->save(); + // Save the original + $this->albumSource->saveOriginal($this->photo, $photoFile); + $this->regenerateThumbnails($originalPhotoResource); } @@ -198,7 +198,7 @@ class PhotoService foreach ($thumbnailsRequired as $thumbnail) { $generatedThumbnailPath = $this->imageHelper->generateThumbnail($originalPhotoResource, $this->photo, $thumbnail); - $this->albumSource->saveThumbnail($this->photo, $thumbnail, $generatedThumbnailPath); + $this->albumSource->saveThumbnail($this->photo, $generatedThumbnailPath, $thumbnail['name']); } }