1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mime\Check; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mime\Interfaces\IMimeChecks; |
7
|
|
|
use kalanis\kw_mime\Interfaces\IMiTranslations; |
8
|
|
|
use kalanis\kw_mime\MimeException; |
9
|
|
|
use kalanis\kw_paths\ArrayPath; |
10
|
|
|
use kalanis\kw_paths\PathsException; |
11
|
|
|
use kalanis\kw_storage\Storage; |
12
|
|
|
use kalanis\kw_storage\StorageException; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DataStorage |
17
|
|
|
* @package kalanis\kw_mime\Check |
18
|
|
|
* System library to detect the mime type in storage |
19
|
|
|
*/ |
20
|
|
|
class DataStorage implements IMimeChecks |
21
|
|
|
{ |
22
|
|
|
use Traits\TCheckCalls; |
23
|
|
|
use Traits\TResult; |
24
|
|
|
use Traits\TStorage; |
25
|
|
|
|
26
|
|
|
/** @var ArrayPath */ |
27
|
|
|
protected $pathLib = null; |
28
|
|
|
|
29
|
6 |
|
public function __construct(?IMiTranslations $lang = null) |
30
|
|
|
{ |
31
|
6 |
|
$this->setMiLang($lang); |
32
|
6 |
|
$this->pathLib = new ArrayPath(); |
33
|
6 |
|
} |
34
|
|
|
|
35
|
5 |
|
public function canUse($source): bool |
36
|
|
|
{ |
37
|
5 |
|
if (!$this->isMimeFunction()) { |
38
|
1 |
|
return false; |
39
|
|
|
} |
40
|
4 |
|
if (is_object($source) && ($source instanceof Storage)) { |
41
|
3 |
|
$this->setStorage($source); |
42
|
3 |
|
return true; |
43
|
|
|
} |
44
|
1 |
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function getMime(array $path): string |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
3 |
|
$this->checkMimeFunction(); |
51
|
3 |
|
$target = $this->pathLib->setArray($path)->getString(); |
52
|
3 |
|
$content = $this->getStorage()->get($target); |
53
|
2 |
|
$fileData = 'data://,' . urlencode(strval($content)); |
54
|
2 |
|
return $this->determineResult(mime_content_type($fileData)); |
55
|
1 |
|
} catch (StorageException | PathsException $ex) { |
56
|
1 |
|
throw new MimeException($ex->getMessage(), $ex->getCode(), $ex); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|