| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\FileSystem; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\ProductDownloadResource; |
| 6 |
use FluentCart\App\App; |
| 7 |
use FluentCart\App\Helpers\CustomerHelper; |
| 8 |
use FluentCart\App\Http\Controllers\FrontendControllers\CustomerProfileController; |
| 9 |
use FluentCart\App\Models\ProductDownload; |
| 10 |
use FluentCart\App\Services\URL; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\Framework\Http\URL as BaseUrl; |
| 13 |
|
| 14 |
class DownloadService |
| 15 |
{ |
| 16 |
public function register() |
| 17 |
{ |
| 18 |
add_action('fluent_cart/before_download_check_permission_and_store_log', function ($params) { |
| 19 |
$this->checkPermissionBeforeDownloadPermissionAndStoreLog($params); |
| 20 |
}, 10, 2); |
| 21 |
|
| 22 |
add_action('fluent_cart/download_file', function ($params) { |
| 23 |
$this->downloadFile($params); |
| 24 |
}); |
| 25 |
|
| 26 |
//todo: use when web router removes |
| 27 |
//commented out as downloadFile method dosent exsits in CustomerProfileController class |
| 28 |
// add_action('init', function () { |
| 29 |
// if (!isset($_REQUEST['fluent_cart_download'])) { |
| 30 |
// return; |
| 31 |
// } |
| 32 |
// $request = App::getInstance('request'); |
| 33 |
// $request->params = $request->all(); |
| 34 |
// $this->registerDownloadRoutes($request); |
| 35 |
// }); |
| 36 |
} |
| 37 |
|
| 38 |
public function registerDownloadRoutes($request) |
| 39 |
{ |
| 40 |
(new CustomerProfileController())->downloadFile($request); |
| 41 |
} |
| 42 |
|
| 43 |
private function checkPermissionBeforeDownloadPermissionAndStoreLog($params) |
| 44 |
{ |
| 45 |
$customerHelper = new CustomerHelper(); |
| 46 |
$customerHelper->checkDownloadPermissionAndStoreLog($params); |
| 47 |
} |
| 48 |
|
| 49 |
private function downloadFile($params) |
| 50 |
{ |
| 51 |
$driver = Arr::get($params, 'driver'); |
| 52 |
$file = Arr::get($params, 'file'); |
| 53 |
$bucket = StorageBucketResolver::resolveOrNull($driver); |
| 54 |
(new FileManager($driver))->downloadFile($file,$file, $bucket); |
| 55 |
} |
| 56 |
|
| 57 |
public static function downloadFileFromId($downloadId) |
| 58 |
{ |
| 59 |
$productDownload = ProductDownload::query()->where('download_identifier', $downloadId)->first(); |
| 60 |
if (empty($productDownload)) { |
| 61 |
return __('File not found', 'fluent-cart'); |
| 62 |
} |
| 63 |
$driver = Arr::get($productDownload, 'driver'); |
| 64 |
$file = Arr::get($productDownload, 'file_path'); |
| 65 |
$bucket = StorageBucketResolver::resolveOrNull($driver); |
| 66 |
$downloadName = Arr::get($productDownload, 'file_name'); |
| 67 |
|
| 68 |
try{ |
| 69 |
$fileManager = new FileManager($driver); |
| 70 |
}catch (\Exception $e){ |
| 71 |
return new \WP_Error('fluent_cart_file_manager_error', $e->getMessage()); |
| 72 |
} |
| 73 |
return $fileManager->downloadFile($file,$downloadName, $bucket); |
| 74 |
} |
| 75 |
|
| 76 |
public static function getDownloadableUrlFromDownload($productDownload): string |
| 77 |
{ |
| 78 |
$driver = Arr::get($productDownload, 'driver'); |
| 79 |
$file = Arr::get($productDownload, 'file_path'); |
| 80 |
$fileName = Arr::get($productDownload, 'file_name'); |
| 81 |
$bucket = StorageBucketResolver::resolveOrNull($driver); |
| 82 |
return (new FileManager($driver))->getSignedDownloadUrl($file, $bucket, $productDownload); |
| 83 |
} |
| 84 |
|
| 85 |
public function getDownloadableUrl($params): string |
| 86 |
{ |
| 87 |
$productDownload = ProductDownloadResource::find(Arr::get($params, 'download_id')); |
| 88 |
$identifier = Arr::get($productDownload, 'download_identifier', ''); |
| 89 |
|
| 90 |
return (new BaseUrl())->sign( |
| 91 |
URL::getFrontEndUrl('download-by-id/'), |
| 92 |
[ |
| 93 |
'download_identifier' => $identifier, |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
public function getDownloadablePath($params) |
| 100 |
{ |
| 101 |
$driver = Arr::get($params, 'driver'); |
| 102 |
$file = Arr::get($params, 'file'); |
| 103 |
$bucket = StorageBucketResolver::resolveOrNull($driver); |
| 104 |
return (new FileManager($driver))->getFilePath($file, $bucket); |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|