Uploader.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Service\Util\Support\Upload;
  3. use Illuminate\Http\UploadedFile;
  4. class Uploader
  5. {
  6. protected string $driver = 'local';
  7. /**
  8. * path
  9. *
  10. * @var string
  11. */
  12. protected string $path = '';
  13. /**
  14. * upload
  15. *
  16. * @param UploadedFile $file
  17. * @return array
  18. */
  19. public function upload(UploadedFile $file): array
  20. {
  21. try {
  22. return $this->getDriver()->setUploadedFile($file)->upload();
  23. } catch (\Exception $exception) {
  24. throw new \RuntimeException($exception->getMessage());
  25. }
  26. }
  27. /**
  28. * get driver
  29. *
  30. */
  31. public function getDriver()
  32. {
  33. $drivers = $this->getDrivers();
  34. $driver = $drivers[$this->driver] ?? null;
  35. if (! $driver) {
  36. throw new \RuntimeException('Upload Driver Not Found');
  37. }
  38. return app($driver);
  39. }
  40. /**
  41. * set driver
  42. *
  43. * @param string $driver
  44. * @return $this
  45. */
  46. public function setDriver(string $driver): static
  47. {
  48. $this->driver = $driver;
  49. return $this;
  50. }
  51. /**
  52. * get drivers
  53. *
  54. * @return string[]
  55. */
  56. public function getDrivers(): array
  57. {
  58. return [
  59. 'local' => LocalUpload::class,
  60. 'HuaweiOBS' => HuaweiOBSUpload::class,
  61. ];
  62. }
  63. }