UploadController.php 2.4 KB

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