2016-10-28 04:54:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\AlbumSources;
|
2016-11-03 13:33:35 +00:00
|
|
|
|
2016-10-28 04:54:38 +01:00
|
|
|
use App\Photo;
|
2016-10-28 12:59:36 +01:00
|
|
|
use Guzzle\Http\EntityBody;
|
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use OpenCloud\ObjectStore\Exception\ObjectNotFoundException;
|
2016-10-28 06:24:34 +01:00
|
|
|
use OpenCloud\OpenStack;
|
2016-10-28 04:54:38 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
|
|
|
|
/**
|
2016-11-03 13:33:35 +00:00
|
|
|
* Driver for managing files on an OpenStack Keystone+Swift compatible platform.
|
2016-10-28 04:54:38 +01:00
|
|
|
* @package App\AlbumSources
|
|
|
|
*/
|
|
|
|
class OpenStackSource extends AlbumSourceBase implements IAlbumSource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Deletes an entire album's media contents.
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-28 06:24:34 +01:00
|
|
|
public function deleteAlbumContents()
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
// Because all photos live in a single container, there is nothing "global" to delete for the entire album
|
|
|
|
// The delete routine will have already removed all photos
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a thumbnail file for a photo.
|
|
|
|
* @param Photo $photo Photo to delete the thumbnail from.
|
|
|
|
* @param string $thumbnail Thumbnail to delete (or null to delete the original.)
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-28 06:24:34 +01:00
|
|
|
public function deleteThumbnail(Photo $photo, $thumbnail = null)
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$photoPath = $this->getPathToPhoto($photo, $thumbnail);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$this->getContainer()->deleteObject($photoPath);
|
|
|
|
}
|
|
|
|
catch (ClientErrorResponseException $ex)
|
|
|
|
{
|
|
|
|
// Don't worry if the file no longer exists
|
|
|
|
Log::warning('Failed deleting image from OpenStack.', ['error' => $ex->getMessage(), 'path' => $photoPath]);
|
|
|
|
}
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-28 12:59:36 +01:00
|
|
|
* Fetches the contents of a thumbnail for a photo.
|
|
|
|
* @param Photo $photo Photo to fetch the thumbnail for.
|
|
|
|
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
|
|
|
|
* @return EntityBody
|
2016-10-28 04:54:38 +01:00
|
|
|
*/
|
2016-10-28 12:59:36 +01:00
|
|
|
public function fetchPhotoContent(Photo $photo, $thumbnail = null)
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$object = $this->getContainer()->getObject($this->getPathToPhoto($photo, $thumbnail));
|
|
|
|
|
|
|
|
return $object->getContent();
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-28 12:59:36 +01:00
|
|
|
* Gets the name of this album source.
|
2016-10-28 04:54:38 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-28 12:59:36 +01:00
|
|
|
public function getName()
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
return 'global.album_sources.openstack';
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the absolute URL to the given photo file.
|
|
|
|
* @param Photo $photo Photo to get the URL to.
|
|
|
|
* @param string $thumbnail Thumbnail to get the image to.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-28 06:24:34 +01:00
|
|
|
public function getUrlToPhoto(Photo $photo, $thumbnail = null)
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$cdnUrl = $this->configuration->cdn_url;
|
|
|
|
if (strlen($cdnUrl) > 0)
|
|
|
|
{
|
|
|
|
if (substr($cdnUrl, strlen($cdnUrl) - 1, 1) == '/')
|
|
|
|
{
|
|
|
|
$cdnUrl = substr($cdnUrl, 0, strlen($cdnUrl) - 1);
|
|
|
|
}
|
2016-10-28 06:24:34 +01:00
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
return sprintf('%s/%s', $cdnUrl, $this->getPathToPhoto($photo, $thumbnail));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not using a CDN - use the standard download controller
|
|
|
|
$photoUrl = route('downloadPhoto', [
|
|
|
|
'albumUrlAlias' => $this->album->url_alias,
|
|
|
|
'photoFilename' => $photo->storage_file_name
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!is_null($thumbnail))
|
|
|
|
{
|
|
|
|
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $photoUrl;
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves a generated thumbnail to its permanent location.
|
2016-10-28 12:59:36 +01:00
|
|
|
* @param Photo $photo Photo the image relates to.
|
|
|
|
* @param string $tempFilename Filename containing the image.
|
|
|
|
* @param string $thumbnail Name of the thumbnail (or null for the original.)
|
2016-10-28 04:54:38 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-28 12:59:36 +01:00
|
|
|
public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail = null)
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$photoPath = $this->getPathToPhoto($photo, $thumbnail);
|
2016-10-28 04:54:38 +01:00
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
$container = $this->getContainer();
|
|
|
|
$container->uploadObject($photoPath, fopen($tempFilename, 'r+'));
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
2016-10-28 06:24:34 +01:00
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
protected function getClient()
|
2016-10-28 06:24:34 +01:00
|
|
|
{
|
|
|
|
return new OpenStack($this->configuration->auth_url, [
|
|
|
|
'username' => $this->configuration->username,
|
|
|
|
'password' => decrypt($this->configuration->password),
|
|
|
|
'tenantName' => $this->configuration->tenant_name,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
protected function getContainer()
|
2016-10-28 06:24:34 +01:00
|
|
|
{
|
|
|
|
return $this->getStorageService()->getContainer($this->configuration->container_name);
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
protected function getStorageService()
|
|
|
|
{
|
|
|
|
return $this->getClient()->objectStoreService(
|
|
|
|
$this->configuration->service_name,
|
|
|
|
$this->configuration->service_region,
|
|
|
|
'publicURL'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:41:07 +00:00
|
|
|
protected function getOriginalsFolder()
|
2016-10-28 06:24:34 +01:00
|
|
|
{
|
|
|
|
return '_originals';
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:41:07 +00:00
|
|
|
protected function getPathToPhoto(Photo $photo, $thumbnail = null)
|
2016-10-28 06:24:34 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
return sprintf(
|
|
|
|
'%s/%s/%s',
|
|
|
|
$this->album->url_alias,
|
|
|
|
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
|
|
|
|
$photo->storage_file_name
|
2016-10-28 06:24:34 +01:00
|
|
|
);
|
|
|
|
}
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|