MinioController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare (strict_types = 1);
  3. namespace App\Controller;
  4. use App\JsonRpc\PublicRpcServiceInterface;
  5. use App\Tools\Result;
  6. use Hyperf\Di\Annotation\Inject;
  7. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  8. class MinioController extends AbstractController
  9. {
  10. #[Inject]
  11. protected ValidatorFactoryInterface $validationFactory;
  12. /**
  13. * @var PublicRpcServiceInterface
  14. */
  15. #[Inject]
  16. private $publicServiceClient;
  17. /**
  18. * 获取所有的buckets
  19. * @return array
  20. */
  21. public function listBuckets()
  22. {
  23. $result = $this->publicServiceClient->getBuckets([]);
  24. // 直接返回服务层生成的响应
  25. return Result::success($result['data']);
  26. }
  27. /**
  28. * 上传文件
  29. * @return array
  30. */
  31. public function uploadFiles()
  32. {
  33. $uploadedFile = $this->request->file('file');
  34. if ($uploadedFile && $uploadedFile->isValid()) {
  35. $filename = $uploadedFile->getClientFilename();
  36. $fileContent = base64_encode(file_get_contents($uploadedFile->getRealPath()));
  37. $mimeType = $uploadedFile->getMimeType();
  38. $data['fileContent'] = $fileContent;
  39. $data['fileName'] = $filename;
  40. $data['contentType'] = $mimeType;
  41. $data['ext'] = $uploadedFile->getExtension();
  42. $data['size'] = $uploadedFile->getSize();
  43. $result = $this->publicServiceClient->uploadFile($data);
  44. if($result['code']==200){
  45. return Result::success($result['data']);
  46. }else{
  47. return Result::error("上传失败了!");
  48. }
  49. }else{
  50. return Result::error("请选择需要上传的文件");
  51. }
  52. }
  53. }