FileAction.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\forms\UploadForm;
  6. use common\helpers\ResultHelper;
  7. use common\helpers\UploadHelper;
  8. use common\enums\AttachmentUploadTypeEnum;
  9. /**
  10. * Trait FileAction
  11. * @package common\traits
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. trait FileAction
  15. {
  16. /**
  17. * 根据md5获取文件
  18. *
  19. * @return array
  20. */
  21. public function actionVerifyMd5()
  22. {
  23. $md5 = Yii::$app->request->post('md5');
  24. $drive = Yii::$app->request->post('drive');
  25. $cate_id = Yii::$app->request->post('cate_id');
  26. $upload_type = Yii::$app->request->post('upload_type');
  27. if ($file = Yii::$app->services->attachment->findByMd5($md5, $drive, $cate_id, $upload_type)) {
  28. $file['size'] = Yii::$app->formatter->asShortSize($file['size'], 2);
  29. $file['upload_type'] = UploadHelper::formattingFileType($file['specific_type'], $file['extension'], $file['upload_type']);
  30. return ResultHelper::json(200, '获取成功', $file);
  31. }
  32. return ResultHelper::json(404, '找不到文件');
  33. }
  34. /**
  35. * base64编码的上传
  36. *
  37. * @return array
  38. */
  39. public function actionBase64()
  40. {
  41. try {
  42. // 保存扩展名称
  43. $extend = Yii::$app->request->post('extend', 'jpg');
  44. !in_array($extend, Yii::$app->params['uploadConfig']['images']['extensions']) && $extend = 'jpg';
  45. $data = [
  46. 'fileData' => base64_decode(Yii::$app->request->post('image', '')),
  47. 'extend' => $extend
  48. ];
  49. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES, 'base64');
  50. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  51. } catch (\Exception $e) {
  52. return ResultHelper::json(404, $e->getMessage());
  53. }
  54. }
  55. /**
  56. * Markdown 图片上传
  57. *
  58. * @return array
  59. * @throws \yii\web\NotFoundHttpException
  60. */
  61. public function actionImagesMarkdown()
  62. {
  63. try {
  64. $data = Yii::$app->request->get();
  65. $data['fileName'] = 'editormd-image-file';
  66. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES);
  67. $info = $upload->getInfo();
  68. Yii::$app->response->format = Response::FORMAT_JSON;
  69. return [
  70. 'success' => 1,
  71. 'url' => $info['url'],
  72. ];
  73. } catch (\Exception $e) {
  74. return ResultHelper::json(404, $e->getMessage());
  75. }
  76. }
  77. /**
  78. * 图片上传
  79. *
  80. * @return array
  81. * @throws \yii\web\NotFoundHttpException
  82. */
  83. public function actionImages()
  84. {
  85. try {
  86. $data = Yii::$app->request->post();
  87. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES);
  88. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  89. } catch (\Exception $e) {
  90. return ResultHelper::json(422, $e->getMessage());
  91. }
  92. }
  93. /**
  94. * 文件上传
  95. *
  96. * @return array
  97. * @throws \yii\web\NotFoundHttpException
  98. */
  99. public function actionFiles()
  100. {
  101. try {
  102. $data = Yii::$app->request->post();
  103. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::FILES);
  104. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  105. } catch (\Exception $e) {
  106. return ResultHelper::json(404, $e->getMessage());
  107. }
  108. }
  109. /**
  110. * 视频上传
  111. *
  112. * @return array
  113. * @throws \yii\web\NotFoundHttpException
  114. */
  115. public function actionVideos()
  116. {
  117. try {
  118. $data = Yii::$app->request->post();
  119. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::VIDEOS);
  120. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  121. } catch (\Exception $e) {
  122. return ResultHelper::json(404, $e->getMessage());
  123. }
  124. }
  125. /**
  126. * 语音上传
  127. *
  128. * @return array
  129. * @throws \yii\web\NotFoundHttpException
  130. */
  131. public function actionVoices()
  132. {
  133. try {
  134. $data = Yii::$app->request->post();
  135. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::VOICES);
  136. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  137. } catch (\Exception $e) {
  138. return ResultHelper::json(404, $e->getMessage());
  139. }
  140. }
  141. /**
  142. * 合并
  143. *
  144. * @return array|mixed
  145. * @throws \League\Flysystem\FileExistsException
  146. * @throws \League\Flysystem\FileNotFoundException
  147. * @throws \Exception
  148. */
  149. public function actionMerge()
  150. {
  151. $guid = Yii::$app->request->post('guid');
  152. $upload = Yii::$app->cache->get(UploadHelper::PREFIX_MERGE_CACHE . $guid);
  153. if (empty($upload)) {
  154. return ResultHelper::json(404, '找不到文件信息, 合并文件失败');
  155. }
  156. /** @var UploadForm $upload */
  157. $upload->superAddition = true;
  158. $upload->fileSystemInit();
  159. UploadHelper::merge($upload);
  160. Yii::$app->cache->delete('upload-file-guid:' . $guid);
  161. return ResultHelper::json(200, '上传成功', $upload->getInfo());
  162. }
  163. /**
  164. * oss直传配置
  165. *
  166. * @return array
  167. * @throws \Exception
  168. */
  169. public function actionOssAccredit()
  170. {
  171. // 上传类型
  172. $type = Yii::$app->request->get('type');
  173. $typeConfig = Yii::$app->params['uploadConfig'][$type];
  174. $path = $typeConfig['path'] . date($typeConfig['subName'], time()) . "/";
  175. $oss = Yii::$app->services->extendUpload->ossConfig($typeConfig['maxSize'], $path, 60 * 60 * 2, $type);
  176. return $oss;
  177. }
  178. /**
  179. * 权限验证
  180. *
  181. * @param string $action 当前的方法
  182. * @param null $model 当前的模型类
  183. * @param array $params $_GET变量
  184. * @throws \yii\web\BadRequestHttpException
  185. */
  186. public function checkAccess($action, $model = null, $params = [])
  187. {
  188. // 方法名称
  189. if (in_array($action, ['index', 'view', 'update', 'create', 'delete'])) {
  190. throw new \yii\web\BadRequestHttpException('权限不足');
  191. }
  192. }
  193. }
粤ICP备19079148号