Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
17 / 20 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Base64fileEncoder | |
85.00% |
17 / 20 |
|
50.00% |
1 / 2 |
8.22 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| encode | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
7.19 | |||
| 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) 2015-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * @author Mikhail Kamarouski, <kamarouski@1pt.com> |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoItems\model\pack\encoders; |
| 24 | |
| 25 | use oat\tao\helpers\Base64; |
| 26 | use oat\tao\model\media\MediaAsset; |
| 27 | use core_kernel_persistence_Exception; |
| 28 | use oat\taoMediaManager\model\MediaSource; |
| 29 | use tao_models_classes_FileNotFoundException; |
| 30 | use tao_models_classes_service_StorageDirectory; |
| 31 | use oat\taoItems\model\pack\ExceptionMissingAsset; |
| 32 | use oat\tao\model\media\sourceStrategy\HttpSource; |
| 33 | |
| 34 | /** |
| 35 | * Class Base64fileEncoder |
| 36 | * Helper, encode file by uri for embedding using base64 algorithm |
| 37 | * |
| 38 | * @package oat\taoItems\model\pack\encoders |
| 39 | */ |
| 40 | class Base64fileEncoder implements Encoding |
| 41 | { |
| 42 | /** @var tao_models_classes_service_StorageDirectory */ |
| 43 | private $directory; |
| 44 | |
| 45 | /** Applied data-uri format placeholder */ |
| 46 | public const DATA_PREFIX = 'data:%s;base64,%s'; |
| 47 | |
| 48 | /** |
| 49 | * Base64fileEncoder constructor. |
| 50 | * |
| 51 | * @param tao_models_classes_service_StorageDirectory $directory |
| 52 | */ |
| 53 | public function __construct(tao_models_classes_service_StorageDirectory $directory) |
| 54 | { |
| 55 | $this->directory = $directory; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string|MediaAsset $data name of the assert |
| 60 | * |
| 61 | * @throws ExceptionMissingAsset |
| 62 | * @throws core_kernel_persistence_Exception |
| 63 | * @throws tao_models_classes_FileNotFoundException |
| 64 | * |
| 65 | * @return mixed|string |
| 66 | */ |
| 67 | public function encode($data) |
| 68 | { |
| 69 | // Skip if external resource |
| 70 | if (filter_var($data, FILTER_VALIDATE_URL)) { |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | if ($data instanceof MediaAsset) { |
| 75 | $mediaSource = $data->getMediaSource(); |
| 76 | $data = $data->getMediaIdentifier(); |
| 77 | |
| 78 | if ($mediaSource instanceof HttpSource || Base64::isEncodedImage($data)) { |
| 79 | return $data; |
| 80 | } |
| 81 | |
| 82 | if ($mediaSource instanceof MediaSource) { |
| 83 | $fileInfo = $mediaSource->getFileInfo($data); |
| 84 | $stream = $mediaSource->getFileStream($data); |
| 85 | |
| 86 | return sprintf(self::DATA_PREFIX, $fileInfo['mime'], base64_encode($stream->getContents())); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | $file = $this->directory->getFile($data); |
| 91 | |
| 92 | if ($file->exists()) { |
| 93 | return sprintf(self::DATA_PREFIX, $file->getMimeType(), base64_encode($file->read())); |
| 94 | } |
| 95 | |
| 96 | throw new ExceptionMissingAsset(sprintf( |
| 97 | 'Assets %s not found at %s', |
| 98 | $data, |
| 99 | $file->getPrefix() |
| 100 | )); |
| 101 | } |
| 102 | } |