Skip to content

Commit d628f57

Browse files
authored
[NSDTRINITY-90] fix for not uploaded physical files in asset import and clean up temporary unzipped files (#15)
in co-working with @lukadschaak
1 parent 5f56af2 commit d628f57

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

src/Controller/Admin/Base/AbstractImportBaseController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\ImportRepositoryInterface;
77
use Pimcore\Model\Element\AbstractElement;
88
use Pimcore\Model\Element\DuplicateFullPathException;
9+
use Psr\Log\LoggerInterface;
910
use Symfony\Component\HttpFoundation\File\UploadedFile;
1011
use Symfony\Component\HttpFoundation\JsonResponse;
1112
use Symfony\Component\HttpFoundation\Request;
@@ -39,6 +40,7 @@ abstract class AbstractImportBaseController
3940
* @param ImportRepositoryInterface<TElement> $repository
4041
*/
4142
public function __construct(
43+
protected LoggerInterface $logger,
4244
protected ImportRepositoryInterface $repository,
4345
protected string $elementType = 'Element',
4446
) {
@@ -66,6 +68,12 @@ public function import(Request $request): JsonResponse
6668
}
6769
} catch (\Exception $e) {
6870
return $this->createJsonResponse(false, $e->getMessage(), 500);
71+
} finally {
72+
try {
73+
$this->cleanUp();
74+
} catch (\Throwable $cleanupError) {
75+
$this->logger->warning($cleanupError->getMessage());
76+
}
6977
}
7078

7179
return $this->createJsonResponse(true, $this->createResultMessage());
@@ -123,4 +131,9 @@ protected function createResultMessage(): string
123131
* @throws DuplicateFullPathException
124132
*/
125133
abstract protected function importByFile(UploadedFile $file, string $format): array;
134+
135+
protected function cleanUp(): void
136+
{
137+
// implement clean ups in subclasses if necessary
138+
}
126139
}

src/Controller/Admin/ImportAssetsController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Neusta\Pimcore\ImportExportBundle\Import\ZipImporter;
88
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\AssetRepository;
99
use Pimcore\Model\Asset;
10+
use Psr\Log\LoggerInterface;
1011
use Symfony\Component\HttpFoundation\File\UploadedFile;
1112
use Symfony\Component\HttpFoundation\JsonResponse;
1213
use Symfony\Component\HttpFoundation\Request;
@@ -21,11 +22,12 @@ final class ImportAssetsController extends AbstractImportBaseController
2122
* @param Importer<\ArrayObject<int|string, mixed>, Asset> $importer
2223
*/
2324
public function __construct(
25+
LoggerInterface $logger,
2426
private Importer $importer,
2527
private ZipImporter $zipImporter,
2628
AssetRepository $assetRepository,
2729
) {
28-
parent::__construct($assetRepository, 'Asset');
30+
parent::__construct($logger, $assetRepository, 'Asset');
2931
}
3032

3133
#[Route(
@@ -52,10 +54,7 @@ protected function importByFile(UploadedFile $file, string $format): array
5254
&& $asset->getKey()
5355
&& \array_key_exists($asset->getKey(), $zipContent[$asset->getType()])
5456
) {
55-
$asset->setData($zipContent[$asset->getType()][$asset->getKey()]);
56-
if ($this->overwrite) {
57-
$asset->save(['versionNote' => 'added by pimcore-import-export-bundle']);
58-
}
57+
$asset->setData(file_get_contents($zipContent[$asset->getType()][$asset->getKey()]->getRealPath()));
5958
}
6059
}
6160

@@ -71,4 +70,9 @@ protected function importByFile(UploadedFile $file, string $format): array
7170
throw new \Exception('Error reading uploaded file: ' . $e->getMessage(), 0, $e);
7271
}
7372
}
73+
74+
protected function cleanUp(): void
75+
{
76+
$this->zipImporter->cleanUp();
77+
}
7478
}

src/Controller/Admin/ImportDataObjectsController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Neusta\Pimcore\ImportExportBundle\Import\Importer;
77
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\DataObjectRepository;
88
use Pimcore\Model\DataObject;
9+
use Psr\Log\LoggerInterface;
910
use Symfony\Component\HttpFoundation\File\UploadedFile;
1011
use Symfony\Component\HttpFoundation\JsonResponse;
1112
use Symfony\Component\HttpFoundation\Request;
@@ -20,10 +21,11 @@ final class ImportDataObjectsController extends AbstractImportBaseController
2021
* @param Importer<\ArrayObject<int|string, mixed>, DataObject> $importer
2122
*/
2223
public function __construct(
24+
LoggerInterface $logger,
2325
DataObjectRepository $repository,
2426
private Importer $importer,
2527
) {
26-
parent::__construct($repository, 'DataObject');
28+
parent::__construct($logger, $repository, 'DataObject');
2729
}
2830

2931
#[Route(

src/Controller/Admin/ImportDocumentsController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Neusta\Pimcore\ImportExportBundle\Import\Importer;
77
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\DocumentRepository;
88
use Pimcore\Model\Document;
9+
use Psr\Log\LoggerInterface;
910
use Symfony\Component\HttpFoundation\File\UploadedFile;
1011
use Symfony\Component\HttpFoundation\JsonResponse;
1112
use Symfony\Component\HttpFoundation\Request;
@@ -20,10 +21,11 @@ final class ImportDocumentsController extends AbstractImportBaseController
2021
* @param Importer<\ArrayObject<int|string, mixed>, Document> $importer
2122
*/
2223
public function __construct(
24+
LoggerInterface $logger,
2325
DocumentRepository $repository,
2426
private Importer $importer,
2527
) {
26-
parent::__construct($repository, 'Document');
28+
parent::__construct($logger, $repository, 'Document');
2729
}
2830

2931
#[Route(

src/Import/ZipImporter.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
namespace Neusta\Pimcore\ImportExportBundle\Import;
44

55
use Symfony\Component\Filesystem\Filesystem;
6+
use Symfony\Component\Finder\Finder;
67

78
class ZipImporter
89
{
10+
/** @var string */
11+
private const ZIPIMPORT_DIR_PREFIX = 'zipimport_';
912
private Filesystem $filesystem;
1013
private string $tempDir;
1114

12-
public function __construct(?string $tempDir = null)
13-
{
15+
public function __construct(
16+
?string $tempDir = null,
17+
) {
1418
$this->filesystem = new Filesystem();
1519
$this->tempDir = $tempDir ?? sys_get_temp_dir();
1620
}
@@ -47,7 +51,7 @@ public function import(string $zipPath): array
4751

4852
private function createTempExtractPath(): string
4953
{
50-
$extractPath = $this->tempDir . '/zipimport_' . uniqid();
54+
$extractPath = $this->tempDir . '/' . self::ZIPIMPORT_DIR_PREFIX . uniqid();
5155
$this->filesystem->mkdir($extractPath);
5256

5357
return $extractPath;
@@ -66,7 +70,7 @@ private function findYamlFile(string $dir): ?string
6670
}
6771

6872
/**
69-
* @return array<string, array<string, string>>
73+
* @return array<string, array<string, \SplFileInfo>>
7074
*/
7175
private function collectAssetDirectories(string $basePath): array
7276
{
@@ -88,7 +92,7 @@ private function collectAssetDirectories(string $basePath): array
8892
continue;
8993
}
9094

91-
$files = $this->collectFilesFromDir($fullPath);
95+
$files = $this->collectFileInfosFromDir($fullPath);
9296
if (!empty($files)) {
9397
$assets[$entry] = $files;
9498
}
@@ -98,22 +102,31 @@ private function collectAssetDirectories(string $basePath): array
98102
}
99103

100104
/**
101-
* @return array<string, string>
105+
* @return array<string, \SplFileInfo>
102106
*/
103-
private function collectFilesFromDir(string $dir): array
107+
private function collectFileInfosFromDir(string $dir): array
104108
{
105109
$files = [];
106110
$rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
107111
foreach ($rii as $file) {
108112
if ($file->isFile()) {
109113
$filename = $file->getFilename();
110-
$content = file_get_contents($file->getPathname());
111-
if (false !== $content) {
112-
$files[$filename] = $content;
113-
}
114+
$files[$filename] = new \SplFileInfo($file->getPathname());
114115
}
115116
}
116117

117118
return $files;
118119
}
120+
121+
public function cleanUp(): void
122+
{
123+
$finder = new Finder();
124+
$finder->directories()
125+
->in($this->tempDir)
126+
->name(self::ZIPIMPORT_DIR_PREFIX . '*');
127+
128+
foreach ($finder as $dir) {
129+
$this->filesystem->remove($dir->getRealPath());
130+
}
131+
}
119132
}

0 commit comments

Comments
 (0)