|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Reactor package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Http\Controllers; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Filesystem\Handlers\Downloader; |
|
19
|
|
|
use O2System\Reactor\Http\Controller; |
|
20
|
|
|
use O2System\Spl\Info\SplFileInfo; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Storage |
|
24
|
|
|
* |
|
25
|
|
|
* @package O2System\Reactor\Http\Controllers |
|
26
|
|
|
*/ |
|
27
|
|
|
class Storage extends Controller |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Storage::$inherited |
|
31
|
|
|
* |
|
32
|
|
|
* Controller inherited flag. |
|
33
|
|
|
* |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
static public $inherited = true; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Storage::$directoryPath |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $directoryPath; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Storage::$speedLimit |
|
47
|
|
|
* |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
public $speedLimit = 1024; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Storage::$resumeable |
|
54
|
|
|
* |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public $resumeable = true; |
|
58
|
|
|
|
|
59
|
|
|
// ------------------------------------------------------------------------ |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Storage::__construct |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->directoryPath = PATH_STORAGE; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------ |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Storage::route |
|
73
|
|
|
*/ |
|
74
|
|
|
public function route() |
|
75
|
|
|
{ |
|
76
|
|
|
$segments = server_request()->getUri()->segments->getArrayCopy(); |
|
77
|
|
|
array_shift($segments); |
|
78
|
|
|
|
|
79
|
|
|
$download = false; |
|
80
|
|
|
|
|
81
|
|
|
if (false !== ($key = array_search('download', $segments))) { |
|
82
|
|
|
$download = true; |
|
83
|
|
|
unset($segments[ $key ]); |
|
84
|
|
|
$segments = array_values($segments); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (count($segments)) { |
|
88
|
|
|
$filePath = $this->directoryPath . implode(DIRECTORY_SEPARATOR, $segments); |
|
89
|
|
|
if (is_file($filePath)) { |
|
90
|
|
|
if ($download) { |
|
91
|
|
|
$downloader = new Downloader($filePath); |
|
92
|
|
|
$downloader |
|
93
|
|
|
->speedLimit($this->speedLimit) |
|
94
|
|
|
->resumeable($this->resumeable) |
|
95
|
|
|
->download(); |
|
96
|
|
|
} else { |
|
97
|
|
|
$fileInfo = new SplFileInfo($filePath); |
|
98
|
|
|
header('Content-Disposition: filename=' . $fileInfo->getFilename()); |
|
99
|
|
|
header('Content-Transfer-Encoding: binary'); |
|
100
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
|
101
|
|
|
header('Content-Type: ' . $fileInfo->getMime()); |
|
102
|
|
|
echo readfile($filePath); |
|
103
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
redirect_url('error/404'); |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
|
|
redirect_url('error/403'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.