-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.php
81 lines (72 loc) · 2.31 KB
/
File.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/
namespace craft\wpimport\acfadapters;
use craft\base\FieldInterface;
use craft\fields\Assets;
use craft\helpers\Assets as AssetsHelper;
use craft\helpers\FileHelper;
use craft\wpimport\BaseAcfAdapter;
use craft\wpimport\generators\volumes\Uploads;
use craft\wpimport\importers\Media;
use Illuminate\Support\Collection;
use Throwable;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class File extends BaseAcfAdapter
{
public static function type(): string
{
return 'file';
}
public function create(array $data): FieldInterface
{
$sourceKey = sprintf('volume:%s', Uploads::get()->uid);
$field = new Assets();
$field->maxRelations = 1;
$field->sources = [$sourceKey];
$field->viewMode = 'large';
$field->defaultUploadLocationSource = $sourceKey;
if ($data['mime_types']) {
// Get all the allowed file extensions
$allowedExtensions = [];
$types = array_map('trim', explode(',', $data['mime_types']));
foreach ($types as $type) {
if (str_contains($type, '/')) {
array_push($allowedExtensions, ...FileHelper::getExtensionsByMimeType($type));
} else {
$allowedExtensions[] = $type;
}
}
$field->restrictFiles = true;
$field->allowedKinds = [];
$fileKinds = AssetsHelper::getFileKinds();
foreach ($allowedExtensions as $extension) {
foreach ($fileKinds as $kind => $kindInfo) {
if (in_array($extension, $kindInfo['extensions'])) {
$field->allowedKinds[] = $kind;
break;
}
}
}
}
return $field;
}
public function normalizeValue(mixed $value, array $data): mixed
{
return Collection::make((array)$value)
->map(function(int $id) {
try {
return $this->command->import(Media::SLUG, $id);
} catch (Throwable) {
return null;
}
})
->filter()
->all();
}
}