-
Notifications
You must be signed in to change notification settings - Fork 139
/
Asset.php
305 lines (247 loc) · 8.57 KB
/
Asset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace craft\feedme\elements;
use Cake\Utility\Hash;
use Craft;
use craft\base\ElementInterface;
use craft\elements\Asset as AssetElement;
use craft\feedme\base\Element;
use craft\feedme\events\FeedProcessEvent;
use craft\feedme\helpers\AssetHelper;
use craft\feedme\helpers\DuplicateHelper;
use craft\feedme\services\Process;
use craft\helpers\Assets as AssetsHelper;
use craft\helpers\UrlHelper;
use craft\models\VolumeFolder;
use yii\base\Event;
use yii\base\Exception;
/**
*
* @property-read string $mappingTemplate
* @property-read mixed $groups
* @property-write mixed $model
* @property-read string $groupsTemplate
* @property-read string $columnTemplate
*/
class Asset extends Element
{
// Properties
// =========================================================================
/**
* @var string
*/
public static string $name = 'Asset';
/**
* @var string
*/
public static string $class = AssetElement::class;
// Templates
// =========================================================================
/**
* @inheritDoc
*/
public function getGroupsTemplate(): string
{
return 'feed-me/_includes/elements/assets/groups';
}
/**
* @inheritDoc
*/
public function getColumnTemplate(): string
{
return 'feed-me/_includes/elements/assets/column';
}
/**
* @inheritDoc
*/
public function getMappingTemplate(): string
{
return 'feed-me/_includes/elements/assets/map';
}
// Public Methods
// =========================================================================
/**
* @inheritDoc
*/
public function init(): void
{
// If we are adding a new asset, it has to be done before the content is populated on the element.
// We of course, want content to be populated on the newly-created element, not one that won't be uploaded
Event::on(Process::class, Process::EVENT_STEP_BEFORE_PARSE_CONTENT, function(FeedProcessEvent $event) {
$this->_handleImageCreation($event);
});
}
/**
* @inheritDoc
*/
public function getGroups(): array
{
return Craft::$app->volumes->getAllVolumes();
}
/**
* @inheritDoc
*/
public function getQuery($settings, array $params = []): mixed
{
$query = AssetElement::find()
->status(null)
->volumeId($settings['elementGroup'][AssetElement::class])
->includeSubfolders()
->siteId(Hash::get($settings, 'siteId') ?: Craft::$app->getSites()->getPrimarySite()->id);
Craft::configure($query, $params);
return $query;
}
/**
* @inheritDoc
*/
public function setModel($settings): ElementInterface
{
$this->element = new AssetElement();
$this->element->volumeId = $settings['elementGroup'][AssetElement::class];
$siteId = Hash::get($settings, 'siteId');
if ($siteId) {
$this->element->siteId = $siteId;
}
return $this->element;
}
public function beforeSave($element, $settings): bool
{
parent::beforeSave($element, $settings);
// If we don't have an ID for this element, we don't want to continue. The reason is that we only want Feed Me to
// update an asset, not create a new one. Instead, asset-creation (upload from remote) is handled earlier in processing.
// If this were to proceed, we'd get invalid assets created.
if (!$element->id) {
return false;
}
return true;
}
// Private Methods
// =========================================================================
/**
* @param $event
* @throws Exception
*/
private function _handleImageCreation($event): void
{
$feed = $event->feed;
$feedData = $event->feedData;
// If we're not adding new assets, skip this altogether
if (!DuplicateHelper::isAdd($feed)) {
return;
}
$fieldInfo = $feed['fieldMapping']['urlOrPath'] ?? [];
// Just in case...
if (!$fieldInfo) {
return;
}
$folderIdInfo = $feed['fieldMapping']['folderId'] ?? [];
$fileNameInfo = $feed['fieldMapping']['filename'] ?? [];
$value = $this->fetchSimpleValue($feedData, $fieldInfo);
$folderId = $this->parseFolderId($feedData, $folderIdInfo);
$newFilename = $this->fetchSimpleValue($feedData, $fileNameInfo);
$conflict = Hash::get($fieldInfo, 'options.conflict');
// Do we want to match existing element? If one exists, we need to set our element to be that
if ($conflict === AssetElement::SCENARIO_INDEX) {
// Make sure to parse the URL into a filename to find the asset by
$filename = $this->_getFilename($value);
$filename = AssetsHelper::prepareAssetName($filename);
$foundElement = AssetElement::find()
->folderId($folderId)
->filename($filename)
->includeSubfolders(true)
->siteId($feed['siteId'])
->one();
if ($foundElement) {
$event->element = $foundElement;
return;
}
}
// We can't find an existing asset, we need to download from a remote URL, or local path
$uploadedElementIds = AssetHelper::fetchRemoteImage([$value], $fieldInfo, $this->feed, null, $this->element, $folderId, $newFilename);
if ($uploadedElementIds) {
$foundElement = AssetElement::find()
->id($uploadedElementIds[0])
->siteId($feed['siteId'])
->one();
if ($foundElement) {
$event->element = $foundElement;
}
}
}
/**
* @param $value
* @return string
*/
private function _getFilename($value): string
{
// If this is an absolute URL, we're uploading the asset. Parse it to just get the filename
if (UrlHelper::isAbsoluteUrl($value)) {
return AssetHelper::getRemoteUrlFilename($value);
}
// Otherwise, probably a local path
return basename($value);
}
// Protected Methods
// =========================================================================
/**
* @param $feedData
* @param $fieldInfo
* @return string
*/
protected function parseFilename($feedData, $fieldInfo): string
{
$value = $this->fetchSimpleValue($feedData, $fieldInfo);
return $this->_getFilename($value);
}
/**
* @param $feedData
* @param $fieldInfo
* @return int|null
* @throws \craft\errors\AssetException
* @throws \craft\errors\FsException
* @throws \craft\errors\FsObjectExistsException
*/
protected function parseFolderId($feedData, $fieldInfo): ?int
{
$value = $this->fetchSimpleValue($feedData, $fieldInfo);
$create = Hash::get($fieldInfo, 'options.create');
$assets = Craft::$app->getAssets();
$volumeId = $this->element->volumeId;
$rootFolder = $assets->getRootFolderByVolumeId($volumeId);
if (is_numeric($value)) {
return $value;
}
$folder = $assets->findFolder([
'name' => $value,
'volumeId' => $volumeId,
]);
if ($folder) {
return $folder->id;
}
if ($create) {
$lastCreatedFolder = null;
// Process all folders (create them)
foreach (explode('/', $value) as $folderName) {
$existingFolder = $assets->findFolder([
'name' => $folderName,
'volumeId' => $volumeId,
]);
if ($existingFolder) {
$lastCreatedFolder = $existingFolder;
continue;
}
$parentFolder = $lastCreatedFolder ?? $rootFolder;
$folderModel = new VolumeFolder();
$folderModel->name = $folderName;
$folderModel->parentId = $parentFolder->id;
$folderModel->volumeId = $volumeId;
$folderModel->path = $parentFolder->path . $folderName . '/';
$assets->createFolder($folderModel);
$lastCreatedFolder = $folderModel;
}
// Then, we just want the lowest level folder to use
return $lastCreatedFolder->id;
}
// If we've provided a bad folder, just return the root - we always need a folderId
return $rootFolder->id;
}
}