-
Notifications
You must be signed in to change notification settings - Fork 0
/
Media.php
101 lines (89 loc) · 3.5 KB
/
Media.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/
namespace craft\wpimport\importers;
use Craft;
use craft\base\ElementInterface;
use craft\elements\Asset;
use craft\enums\CmsEdition;
use craft\fs\Local;
use craft\helpers\Assets;
use craft\helpers\DateTimeHelper;
use craft\helpers\FileHelper;
use craft\wpimport\BaseImporter;
use craft\wpimport\generators\fields\Caption;
use craft\wpimport\generators\fields\Description;
use craft\wpimport\generators\filesystems\Uploads as UploadsFs;
use craft\wpimport\generators\volumes\Uploads;
use craft\wpimport\generators\volumes\Uploads as UploadsVolume;
use yii\console\Exception;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Media extends BaseImporter
{
public const SLUG = 'media';
public function slug(): string
{
return self::SLUG;
}
public function apiUri(): string
{
return 'wp/v2/media';
}
public function label(): string
{
return 'Media';
}
public function elementType(): string
{
return Asset::class;
}
public function populate(ElementInterface $element, array $data): void
{
$wpPath = $data['media_details']['file'] ?? pathinfo($data['source_url'], PATHINFO_BASENAME);
$path = pathinfo($wpPath, PATHINFO_DIRNAME);
$filename = Assets::prepareAssetName(pathinfo($wpPath, PATHINFO_BASENAME));
$folder = Craft::$app->assets->ensureFolderByFullPathAndVolume($path !== '.' ? $path : '', UploadsVolume::get());
/** @var Asset $element */
$element->volumeId = Uploads::get()->id;
$element->title = $data['title']['raw'];
/** @var Local $fs */
$fs = UploadsFs::get();
$destPath = sprintf('%s/%s/%s', $fs->getRootPath(), $path, $filename);
if (file_exists($destPath)) {
$element->folderId = $folder->id;
$element->folderPath = $folder->path;
$element->filename = $filename;
$element->kind = Assets::getFileKindByExtension($filename);
$element->setScenario(Asset::SCENARIO_INDEX);
} else {
$response = $this->command->client->get($data['source_url']);
if ($response->getStatusCode() !== 200) {
throw new Exception("No file found at {$data['source_url']}");
}
$tempPath = sprintf('%s/%s', Craft::$app->path->getTempAssetUploadsPath(), $filename);
FileHelper::writeToFile($tempPath, $response->getBody()->getContents());
$element->tempFilePath = $tempPath;
$element->newFolderId = $folder->id;
$element->newFilename = $filename;
$element->avoidFilenameConflicts = true;
}
$element->setWidth($data['media_details']['width'] ?? null);
$element->setHeight($data['media_details']['height'] ?? null);
$element->size = $data['media_details']['filesize'] ?? null;
$element->dateCreated = DateTimeHelper::toDateTime($data['date_gmt']);
$element->dateUpdated = DateTimeHelper::toDateTime($data['modified_gmt']);
if ($data['author'] && Craft::$app->edition->value >= CmsEdition::Pro->value) {
$element->uploaderId = $this->command->import(User::SLUG, $data['author']);
}
$element->alt = $data['alt_text'];
$element->setFieldValues([
Caption::get()->handle => $data['caption']['raw'],
Description::get()->handle => $data['description']['raw'],
]);
}
}