UploadController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\BaseAttachmentModel;
  4. use App\Models\ClassModel;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\Services\Uploader;
  8. class UploadController extends Controller
  9. {
  10. public function ueditorUpload(Request $request)
  11. {
  12. $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(url('/plugins/ueditor/config.json'))), true);
  13. $action = $_GET['action'];
  14. switch ($action) {
  15. case 'config':
  16. $result = json_encode($CONFIG);
  17. break;
  18. /* 上传图片 */
  19. case 'uploadimage':
  20. $config = array(
  21. "pathFormat" => $CONFIG['imagePathFormat'],
  22. "maxSize" => $CONFIG['imageMaxSize'],
  23. "allowFiles" => $CONFIG['imageAllowFiles']
  24. );
  25. $fieldName = $CONFIG['imageFieldName'];
  26. $up = new Uploader($fieldName, $config);
  27. $fileInfo = $up->getFileInfo();
  28. if($fileInfo['state'] == 'SUCCESS') {
  29. BaseAttachmentModel::create([
  30. 'path' => $fileInfo['url'],
  31. 'name' => $fileInfo['title'],
  32. // 'type' => $fileInfo['type'],
  33. 'class' => 'ueditor',
  34. 'size' => $fileInfo['size'],
  35. ]);
  36. ClassModel::firstOrCreate(['class' => 'ueditor']);
  37. }
  38. return json_encode($up->getFileInfo());
  39. /* 上传涂鸦 */
  40. case 'uploadscrawl':
  41. /* 上传视频 */
  42. case 'uploadvideo':
  43. /* 上传文件 */
  44. case 'uploadfile':
  45. /* 列出图片 */
  46. case 'listimage':
  47. /* 列出文件 */
  48. case 'listfile':
  49. /* 抓取远程文件 */
  50. case 'catchimage':
  51. default:
  52. $result = json_encode(array(
  53. 'state'=> '请求地址出错'
  54. ));
  55. break;
  56. }
  57. /* 输出结果 */
  58. if (isset($_GET["callback"])) {
  59. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  60. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  61. } else {
  62. return json_encode(array(
  63. 'state'=> 'callback参数不合法'
  64. ));
  65. }
  66. } else {
  67. return $result;
  68. }
  69. }
  70. }