1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TildBJ\Seeder\Provider\Provider; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Image |
7
|
|
|
* |
8
|
|
|
* @package TildBJ\Seeder\Provider\Provider |
9
|
|
|
*/ |
10
|
|
|
class Image implements \TildBJ\Seeder\Provider |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \TildBJ\Seeder\Faker $faker |
15
|
|
|
*/ |
16
|
|
|
protected $faker; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Image constructor. |
20
|
|
|
* @param \TildBJ\Seeder\Faker $faker |
21
|
|
|
*/ |
22
|
|
|
public function __construct(\TildBJ\Seeder\Faker $faker) |
23
|
|
|
{ |
24
|
|
|
$this->faker = $faker; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
* @throws \Exception |
30
|
|
|
* @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException |
31
|
|
|
* @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException |
32
|
|
|
* @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException |
33
|
|
|
* @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException |
34
|
|
|
*/ |
35
|
|
|
public function generate() |
36
|
|
|
{ |
37
|
|
|
/** @var \TYPO3\CMS\Core\Resource\StorageRepository $storageRepository */ |
38
|
|
|
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
39
|
|
|
\TYPO3\CMS\Core\Resource\StorageRepository::class |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */ |
43
|
|
|
$storage = reset($storageRepository->findAll()); |
|
|
|
|
44
|
|
|
|
45
|
|
|
if (!is_dir(PATH_site . 'typo3temp/import/')) { |
46
|
|
|
mkdir(PATH_site . 'typo3temp/import/'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$folder = $storage->getFolder($storage->getDefaultFolder()->getIdentifier() . 'import'); |
51
|
|
|
} catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $exception) { |
52
|
|
|
$folder = $storage->createFolder($storage->getDefaultFolder()->getIdentifier() . 'import'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$url = $this->faker->getImageUrl(); |
56
|
|
|
$fileName = uniqid() . '.jpg'; |
57
|
|
|
if ($storage->hasFile($folder->getIdentifier() . $fileName)) { |
58
|
|
|
$file = $storage->getFile($folder->getIdentifier() . $fileName); |
59
|
|
|
} else { |
60
|
|
|
$img = PATH_site . 'typo3temp/import/' . $fileName; |
61
|
|
|
file_put_contents($img, file_get_contents($url)); |
62
|
|
|
|
63
|
|
|
$file = $storage->addFile($img, $folder, $fileName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var \TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory */ |
67
|
|
|
$resourceFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
68
|
|
|
\TYPO3\CMS\Core\Resource\ResourceFactory::class |
69
|
|
|
); |
70
|
|
|
$falFileReference = $resourceFactory->createFileReferenceObject( |
71
|
|
|
[ |
72
|
|
|
'uid_local' => $file->getUid(), |
73
|
|
|
'uid_foreign' => uniqid('NEW_'), |
74
|
|
|
'uid' => uniqid('NEW_'), |
75
|
|
|
'crop' => null, |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
/** @var \TYPO3\CMS\Extbase\Domain\Model\FileReference $fileReference */ |
79
|
|
|
$fileReference = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
80
|
|
|
\TYPO3\CMS\Extbase\Domain\Model\FileReference::class |
81
|
|
|
); |
82
|
|
|
$fileReference->setOriginalResource($falFileReference); |
83
|
|
|
return $file->getProperty('uid'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|