|
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
|
|
|
protected ArrayPath $pathLib; |
|
27
|
|
|
|
|
28
|
6 |
|
public function __construct(?IMiTranslations $lang = null) |
|
29
|
|
|
{ |
|
30
|
6 |
|
$this->setMiLang($lang); |
|
31
|
6 |
|
$this->pathLib = new ArrayPath(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
public function canUse($source): bool |
|
35
|
|
|
{ |
|
36
|
5 |
|
if (!$this->isMimeFunction()) { |
|
37
|
1 |
|
return false; |
|
38
|
|
|
} |
|
39
|
4 |
|
if (is_object($source) && ($source instanceof Storage)) { |
|
40
|
3 |
|
$this->setStorage($source); |
|
41
|
3 |
|
return true; |
|
42
|
|
|
} |
|
43
|
1 |
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
public function getMime(array $path): string |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
3 |
|
$this->checkMimeFunction(); |
|
50
|
3 |
|
$target = $this->pathLib->setArray($path)->getString(); |
|
51
|
3 |
|
$content = $this->getStorage()->get($target); |
|
52
|
2 |
|
$fileData = 'data://,' . urlencode(strval($content)); |
|
53
|
2 |
|
return $this->determineResult(mime_content_type($fileData)); |
|
54
|
1 |
|
} catch (StorageException | PathsException $ex) { |
|
55
|
1 |
|
throw new MimeException($ex->getMessage(), $ex->getCode(), $ex); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|