forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 119
/
MediaSourceService.php
393 lines (347 loc) · 12.6 KB
/
MediaSourceService.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
namespace Drupal\islandora\MediaSource;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\file\FileInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Utility functions for working with source files for Media.
*/
class MediaSourceService {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* File system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Islandora Utility service.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $islandoraUtils;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* File system service.
* @param \Drupal\islandora\IslandoraUtils $islandora_utils
* Utility service.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
AccountInterface $account,
LanguageManagerInterface $language_manager,
FileSystemInterface $file_system,
IslandoraUtils $islandora_utils
) {
$this->entityTypeManager = $entity_type_manager;
$this->account = $account;
$this->languageManager = $language_manager;
$this->fileSystem = $file_system;
$this->islandoraUtils = $islandora_utils;
}
/**
* Gets the name of a source field for a Media.
*
* @param string $media_type
* Media bundle whose source field you are searching for.
*
* @return string|null
* Field name if it exists in configuration, else NULL.
*/
public function getSourceFieldName($media_type) {
$bundle = $this->entityTypeManager->getStorage('media_type')->load($media_type);
if (!$bundle) {
throw new NotFoundHttpException("Bundle $media_type does not exist");
}
$type_configuration = $bundle->get('source_configuration');
if (!isset($type_configuration['source_field'])) {
return NULL;
}
return $type_configuration['source_field'];
}
/**
* Gets the value of a source field for a Media.
*
* @param \Drupal\media\MediaInterface $media
* Media whose source field you are searching for.
*
* @return \Drupal\file\FileInterface|\Drupal\Core\Entity\EntityInterface|false|null
* The first source entity if there is one, generally expected to be of
* \Drupal\file\FileInterface. Boolean FALSE if there was no such entity.
* NULL if the source field does not refer to Drupal entities (as in, the
* field is not a \Drupal\Core\Field\EntityReferenceFieldItemListInterface
* implementation).
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function getSourceFile(MediaInterface $media) {
// Get the source field for the media type.
$source_field = $this->getSourceFieldName($media->bundle());
if (empty($source_field)) {
throw new NotFoundHttpException("Source field not set for {$media->bundle()} media");
}
// Get the file from the media.
$source_list = $media->get($source_field);
if ($source_list instanceof EntityReferenceFieldItemListInterface) {
$files = $source_list->referencedEntities();
return reset($files);
}
return NULL;
}
/**
* Updates a media's source field with the supplied resource.
*
* @param \Drupal\media\MediaInterface $media
* The media to update.
* @param resource $resource
* New file contents as a resource.
* @param string $mimetype
* New mimetype of contents.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function updateSourceField(
MediaInterface $media,
$resource,
$mimetype
) {
$source_field = $this->getSourceFieldName($media->bundle());
$file = $this->getSourceFile($media);
// Update it.
$this->updateFile($file, $resource, $mimetype);
$file->save();
// Set fields provided by type plugin and mapped in bundle configuration
// for the media.
foreach ($media->bundle->entity->getFieldMap() as $source => $destination) {
if ($media->hasField($destination) && $value = $media->getSource()->getMetadata($media, $source)) {
$media->set($destination, $value);
}
// Ensure width and height are updated on File reference when it's an
// image. Otherwise you run into scaling problems when updating images
// with different sizes.
if ($source == 'width' || $source == 'height') {
$media->get($source_field)->first()->set($source, $value);
}
}
$media->save();
}
/**
* Updates a File's binary contents on disk.
*
* @param \Drupal\file\FileInterface $file
* File to update.
* @param resource $resource
* Stream holding the new contents.
* @param string $mimetype
* Mimetype of new contents.
*/
protected function updateFile(FileInterface $file, $resource, $mimetype = NULL) {
$uri = $file->getFileUri();
$destination = fopen($uri, 'wb');
if (!$destination) {
throw new HttpException(500, "File $uri could not be opened to write.");
}
$content_length = stream_copy_to_stream($resource, $destination);
fclose($destination);
if ($content_length === FALSE) {
throw new HttpException(500, "Request body could not be copied to $uri");
}
if ($content_length === 0) {
// Clean up the newly created, empty file.
unlink($uri);
throw new HttpException(400, "No bytes were copied to $uri");
}
if (!empty($mimetype)) {
$file->setMimeType($mimetype);
}
// Flush the image cache for the image so thumbnails get regenerated.
image_path_flush($uri);
}
/**
* Creates a new Media using the provided resource, adding it to a Node.
*
* @param \Drupal\node\NodeInterface $node
* The node to reference the newly created Media.
* @param \Drupal\media\MediaTypeInterface $media_type
* Media type for new media.
* @param \Drupal\taxonomy\TermInterface $taxonomy_term
* Term from the 'Behavior' vocabulary to give to new media.
* @param resource $resource
* New file contents as a resource.
* @param string $mimetype
* New mimetype of contents.
* @param string $content_location
* Drupal/PHP stream wrapper for where to upload the binary.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function putToNode(
NodeInterface $node,
MediaTypeInterface $media_type,
TermInterface $taxonomy_term,
$resource,
$mimetype,
$content_location
) {
$existing = $this->islandoraUtils->getMediaReferencingNodeAndTerm($node, $taxonomy_term);
if (!empty($existing)) {
// Just update already existing media.
$media = $this->entityTypeManager->getStorage('media')->load(reset($existing));
$this->updateSourceField(
$media,
$resource,
$mimetype
);
return FALSE;
}
else {
// Otherwise, the media doesn't exist yet.
// So make everything by hand.
// Get the source field for the media type.
$bundle = $media_type->id();
$source_field = $this->getSourceFieldName($bundle);
if (empty($source_field)) {
throw new NotFoundHttpException("Source field not set for $bundle media");
}
// Construct the File.
$file = $this->entityTypeManager->getStorage('file')->create([
'uid' => $this->account->id(),
'uri' => $content_location,
'filename' => $this->fileSystem->basename($content_location),
'filemime' => $mimetype,
]);
$file->setPermanent();
// Validate file extension.
$source_field_config = $this->entityTypeManager->getStorage('field_config')->load("media.$bundle.$source_field");
$valid_extensions = $source_field_config->getSetting('file_extensions');
$errors = file_validate_extensions($file, $valid_extensions);
if (!empty($errors)) {
throw new BadRequestHttpException("Invalid file extension. Valid types are $valid_extensions");
}
$directory = $this->fileSystem->dirname($content_location);
if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable");
}
// Copy over the file content.
$this->updateFile($file, $resource, $mimetype);
$file->save();
// Construct the Media.
$media_struct = [
'bundle' => $bundle,
'uid' => $this->account->id(),
'name' => $file->getFilename(),
'langcode' => $this->languageManager->getDefaultLanguage()->getId(),
"$source_field" => [
'target_id' => $file->id(),
],
IslandoraUtils::MEDIA_OF_FIELD => [
'target_id' => $node->id(),
],
IslandoraUtils::MEDIA_USAGE_FIELD => [
'target_id' => $taxonomy_term->id(),
],
];
// Set alt text.
if ($source_field_config->getSetting('alt_field') && $source_field_config->getSetting('alt_field_required')) {
$media_struct[$source_field]['alt'] = $file->getFilename();
}
$media = $this->entityTypeManager->getStorage('media')->create($media_struct);
$media->save();
return $media;
}
}
/**
* Creates a new File using the provided resource, adding it to a Media.
*
* @param \Drupal\media\MediaInterface $media
* The Media that will receive the new file.
* @param string $destination_field
* The field on the media where the file will go.
* @param resource $resource
* New file contents as a resource.
* @param string $mimetype
* New mimetype of contents.
* @param string $content_location
* Drupal/PHP stream wrapper for where to upload the binary.
*
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function putToMedia(
MediaInterface $media,
$destination_field,
$resource,
$mimetype,
$content_location
) {
if ($media->hasField($destination_field)) {
// Construct the File.
$file = $this->entityTypeManager->getStorage('file')->create([
'uid' => $this->account->id(),
'uri' => $content_location,
'filename' => $this->fileSystem->basename($content_location),
'filemime' => $mimetype,
]);
$file->setPermanent();
// Validate file extension.
$bundle = $media->bundle();
$destination_field_config = $this->entityTypeManager->getStorage('field_config')->load("media.$bundle.$destination_field");
$valid_extensions = $destination_field_config->getSetting('file_extensions');
$errors = file_validate_extensions($file, $valid_extensions);
if (!empty($errors)) {
throw new BadRequestHttpException("Invalid file extension. Valid types are $valid_extensions");
}
$directory = $this->fileSystem->dirname($content_location);
if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable");
}
// Copy over the file content.
$this->updateFile($file, $resource, $mimetype);
$file->save();
// Update the media.
$media->{$destination_field}->setValue([
'target_id' => $file->id(),
]);
$media->save();
}
else {
throw new BadRequestHttpException("Media does not have destination field $destination_field");
}
}
}