1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Teebot\Entity; |
4
|
|
|
|
5
|
|
|
use Teebot\Command\Executor; |
6
|
|
|
use Teebot\Exception\Critical; |
7
|
|
|
use Teebot\Exception\Output; |
8
|
|
|
|
9
|
|
|
class File extends AbstractEntity |
10
|
|
|
{ |
11
|
|
|
const ENTITY_TYPE = 'File'; |
12
|
|
|
|
13
|
|
|
protected $file_id; |
14
|
|
|
|
15
|
|
|
protected $file_size; |
16
|
|
|
|
17
|
|
|
protected $file_path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
|
|
public function getFileId() |
23
|
|
|
{ |
24
|
|
|
return $this->file_id; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $file_id |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function setFileId($file_id) |
33
|
|
|
{ |
34
|
|
|
$this->file_id = $file_id; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function getFileSize() |
43
|
|
|
{ |
44
|
|
|
return $this->file_size; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $file_size |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setFileSize($file_size) |
53
|
|
|
{ |
54
|
|
|
$this->file_size = $file_size; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function getFilePath() |
63
|
|
|
{ |
64
|
|
|
return $this->file_path; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $file_path |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setFilePath($file_path) |
73
|
|
|
{ |
74
|
|
|
$this->file_path = $file_path; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getFullPath() |
80
|
|
|
{ |
81
|
|
|
if (!$this->file_path) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$basePath = Executor::getInstance() |
86
|
|
|
->getConfig() |
87
|
|
|
->getFileBasePath(); |
88
|
|
|
|
89
|
|
|
if (!$basePath) { |
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $basePath . $this->file_path; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function download($storePath) |
97
|
|
|
{ |
98
|
|
|
if (file_exists($storePath) && !is_writable($storePath)) { |
99
|
|
|
throw new Critical('File "' . $storePath . '" is already exist!"'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$filePath = $this->getFullPath(); |
103
|
|
|
|
104
|
|
|
if (!$filePath) { |
|
|
|
|
105
|
|
|
throw new Critical('Unable to get download path to file!'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$contents = file_get_contents($filePath); |
110
|
|
|
|
111
|
|
|
file_put_contents($storePath, $contents); |
112
|
|
|
} catch (\Exception $e) { |
113
|
|
|
Output::log(new Critical($e->getMessage())); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return is_readable($storePath); |
117
|
|
|
} |
118
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: