137 lines
3.8 KiB
PHP
137 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\AlbumSources;
|
|
|
|
use App\Photo;
|
|
use App\Services\DropboxService;
|
|
use GuzzleHttp\Psr7\Stream;
|
|
use function GuzzleHttp\Psr7\stream_for;
|
|
|
|
class DropboxSource extends AlbumSourceBase implements IAlbumSource
|
|
{
|
|
/**
|
|
* @var DropboxService
|
|
*/
|
|
private $dropboxClient;
|
|
|
|
/**
|
|
* Deletes an entire album's media contents.
|
|
* @return void
|
|
*/
|
|
public function deleteAlbumContents()
|
|
{
|
|
try
|
|
{
|
|
$albumPathOnStorage = sprintf('/%s', $this->album->url_alias);
|
|
|
|
$this->getClient()->deleteFile($albumPathOnStorage);
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
// Don't worry too much if the delete fails - the file may have been removed on Dropbox itself
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function deleteThumbnail(Photo $photo, $thumbnail = null)
|
|
{
|
|
try
|
|
{
|
|
$pathOnStorage = $this->getPathToPhoto($photo, $thumbnail);
|
|
|
|
$this->getClient()->deleteFile($pathOnStorage);
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
// Don't worry too much if the delete fails - the file may have been removed on Dropbox itself
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 Stream
|
|
*/
|
|
public function fetchPhotoContent(Photo $photo, $thumbnail = null)
|
|
{
|
|
$pathOnStorage = $this->getPathToPhoto($photo, $thumbnail);
|
|
|
|
return stream_for($this->getClient()->downloadFile($pathOnStorage));
|
|
}
|
|
|
|
/**
|
|
* Gets the name of this album source.
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return 'global.album_sources.dropbox';
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function getUrlToPhoto(Photo $photo, $thumbnail = null)
|
|
{
|
|
$photoUrl = route('downloadPhoto', [
|
|
'albumUrlAlias' => $this->album->url_path,
|
|
'photoFilename' => $photo->storage_file_name
|
|
]);
|
|
|
|
if (!is_null($thumbnail))
|
|
{
|
|
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
|
|
}
|
|
|
|
return $photoUrl;
|
|
}
|
|
|
|
/**
|
|
* Saves a generated thumbnail to its permanent location.
|
|
* @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.)
|
|
* @return mixed
|
|
*/
|
|
public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail = null)
|
|
{
|
|
$pathOnStorage = $this->getPathToPhoto($photo, $thumbnail);
|
|
|
|
$this->getClient()->uploadFile($tempFilename, $pathOnStorage);
|
|
}
|
|
|
|
private function getClient()
|
|
{
|
|
if (is_null($this->dropboxClient))
|
|
{
|
|
$this->dropboxClient = new DropboxService();
|
|
$this->dropboxClient->setAccessToken(decrypt($this->configuration->access_token));
|
|
}
|
|
|
|
return $this->dropboxClient;
|
|
}
|
|
|
|
private function getOriginalsFolder()
|
|
{
|
|
return '_originals';
|
|
}
|
|
|
|
private function getPathToPhoto(Photo $photo, $thumbnail = null)
|
|
{
|
|
return sprintf(
|
|
'/%s/%s/%s',
|
|
$this->album->url_alias,
|
|
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
|
|
$photo->storage_file_name
|
|
);
|
|
}
|
|
} |