Skip to content
Draft
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 @@ -10,7 +10,7 @@ insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{php}]
[*.php]
charset = utf-8
indent_style = tab

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@eaDir
.idea
55 changes: 43 additions & 12 deletions Classes/Domain/Repository/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use TYPO3\CMS\Core\Utility\ArrayUtility;
use Aws\S3\S3Client;
use Aws\Credentials\Credentials;
use \TYPO3\CMS\Core\Core\ApplicationContext;
use \TYPO3\CMS\Core\Utility\GeneralUtility;

/***************************************************************
* Copyright notice
Expand Down Expand Up @@ -36,7 +38,6 @@ class FileRepository extends \TYPO3\CMS\Extbase\Persistence\Repository

protected $baseStoragePath = 'fileadmin/cicbase/documents';
protected $holdStoragePath = 'typo3temp/cicbase/documents';
protected $AWSEnabled = true;
protected $cicbaseConfiguration = [];

/**
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function getCache()
try {
$cache = $this->cacheManager->getCache('cicbase_cache');
} catch (\TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException $e) {
throw new \Exception ('Unable to load the cicbase cache.');
throw new \Exception ($this->getExceptionMessage($e, 'Unable to load the cicbase cache.'));
}
return $cache;
}
Expand Down Expand Up @@ -220,15 +221,21 @@ protected function getRelativeDestinationPath(\CIC\Cicbase\Domain\Model\File $fi
*/
protected function initializeS3()
{
return new S3Client([
$args = [
'version' => 'latest',
'region' => $this->cicbaseConfiguration['AWSRegion'],
'credentials' => [
//'debug' => true
];

// Credentials are optional, access could be set by IAM roles
if($this->cicbaseConfiguration['AWSKey'] || $this->cicbaseConfiguration['AWSSecret']) {
$args['credentials'] = [
'key' => $this->cicbaseConfiguration['AWSKey'],
'secret' => $this->cicbaseConfiguration['AWSSecret']
],
//'debug' => true
]);
];
}

return new S3Client($args);
}

/**
Expand All @@ -244,8 +251,6 @@ protected function moveToAWSDestination($relativeDestinationPath, $destinationFi
// make sure we have adequate configuration.
if (!$this->cicbaseConfiguration['AWSTemporaryBucketName'] ||
!$this->cicbaseConfiguration['AWSPermanentBucketName'] ||
!$this->cicbaseConfiguration['AWSKey'] ||
!$this->cicbaseConfiguration['AWSSecret'] ||
!$this->cicbaseConfiguration['AWSRegion']
) {
throw new \Exception ('AWS File Storage is enabled, yet it is not properly configured in the extension manager');
Expand Down Expand Up @@ -283,7 +288,10 @@ protected function moveToAWSDestination($relativeDestinationPath, $destinationFi
'Key' => $source . '/' . $fileObject->getFilename()
]);
} catch (\Exception $e) {
return new \TYPO3\CMS\Extbase\Error\Error('Unable to save file to AWS S3', 1336600878);
return new \TYPO3\CMS\Extbase\Error\Error(
$this->getExceptionMessage($e, 'Unable to save file to AWS S3'),
1336600878
);
}
} else {
try {
Expand All @@ -297,7 +305,10 @@ protected function moveToAWSDestination($relativeDestinationPath, $destinationFi
$fileObject->setPath($relativeDestinationPath);
$fileObject->setAwsBucket($destinationBucket);
} catch (\Exception $e) {
return new \TYPO3\CMS\Extbase\Error\Error('Unable to save file to AWS S3', 1336600875);
return new \TYPO3\CMS\Extbase\Error\Error(
$this->getExceptionMessage($e, 'Unable to save file to AWS S3'),
1336600875
);
}
}
}
Expand All @@ -322,7 +333,9 @@ protected function moveToDestination($relativeDestinationPath, $destinationFilen
} catch (\Exception $e) {
// This is a 'compile-time' error, not a run-time one.
// Throwing an exception is appropriate.
throw new \Exception ('Cannot create directory for storing files: ' . $absoluteDestinationPath);
throw new \Exception (
$this->getExceptionMessage($e, 'Cannot create directory for storing files: ' . $absoluteDestinationPath)
);
}
}
$source = $fileObject->getPath();
Expand Down Expand Up @@ -381,4 +394,22 @@ public function initializeObject()
$this->defaultQuerySettings->setStoragePageIds(explode(',', $configuration['storagePids'][$this->objectType]));
}
}

/**
* Get the exception message along with additional message, if in testing or development context
*
* @param \Exception $exception The exception object
* @param string $message Additional message to be appended
* @return string The combined message if in testing or development context, otherwise just the additional message
*/
private function getExceptionMessage(\Exception $exception, string $message)
{
$applicationContext = GeneralUtility::getApplicationContext();
if($applicationContext->isTesting() || $applicationContext->isDevelopment()) {
$exceptionMessage = $exception->getMessage();
return $message . " Exception message: ". $exceptionMessage;
}

return $message;
}
}
Loading