forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageConverter.php
266 lines (242 loc) · 9.98 KB
/
ImageConverter.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
<?php
/**
* File containing the Image converter.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\IO\IOServiceInterface;
use eZ\Publish\Core\IO\UrlRedecoratorInterface;
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
class ImageConverter implements Converter
{
/** @var \eZ\Publish\Core\IO\IOServiceInterface */
private $imageIoService;
/** @var \eZ\Publish\Core\IO\UrlRedecoratorInterface */
private $urlRedecorator;
public function __construct(IOServiceInterface $imageIoService, UrlRedecoratorInterface $urlRedecorator)
{
$this->imageIoService = $imageIoService;
$this->urlRedecorator = $urlRedecorator;
}
/**
* Converts data from $value to $storageFieldValue.
*
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
*/
public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
{
if (isset($value->data)) {
// Determine what needs to be stored
if (isset($value->data['width']) && isset($value->data['fieldId'])) {
// width + field id set means that something really needs to be stored
$storageFieldValue->dataText = $this->createLegacyXml($value->data);
} elseif (isset($value->data['fieldId'])) {
// $fieldId without width mleans an empty field
$storageFieldValue->dataText = $this->createEmptyLegacyXml($value->data);
}
// otherwise the image is unprocessed and the DB field stays empty
// there will be a subsequent call to this method, after the image
// has been stored
}
}
/**
* Creates an XML considered "empty" by the legacy storage.
*
* @param array $contentMetaData
*
* @return string
*/
protected function createEmptyLegacyXml($contentMetaData)
{
return $this->fillXml(
array_merge(
array(
'uri' => '',
'path' => '',
'width' => '',
'height' => '',
'mime' => '',
'alternativeText' => '',
),
$contentMetaData
),
array(
'basename' => '',
'extension' => '',
'dirname' => '',
'filename' => '',
),
time()
);
}
/**
* Returns the XML required by the legacy database.
*
* @param array $data
*
* @return string
*/
protected function createLegacyXml(array $data)
{
$data['uri'] = $this->urlRedecorator->redecorateFromSource($data['uri']);
$pathInfo = pathinfo($data['uri']);
return $this->fillXml($data, $pathInfo, time());
}
/**
* Fill the XML template with the data provided.
*
* @param array $imageData
* @param array $pathInfo
* @param int $timestamp
*
* @return string
*/
protected function fillXml($imageData, $pathInfo, $timestamp)
{
// <?xml version="1.0" encoding="utf-8"
// <ezimage serial_number="1" is_valid="1" filename="River-Boat.jpg" suffix="jpg" basename="River-Boat" dirpath="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US" url="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US/River-Boat.jpg" original_filename="bbbbc2fe.jpg" mime_type="image/jpeg" width="770" height="512" alternative_text="Old River Boat" alias_key="1293033771" timestamp="1342530101">
// <original attribute_id="322" attribute_version="1" attribute_language="eng-US"/>
// <information Height="512" Width="770" IsColor="1"/>
// </ezimage>
$xml = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="%s" filename="%s"
suffix="%s" basename="%s" dirpath="%s" url="%s"
original_filename="%s" mime_type="%s" width="%s"
height="%s" alternative_text="%s" alias_key="%s" timestamp="%s">
<original attribute_id="%s" attribute_version="%s" attribute_language="%s"/>
<information Height="%s" Width="%s" IsColor="%s"/>
</ezimage>
EOT;
return sprintf(
$xml,
// <ezimage>
($pathInfo['basename'] !== '' ? '1' : ''), // is_valid="%s"
htmlspecialchars($pathInfo['basename']), // filename="%s"
htmlspecialchars($pathInfo['extension']), // suffix="%s"
htmlspecialchars($pathInfo['filename']), // basename="%s"
htmlspecialchars($pathInfo['dirname']), // dirpath
htmlspecialchars($imageData['uri']), // url
htmlspecialchars($pathInfo['basename']), // @todo: Needs original file name, for whatever reason?
htmlspecialchars($imageData['mime']), // mime_type
htmlspecialchars($imageData['width']), // width
htmlspecialchars($imageData['height']), // height
htmlspecialchars($imageData['alternativeText']), // alternative_text
htmlspecialchars(1293033771), // alias_key, fixed for the original image
htmlspecialchars($timestamp), // timestamp
// <original>
$imageData['fieldId'],
$imageData['versionNo'],
$imageData['languageCode'],
// <information>
$imageData['height'], // Height
$imageData['width'], // Width
1 // IsColor @todo Do we need to fix that here?
);
}
/**
* Converts data from $value to $fieldValue.
*
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
*/
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
{
if (empty($value->dataText)) {
// Special case for anonymous user
return;
}
$fieldValue->data = $this->parseLegacyXml($value->dataText);
}
/**
* Parses the XML from the legacy database.
*
* Returns only the data required by the FieldType, nothing more.
*
* @param string $xml
*
* @return array
*/
protected function parseLegacyXml($xml)
{
$extractedData = array();
$dom = new \DOMDocument();
$dom->loadXml($xml);
$ezimageTag = $dom->documentElement;
if (!$ezimageTag->hasAttribute('url')) {
throw new \RuntimeException('Missing attribute "url" in <ezimage/> tag.');
}
if (($legacyUrl = $ezimageTag->getAttribute('url')) === '') {
// Detected XML considered "empty" by the legacy storage
return null;
}
$url = $this->urlRedecorator->redecorateFromTarget($legacyUrl);
$extractedData['id'] = $this->imageIoService->loadBinaryFileByUri($url)->id;
if (!$ezimageTag->hasAttribute('filename')) {
throw new \RuntimeException('Missing attribute "filename" in <ezimage/> tag.');
}
$extractedData['fileName'] = $ezimageTag->getAttribute('filename');
$extractedData['width'] = $ezimageTag->getAttribute('width');
$extractedData['height'] = $ezimageTag->getAttribute('height');
$extractedData['mime'] = $ezimageTag->getAttribute('mime_type');
if (!$ezimageTag->hasAttribute('alternative_text')) {
throw new \RuntimeException('Missing attribute "alternative_text" in <ezimage/> tag.');
}
$extractedData['alternativeText'] = $ezimageTag->getAttribute('alternative_text');
return $extractedData;
}
/**
* Converts field definition data in $fieldDef into $storageFieldDef.
*
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
*/
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
{
$storageDef->dataInt1 = (isset($fieldDef->fieldTypeConstraints->validators['FileSizeValidator']['maxFileSize'])
? round($fieldDef->fieldTypeConstraints->validators['FileSizeValidator']['maxFileSize'] / 1024 / 1024)
: 0);
}
/**
* Converts field definition data in $storageDef into $fieldDef.
*
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
*/
public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
{
$fieldDef->fieldTypeConstraints = new FieldTypeConstraints(
array(
'validators' => array(
'FileSizeValidator' => array(
'maxFileSize' => ($storageDef->dataInt1 != 0
? (int)$storageDef->dataInt1 * 1024 * 1024
: null),
),
),
)
);
}
/**
* Returns the name of the index column in the attribute table.
*
* Returns the name of the index column the datatype uses, which is either
* "sort_key_int" or "sort_key_string". This column is then used for
* filtering and sorting for this type.
*
* @return string
*/
public function getIndexColumn()
{
// @todo: Correct?
return 'sort_key_string';
}
}