-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathAsset.php
More file actions
112 lines (94 loc) · 4.27 KB
/
Asset.php
File metadata and controls
112 lines (94 loc) · 4.27 KB
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
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\AdminBundle\Service\GridData;
use Pimcore\Bundle\AdminBundle\Service\ElementService;
use Pimcore\Model;
use Pimcore\Model\Asset\MetaData\ClassDefinition\Data\Data;
use Pimcore\Model\Element\Service;
use Pimcore\Model\Exception\UnsupportedException;
/**
*
* @internal
*/
class Asset extends Element
{
public static function getData(Model\Asset $asset, ?array $fields = null, ?string $requestedLanguage = null, array $params = []): array
{
$data = self::gridElementData($asset);
$loader = null;
if (!empty($fields)) {
$data = [
'id' => $asset->getId(),
'id~system' => $asset->getId(),
'type~system' => $asset->getType(),
'mimetype~system' => $asset->getMimeType(),
'fullpath~system' => $asset->getRealFullPath(),
'filename~system' => $asset->getKey(),
'creationDate~system' => $asset->getCreationDate(),
'modificationDate~system' => $asset->getModificationDate(),
'idPath~system' => Service::getIdPath($asset),
];
$requestedLanguage = str_replace('default', '', $requestedLanguage);
foreach ($fields as $field) {
$fieldDef = explode('~', $field);
if (isset($fieldDef[1]) && $fieldDef[1] === 'system') {
if ($fieldDef[0] === 'preview') {
$data[$field] = self::getPreviewThumbnail($asset, ['treepreview' => true, 'origin' => 'folderPreview', 'width' => 108, 'height' => 70, 'frame' => true]);
} elseif ($fieldDef[0] === 'size') {
$size = $asset->getFileSize();
$data[$field] = formatBytes($size);
}
} else {
if (isset($fieldDef[1])) {
$language = ($fieldDef[1] === 'none' ? '' : $fieldDef[1]);
$rawMetaData = $asset->getMetadata($fieldDef[0], $language, true, true);
} else {
$rawMetaData = $asset->getMetadata($field, $requestedLanguage, true, true);
}
$metaData = $rawMetaData['data'] ?? null;
if ($rawMetaData) {
$type = $rawMetaData['type'];
if (!$loader) {
$loader = \Pimcore::getContainer()->get('pimcore.implementation_loader.asset.metadata.data');
}
$metaData = $rawMetaData['data'] ?? null;
try {
/** @var Data $instance */
$instance = $loader->build($type);
$metaData = $instance->getDataForListfolderGrid($rawMetaData['data'] ?? null, $rawMetaData);
} catch (UnsupportedException $e) {
}
}
$data[$field] = $metaData;
}
}
}
return $data;
}
public static function getPreviewThumbnail(Model\Asset $asset, array $params = [], bool $onlyMethod = false): ?string
{
$thumbnailMethod = '';
if ($asset instanceof Model\Asset\Image) {
$thumbnailMethod = 'getThumbnail';
} elseif ($asset instanceof Model\Asset\Video && \Pimcore\Video::isAvailable()) {
$thumbnailMethod = 'getImageThumbnail';
} elseif ($asset instanceof Model\Asset\Document && \Pimcore\Document::isAvailable()) {
$thumbnailMethod = 'getImageThumbnail';
}
if ($onlyMethod) {
return $thumbnailMethod;
}
// Delegate to ElementService to keep list view and tile view behavior aligned
$elementService = \Pimcore::getContainer()->get(ElementService::class);
return $elementService->getThumbnailUrl($asset, $params);
}
}