Uploader.php 1.4 KB

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