1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Service\Util\Support\Upload;
- use Illuminate\Http\UploadedFile;
- class Uploader
- {
- protected string $driver = 'local';
- /**
- * path
- *
- * @var string
- */
- protected string $path = '';
- /**
- * upload
- *
- * @param UploadedFile $file
- * @return array
- */
- public function upload(UploadedFile $file): array
- {
- try {
- return $this->getDriver()->setUploadedFile($file)->upload();
- } catch (\Exception $exception) {
- throw new \RuntimeException($exception->getMessage());
- }
- }
- /**
- * get driver
- *
- */
- public function getDriver()
- {
- $drivers = $this->getDrivers();
- $driver = $drivers[$this->driver] ?? null;
- if (! $driver) {
- throw new \RuntimeException('Upload Driver Not Found');
- }
- return app($driver);
- }
- /**
- * set driver
- *
- * @param string $driver
- * @return $this
- */
- public function setDriver(string $driver): static
- {
- $this->driver = $driver;
- return $this;
- }
- /**
- * get drivers
- *
- * @return string[]
- */
- public function getDrivers(): array
- {
- return [
- 'local' => LocalUpload::class,
- 'HuaweiOBS' => HuaweiOBSUpload::class,
- ];
- }
- }
|