1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
6
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadFileType; |
7
|
|
|
|
8
|
|
|
class GuessLaruploadFileTypeAction |
9
|
|
|
{ |
10
|
|
|
public function __construct(private readonly UploadedFile $file) {} |
11
|
|
|
|
12
|
|
|
public static function make(UploadedFile $file): self |
13
|
|
|
{ |
14
|
|
|
return new self($file); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
public function calc(): ?LaruploadFileType |
19
|
|
|
{ |
20
|
|
|
if ($this->file->isValid()) { |
21
|
|
|
$mime = $this->file->getMimeType(); |
22
|
|
|
|
23
|
|
|
return $this->mimeToType($mime); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function isImage(): bool |
30
|
|
|
{ |
31
|
|
|
return $this->mimeToType($this->file->getMimeType()) === LaruploadFileType::IMAGE; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function mimeToType(string $mime = null): LaruploadFileType |
35
|
|
|
{ |
36
|
|
|
if (str_contains($mime, 'image/')) { |
|
|
|
|
37
|
|
|
return LaruploadFileType::IMAGE; |
38
|
|
|
} |
39
|
|
|
else if (str_contains($mime, 'video/')) { |
40
|
|
|
return LaruploadFileType::VIDEO; |
41
|
|
|
} |
42
|
|
|
else if (str_contains($mime, 'audio/')) { |
43
|
|
|
return LaruploadFileType::AUDIO; |
44
|
|
|
} |
45
|
|
|
else if ($this->isDocument($mime)) { |
|
|
|
|
46
|
|
|
return LaruploadFileType::DOCUMENT; |
47
|
|
|
} |
48
|
|
|
else if ($this->isCompressed($mime)) { |
|
|
|
|
49
|
|
|
return LaruploadFileType::COMPRESSED; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return LaruploadFileType::FILE; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function isDocument(string $mime): bool { |
56
|
|
|
$mimeTypes = [ |
57
|
|
|
// pdf, epub, csv, tsv, txt |
58
|
|
|
'application/pdf', 'application/epub+zip', 'text/csv', 'text/tab-separated-values', 'text/plain', |
59
|
|
|
// doc, docx |
60
|
|
|
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
61
|
|
|
// ppt, pptx |
62
|
|
|
'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
63
|
|
|
// xls, xlsx |
64
|
|
|
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
65
|
|
|
// odt, ods, odp |
66
|
|
|
'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.oasis.opendocument.text', |
67
|
|
|
// rtf |
68
|
|
|
'application/rtf', |
69
|
|
|
// html, xml |
70
|
|
|
'application/xhtml+xml', 'text/html', 'text/xml', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
return in_array($mime, $mimeTypes); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function isCompressed(string $mime): bool { |
77
|
|
|
$mimeTypes = [ |
78
|
|
|
'application/zip', 'application/x-compressed', 'application/x-zip-compressed', 'multipart/x-zip', |
79
|
|
|
'application/x-rar-compressed', 'application/x-7z-compressed', 'application/x-tar', |
80
|
|
|
'application/gzip', 'application/x-bzip2', 'application/x-xz', 'application/x-lzip', 'application/x-lz4', |
81
|
|
|
'application/zstd', 'application/x-compress', 'application/gzip', 'application/x-bzip-compressed-tar', |
82
|
|
|
'application/x-bzip2-compressed-tar' |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
return in_array($mime, $mimeTypes); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|