1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Upload; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class resource upload |
8
|
|
|
*/ |
9
|
|
|
abstract class Resource |
10
|
|
|
{ |
11
|
|
|
use TraitUpload; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var object|null |
15
|
|
|
*/ |
16
|
|
|
private ?object $files; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private string $path; |
21
|
|
|
/** |
22
|
|
|
* @var array|null |
23
|
|
|
*/ |
24
|
|
|
private ?array $mimeType; |
25
|
|
|
/** |
26
|
|
|
* @var array|object |
27
|
|
|
*/ |
28
|
|
|
private array|object $data; |
29
|
|
|
/** |
30
|
|
|
* @var object |
31
|
|
|
*/ |
32
|
|
|
private object $response; |
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
protected ?string $url; |
37
|
|
|
/** |
38
|
|
|
* @var string|null |
39
|
|
|
*/ |
40
|
|
|
protected ?string $key; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
*/ |
45
|
|
|
public function __construct(?string $url = null, ?string $key = null) |
46
|
|
|
{ |
47
|
|
|
$this->url = $url; |
48
|
|
|
$this->key = $key; |
49
|
|
|
$this->setFiles(); |
50
|
|
|
} |
51
|
|
|
/** |
52
|
|
|
* @return object|null |
53
|
|
|
*/ |
54
|
|
|
protected function getFiles(): ?object |
55
|
|
|
{ |
56
|
|
|
return $this->files; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* convert in object and count files |
61
|
|
|
*/ |
62
|
|
|
private function setFiles(): bool |
63
|
|
|
{ |
64
|
|
|
$files = []; |
65
|
|
|
if (!empty($_FILES)) { |
66
|
|
|
$this->setMimeType(); |
67
|
|
|
$upload = $_FILES; |
68
|
|
|
foreach ($upload as $key => $file) { |
69
|
|
|
if($this->url) |
70
|
|
|
{ |
71
|
|
|
$url_array = explode("/", $this->url); |
72
|
|
|
$file['name'] = end($url_array); |
73
|
|
|
$finfo = new \finfo(FILEINFO_MIME_TYPE); |
74
|
|
|
$mime_type = $finfo->buffer(file_get_contents($this->url)); |
75
|
|
|
$file['type'] = $mime_type; |
76
|
|
|
$file['key'] = $this->key; |
77
|
|
|
} |
78
|
|
|
else{ |
79
|
|
|
$file['key'] = $key; |
80
|
|
|
} |
81
|
|
|
[$type] = explode("/", $file['type']); |
82
|
|
|
$this->setPath($type); |
83
|
|
|
$file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION); |
84
|
|
|
$file['name'] = $this->slug(pathinfo($file['name'], PATHINFO_FILENAME)); |
|
|
|
|
85
|
|
|
$file['path'] = $this->getPath(); |
86
|
|
|
$file['directory'] = dirname(__DIR__, 4) . "/" . $this->getPath(); |
87
|
|
|
$files[] = (object)$file; |
88
|
|
|
if (!in_array($file['type'], $this->getMimeType(), true)) { |
89
|
|
|
$this->files = null; |
90
|
|
|
$this->setResponse(400, "error","invalid file format ".$file['type'], "upload", dynamic: $file['type'] ); |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
$this->files = (object)$files; |
95
|
|
|
} else { |
96
|
|
|
$this->files = null; |
97
|
|
|
} |
98
|
|
|
$this->setResponse(200, "success","defined attribute", "upload"); |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string|null |
104
|
|
|
*/ |
105
|
|
|
private function getPath(): ?string |
106
|
|
|
{ |
107
|
|
|
return $this->path; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $type |
112
|
|
|
*/ |
113
|
|
|
private function setPath(string $type): void |
114
|
|
|
{ |
115
|
|
|
$this->path = UPLOAD_DIR . "/$type/" . date('Y') . "/" . date('m') . "/" . date('d'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array|null |
120
|
|
|
*/ |
121
|
|
|
private function getMimeType(): ?array |
122
|
|
|
{ |
123
|
|
|
return $this->mimeType; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* valide mimetype accept |
128
|
|
|
*/ |
129
|
|
|
private function setMimeType(): void |
130
|
|
|
{ |
131
|
|
|
$this->mimeType = UPLOAD_MIMETYPE; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return object |
136
|
|
|
*/ |
137
|
|
|
protected function getData(): object |
138
|
|
|
{ |
139
|
|
|
return (object) $this->data; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $key |
144
|
|
|
* @param string $value |
145
|
|
|
*/ |
146
|
|
|
protected function setData(string $key, string $value): void |
147
|
|
|
{ |
148
|
|
|
$this->data[$key] = $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return object |
153
|
|
|
*/ |
154
|
|
|
protected function getResponse(): object |
155
|
|
|
{ |
156
|
|
|
return $this->response; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $code |
161
|
|
|
* @param string $type |
162
|
|
|
* @param string $text |
163
|
|
|
* @param string $model |
164
|
|
|
* @param object|null $data |
165
|
|
|
* @param string|null $dynamic |
166
|
|
|
*/ |
167
|
|
|
protected function setResponse(int $code, string $type, string $text, string $model, ?object $data = null, ?string $dynamic = null): void |
168
|
|
|
{ |
169
|
|
|
$this->response = (object)[ |
170
|
|
|
"code" => $code, |
171
|
|
|
"type" => $type, |
172
|
|
|
"text" => $text, |
173
|
|
|
"model" => $model, |
174
|
|
|
"data" => $data, |
175
|
|
|
"dynamic" => $dynamic |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
} |