Skip to content

Commit

Permalink
Updated Microsoft Azure Storage Library to the latest version. Added …
Browse files Browse the repository at this point in the history
…TYPO3 Version 9 compatibility.
  • Loading branch information
benjaminhirsch committed Oct 22, 2018
1 parent b36023e commit 1dae21c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
72 changes: 33 additions & 39 deletions Classes/Driver/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace B3N\AzureStorage\TYPO3\Driver;

use B3N\AzureStorage\TYPO3\Exceptions\InvalidConfigurationException;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\Internal\IBlob;
use MicrosoftAzure\Storage\Blob\Models\Blob;
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\GetBlobPropertiesResult;
use MicrosoftAzure\Storage\Blob\Models\GetBlobResult;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsResult;
use MicrosoftAzure\Storage\Common\ServicesBuilder;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Resource\Driver\AbstractHierarchicalFilesystemDriver;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Resource\StorageRepository;

class StorageDriver extends AbstractHierarchicalFilesystemDriver
{
Expand Down Expand Up @@ -44,9 +44,8 @@ class StorageDriver extends AbstractHierarchicalFilesystemDriver
/**
* @var null|string
*/
private $endpoint = null;


private $endpoint;

/**
* @var string
*/
Expand Down Expand Up @@ -76,6 +75,8 @@ class StorageDriver extends AbstractHierarchicalFilesystemDriver
* Initialize this driver and expose the capabilities for the repository to use
*
* @param array $configuration
*
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public function __construct(array $configuration = [])
{
Expand All @@ -100,7 +101,6 @@ public function __destruct()

/**
* Processes the configuration for this driver.
* @throws InvalidConfigurationException
*/
public function processConfiguration()
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public function processConfiguration()
public function initialize()
{
if (!empty($this->account) && !empty($this->accesskey) && !empty($this->container)) {
$this->blobService = ServicesBuilder::getInstance()->createBlobService('
$this->blobService = BlobRestProxy::createBlobService('
DefaultEndpointsProtocol=' . $this->protocol . ';AccountName=' . $this->account . ';AccountKey=' . $this->accesskey);
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ public function getPublicUrl($identifier)
$uriParts = array_map('rawurlencode', $uriParts);
$identifier = implode('/', $uriParts);

if (!is_null($this->endpoint)) {
if ($this->endpoint !== null) {
if (substr($this->endpoint, -1) === '/') {
$url = $this->protocol . '://' . $this->endpoint . $this->container . '/' . $identifier;
} else {
Expand Down Expand Up @@ -280,11 +280,7 @@ public function folderExists($folderIdentifier)

$blob = $this->getBlobProperties($folderIdentifier);

if ($blob instanceof GetBlobPropertiesResult) {
return true;
}

return false;
return $blob instanceof GetBlobPropertiesResult;
}

/**
Expand Down Expand Up @@ -333,7 +329,7 @@ public function addFile($localFilePath, $targetFolderIdentifier, $newFileName =
$contentType = $fileExtensionToMimeTypeMapping[$pathInfo['extension']];
}

$options = new CreateBlobOptions();
$options = new CreateBlockBlobOptions();
$options->setContentType($contentType);
$options->setCacheControl($this->cacheControl);

Expand Down Expand Up @@ -493,9 +489,9 @@ public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderId
*/
public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName)
{
return (count($this->moveOrCopyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier,
return count($this->moveOrCopyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier,
$newFolderName,
'copy'))) ? true : false;
'copy')) ? true : false;
}

/**
Expand Down Expand Up @@ -547,11 +543,7 @@ public function fileExistsInFolder($fileName, $folderIdentifier)
$folderIdentifier = $this->normalizeFolderName($folderIdentifier);
$blob = $this->getBlobProperties($folderIdentifier . $fileName);

if ($blob instanceof GetBlobPropertiesResult) {
return true;
}

return false;
return $blob instanceof GetBlobPropertiesResult;
}

/**
Expand All @@ -567,11 +559,7 @@ public function folderExistsInFolder($folderName, $folderIdentifier)
$folderName = $this->normalizeFolderName($folderName);
$blob = $this->getBlobProperties($this->normalizeFolderName($folderIdentifier . $folderName));

if ($blob instanceof GetBlobPropertiesResult) {
return true;
}

return false;
return $blob instanceof GetBlobPropertiesResult;
}

/**
Expand Down Expand Up @@ -672,9 +660,11 @@ public function isWithin($folderIdentifier, $identifier)
* Returns information about a file.
*
* @param string $fileIdentifier
* @param array $propertiesToExtract Array of properties which are be extracted
* @param array $propertiesToExtract Array of properties which are be extracted
* If empty all will be extracted
*
* @return array
* @throws \TYPO3\CMS\Core\Resource\Exception\InvalidPathException
*/
public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = [])
{
Expand Down Expand Up @@ -709,7 +699,9 @@ public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtr
* Returns information about a file.
*
* @param string $folderIdentifier
*
* @return array
* @throws \TYPO3\CMS\Core\Resource\Exception\InvalidPathException
*/
public function getFolderInfoByIdentifier($folderIdentifier)
{
Expand Down Expand Up @@ -826,7 +818,7 @@ private function getListBlobs($identifier, $nextMarker = null)
$options->setIncludeMetadata(false);
$options->setDelimiter($this->getProcessingFolder());

if (!is_null($nextMarker)) {
if ($nextMarker !== null) {
$options->setMarker($nextMarker);
}

Expand Down Expand Up @@ -989,12 +981,14 @@ protected function applyFilterMethodsToDirectoryItem(
) {
foreach ($filterMethods as $filter) {
if (is_array($filter)) {
$result = call_user_func($filter, $itemName, $itemIdentifier, $parentIdentifier, [], $this);
$result = $filter($itemName, $itemIdentifier, $parentIdentifier, [], $this);
// We have to use -1 as the „don't include“ return value, as call_user_func() will return FALSE
// If calling the method succeeded and thus we can't use that as a return value.
if ($result === -1) {
return false;
} elseif ($result === false) {
}

if ($result === false) {
throw new \RuntimeException('Could not apply file/folder name filter ' . $filter[0] . '::' . $filter[1]);
}
}
Expand Down Expand Up @@ -1028,13 +1022,11 @@ public function countFoldersInFolder($folderIdentifier, $recursive = false, arra
$iterator->next();

if ($folderIdentifier === $this->getDefaultFolder()) {
if ($this->isFolder($blob->getName()) && substr_count($blob->getName(), '/') == 1) {
$folders++;
}
} else {
if ($this->isFolder(str_replace($folderIdentifier, '', $blob->getName()))) {
if ($this->isFolder($blob->getName()) && substr_count($blob->getName(), '/') === 1) {
$folders++;
}
} else if ($this->isFolder(str_replace($folderIdentifier, '', $blob->getName()))) {
$folders++;
}
}

Expand All @@ -1059,7 +1051,9 @@ private function normalizeFolderName($folderName)
* Returns the identifier of the folder the file resides in
*
* @param string $fileIdentifier
*
* @return mixed
* @throws \TYPO3\CMS\Core\Resource\Exception\InvalidPathException
*/
public function getParentFolderIdentifierOfIdentifier($fileIdentifier)
{
Expand All @@ -1075,7 +1069,7 @@ protected function getStorage()
{
if (!$this->storage) {
/** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
$storageRepository = GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\StorageRepository');
$storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
$this->storage = $storageRepository->findByUid($this->storageUid);
}

Expand Down Expand Up @@ -1173,10 +1167,10 @@ protected function getBlob($fileIdentifier)
/**
* @param $name
* @param string $content
* @param CreateBlobOptions|null $options
* @param CreateBlockBlobOptions|null $options
* @return \MicrosoftAzure\Storage\Blob\Models\CopyBlobResult
*/
protected function createBlockBlob($name, $content = '', CreateBlobOptions $options = null)
protected function createBlockBlob($name, $content = '', CreateBlockBlobOptions $options = null)
{
if (!is_string($content)) {
throw new \InvalidArgumentException('Content was not type of string');
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
"MIT"
],
"require": {
"typo3/cms-core": ">=7.6.0,<=9",
"microsoft/azure-storage": "^0.15.0"
"typo3/cms-core": ">=7.6.0",
"microsoft/azure-storage-blob": "^1.2.0"
},
"require-dev": { },
"autoload": {
"psr-4": {
"B3N\\AzureStorage\\TYPO3\\": "Classes"
Expand Down

0 comments on commit 1dae21c

Please sign in to comment.