1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mime\Check; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
8
|
|
|
use kalanis\kw_files\Interfaces\IProcessFiles; |
9
|
|
|
use kalanis\kw_files\Traits\TFile; |
10
|
|
|
use kalanis\kw_mime\Interfaces\IMimeChecks; |
11
|
|
|
use kalanis\kw_mime\Interfaces\IMiTranslations; |
12
|
|
|
use kalanis\kw_mime\MimeException; |
13
|
|
|
use kalanis\kw_paths\ArrayPath; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
use kalanis\kw_paths\Stuff; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ResourceFiles |
20
|
|
|
* @package kalanis\kw_mime\Check |
21
|
|
|
* System library to detect the mime type by files processor - pass as file in temp dir |
22
|
|
|
*/ |
23
|
|
|
class TempFiles implements IMimeChecks |
24
|
|
|
{ |
25
|
|
|
use Traits\TResult; |
26
|
|
|
use Traits\TToLocalFile; |
27
|
|
|
use TFile; |
28
|
|
|
|
29
|
|
|
protected ArrayPath $pathLib; |
30
|
|
|
|
31
|
5 |
|
public function __construct(?IFLTranslations $flLang = null, ?IMiTranslations $miLang = null) |
32
|
|
|
{ |
33
|
5 |
|
$this->setFlLang($flLang); |
34
|
5 |
|
$this->setMiLang($miLang); |
35
|
5 |
|
$this->pathLib = new ArrayPath(); |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
public function canUse($source): bool |
39
|
|
|
{ |
40
|
4 |
|
if (!$this->isMimeFunction()) { |
41
|
1 |
|
return false; |
42
|
|
|
} |
43
|
3 |
|
if (is_object($source) && ($source instanceof IProcessFiles)) { |
44
|
2 |
|
$this->setProcessFile($source); |
45
|
2 |
|
return true; |
46
|
|
|
} |
47
|
1 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
public function getMime(array $path): string |
51
|
|
|
{ |
52
|
3 |
|
$tempFile = tempnam(sys_get_temp_dir(), 'MimeCheck'); |
53
|
3 |
|
if (false === $tempFile) { |
54
|
|
|
// @codeCoverageIgnoreStart |
55
|
|
|
throw new MimeException($this->getMiLang()->miCannotLoadTempFile()); |
56
|
|
|
} |
57
|
|
|
// @codeCoverageIgnoreEnd |
58
|
|
|
|
59
|
3 |
|
$this->checkMimeFunction(); |
60
|
|
|
try { |
61
|
3 |
|
$content = $this->getProcessFile()->readFile($path); |
62
|
2 |
|
$this->readSourceToLocalFile(Stuff::arrayToPath($path), $content, $tempFile); |
63
|
2 |
|
$mime = mime_content_type($tempFile); |
64
|
1 |
|
} catch (FilesException | PathsException $ex) { |
65
|
1 |
|
throw new MimeException($ex->getMessage(), $ex->getCode(), $ex); |
66
|
|
|
} finally { |
67
|
3 |
|
@unlink($tempFile); |
|
|
|
|
68
|
|
|
} |
69
|
2 |
|
return $this->determineResult($mime); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: