1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AhmadMayahi\Vision\Support; |
6
|
|
|
|
7
|
|
|
use AhmadMayahi\Vision\Contracts\File as FileContract; |
8
|
|
|
use AhmadMayahi\Vision\Exceptions\FileException; |
9
|
|
|
use AhmadMayahi\Vision\Exceptions\UnsupportedFileTypeException; |
10
|
|
|
use Exception; |
11
|
|
|
use SplFileInfo; |
12
|
|
|
use SplFileObject; |
13
|
|
|
|
14
|
|
|
class File implements FileContract |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string|resource|SplFileInfo|SplFileObject $file Input file |
18
|
|
|
* @param null|string $tempDirPath Full path to temporary directory |
19
|
|
|
*/ |
20
|
|
|
public function __construct(private $file, private ?string $tempDirPath = null) |
21
|
|
|
{ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generates vision compatible to be used as a file input in Google\Cloud\Vision\V1\ImageAnnotatorClient |
26
|
|
|
* |
27
|
|
|
* @return resource|string |
28
|
|
|
* @throws FileException |
29
|
|
|
*/ |
30
|
|
|
public function toVisionFile() |
31
|
|
|
{ |
32
|
|
|
if (is_resource($this->file)) { |
33
|
|
|
return $this->file; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->file instanceof SplFileInfo) { |
37
|
|
|
$fileObj = $this->file->openFile(); |
38
|
|
|
|
39
|
|
|
return (string) $fileObj->fread($this->file->getSize()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($this->isGoogleStoragePath()) { |
43
|
|
|
return (string) $this->file; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($this->isLocalFile()) { |
47
|
|
|
return (string) file_get_contents($this->file); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new FileException('File not found or not compatible!'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getLocalPathname(): string |
54
|
|
|
{ |
55
|
|
|
if ($this->isGoogleStoragePath()) { |
56
|
|
|
throw new Exception('Google Storage is not supported for this operation!'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (is_resource($this->file)) { |
60
|
|
|
return $this->getStreamFilePath(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->file instanceof SplFileInfo) { |
64
|
|
|
return $this->file->getPathname(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->isLocalFile()) { |
68
|
|
|
return (string) $this->file; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new FileException('Cannot get the local file path'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getStreamFilePath(): string |
75
|
|
|
{ |
76
|
|
|
if (false === $this->isResource()) { |
77
|
|
|
throw new FileException('File is not resource!'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$tempFile = $this->createTempFile(); |
81
|
|
|
$this->saveStream($tempFile); |
82
|
|
|
|
83
|
|
|
return $tempFile; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function createTempFile(): string |
87
|
|
|
{ |
88
|
|
|
return tempnam($this->getTempDir(), sha1(uniqid())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function saveStream(string $tempFile): void |
92
|
|
|
{ |
93
|
|
|
file_put_contents($tempFile, stream_get_contents($this->file)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getContents() |
97
|
|
|
{ |
98
|
|
|
if ($this->isGoogleStoragePath()) { |
99
|
|
|
throw new UnsupportedFileTypeException('Google Storage is not supported!'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($this->isLocalFile()) { |
103
|
|
|
return file_get_contents($this->file); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_resource($this->file)) { |
107
|
|
|
return stream_get_contents($this->file); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->file instanceof SplFileInfo) { |
111
|
|
|
return file_get_contents($this->file->getPathname()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function isGoogleStoragePath(): bool |
116
|
|
|
{ |
117
|
|
|
return is_string($this->file) && str_starts_with($this->file, 'gs://'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function isResource(): bool |
121
|
|
|
{ |
122
|
|
|
return is_resource($this->file); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function isLocalFile(): bool |
126
|
|
|
{ |
127
|
|
|
return is_string($this->file) && file_exists($this->file); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function getTempDir(): string |
131
|
|
|
{ |
132
|
|
|
return $this->tempDirPath ?? sys_get_temp_dir(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|