PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.0
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / FileSystem / DownloadService.php

DownloadService.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.0, at app/Services/FileSystem/DownloadService.php

110 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $productDownload = ProductDownloadResource::find(Arr::get($params, 'download_id'));
54 $bucket = Arr::get($productDownload, 'settings.bucket', '');
55 (new FileManager($driver))->downloadFile($file,$file, $bucket);
56 }
57
58 public static function downloadFileFromId($downloadId)
59 {
60 $productDownload = ProductDownload::query()->where('download_identifier', $downloadId)->first();
61 if (empty($productDownload)) {
62 return __('File not found', 'fluent-cart');
63 }
64 $driver = Arr::get($productDownload, 'driver');
65 $file = Arr::get($productDownload, 'file_path');
66 $bucket = Arr::get($productDownload, 'settings.bucket', '');
67 $downloadName = Arr::get($productDownload, 'file_name');
68
69 try{
70 $fileManager = new FileManager($driver);
71 }catch (\Exception $e){
72 return new \WP_Error('fluent_cart_file_manager_error', $e->getMessage());
73 }
74 return $fileManager->downloadFile($file,$downloadName, $bucket);
75 }
76
77 public static function getDownloadableUrlFromDownload($productDownload): string
78 {
79 $driver = Arr::get($productDownload, 'driver');
80 $file = Arr::get($productDownload, 'file_path');
81 $fileName = Arr::get($productDownload, 'file_name');
82 $bucket = Arr::get($productDownload, 'settings.bucket', '');
83 return (new FileManager($driver))->getSignedDownloadUrl($file, $bucket, $productDownload);
84 }
85
86 public function getDownloadableUrl($params): string
87 {
88 $productDownload = ProductDownloadResource::find(Arr::get($params, 'download_id'));
89 $identifier = Arr::get($productDownload, 'download_identifier', '');
90
91 return (new BaseUrl())->sign(
92 URL::getFrontEndUrl('download-by-id/'),
93 [
94 'download_identifier' => $identifier,
95 ]
96 );
97 }
98
99
100 public function getDownloadablePath($params)
101 {
102 $driver = Arr::get($params, 'driver');
103 $file = Arr::get($params, 'file');
104 $productDownload = ProductDownloadResource::find(Arr::get($params, 'download_id'));
105 $bucket = Arr::get($productDownload, 'settings.bucket', '');
106 return (new FileManager($driver))->getFilePath($file, $bucket);
107 }
108
109 }
110