Skip to content

Commit

Permalink
Merge pull request #22 from flownative/flow-7-compatibility
Browse files Browse the repository at this point in the history
Flow 7 compatibility
  • Loading branch information
kdambekalns authored Dec 21, 2020
2 parents a5b5938 + bcd815b commit 8d4716e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
15 changes: 4 additions & 11 deletions Classes/GcsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Neos\Flow\ResourceManagement\Storage\StorageObject;
use Neos\Flow\ResourceManagement\Storage\WritableStorageInterface;
use Neos\Flow\Utility\Environment;
use Neos\Utility\Exception\FilesException;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -227,15 +226,13 @@ public function importResource($source, $collectionName): PersistentResource
public function importResourceFromContent($content, $collectionName): PersistentResource
{
$sha1Hash = sha1($content);
$md5Hash = md5($content);
$filename = $sha1Hash;

$resource = new PersistentResource();
$resource->setFilename($filename);
$resource->setFileSize(strlen($content));
$resource->setCollectionName($collectionName);
$resource->setSha1($sha1Hash);
$resource->setMd5($md5Hash);

$this->getCurrentBucket()->upload($content, [
'name' => $this->keyPrefix . $sha1Hash,
Expand Down Expand Up @@ -276,14 +273,12 @@ public function importUploadedResource(array $uploadInfo, string $collectionName
}

$sha1Hash = sha1_file($newSourcePathAndFilename);
$md5Hash = md5_file($newSourcePathAndFilename);

$resource = new PersistentResource();
$resource->setFilename($originalFilename);
$resource->setCollectionName($collectionName);
$resource->setFileSize(filesize($newSourcePathAndFilename));
$resource->setSha1($sha1Hash);
$resource->setMd5($md5Hash);

try {
$this->getCurrentBucket()->upload(fopen($newSourcePathAndFilename, 'rb'), [
Expand Down Expand Up @@ -369,24 +364,24 @@ public function getStreamByResourcePath($relativePath)
/**
* Retrieve all Objects stored in this storage.
*
* @return array<\Neos\Flow\ResourceManagement\Storage\StorageObject>
* @return StorageObject[]
* @api
*/
public function getObjects()
{
$objects = [];
foreach ($this->resourceManager->getCollectionsByStorage($this) as $collection) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$objects = array_merge($objects, $this->getObjectsByCollection($collection));
}

return $objects;
}

/**
* Retrieve all Objects stored in this storage, filtered by the given collection name
*
* @param CollectionInterface $collection
* @return array<\Neos\Flow\ResourceManagement\Storage\StorageObject>
* @return StorageObject[]
* @api
*/
public function getObjectsByCollection(CollectionInterface $collection): array
Expand Down Expand Up @@ -421,13 +416,11 @@ public function getObjectsByCollection(CollectionInterface $collection): array
protected function importTemporaryFile(string $temporaryPathAndFilename, string $collectionName): PersistentResource
{
$sha1Hash = sha1_file($temporaryPathAndFilename);
$md5Hash = md5_file($temporaryPathAndFilename);

$resource = new PersistentResource();
$resource->setFileSize(filesize($temporaryPathAndFilename));
$resource->setCollectionName($collectionName);
$resource->setSha1($sha1Hash);
$resource->setMd5($md5Hash);

$bucket = $this->getCurrentBucket();
if (!$bucket->object($this->keyPrefix . $sha1Hash)->exists()) {
Expand All @@ -449,7 +442,7 @@ protected function importTemporaryFile(string $temporaryPathAndFilename, string
throw $exception;
}

$this->logger->info(sprintf('Google Cloud Storage: Successfully imported resource as object "%s" into bucket "%s" with MD5 hash "%s"', $sha1Hash, $this->bucketName, $resource->getMd5() ?: 'unknown'), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->info(sprintf('Google Cloud Storage: Successfully imported resource as object "%s" into bucket "%s" with SHA1 hash "%s"', $sha1Hash, $this->bucketName, $resource->getSha1() ?: 'unknown'), LogEnvironment::fromMethodName(__METHOD__));
} else {
$this->logger->info(sprintf('Google Cloud Storage: Did not import resource as object "%s" into bucket "%s" because that object already existed.', $sha1Hash, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
}
Expand Down
24 changes: 12 additions & 12 deletions Classes/GcsTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Storage\StorageObject;
use GuzzleHttp\Psr7\Uri;
use Neos\Error\Messages\Error;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
Expand Down Expand Up @@ -393,7 +394,7 @@ private function publishCollectionFromDifferentGoogleCloudStorage(CollectionInte
} catch (\Exception $e) {
$this->messageCollector->append(sprintf('Could not publish resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $object->getSha1(), $collection->getName(), $storageBucketName, $this->bucketName, $e->getMessage()));
}
$this->logger->debug(sprintf('Successfully copied resource as object "%s" (MD5: %s) from bucket "%s" to bucket "%s" (with GZIP compression)', $targetObjectName, $object->getMd5() ?: 'unknown', $storageBucketName, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Successfully copied resource as object "%s" (SHA1: %s) from bucket "%s" to bucket "%s" (with GZIP compression)', $targetObjectName, $object->getSha1() ?: 'unknown', $storageBucketName, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
} else {
try {
$this->logger->debug(sprintf('Copy object "%s" to bucket "%s"', $targetObjectName, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
Expand All @@ -414,7 +415,7 @@ private function publishCollectionFromDifferentGoogleCloudStorage(CollectionInte
}
continue;
}
$this->logger->debug(sprintf('Successfully copied resource as object "%s" (MD5: %s) from bucket "%s" to bucket "%s"', $targetObjectName, $object->getMd5() ?: 'unknown', $storageBucketName, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Successfully copied resource as object "%s" (SHA1: %s) from bucket "%s" to bucket "%s"', $targetObjectName, $object->getSha1() ?: 'unknown', $storageBucketName, $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
}
unset($targetObjectName);
$iteration++;
Expand Down Expand Up @@ -488,18 +489,18 @@ public function publishResource(PersistentResource $resource, CollectionInterfac
} catch (GoogleException $e) {
$googleError = json_decode($e->getMessage());
if ($googleError instanceof \stdClass && isset($googleError->error->message)) {
$this->messageCollector->append(sprintf('Could not copy resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $resource->getSha1(), $collection->getName(), $storage->getBucketName(), $this->bucketName, $googleError->error->message), LOG_ERR, 1446721791);
$this->messageCollector->append(sprintf('Could not copy resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $resource->getSha1(), $collection->getName(), $storage->getBucketName(), $this->bucketName, $googleError->error->message), Error::SEVERITY_ERROR, 1446721791);
} else {
$this->messageCollector->append(sprintf('Could not copy resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $resource->getSha1(), $collection->getName(), $storage->getBucketName(), $this->bucketName, $e->getMessage()), LOG_ERR, 1446721791);
$this->messageCollector->append(sprintf('Could not copy resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $resource->getSha1(), $collection->getName(), $storage->getBucketName(), $this->bucketName, $e->getMessage()), Error::SEVERITY_ERROR, 1446721791);
}
return;
}

$this->logger->debug(sprintf('Successfully published resource as object "%s" (MD5: %s) by copying from bucket "%s" to bucket "%s"', $targetObjectName, $resource->getMd5() ?: 'unknown', $storage->getBucketName(), $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Successfully published resource as object "%s" (SHA1: %s) by copying from bucket "%s" to bucket "%s"', $targetObjectName, $resource->getSha1() ?: 'unknown', $storage->getBucketName(), $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
} else {
$sourceStream = $resource->getStream();
if ($sourceStream === false) {
$this->messageCollector->append(sprintf('Could not publish resource with SHA1 hash %s of collection %s because there seems to be no corresponding data in the storage.', $resource->getSha1(), $collection->getName()), LOG_ERR, 1446721810);
$this->messageCollector->append(sprintf('Could not publish resource with SHA1 hash %s of collection %s because there seems to be no corresponding data in the storage.', $resource->getSha1(), $collection->getName()), Error::SEVERITY_ERROR, 1446721810);
return;
}
$this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($resource), $resource);
Expand All @@ -523,7 +524,7 @@ public function unpublishResource(PersistentResource $resource): void
try {
$objectName = $this->keyPrefix . $this->getRelativePublicationPathAndFilename($resource);
$this->getCurrentBucket()->object($objectName)->delete();
$this->logger->debug(sprintf('Successfully unpublished resource as object "%s" (MD5: %s) from bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Successfully unpublished resource as object "%s" (SHA1: %s) from bucket "%s"', $objectName, $resource->getSha1() ?: 'unknown', $this->bucketName), LogEnvironment::fromMethodName(__METHOD__));
} catch (NotFoundException $e) {
}
}
Expand Down Expand Up @@ -552,7 +553,6 @@ public function getPublicPersistentResourceUri(PersistentResource $resource): st
'{bucketName}' => $this->bucketName,
'{keyPrefix}' => $this->keyPrefix,
'{sha1}' => $resource->getSha1(),
'{md5}' => $resource->getMd5(),
'{filename}' => $resource->getFilename(),
'{fileExtension}' => $resource->getFileExtension()
];
Expand Down Expand Up @@ -603,16 +603,16 @@ protected function publishFile($sourceStream, string $relativeTargetPathAndFilen
$sourceStream = fopen($temporaryTargetPathAndFilename, 'rb');
$uploadParameters['metadata']['contentEncoding'] = 'gzip';

$this->logger->debug(sprintf('Converted resource data of object "%s" in bucket "%s" with MD5 hash "%s" to GZIP with level %s.', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown', $this->gzipCompressionLevel), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Converted resource data of object "%s" in bucket "%s" with SHA1 hash "%s" to GZIP with level %s.', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown', $this->gzipCompressionLevel), LogEnvironment::fromMethodName(__METHOD__));
} catch (\Exception $e) {
$this->messageCollector->append(sprintf('Failed publishing resource as object "%s" in bucket "%s" with MD5 hash "%s": %s', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown', $e->getMessage()), LOG_WARNING, 1520257344878);
$this->messageCollector->append(sprintf('Failed publishing resource as object "%s" in bucket "%s" with SHA1 hash "%s": %s', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown', $e->getMessage()), Error::SEVERITY_WARNING, 1520257344878);
}
}
try {
$this->getCurrentBucket()->upload($sourceStream, $uploadParameters);
$this->logger->debug(sprintf('Successfully published resource as object "%s" in bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown'), LogEnvironment::fromMethodName(__METHOD__));
$this->logger->debug(sprintf('Successfully published resource as object "%s" in bucket "%s" with SHA1 hash "%s"', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown'), LogEnvironment::fromMethodName(__METHOD__));
} catch (\Exception $e) {
$this->messageCollector->append(sprintf('Failed publishing resource as object "%s" in bucket "%s" with MD5 hash "%s": %s', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown', $e->getMessage()), LOG_WARNING, 1506847965352);
$this->messageCollector->append(sprintf('Failed publishing resource as object "%s" in bucket "%s" with SHA1 hash "%s": %s', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown', $e->getMessage()), Error::SEVERITY_WARNING, 1506847965352);
} finally {
if (is_resource($sourceStream)) {
fclose($sourceStream);
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ The possible placeholders are:
- `{bucketName}` The target's bucket name
- `{keyPrefix}` The target's configured key prefix
- `{sha1}` The resource's SHA1
- `{md5}` The resource's MD5 🙄
- `{filename}` The resource's full filename, for example "logo.svg"
- `{fileExtension}` The resource's file extension, for example "svg"

Expand Down
25 changes: 21 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
"MIT"
],
"require": {
"neos/flow": "^5.2 || ^6.0 || @dev",
"neos/flow": "^5.2 || ^6.0 || ^7.0",
"google/cloud-storage": "1.1.*",
"guzzlehttp/psr7": "1.*",
"ext-json": "*",
"ext-pdo": "*",
"ext-zlib": "*"
Expand Down Expand Up @@ -65,7 +64,25 @@
"Neos.Kickstart-20161124230102",
"Neos.Imagine-20161124231742",
"Neos.Media-20161124233100",
"Neos.Kickstarter-20161125110814"
"Neos.Kickstarter-20161125110814",
"TYPO3.Form-20160601101500",
"Neos.Twitter.Bootstrap-20161124204912",
"Neos.Form-20161124205254",
"Neos.Party-20161124225257",
"Neos.SwiftMailer-20161130105617",
"Neos.Seo-20170127154600",
"Neos.Flow-20180415105700",
"Neos.Neos-20180907103800",
"Neos.Neos.Ui-20190319094900",
"Neos.Flow-20190425144900",
"Neos.Flow-20190515215000",
"Neos.NodeTypes-20190917101945",
"Neos.NodeTypes-20200120114136",
"Neos.Flow-20200813181400",
"Neos.Flow-20201003165200",
"Neos.Flow-20201109224100",
"Neos.Flow-20201205172733",
"Neos.Flow-20201207104500"
]
}
}
}

0 comments on commit 8d4716e

Please sign in to comment.