Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.67% |
4 / 109 |
|
6.25% |
1 / 16 |
CRAP | |
0.00% |
0 / 1 |
| MediaService | |
3.67% |
4 / 109 |
|
6.25% |
1 / 16 |
1130.03 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createMediaInstance | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
| createSharedStimulusInstance | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
2 | |||
| editMediaInstance | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
| deleteResource | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| dispatchMediaSavedEvent | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| isXmlAllowedMimeType | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| removeFromFilesystem | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getLink | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getMediaSavedEventDispatcher | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRepositoryService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResourceMimeType | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getFileSourceUnserializer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model; |
| 24 | |
| 25 | use common_ext_ExtensionsManager; |
| 26 | use core_kernel_classes_Literal; |
| 27 | use core_kernel_classes_Resource as RdfResource; |
| 28 | use oat\generis\model\OntologyRdfs; |
| 29 | use oat\oatbox\event\EventManager; |
| 30 | use oat\oatbox\filesystem\File; |
| 31 | use oat\oatbox\log\LoggerAwareTrait; |
| 32 | use oat\oatbox\service\ConfigurableService; |
| 33 | use oat\oatbox\service\ServiceManager; |
| 34 | use oat\tao\model\ClassServiceTrait; |
| 35 | use oat\tao\model\GenerisServiceTrait; |
| 36 | use oat\taoMediaManager\model\fileManagement\FileManagement; |
| 37 | use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer; |
| 38 | use oat\taoMediaManager\model\relation\event\MediaRemovedEvent; |
| 39 | use oat\taoMediaManager\model\relation\event\MediaSavedEventDispatcher; |
| 40 | use oat\taoRevision\model\RepositoryInterface; |
| 41 | use tao_helpers_File; |
| 42 | |
| 43 | /** |
| 44 | * Service methods to manage the Media |
| 45 | * |
| 46 | * @author Antoine Robin, <antoine.robin@vesperiagroup.com> |
| 47 | */ |
| 48 | class MediaService extends ConfigurableService |
| 49 | { |
| 50 | use ClassServiceTrait; |
| 51 | use GenerisServiceTrait; |
| 52 | use LoggerAwareTrait; |
| 53 | |
| 54 | /** @deprecated Use {@link TaoMediaOntology::CLASS_URI_MEDIA_ROOT} instead */ |
| 55 | public const ROOT_CLASS_URI = TaoMediaOntology::CLASS_URI_MEDIA_ROOT; |
| 56 | |
| 57 | /** @deprecated Use {@link TaoMediaOntology::PROPERTY_LINK} instead */ |
| 58 | public const PROPERTY_LINK = TaoMediaOntology::PROPERTY_LINK; |
| 59 | |
| 60 | /** @deprecated Use {@link TaoMediaOntology::PROPERTY_LANGUAGE} instead */ |
| 61 | public const PROPERTY_LANGUAGE = TaoMediaOntology::PROPERTY_LANGUAGE; |
| 62 | |
| 63 | /** @deprecated Use {@link TaoMediaOntology::PROPERTY_ALT_TEXT} instead */ |
| 64 | public const PROPERTY_ALT_TEXT = TaoMediaOntology::PROPERTY_ALT_TEXT; |
| 65 | |
| 66 | /** @deprecated Use {@link TaoMediaOntology::PROPERTY_MD5} instead */ |
| 67 | public const PROPERTY_MD5 = TaoMediaOntology::PROPERTY_MD5; |
| 68 | |
| 69 | /** @deprecated Use {@link TaoMediaOntology::PROPERTY_MIME_TYPE} instead */ |
| 70 | public const PROPERTY_MIME_TYPE = TaoMediaOntology::PROPERTY_MIME_TYPE; |
| 71 | |
| 72 | public const SHARED_STIMULUS_MIME_TYPE = 'application/qti+xml'; |
| 73 | |
| 74 | public const MEDIA_ALLOWED_TYPES = [ |
| 75 | 'application/xml', |
| 76 | 'text/xml', |
| 77 | MediaService::SHARED_STIMULUS_MIME_TYPE, |
| 78 | ]; |
| 79 | |
| 80 | /** |
| 81 | * @deprecated |
| 82 | */ |
| 83 | public static function singleton() |
| 84 | { |
| 85 | return ServiceManager::getServiceManager()->get(self::class); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * (non-PHPdoc) |
| 90 | * @see \tao_models_classes_ClassService::getRootClass() |
| 91 | */ |
| 92 | public function getRootClass() |
| 93 | { |
| 94 | return $this->getClass(TaoMediaOntology::CLASS_URI_MEDIA_ROOT); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Create a media instance from a file, and define its class and language |
| 99 | * |
| 100 | * @param string|File $fileSource path to the file to create instance from |
| 101 | * @param string $classUri parent to add the instance to |
| 102 | * @param string $language language of the content |
| 103 | * @param string $label label of the instance |
| 104 | * @param string $mimeType mimeType of the file |
| 105 | * @param string|null $userId owner of the resource |
| 106 | * @return string | bool $instanceUri or false on error |
| 107 | */ |
| 108 | public function createMediaInstance( |
| 109 | $fileSource, |
| 110 | $classUri, |
| 111 | $language, |
| 112 | $label = null, |
| 113 | $mimeType = null, |
| 114 | $userId = null |
| 115 | ) { |
| 116 | $link = $this->getFileManager()->storeFile($fileSource, $label); |
| 117 | |
| 118 | if ($link === false) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | $clazz = $this->getClass($classUri); |
| 123 | |
| 124 | //create media instance |
| 125 | if (is_null($label)) { |
| 126 | $label = $fileSource instanceof File ? $fileSource->getBasename() : basename($fileSource); |
| 127 | } |
| 128 | |
| 129 | $content = $fileSource instanceof File ? $fileSource->read() : file_get_contents($fileSource); |
| 130 | |
| 131 | if (is_null($mimeType)) { |
| 132 | $mimeType = $fileSource instanceof File ? $fileSource->getMimeType() : tao_helpers_File::getMimeType( |
| 133 | $fileSource |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | $properties = [ |
| 138 | OntologyRdfs::RDFS_LABEL => $label, |
| 139 | TaoMediaOntology::PROPERTY_LINK => $link, |
| 140 | TaoMediaOntology::PROPERTY_LANGUAGE => $language, |
| 141 | TaoMediaOntology::PROPERTY_MD5 => md5($content), |
| 142 | TaoMediaOntology::PROPERTY_MIME_TYPE => $mimeType, |
| 143 | TaoMediaOntology::PROPERTY_ALT_TEXT => $label |
| 144 | ]; |
| 145 | |
| 146 | $instance = $clazz->createInstanceWithProperties($properties); |
| 147 | $id = $instance->getUri(); |
| 148 | |
| 149 | $this->dispatchMediaSavedEvent('Initial import', $instance, $link, $mimeType, $userId, $content); |
| 150 | |
| 151 | return $id; |
| 152 | } |
| 153 | |
| 154 | public function createSharedStimulusInstance( |
| 155 | string $link, |
| 156 | string $classUri, |
| 157 | string $language, |
| 158 | string $userId = null |
| 159 | ): string { |
| 160 | $content = $this->getFileManager()->getFileStream($link)->getContents(); |
| 161 | $clazz = $this->getClass($classUri); |
| 162 | $label = basename($link); |
| 163 | |
| 164 | $properties = [ |
| 165 | OntologyRdfs::RDFS_LABEL => $label, |
| 166 | TaoMediaOntology::PROPERTY_LINK => $link, |
| 167 | TaoMediaOntology::PROPERTY_LANGUAGE => $language, |
| 168 | TaoMediaOntology::PROPERTY_MD5 => md5($content), |
| 169 | TaoMediaOntology::PROPERTY_MIME_TYPE => self::SHARED_STIMULUS_MIME_TYPE, |
| 170 | TaoMediaOntology::PROPERTY_ALT_TEXT => $label, |
| 171 | ]; |
| 172 | |
| 173 | $instance = $clazz->createInstanceWithProperties($properties); |
| 174 | $id = $instance->getUri(); |
| 175 | |
| 176 | $this->dispatchMediaSavedEvent( |
| 177 | 'Initial import', |
| 178 | $instance, |
| 179 | $link, |
| 180 | self::SHARED_STIMULUS_MIME_TYPE, |
| 181 | $userId, |
| 182 | $content |
| 183 | ); |
| 184 | |
| 185 | return $id; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Edit a media instance with a new file and/or a new language |
| 190 | * |
| 191 | * @param string|File $fileSource |
| 192 | */ |
| 193 | public function editMediaInstance( |
| 194 | $fileSource, |
| 195 | string $id, |
| 196 | string $language = null, |
| 197 | string $userId = null |
| 198 | ): bool { |
| 199 | $instance = $this->getResource($id); |
| 200 | $link = $this->getLink($instance); |
| 201 | $fileManager = $this->getFileManager(); |
| 202 | |
| 203 | if (!empty($link)) { |
| 204 | $fileManager->deleteFile($link); |
| 205 | } |
| 206 | |
| 207 | $link = $fileManager->storeFile($fileSource, $instance->getLabel()); |
| 208 | |
| 209 | if ($link !== false) { |
| 210 | $md5 = $fileSource instanceof File ? md5($fileSource->read()) : md5_file($fileSource); |
| 211 | $mime = $this->getResourceMimeType($instance) ?? ''; |
| 212 | |
| 213 | $instance->editPropertyValues( |
| 214 | $this->getProperty(TaoMediaOntology::PROPERTY_LINK), |
| 215 | $link |
| 216 | ); |
| 217 | $instance->editPropertyValues( |
| 218 | $this->getProperty(TaoMediaOntology::PROPERTY_MD5), |
| 219 | $md5 |
| 220 | ); |
| 221 | |
| 222 | if ($language) { |
| 223 | $instance->editPropertyValues( |
| 224 | $this->getProperty(TaoMediaOntology::PROPERTY_LANGUAGE), |
| 225 | $language |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | $this->dispatchMediaSavedEvent('Imported new file', $instance, $fileSource, $mime, $userId); |
| 230 | } |
| 231 | |
| 232 | return $link !== false; |
| 233 | } |
| 234 | |
| 235 | public function deleteResource(RdfResource $resource) |
| 236 | { |
| 237 | $link = $this->getLink($resource); |
| 238 | |
| 239 | if ($this->removeFromFilesystem($link) && $resource->delete()) { |
| 240 | $this->getEventManager() |
| 241 | ->trigger(new MediaRemovedEvent($resource->getUri())); |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @param string|File $link |
| 251 | */ |
| 252 | public function dispatchMediaSavedEvent( |
| 253 | string $commitMessage, |
| 254 | RdfResource $instance, |
| 255 | $link, |
| 256 | string $mimeType, |
| 257 | string $userId = null, |
| 258 | string $content = null |
| 259 | ): void { |
| 260 | $eventDispatcher = $this->getMediaSavedEventDispatcher(); |
| 261 | if ($content) { |
| 262 | $eventDispatcher->dispatchFromContent($instance->getUri(), $mimeType, $content); |
| 263 | } else { |
| 264 | $eventDispatcher->dispatchFromFile($instance->getUri(), $link, $this->getResourceMimeType($instance)); |
| 265 | } |
| 266 | |
| 267 | if ($this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID)->isEnabled('taoRevision')) { |
| 268 | $this->logInfo('Auto generating initial revision'); |
| 269 | $this->getRepositoryService()->commit($instance, __($commitMessage), null, $userId); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | public function isXmlAllowedMimeType(string $type): bool |
| 274 | { |
| 275 | $paramsPos = strpos($type, ';'); |
| 276 | if ($paramsPos > 0) { |
| 277 | $type = substr($type, 0, $paramsPos); |
| 278 | } |
| 279 | |
| 280 | return in_array($type, self::MEDIA_ALLOWED_TYPES, true); |
| 281 | } |
| 282 | |
| 283 | private function removeFromFilesystem($link): bool |
| 284 | { |
| 285 | $directory = dirname($link); |
| 286 | |
| 287 | if ($directory !== '.') { |
| 288 | return $this->getFileManager()->deleteDirectory($directory); |
| 289 | } |
| 290 | |
| 291 | return $this->getFileManager()->deleteFile($link); |
| 292 | } |
| 293 | |
| 294 | private function getLink(RdfResource $resource): string |
| 295 | { |
| 296 | $instance = $resource->getUniquePropertyValue( |
| 297 | $this->getProperty(TaoMediaOntology::PROPERTY_LINK) |
| 298 | ); |
| 299 | |
| 300 | $link = $instance instanceof RdfResource ? $instance->getUri() : (string)$instance; |
| 301 | return $this->getFileSourceUnserializer()->unserialize($link); |
| 302 | } |
| 303 | |
| 304 | private function getMediaSavedEventDispatcher(): MediaSavedEventDispatcher |
| 305 | { |
| 306 | return $this->getServiceLocator()->get(MediaSavedEventDispatcher::class); |
| 307 | } |
| 308 | |
| 309 | private function getFileManager(): FileManagement |
| 310 | { |
| 311 | return $this->getServiceLocator()->get(FileManagement::SERVICE_ID); |
| 312 | } |
| 313 | |
| 314 | private function getRepositoryService(): RepositoryInterface |
| 315 | { |
| 316 | return $this->getServiceLocator()->get(RepositoryInterface::SERVICE_ID); |
| 317 | } |
| 318 | |
| 319 | private function getEventManager(): EventManager |
| 320 | { |
| 321 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 322 | } |
| 323 | |
| 324 | private function getResourceMimeType(RdfResource $resource): ?string |
| 325 | { |
| 326 | $container = $resource->getUniquePropertyValue( |
| 327 | $resource->getProperty(TaoMediaOntology::PROPERTY_MIME_TYPE) |
| 328 | ); |
| 329 | |
| 330 | if ($container instanceof core_kernel_classes_Literal) { |
| 331 | $mimeType = (string)$container; |
| 332 | |
| 333 | return $mimeType === MediaService::SHARED_STIMULUS_MIME_TYPE ? $mimeType : null; |
| 334 | } |
| 335 | |
| 336 | return null; |
| 337 | } |
| 338 | |
| 339 | private function getFileSourceUnserializer(): FileSourceUnserializer |
| 340 | { |
| 341 | return $this->getServiceLocator()->get(FileSourceUnserializer::class); |
| 342 | } |
| 343 | } |