UploadAction.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services\Ueditor;
  3. class UploadAction
  4. {
  5. protected $config;
  6. protected $action;
  7. public function __construct($config, $action)
  8. {
  9. $this->config = $config;
  10. $this->action = $action;
  11. }
  12. public function doUpload()
  13. {
  14. /* 上传配置 */
  15. $base64 = "upload";
  16. switch (htmlspecialchars($this->action)) {
  17. case 'uploadimage':
  18. $config = array(
  19. "pathFormat" => $this->config['imagePathFormat'],
  20. "maxSize" => $this->config['imageMaxSize'],
  21. "allowFiles" => $this->config['imageAllowFiles']
  22. );
  23. $fieldName = $this->config['imageFieldName'];
  24. break;
  25. case 'uploadscrawl':
  26. $config = array(
  27. "pathFormat" => $this->config['scrawlPathFormat'],
  28. "maxSize" => $this->config['scrawlMaxSize'],
  29. "allowFiles" => $this->config['scrawlAllowFiles'],
  30. "oriName" => "scrawl.png"
  31. );
  32. $fieldName = $this->config['scrawlFieldName'];
  33. $base64 = "base64";
  34. break;
  35. case 'uploadvideo':
  36. $config = array(
  37. "pathFormat" => $this->config['videoPathFormat'],
  38. "maxSize" => $this->config['videoMaxSize'],
  39. "allowFiles" => $this->config['videoAllowFiles']
  40. );
  41. $fieldName = $this->config['videoFieldName'];
  42. break;
  43. case 'uploadfile':
  44. default:
  45. $config = array(
  46. "pathFormat" => $this->config['filePathFormat'],
  47. "maxSize" => $this->config['fileMaxSize'],
  48. "allowFiles" => $this->config['fileAllowFiles']
  49. );
  50. $fieldName = $this->config['fileFieldName'];
  51. break;
  52. }
  53. /* 生成上传实例对象并完成上传 */
  54. $up = new UploaderService($fieldName, $config, $base64);
  55. /**
  56. * 得到上传文件所对应的各个参数,数组结构
  57. * array(
  58. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  59. * "url" => "", //返回的地址
  60. * "title" => "", //新文件名
  61. * "original" => "", //原始文件名
  62. * "type" => "" //文件类型
  63. * "size" => "", //文件大小
  64. * )
  65. */
  66. /* 返回数据 */
  67. return json_encode($up->getFileInfo());
  68. }
  69. }