| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\FileSystem; |
| 4 |
|
| 5 |
use FluentCart\Api\StorageDrivers; |
| 6 |
use FluentCart\App\Services\FileSystem\Drivers\BaseDriver; |
| 7 |
use FluentCart\App\Services\FileSystem\Drivers\Local\LocalDriver; |
| 8 |
use FluentCart\App\Services\FileSystem\Drivers\S3\S3Driver; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
|
| 11 |
class FileManager |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var BaseDriver|S3Driver|LocalDriver $driver |
| 15 |
**/ |
| 16 |
private $driver; |
| 17 |
protected ?string $dirPath; |
| 18 |
protected ?string $dirName; |
| 19 |
|
| 20 |
|
| 21 |
public function getDriver() |
| 22 |
{ |
| 23 |
return $this->driver; |
| 24 |
} |
| 25 |
|
| 26 |
public function __construct( |
| 27 |
string $driver = 'local', |
| 28 |
?string $dirPath = null, |
| 29 |
?string $dirName = null, |
| 30 |
$inActiveMode = false |
| 31 |
) |
| 32 |
{ |
| 33 |
$this->dirName = $dirName; |
| 34 |
$this->dirPath = $dirPath; |
| 35 |
$this->driver = $this->resolveDriver($driver, $inActiveMode); |
| 36 |
} |
| 37 |
|
| 38 |
public function listFiles(array $params = []) |
| 39 |
{ |
| 40 |
return $this->driver->listFiles($params); |
| 41 |
} |
| 42 |
|
| 43 |
public function bucketLists() |
| 44 |
{ |
| 45 |
return $this->driver->buckets(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public function uploadFile(string $localFilePath, string $uploadToFilePath, $file, array $params = []) |
| 50 |
{ |
| 51 |
return $this->driver->uploadFile($localFilePath, $uploadToFilePath, $file, $params); |
| 52 |
} |
| 53 |
|
| 54 |
public function downloadFile(string $filePath, string $fileName, $bucket = null) |
| 55 |
{ |
| 56 |
return $this->driver->downloadFile($filePath, $fileName, $bucket); |
| 57 |
} |
| 58 |
|
| 59 |
public function getSignedDownloadUrl(string $filePath, $bucket = null, $productDownload = null): ?string |
| 60 |
{ |
| 61 |
return $this->driver->getSignedDownloadUrl($filePath, $bucket, $productDownload); |
| 62 |
} |
| 63 |
|
| 64 |
public function getFilePath(string $filePath, $bucket = null) |
| 65 |
{ |
| 66 |
return $this->driver->getFilePath($filePath, null, $bucket); |
| 67 |
} |
| 68 |
|
| 69 |
public function deleteFile(string $filePath, $bucket = null) |
| 70 |
{ |
| 71 |
return $this->driver->deleteFile($filePath, $bucket); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
private function resolveDriver(string $driver, $inActiveMode) |
| 76 |
{ |
| 77 |
|
| 78 |
$storageDrivers = new StorageDrivers(); |
| 79 |
|
| 80 |
if($inActiveMode){ |
| 81 |
$storages = $storageDrivers->getAll(); |
| 82 |
}else{ |
| 83 |
$storages = $storageDrivers->getActive(true); |
| 84 |
} |
| 85 |
|
| 86 |
$storage = Arr::get($storages, $driver.'.instance'); |
| 87 |
|
| 88 |
if(empty($storage)){ |
| 89 |
throw new \Exception(esc_html__('Invalid driver', 'fluent-cart')); |
| 90 |
} |
| 91 |
|
| 92 |
$driverClass = $storage->getDriverClass(); |
| 93 |
|
| 94 |
return new $driverClass($this->dirPath, $this->dirName); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public function saveSettings($params) |
| 99 |
{ |
| 100 |
return $this->driver->saveSettings($params); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|