-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPluploadAction.php
106 lines (93 loc) · 3.08 KB
/
PluploadAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace boundstate\plupload;
use Yii;
use yii\base\Action;
use yii\helpers\FileHelper;
use yii\web\HttpException;
use yii\web\Response;
use yii\web\UploadedFile;
/**
* PluploadAction class file.
*/
class PluploadAction extends Action
{
/**
* @var string file input name.
*/
public $inputName = 'file';
/**
* @var string the directory to store temporary files during conversion. You may use path alias here.
* If not set, it will use the "plupload" subdirectory under the application runtime path.
*/
public $tempPath = '@runtime/plupload';
/**
* @var integer the permission to be set for newly created cache files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;
/**
* @var callable success callback with signature: `function($filename, $params)`
*/
public $onComplete;
/**
* Initializes the action and ensures the temp path exists.
*/
public function init()
{
parent::init();
Yii::$app->response->format = Response::FORMAT_JSON;
$this->tempPath = Yii::getAlias($this->tempPath);
if (!is_dir($this->tempPath)) {
FileHelper::createDirectory($this->tempPath, $this->dirMode, true);
}
}
/**
* Runs the action.
* This method displays the view requested by the user.
* @throws HttpException if the view is invalid
*/
public function run()
{
$uploadedFile = UploadedFile::getInstanceByName($this->inputName);
$params = Yii::$app->request->getBodyParams();
$filename = $this->getUnusedPath($this->tempPath . DIRECTORY_SEPARATOR . $uploadedFile->name);
$isUploadComplete = ChunkUploader::process($uploadedFile, $filename);
if ($isUploadComplete) {
if ($this->onComplete) {
return call_user_func($this->onComplete, $filename, $params);
} else {
return [
'filename' => $filename,
'params' => $params,
];
}
}
return null;
}
/**
* Returns an unused file path by adding a filename suffix if necessary.
* @param string $path
* @return string
*/
protected function getUnusedPath($path) {
$newPath = $path;
$info = pathinfo($path);
$suffix = 1;
while (file_exists($newPath)) {
$newPath = $info['dirname'] . DIRECTORY_SEPARATOR . "{$info['filename']}_{$suffix}";
if (isset($info['extension'])) {
$newPath .= ".{$info['extension']}";
}
$suffix++;
}
return $newPath;
}
}