Upload.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Constants\Util;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Validation\ValidationException;
  8. use Exception;
  9. class Upload extends Controller
  10. {
  11. /**
  12. * @api {post} /admin/upload/uploadFile 上传文件
  13. * @apiGroup Upload
  14. * @apiUse result
  15. * @apiUse header
  16. * @apiVersion 1.0.0
  17. * @apiParam {File} image 图像文件的路径(本地路径)
  18. * - 文件大小限制:最大 10MB。
  19. * - 支持图像格式:`jpg`, `jpeg`, `png`,`webp`。
  20. */
  21. public function store()
  22. {
  23. try {
  24. request()->validate([
  25. 'image' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:10240'],
  26. ]);
  27. if (request()->hasFile('image')) {
  28. $image = request()->file('image');
  29. $imageName = md5_file($image->getRealPath());
  30. if ($image->getClientOriginalExtension() == '') {
  31. $imageName .= '.webp';
  32. } else {
  33. $imageName .= '.' . $image->getClientOriginalExtension();
  34. }
  35. $filePath = storage_path('app/public/images/' . $imageName);
  36. if (!file_exists($filePath)) {
  37. $path = $image->storeAs('images', $imageName, 'public');
  38. $path = Storage::url($path);
  39. } else {
  40. $path = Storage::url('images/' . $imageName);
  41. }
  42. } else {
  43. throw new Exception('', HttpStatus::FILE_UPLOAD_ERROR);
  44. }
  45. } catch (ValidationException $e) {
  46. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  47. } catch (Exception $e) {
  48. return $this->error($e->getCode());
  49. }
  50. return $this->success(['path' => Util::ensureUrl($path)]);
  51. }
  52. /**
  53. * @api {post} /admin/upload/uploadVideo 上传视频
  54. * @apiGroup Upload
  55. * @apiUse result
  56. * @apiUse header
  57. * @apiVersion 1.0.0
  58. * @apiParam {File} file 附件的路径(本地路径)
  59. * - 文件大小限制:最大 50MB。
  60. * - 支持视频格式:`mp4`,`mov`,`avi`,`webm`
  61. */
  62. public function video()
  63. {
  64. try {
  65. request()->validate([
  66. 'file' => [
  67. 'required',
  68. 'file',
  69. 'mimes:jpg,jpeg,png,webp,mp4,mov,avi,webm',
  70. 'max:51200' // 最大 50MB,单位KB
  71. ],
  72. ]);
  73. if (request()->hasFile('file')) {
  74. $file = request()->file('file');
  75. $fileName = md5_file($file->getRealPath());
  76. // 获取原始扩展名
  77. $extension = $file->getClientOriginalExtension();
  78. $fileName .= $extension ? ('.' . $extension) : '';
  79. // 根据类型判断存放目录(可选)
  80. $isImage = in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'webp']);
  81. $dir = $isImage ? 'images' : 'videos';
  82. $filePath = storage_path("app/public/{$dir}/" . $fileName);
  83. if (!file_exists($filePath)) {
  84. $path = $file->storeAs($dir, $fileName, 'public');
  85. $path = Storage::url($path);
  86. } else {
  87. $path = Storage::url("{$dir}/" . $fileName);
  88. }
  89. } else {
  90. throw new Exception('', HttpStatus::FILE_UPLOAD_ERROR);
  91. }
  92. } catch (ValidationException $e) {
  93. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  94. } catch (Exception $e) {
  95. return $this->error($e->getCode());
  96. }
  97. return $this->success(['path' => Util::ensureUrl($path)]);
  98. }
  99. }