-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileUploadComponent.php
More file actions
80 lines (73 loc) · 2.47 KB
/
FileUploadComponent.php
File metadata and controls
80 lines (73 loc) · 2.47 KB
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
<?php
/**
* FileUpload Component
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Component', 'Controller');
App::uses('FileModel', 'Files.Model');
App::uses('TemporaryUploadFile', 'Files.Utility');
/**
* FileUpload Component
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Files\Controller\Component
*/
class FileUploadComponent extends Component {
/**
* @var Controller 呼び出し元コントローラ
*/
public $controller;
/**
* Called before the Controller::beforeFilter().
*
* @param Controller $controller Instantiating controller
* @return void
*/
public function initialize(Controller $controller) {
$this->controller = $controller;
}
/**
* Called after the Controller::beforeFilter() and before the controller action
*
* @param Controller $controller Controller with components to startup
* @return void
*/
public function startup(Controller $controller) {
// ファイルアップロード等で post_max_size を超えると $_POSTが空っぽになるため、このタイミングでエラー表示
$contentLength = Hash::get($_SERVER, 'CONTENT_LENGTH');
if ($contentLength > CakeNumber::fromReadableSize(ini_get('post_max_size'))) {
$message = __d('files', 'FileUpload.post_max_size.over');
$controller->NetCommons->setFlashNotification($message, array(
'class' => 'danger',
'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL,
));
$controller->redirect($controller->referer());
}
}
/**
* アップロードされたテンポラリファイルを得る。
*
* @param string $fieldName フォームのフィールド名
* @return TemporaryUploadFile
*/
public function getTemporaryUploadFile($fieldName) {
$fileInfo = Hash::get($this->controller->request->data, $fieldName);
return $this->_getTemporaryUploadFile($fileInfo);
}
/**
* TemporaryUploadFileインスタンス生成
*
* @param array $fileInfo $_FILES[xxx]相当の配列
* @return TemporaryUploadFile
*
* @codeCoverageIgnore TempoaryUploadFileは実アップロードされたファイルの情報を渡さないと内部でmov_uploaded_fileが失敗するのでテストできない
*/
protected function _getTemporaryUploadFile($fileInfo) {
return new TemporaryUploadFile($fileInfo);
}
}