UploadAction.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/16
  6. * Time: 上午11:14
  7. */
  8. namespace common\modules\attachment\actions;
  9. use common\modules\attachment\components\UploadedFile;
  10. use common\modules\attachment\models\Attachment;
  11. use Yii;
  12. use yii\base\Action;
  13. use yii\base\DynamicModel;
  14. use yii\base\Exception;
  15. use yii\web\BadRequestHttpException;
  16. use yii\web\Response;
  17. class UploadAction extends Action
  18. {
  19. /**
  20. * @var string Path to directory where files will be uploaded
  21. */
  22. public $path;
  23. /**
  24. * @var string Validator name
  25. */
  26. public $uploadOnlyImage = true;
  27. /**
  28. * @var string Variable's name that Imperavi Redactor sent upon image/file upload.
  29. */
  30. public $uploadParam = 'file';
  31. /**
  32. * @var string 参数指定文件名
  33. */
  34. public $uploadQueryParam = 'fileparam';
  35. public $multiple = false;
  36. /**
  37. * @var array Model validator options
  38. */
  39. public $validatorOptions = [];
  40. /**
  41. * @var string Model validator name
  42. */
  43. private $_validator = 'image';
  44. public $deleteUrl = ['/upload/delete'];
  45. public $callback;
  46. public $itemCallback;
  47. /**
  48. * @inheritdoc
  49. */
  50. public function init()
  51. {
  52. if (Yii::$app->request->get($this->uploadQueryParam)) {
  53. $this->uploadParam = Yii::$app->request->get($this->uploadQueryParam);
  54. }
  55. if ($this->uploadOnlyImage !== true) {
  56. $this->_validator = 'file';
  57. }
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function run()
  63. {
  64. Yii::$app->response->format = Response::FORMAT_JSON;
  65. if (Yii::$app->request->isPost) {
  66. $files = UploadedFile::getInstancesByName($this->uploadParam);
  67. // p($files);
  68. if (!$this->multiple) {
  69. $res = [$this->uploadOne($files[0])];
  70. } else {
  71. $res = $this->uploadMore($files);
  72. }
  73. $result = [
  74. 'files' => $res
  75. ];
  76. if ($this->callback instanceof \Closure) {
  77. $result = call_user_func($this->callback, $result);
  78. }
  79. return $result;
  80. } else {
  81. throw new BadRequestHttpException('Only POST is allowed');
  82. }
  83. }
  84. private function uploadMore(array $files) {
  85. $res = [];
  86. foreach ($files as $file) {
  87. $result = $this->uploadOne($file);
  88. $res[] = $result;
  89. }
  90. return $res;
  91. }
  92. private function uploadOne(UploadedFile $file)
  93. {
  94. try {
  95. $model = new DynamicModel(compact('file'));
  96. $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
  97. if ($model->hasErrors()) {
  98. throw new Exception($model->getFirstError('file'));
  99. } else {
  100. $attachment = Attachment::uploadFromPost($this->path, $file);
  101. $result = [
  102. 'id' => $attachment->id,
  103. 'name' => $attachment->name,
  104. 'hash' => $attachment->hash,
  105. 'url' => $attachment->url,
  106. 'path' => $attachment->path,
  107. 'extension' => $attachment->extension,
  108. 'type' => $attachment->type,
  109. 'size' => $attachment->size
  110. ];
  111. if ($this->uploadOnlyImage !== true) {
  112. $result['filename'] = $file->name;
  113. }
  114. }
  115. if ($this->itemCallback instanceof \Closure) {
  116. $result = call_user_func($this->itemCallback, $result);
  117. }
  118. } catch (Exception $e) {
  119. $result = [
  120. 'error' => $e->getMessage()
  121. ];
  122. }
  123. return $result;
  124. }
  125. }