Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo3 12 compatibility #31

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ insert_final_newline = true
trim_trailing_whitespace = true

# TS/JS-Files
[*.{ts,js}]
[*.{ts,js,mjs}]
indent_size = 2

# JSON-Files
Expand Down
49 changes: 20 additions & 29 deletions Classes/Driver/DropboxDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

use Spatie\Dropbox\Client;
use Spatie\Dropbox\Exceptions\BadRequest;
use StefanFroemken\Dropbox\Helper\FlashMessageHelper;
use StefanFroemken\Dropbox\Service\AutoRefreshingDropboxTokenService;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Resource\Driver\AbstractDriver;
use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
use TYPO3\CMS\Core\Resource\ResourceStorageInterface;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;

Expand All @@ -35,10 +33,6 @@ class DropboxDriver extends AbstractDriver

protected ?Client $dropboxClient = null;

protected FlashMessageService $flashMessageService;

protected array $settings = [];

/**
* A list of all supported hash algorithms, written all lower case.
*
Expand All @@ -53,8 +47,7 @@ public function processConfiguration(): void

public function initialize(): void
{
$this->cache = GeneralUtility::makeInstance(CacheManager::class)
->getCache('dropbox');
$this->cache = $this->getCacheManager()->getCache('dropbox');

if (!empty($this->configuration['refreshToken']) && !empty($this->configuration['appKey'])) {
$this->dropboxClient = new Client(
Expand All @@ -67,14 +60,12 @@ public function initialize(): void
} else {
$this->dropboxClient = null;
}

$this->flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
}

public function getCapabilities(): int
{
// If PUBLIC is available, each file will initiate a request to Dropbox-Api to retrieve a public share link
// this is extremely slow.
// Do not allow PUBLIC here, as each file will initiate a request to Dropbox-Api to retrieve a public share
// link which is extremely slow.

return ResourceStorageInterface::CAPABILITY_BROWSABLE + ResourceStorageInterface::CAPABILITY_WRITABLE;
}
Expand Down Expand Up @@ -674,6 +665,7 @@ public function resourceExists(string $identifier): bool
}

$identifier = $identifier === '/' ? $identifier : rtrim($identifier, '/');

return $this->getMetaData($identifier) !== [];
}

Expand All @@ -686,10 +678,10 @@ protected function copyFileToTemporaryPath(string $fileIdentifier): string
try {
file_put_contents($temporaryPath, stream_get_contents($this->dropboxClient->download($fileIdentifier)));
} catch (BadRequest $badRequest) {
$this->addFlashMessage(
$this->getFlashMessageHelper()->addFlashMessage(
'The file meta extraction has been interrupted, because file has been removed in the meanwhile.',
'File Meta Extraction aborted',
AbstractMessage::INFO
ContextualFeedbackSeverity::INFO
);

return '';
Expand All @@ -698,22 +690,21 @@ protected function copyFileToTemporaryPath(string $fileIdentifier): string
return $temporaryPath;
}

public function addFlashMessage(string $message, string $title = '', int $severity = AbstractMessage::OK): void
/**
* DropboxDriver was called with constructor arguments. So, no DI possible.
* We have to instantiate the CacheManager on our own.
*/
private function getCacheManager(): CacheManager
{
// We activate storeInSession, so that messages can be displayed when click on Save&Close button.
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
$message,
$title,
$severity,
true
);

$this->getFlashMessageQueue()->enqueue($flashMessage);
return GeneralUtility::makeInstance(CacheManager::class);
}

protected function getFlashMessageQueue(): FlashMessageQueue
/**
* DropboxDriver was called with constructor arguments. So, no DI possible.
* We have to instantiate the FlashMessageHelper on our own.
*/
private function getFlashMessageHelper(): FlashMessageHelper
{
return $this->flashMessageService->getMessageQueueByIdentifier();
return GeneralUtility::makeInstance(FlashMessageHelper::class);
}
}
170 changes: 170 additions & 0 deletions Classes/Helper/FlashMessageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?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\Helper;

use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Contains methods to create FlashMessages.
*/
class FlashMessageHelper
{
protected FlashMessageService $flashMessageService;

public function __construct(FlashMessageService $flashMessageService)
{
$this->flashMessageService = $flashMessageService;
}

public function addFlashMessage(string $message, string $title = '', int $severity = AbstractMessage::OK): void
{
// We activate storeInSession, so that messages can be displayed when click on Save&Close button.
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
$message,
$title,
$severity,
true
);

$this->getFlashMessageQueue()->enqueue($flashMessage);
}

/**
* @return FlashMessage[]
*/
public function getAllFlashMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessageQueue()->getAllMessagesAndFlush();
}

return $this->getFlashMessageQueue()->getAllMessages();
}

public function hasMessages(): bool
{
return !empty($this->getAllFlashMessages(false));
}

/**
* @param int $severity Must be one of the constants in AbstractMessage class
* @return FlashMessage[]
*/
protected function getFlashMessagesBySeverity(int $severity): array
{
return $this->getFlashMessageQueue()->getAllMessages($severity);
}

/**
* @param int $severity Must be one of the constants in AbstractMessage class
* @return FlashMessage[]
*/
public function getFlashMessagesBySeverityAndFlush(int $severity): array
{
return $this->getFlashMessageQueue()->getAllMessagesAndFlush($severity);
}

public function hasErrorMessages(): bool
{
return !empty($this->getErrorMessages(false));
}

/**
* @return AbstractMessage[]
*/
public function getErrorMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessagesBySeverityAndFlush(AbstractMessage::ERROR);
}

return $this->getFlashMessagesBySeverity(AbstractMessage::ERROR);
}

public function hasWarningMessages(): bool
{
return !empty($this->getWarningMessages(false));
}

/**
* @return AbstractMessage[]
*/
public function getWarningMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessagesBySeverityAndFlush(AbstractMessage::WARNING);
}

return $this->getFlashMessagesBySeverity(AbstractMessage::WARNING);
}

public function hasOkMessages(): bool
{
return !empty($this->getOkMessages(false));
}

/**
* @return AbstractMessage[]
*/
public function getOkMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessagesBySeverityAndFlush(AbstractMessage::OK);
}

return $this->getFlashMessagesBySeverity(AbstractMessage::OK);
}

public function hasInfoMessages(): bool
{
return !empty($this->getInfoMessages(false));
}

/**
* @return AbstractMessage[]
*/
public function getInfoMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessagesBySeverityAndFlush(AbstractMessage::INFO);
}

return $this->getFlashMessagesBySeverity(AbstractMessage::INFO);
}

public function hasNoticeMessages(): bool
{
return !empty($this->getNoticeMessages(false));
}

/**
* @return AbstractMessage[]
*/
public function getNoticeMessages(bool $flush = true): array
{
if ($flush) {
return $this->getFlashMessagesBySeverityAndFlush(AbstractMessage::NOTICE);
}

return $this->getFlashMessagesBySeverity(AbstractMessage::NOTICE);
}

protected function getFlashMessageQueue(): FlashMessageQueue
{
return $this->flashMessageService->getMessageQueueByIdentifier();
}
}
10 changes: 7 additions & 3 deletions Classes/Service/AutoRefreshingDropboxTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ public function __construct(string $refreshToken, string $appKey)

/**
* If refresh() was called, the Dropbox Client fails to process the request,
* which results in an exception you can access here from the argument $exception.
* which results in an exception which we will catch here and try to retrieve a fresh access token.
*
* @return bool Whether the token was refreshed or not.
*/
public function refresh(ClientException $exception): bool
{
// We only catch unauthorized exceptions to refresh the access token
if ($exception->getCode() !== 400) {
// We catch bad request (400) to build up first access token
// We catch unauthorized (401) to refresh the access token
if (
$exception->getCode() !== 400
&& $exception->getCode() !== 401
) {
return false;
}

Expand Down
Loading
Loading