Skip to content

Commit

Permalink
Merge pull request #8 from benjaminhirsch/development
Browse files Browse the repository at this point in the history
Release Version 0.2.0
  • Loading branch information
benjaminhirsch authored May 20, 2017
2 parents b82c000 + e017357 commit acecc23
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 223 deletions.
105 changes: 51 additions & 54 deletions Classes/Driver/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use MicrosoftAzure\Storage\Blob\Models\GetBlobResult;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsResult;
use MicrosoftAzure\Storage\Common\ServiceException;
use MicrosoftAzure\Storage\Common\ServicesBuilder;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
Expand Down Expand Up @@ -61,27 +60,18 @@ class StorageDriver extends AbstractHierarchicalFilesystemDriver
*/
private $cache;

/**
* @var bool
*/
private $cacheEnabled;

/**
* Initialize this driver and expose the capabilities for the repository to use
*
* @param array $configuration
*/
public function __construct(array $configuration = [], $cacheEnabled = true)
public function __construct(array $configuration = [])
{
parent::__construct($configuration);

$this->capabilities = ResourceStorage::CAPABILITY_BROWSABLE | ResourceStorage::CAPABILITY_PUBLIC | ResourceStorage::CAPABILITY_WRITABLE;

$this->cacheEnabled = $cacheEnabled;

if ($this->cacheEnabled === true) {
$this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('azurestorage');
}
$this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('azurestorage');
}

/**
Expand Down Expand Up @@ -230,6 +220,8 @@ public function deleteFolder($folderIdentifier, $deleteRecursively = false)
foreach ($blobs as $blob) {
$this->blobService->deleteBlob($this->container, $blob->getName());
}

return true;
}

/**
Expand Down Expand Up @@ -262,7 +254,7 @@ public function folderExists($folderIdentifier)

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

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

Expand Down Expand Up @@ -395,6 +387,8 @@ public function replaceFile($fileIdentifier, $localFilePath)
$targetFolder = $this->normalizeFolderName(dirname($fileIdentifier));
$newName = basename($fileIdentifier);
$this->addFile($localFilePath, $targetFolder, $newName);

return true;
}

/**
Expand All @@ -404,23 +398,19 @@ public function replaceFile($fileIdentifier, $localFilePath)
*
* @param string $fileIdentifier
* @return bool TRUE if deleting the file succeeded
* @throws \Exception
*/
public function deleteFile($fileIdentifier)
{
try {
if ($this->fileExists($fileIdentifier)) {
$this->blobService->deleteBlob($this->container, $fileIdentifier);

if ($this->cacheEnabled === true) {
$this->cache->remove($this->hash($fileIdentifier));
}
$this->cache->remove($this->hash($fileIdentifier));
}

return true;
} catch (ServiceException $e) {

throw new \Exception($e->getMessage());
} catch (\Throwable $e) {
return false;
}
}

Expand Down Expand Up @@ -450,6 +440,10 @@ public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier,
$targetName = $this->normalizeFolderName($targetFolderIdentifier) . $newFileName;
$this->move($fileIdentifier, $targetName);

if ($this->cache->has($this->hash($fileIdentifier))) {
$this->cache->remove($this->hash($fileIdentifier));
}

return $targetName;
}

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

/**
Expand All @@ -497,7 +492,7 @@ public function getFileContents($fileIdentifier)
/** @var GetBlobResult $blob */
$blob = $this->getBlob($fileIdentifier, true);

if ($blob !== false) {
if ($blob instanceof GetBlobResult) {
$content = stream_get_contents($blob->getContentStream());
}

Expand All @@ -514,6 +509,8 @@ public function getFileContents($fileIdentifier)
public function setFileContents($fileIdentifier, $contents)
{
$this->blobService->createBlockBlob($this->container, $fileIdentifier, $contents);

return strlen($contents);
}

/**
Expand All @@ -528,7 +525,7 @@ public function fileExistsInFolder($fileName, $folderIdentifier)
$folderIdentifier = $this->normalizeFolderName($folderIdentifier);
$blob = $this->getBlob($folderIdentifier . $fileName);

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

Expand All @@ -548,7 +545,7 @@ public function folderExistsInFolder($folderName, $folderIdentifier)
$folderName = $this->normalizeFolderName($folderName);
$blob = $this->getBlob($this->normalizeFolderName($folderIdentifier . $folderName));

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

Expand Down Expand Up @@ -605,17 +602,14 @@ public function getPermissions($identifier)
*
* @param string $identifier
* @return void
* @throws \Exception
*/
public function dumpFileContents($identifier)
{
try {
/** @var GetBlobResult $blob */
$blob = $this->getBlob($identifier, true);
fpassthru($blob->getContentStream());
} catch (ServiceException $e) {
throw new \Exception($e->getMessage());
}
} catch (\Throwable $e) { }
}

/**
Expand All @@ -634,12 +628,12 @@ public function dumpFileContents($identifier)
public function isWithin($folderIdentifier, $identifier)
{
$folderIdentifier = $this->normalizeFolderName($folderIdentifier);

if ($folderIdentifier === '') {
return true;
}

return GeneralUtility::isFirstPartOfStr($identifier, $folderIdentifier);

}

/**
Expand Down Expand Up @@ -716,7 +710,6 @@ public function getFileInFolder($fileName, $folderIdentifier)
* should fall back to "name".
* @param bool $sortRev TRUE to indicate reverse sorting (last to first)
* @return array of FileIdentifiers
* @throws \Exception
*/
public function getFilesInFolder(
$folderIdentifier,
Expand Down Expand Up @@ -757,9 +750,7 @@ public function getFilesInFolder(

}

} catch (ServiceException $e) {
throw new \Exception($e->getMessage());
}
} catch (\Throwable $e) { }

return $files;

Expand Down Expand Up @@ -1014,7 +1005,7 @@ protected function getStorage()
*/
protected function getProcessingFolder()
{
return $this->getStorage()->getProcessingFolder()->getName();
return $this->getStorage()->getProcessingFolders() ? $this->getStorage()->getProcessingFolders()[0] : '_processed_';
}

/**
Expand All @@ -1026,6 +1017,11 @@ protected function isFolder($fileIdentifier)
return (substr($fileIdentifier, -1) === '/');
}

/**
* @param $fileIdentifier
* @param bool $force
* @return bool|GetBlobResult
*/
protected function getBlob($fileIdentifier, $force = false)
{

Expand All @@ -1036,22 +1032,16 @@ protected function getBlob($fileIdentifier, $force = false)
try {

$blob = null;
if ($this->cacheEnabled === true) {
$blob = $this->cache->get($this->hash($fileIdentifier));
}
$blob = $this->cache->get($this->hash($fileIdentifier));

if (!$blob instanceof GetBlobResult || $force === true) {
/** @var GetBlobResult $blob */
$blob = $this->blobService->getBlob($this->container, $fileIdentifier);

if ($this->cacheEnabled === true) {
$this->cache->set($this->hash($fileIdentifier), $blob);
}
$this->cache->set($this->hash($fileIdentifier), $blob);
}

} catch (ServiceException $e) {

}
} catch (\Throwable $e) { }

if (isset($blob) && $blob instanceof GetBlobResult) {
return $blob;
Expand All @@ -1060,7 +1050,13 @@ protected function getBlob($fileIdentifier, $force = false)
return false;
}

protected function createBlockBlob($name, $content = '', $options = null)
/**
* @param $name
* @param string $content
* @param CreateBlobOptions|null $options
* @return \MicrosoftAzure\Storage\Blob\Models\CopyBlobResult
*/
protected function createBlockBlob($name, $content = '', CreateBlobOptions $options = null)
{
if (!is_string($content)) {
throw new \InvalidArgumentException('Content was not type of string');
Expand All @@ -1070,7 +1066,7 @@ protected function createBlockBlob($name, $content = '', $options = null)
$content = chr(26);
}

$this->blobService->createBlockBlob($this->container, $name, $content, $options);
return $this->blobService->createBlockBlob($this->container, $name, $content, $options);
}

/**
Expand Down Expand Up @@ -1121,30 +1117,31 @@ protected function moveOrCopyFolderWithinStorage(
/**
* @param string $sourceIdentifier
* @param string $targetIdentifier
* @throws \Exception
*/
protected function copy($sourceIdentifier, $targetIdentifier)
{
try {
$this->blobService->copyBlob($this->container, $targetIdentifier, $this->container, $sourceIdentifier);
} catch (ServiceException $e) {
throw new \Exception($e->getMessage());
}
} catch (\Throwable $e) { }
}

/**
* @param string $sourceIdentifier
* @param string $destinationIdentifier
* @throws \Exception
*/
protected function move($sourceIdentifier, $destinationIdentifier)
{

$this->copy($sourceIdentifier, $destinationIdentifier);

try {
$this->blobService->deleteBlob($this->container, $sourceIdentifier);
} catch (ServiceException $e) {
throw new \Exception($e->getMessage());
}

if ($this->cache->has($this->hash($sourceIdentifier))) {
$this->cache->remove($this->hash($sourceIdentifier));
}

} catch (\Throwable $e) { }
}

/**
Expand Down
Loading

0 comments on commit acecc23

Please sign in to comment.