Skip to content

Commit

Permalink
Prevent downloading full image for preview in BE
Browse files Browse the repository at this point in the history
  • Loading branch information
froemken committed Nov 2, 2023
1 parent a9d3410 commit bc4b520
Show file tree
Hide file tree
Showing 15 changed files with 850 additions and 184 deletions.
69 changes: 69 additions & 0 deletions Classes/Client/DropboxClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Client;

use Spatie\Dropbox\Client;
use StefanFroemken\Dropbox\Configuration\DropboxConfiguration;
use StefanFroemken\Dropbox\Service\AutoRefreshingDropboxTokenService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class DropboxClient
{
private Client $client;

public function __construct(DropboxConfiguration $dropboxConfiguration)
{
$this->client = new Client(
GeneralUtility::makeInstance(
AutoRefreshingDropboxTokenService::class,
$dropboxConfiguration->getRefreshToken(),
$dropboxConfiguration->getAppKey()
)
);
}

/**
* Returns the metadata for an image incl. width/height
*
* Note: Metadata for the root folder is unsupported.
*
* @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
*/
public function getImageMetadata(string $path): array
{
$parameters = [
'path' => $this->normalizePath($path),
'include_media_info' => true,
];

return $this->client->rpcEndpointRequest('files/get_metadata', $parameters);
}

public function normalizePath(string $path): string
{
if (preg_match("/^id:.*|^rev:.*|^(ns:[0-9]+(\/.*)?)/", $path) === 1) {
return $path;
}

$path = trim($path, '/');

return ($path === '') ? '' : '/' . $path;
}

/**
* Useful, if you want to fire your own requests to Dropbox API
*/
public function getClient(): Client
{
return $this->client;
}
}
44 changes: 44 additions & 0 deletions Classes/Client/DropboxClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Client;

use StefanFroemken\Dropbox\Configuration\DropboxConfiguration;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class DropboxClientFactory
{
public function createByResourceStorage(ResourceStorage $resourceStorage): DropboxClient
{
return GeneralUtility::makeInstance(
DropboxClient::class,
$this->buildDropboxConfiguration($resourceStorage->getConfiguration())
);
}

public function createByConfiguration(array $configuration): DropboxClient
{
return GeneralUtility::makeInstance(
DropboxClient::class,
$this->buildDropboxConfiguration($configuration)
);
}

private function buildDropboxConfiguration(array $configuration): DropboxConfiguration
{
return GeneralUtility::makeInstance(
DropboxConfiguration::class,
(string)($configuration['appKey'] ?? ''),
(string)($configuration['refreshToken'] ?? '')
);
}
}
42 changes: 42 additions & 0 deletions Classes/Configuration/DropboxConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Configuration;

/**
* This configuration will be filled by the values of the FlexForm of Dropbox FAL Driver record (sys_file_storage)
*/
class DropboxConfiguration
{
private string $appKey = '';

private string $refreshToken = '';

/**
* @param string $appKey Lookup your AppKey from Dropbox Developer App corner
* @param string $refreshToken Token to create new AccessTokens which are max. valid for 4 hours
*/
public function __construct(string $appKey, string $refreshToken)
{
$this->appKey = $appKey;
$this->refreshToken = $refreshToken;
}

public function getAppKey(): string
{
return $this->appKey;
}

public function getRefreshToken(): string
{
return $this->refreshToken;
}
}
43 changes: 43 additions & 0 deletions Classes/Domain/Factory/PathInfoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Domain\Factory;

use StefanFroemken\Dropbox\Domain\Model\FilePathInfo;
use StefanFroemken\Dropbox\Domain\Model\FolderPathInfo;
use StefanFroemken\Dropbox\Domain\Model\InvalidPathInfo;
use StefanFroemken\Dropbox\Domain\Model\PathInfoInterface;

class PathInfoFactory
{
public function createPathInfo(array $metaData): PathInfoInterface
{
if ($metaData['.tag'] === 'file') {
$filePathInfo = new FilePathInfo($metaData['name'], $metaData['path_display']);
$filePathInfo->setSize((int)($metaData['size'] ?? 0));
$filePathInfo->setServerModified($metaData['server_modified'] ?? '');
$filePathInfo->setClientModified($metaData['client_modified'] ?? '');

return $filePathInfo;
}

if ($metaData['.tag'] === 'folder') {
return new FolderPathInfo($metaData['name'], $metaData['path_display']);
}

return new InvalidPathInfo();
}

public function createPathInfoForRootFolder(): FolderPathInfo
{
return new FolderPathInfo('/', '/');
}
}
29 changes: 29 additions & 0 deletions Classes/Domain/Model/AbstractPathInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Domain\Model;

abstract class AbstractPathInfo implements PathInfoInterface
{
protected string $name = '';

protected string $path = '';

public function getName(): string
{
return $this->name;
}

public function getPath(): string
{
return $this->path;
}
}
58 changes: 58 additions & 0 deletions Classes/Domain/Model/FilePathInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Domain\Model;

class FilePathInfo extends AbstractPathInfo
{
private int $size = 0;

private string $serverModified = '';

private string $clientModified = '';

public function __construct(string $name, string $path)
{
$this->name = $name;
$this->path = $path;
}

public function getSize(): string
{
// We need size as string in DropboxDriver
return (string)$this->size;
}

public function setSize(int $size): void
{
$this->size = $size;
}

public function getServerModified(): string
{
return $this->serverModified;
}

public function setServerModified(string $serverModified): void
{
$this->serverModified = $serverModified;
}

public function getClientModified(): string
{
return $this->clientModified;
}

public function setClientModified(string $clientModified): void
{
$this->clientModified = $clientModified;
}
}
86 changes: 86 additions & 0 deletions Classes/Domain/Model/FolderPathInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/dropbox.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\Dropbox\Domain\Model;

class FolderPathInfo extends AbstractPathInfo
{
private ?\ArrayObject $entries = null;

public function __construct(string $name, string $path)
{
$this->name = $name;
$this->path = $path;
}

/**
* While creating this object just path and name are given, but entries is still null (uninitialized).
* If files or folders of this object are requested, this object will be set to "initialized".
*/
public function isInitialized(): bool
{
return $this->entries !== null;
}

public function hasFolders(): bool
{
return $this->isInitialized() && $this->getFolders()->count();
}

/**
* @return \ArrayObject<FolderPathInfo>
*/
public function getFolders(): \ArrayObject
{
if (!$this->isInitialized()) {
return new \ArrayObject([]);
}

return new \ArrayObject(array_filter($this->entries->getArrayCopy(), static function (PathInfoInterface $pathInfo): bool {
return $pathInfo instanceof FolderPathInfo;
}));
}

public function hasFiles(): bool
{
return $this->isInitialized() && $this->getFiles()->count();
}

/**
* @return \ArrayObject<FilePathInfo>
*/
public function getFiles(): \ArrayObject
{
if (!$this->isInitialized()) {
return new \ArrayObject([]);
}

return new \ArrayObject(array_filter($this->entries->getArrayCopy(), static function (PathInfoInterface $pathInfo): bool {
return $pathInfo instanceof FilePathInfo;
}));
}

public function addEntry(PathInfoInterface $pathInfo): void
{
if ($pathInfo instanceof FilePathInfo || $pathInfo instanceof FolderPathInfo) {
if ($this->entries === null) {
$this->entries = new \ArrayObject();
}

$this->entries->append($pathInfo);
}
}

public function isEmpty(): bool
{
return $this->entries === null || $this->entries->count();
}
}
Loading

0 comments on commit bc4b520

Please sign in to comment.