-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from sitegeist/uploadFieldsInclusion
add failing migrations and include upload fields in the export
- Loading branch information
Showing
10 changed files
with
302 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sitegeist.StoneTablet package. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\StoneTablet\Domain; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\ResourceManagement\ResourceRepository; | ||
use Neos\Utility\Files; | ||
use Sitegeist\StoneTablet\Domain\Form\RegisteredUploadField; | ||
|
||
final class Archive | ||
{ | ||
#[Flow\Inject] | ||
protected ResourceRepository $resourceRepository; | ||
|
||
public function exportResourceFromRegisteredUploadField( | ||
RegisteredUploadField $registeredUploadField, | ||
ExportDirectory $exportDirectory | ||
): string { | ||
$resourceSha1Hash = $registeredUploadField->extractSha1(); | ||
|
||
$uploadFile = $this->resourceRepository->findOneBySha1($resourceSha1Hash); | ||
|
||
if ($uploadFile) { | ||
$resourceStream = $uploadFile->getStream(); | ||
|
||
if($resourceStream) { | ||
file_put_contents( | ||
$exportDirectory->getUploadPath() . $registeredUploadField->extractExportFileName(), | ||
stream_get_contents($resourceStream) | ||
); | ||
fclose($resourceStream); | ||
} | ||
return $uploadFile->getFilename(); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function compressExportDirectory(ExportDirectory $exportDirectory): ?string | ||
{ | ||
$rootPath = realpath($exportDirectory->path); | ||
$archivePath = $exportDirectory->path . 'Archive.zip'; | ||
$zip = new \ZipArchive(); | ||
|
||
if ($zip->open($archivePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) { | ||
foreach (Files::readDirectoryRecursively($exportDirectory->path) as $filePath) { | ||
if (!is_dir($filePath)) | ||
{ | ||
$relativePath = substr($filePath, strlen($rootPath) + 1); | ||
|
||
// Add current file to archive | ||
$zip->addFile($filePath, $relativePath); | ||
} | ||
} | ||
|
||
$zip->close(); | ||
return $archivePath; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sitegeist.StoneTablet package. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\StoneTablet\Domain; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Utility\Files; | ||
|
||
#[Flow\Proxy(false)] | ||
class ExportDirectory | ||
{ | ||
public const EXPORT_ROOT = FLOW_PATH_DATA . 'Temporary/StoneTablet/'; | ||
public const UPLOADS = 'Uploads/'; | ||
public function __construct( | ||
public readonly string $path, | ||
) { | ||
} | ||
|
||
public static function create(): self { | ||
$date = new \DateTime('now'); | ||
$directoryPathAndName = self::EXPORT_ROOT . 'Export-' . $date->format('Y-m-d_H-i') . '/'; | ||
|
||
if (file_exists($directoryPathAndName)) { | ||
Files::removeDirectoryRecursively($directoryPathAndName); | ||
} | ||
|
||
Files::createDirectoryRecursively($directoryPathAndName . self::UPLOADS); | ||
|
||
return new self( | ||
$directoryPathAndName . '/' | ||
); | ||
} | ||
|
||
public function getUploadPath(): string | ||
{ | ||
return $this->path . self::UPLOADS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sitegeist.StoneTablet package. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\StoneTablet\Domain\Form; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\ResourceManagement\PersistentResource; | ||
|
||
/** | ||
* The PersistedUploadFile domain value object | ||
*/ | ||
#[Flow\Proxy(false)] | ||
final class RegisteredUploadField implements \Stringable | ||
{ | ||
const CELL_PREFIX = '#upload_field_'; | ||
public function __construct( | ||
public readonly string $value | ||
) { | ||
} | ||
public static function fromResource(?PersistentResource $resource): self | ||
{ | ||
return new self( | ||
$resource ? self::CELL_PREFIX . $resource->getSha1() . '.' . $resource->getFileExtension() : '' | ||
); | ||
} | ||
public function extractExportFileName(): string | ||
{ | ||
return substr($this->value, strlen(self::CELL_PREFIX)); | ||
} | ||
|
||
public function extractSha1(): string | ||
{ | ||
return strtok( | ||
substr($this->value, strlen(self::CELL_PREFIX)), | ||
'.' | ||
); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.