3 namespace Drupal\file_mdm;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\File\FileSystemInterface;
7 use Drupal\file_mdm\Plugin\FileMetadataPluginManager;
8 use Psr\Log\LoggerInterface;
11 * A file metadata object.
13 class FileMetadata implements FileMetadataInterface {
16 * The FileMetadata plugin manager.
18 * @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
20 protected $pluginManager;
23 * The file_mdm logger.
25 * @var \Psr\Log\LoggerInterface
30 * The file system service.
32 * @var \Drupal\Core\File\FileSystemInterface
34 protected $fileSystem;
37 * The URI of the file.
44 * The hash used to reference the URI.
51 * The local filesystem path to the file.
53 * This is used to allow accessing local copies of files stored remotely, to
54 * minimise remote calls and allow functions that cannot access remote stream
55 * wrappers to operate locally.
59 protected $localTempPath;
62 * The array of FileMetadata plugins for this URI.
64 * @var \Drupal\file_mdm\Plugin\FileMetadataPluginInterface[]
66 protected $plugins = [];
69 * Constructs a FileMetadata object.
71 * @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $plugin_manager
72 * The file metadata plugin manager.
73 * @param \Psr\Log\LoggerInterface $logger
75 * @param \Drupal\Core\File\FileSystemInterface $file_system
76 * The file system service.
78 * The URI of the file.
80 * The hash used to reference the URI by file_mdm.
82 public function __construct(FileMetadataPluginManager $plugin_manager, LoggerInterface $logger, FileSystemInterface $file_system, $uri, $hash) {
83 $this->pluginManager = $plugin_manager;
84 $this->logger = $logger;
85 $this->fileSystem = $file_system;
93 public function getUri() {
100 public function getLocalTempPath() {
101 return $this->localTempPath;
107 public function setLocalTempPath($temp_uri) {
108 $this->localTempPath = $temp_uri;
109 foreach ($this->plugins as $plugin) {
110 $plugin->setLocalTempPath($this->localTempPath);
118 public function copyUriToTemp($temp_uri = NULL) {
119 if ($temp_uri === NULL) {
120 $temp_uri = $this->fileSystem->tempnam('temporary://', 'file_mdm_');
121 $this->fileSystem->unlink($temp_uri);
122 $temp_uri .= '.' . pathinfo($this->getUri(), PATHINFO_EXTENSION);
124 if ($temp_path = file_unmanaged_copy($this->getUri(), $this->fileSystem->realpath($temp_uri), FILE_EXISTS_REPLACE)) {
125 $this->setLocalTempPath($temp_path);
127 return (bool) $temp_path;
133 public function copyTempToUri() {
134 if (($temp_path = $this->getLocalTempPath()) === NULL) {
137 return (bool) file_unmanaged_copy($temp_path, $this->getUri(), FILE_EXISTS_REPLACE);
143 public function getFileMetadataPlugin($metadata_id) {
144 if (!isset($this->plugins[$metadata_id])) {
146 $this->plugins[$metadata_id] = $this->pluginManager->createInstance($metadata_id);
147 $this->plugins[$metadata_id]->setUri($this->uri);
148 $this->plugins[$metadata_id]->setLocalTempPath($this->localTempPath ?: $this->uri);
149 $this->plugins[$metadata_id]->setHash($this->hash);
151 catch (PluginNotFoundException $e) {
155 return $this->plugins[$metadata_id];
161 public function getSupportedKeys($metadata_id, $options = NULL) {
163 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
164 $keys = $plugin->getSupportedKeys($options);
170 catch (\Exception $e) {
171 $this->logger->error($e->getMessage());
180 public function getMetadata($metadata_id, $key = NULL) {
182 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
183 $metadata = $plugin->getMetadata($key);
189 catch (\Exception $e) {
190 $this->logger->error($e->getMessage());
199 public function removeMetadata($metadata_id, $key) {
201 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
202 return $plugin->removeMetadata($key);
205 catch (\Exception $e) {
206 $this->logger->error($e->getMessage());
214 public function setMetadata($metadata_id, $key, $value) {
216 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
217 return $plugin->setMetadata($key, $value);
220 catch (\Exception $e) {
221 $this->logger->error($e->getMessage());
229 public function isMetadataLoaded($metadata_id) {
230 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
231 return $plugin->isMetadataLoaded();
239 public function loadMetadata($metadata_id, $metadata) {
240 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
241 return $plugin->loadMetadata($metadata);
249 public function loadMetadataFromCache($metadata_id) {
250 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
251 return $plugin->loadMetadataFromCache();
259 public function saveMetadataToCache($metadata_id, array $tags = []) {
260 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
261 return $plugin->saveMetadataToCache($tags);
269 public function saveMetadataToFile($metadata_id) {
270 if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
271 return $plugin->saveMetadataToFile();