|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @link https://github.com/rkit/filemanager-yii2 |
|
5
|
|
|
* @copyright Copyright (c) 2015 Igor Romanov |
|
6
|
|
|
* @license [MIT](http://opensource.org/licenses/MIT) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace rkit\filemanager\storages; |
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\helpers\FileHelper; |
|
13
|
|
|
use rkit\filemanager\Storage; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is the local storage for files |
|
17
|
|
|
*/ |
|
18
|
|
|
class LocalStorage extends Storage |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Upload directory |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
34 |
|
private function uploadDir() |
|
26
|
|
|
{ |
|
27
|
34 |
|
if ($this->getFile()->isProtected()) { |
|
28
|
8 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirProtected); |
|
29
|
|
|
} else { |
|
30
|
26 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirUnprotected); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Path to the temporary directory of the file |
|
36
|
|
|
* |
|
37
|
|
|
* @param bool $realPath |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
28 |
|
private function dirTmp($realPath = false) |
|
41
|
|
|
{ |
|
42
|
28 |
|
$file = $this->getFile(); |
|
43
|
|
|
|
|
44
|
28 |
|
$path = $realPath ? $this->uploadDir() : ''; |
|
45
|
28 |
|
$path .= '/' . Yii::$app->fileManager->publicPath . '/tmp'; |
|
46
|
28 |
|
$path .= '/' . $file->getDateOfFile(); |
|
47
|
28 |
|
$path .= '/' . $file->owner_type . '/' . $file->id; |
|
48
|
|
|
|
|
49
|
28 |
|
return $path; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Path to the directory of the file |
|
54
|
|
|
* |
|
55
|
|
|
* @param bool $realPath |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
34 |
|
private function dir($realPath = false) |
|
59
|
|
|
{ |
|
60
|
34 |
|
$file = $this->getFile(); |
|
61
|
|
|
|
|
62
|
34 |
|
if ($file->tmp) { |
|
63
|
28 |
|
return $this->dirTmp($realPath); |
|
64
|
|
|
} else { |
|
65
|
32 |
|
$path = $realPath ? $this->uploadDir() : ''; |
|
66
|
32 |
|
$path .= '/' . Yii::$app->fileManager->publicPath; |
|
67
|
32 |
|
$path .= '/' . $file->getDateOfFile(); |
|
68
|
32 |
|
$path .= '/' . $file->owner_type . '/' . $file->owner_id . '/' . $file->id; |
|
69
|
|
|
|
|
70
|
32 |
|
return $path; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Path to the temporary file |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool $realPath |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
26 |
|
private function pathTmp($realPath = false) |
|
81
|
|
|
{ |
|
82
|
26 |
|
return $this->dirTmp($realPath) . '/'. $this->getFile()->name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Path to the file |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $realPath |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
34 |
|
public function path($realPath = false) |
|
92
|
|
|
{ |
|
93
|
34 |
|
return $this->dir($realPath) . '/'. $this->getFile()->name; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Save the file to the storage or temporary directory |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $tempFile |
|
|
|
|
|
|
100
|
|
|
* @param bool $temporary |
|
|
|
|
|
|
101
|
|
|
* @param bool $isUploadedFile File has been uploaded or manually created |
|
102
|
|
|
* @return \rkit\filemanager\models\File|bool |
|
103
|
|
|
*/ |
|
104
|
35 |
|
public function save($path, $isUploadedFile = true) |
|
105
|
|
|
{ |
|
106
|
35 |
|
$file = $this->getFile(); |
|
107
|
35 |
|
if (file_exists($path)) { |
|
108
|
34 |
|
if (FileHelper::createDirectory($this->dir(true))) { |
|
109
|
34 |
|
$isConsole = Yii::$app instanceof \yii\console\Application; |
|
110
|
34 |
|
if (!$isUploadedFile || $isConsole) { |
|
111
|
34 |
|
$saved = rename($path, $this->path(true)); |
|
112
|
34 |
|
} else { |
|
113
|
|
|
$saved = move_uploaded_file($path, $this->path(true)); // @codeCoverageIgnore |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
34 |
|
if ($saved) { |
|
117
|
34 |
|
return $file; |
|
118
|
|
|
} |
|
119
|
|
|
} // @codeCoverageIgnore |
|
120
|
|
|
} // @codeCoverageIgnore |
|
121
|
|
|
|
|
122
|
1 |
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Save temporary directory to the storage |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
26 |
|
public function saveTmpDirToStorage() |
|
131
|
|
|
{ |
|
132
|
26 |
|
if (file_exists($this->pathTmp(true))) { |
|
133
|
26 |
|
FileHelper::copyDirectory($this->dirTmp(true), $this->dir(true)); |
|
134
|
26 |
|
FileHelper::removeDirectory($this->dirTmp(true)); |
|
135
|
26 |
|
return true; |
|
136
|
|
|
} // @codeCoverageIgnore |
|
137
|
|
|
|
|
138
|
2 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
11 |
|
public function delete() |
|
142
|
|
|
{ |
|
143
|
11 |
|
FileHelper::removeDirectory($this->dir(true)); |
|
144
|
11 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.