-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathAbstractHandler.php
181 lines (156 loc) · 4.7 KB
/
AbstractHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Pion\Laravel\ChunkUpload\Handler;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
use Pion\Laravel\ChunkUpload\Save\AbstractSave;
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
use Session;
/**
* The handler that will detect if we can continue the chunked upload.
*/
abstract class AbstractHandler
{
/**
* @var Request
*/
protected $request;
/**
* @var UploadedFile
*/
protected $file;
/**
* @var AbstractConfig
*/
protected $config;
/**
* AbstractReceiver constructor.
*
* @param Request $request
* @param UploadedFile $file
* @param AbstractConfig $config
*/
public function __construct(Request $request, $file, $config)
{
$this->request = $request;
$this->file = $file;
$this->config = $config;
}
/**
* Checks if the current abstract handler can be used via HandlerFactory.
*
* @param Request $request
*
* @return bool
*/
public static function canBeUsedForRequest(Request $request)
{
return false;
}
/**
* Checks the current setup if session driver was booted - if not, it will generate random hash.
*
* @return bool
*/
public static function canUseSession()
{
// Get the session driver and check if it was started - fully inited by laravel
$session = session();
$driver = $session->getDefaultDriver();
$drivers = $session->getDrivers();
// Check if the driver is valid and started - allow using session
if (isset($drivers[$driver]) && true === $drivers[$driver]->isStarted()) {
return true;
}
return false;
}
/**
* Builds the chunk file name per session and the original name. You can
* provide custom additional name at the end of the generated file name. All chunk
* files has .part extension.
*
* @param string|null $additionalName Make the name more unique (example: use id from request)
* @param string|null $currentChunkIndex Add the chunk index for parallel upload
*
* @return string
*
* @see UploadedFile::getClientOriginalName()
* @see Session::getId()
*/
public function createChunkFileName($additionalName = null, $currentChunkIndex = null)
{
// prepare basic name structure
$array = [
$this->file->getClientOriginalName(),
];
// ensure that the chunk name is for unique for the client session
$useSession = $this->config->chunkUseSessionForName();
$useBrowser = $this->config->chunkUseBrowserInfoForName();
if ($useSession && false === static::canUseSession()) {
$useBrowser = true;
$useSession = false;
}
// the session needs more config on the provider
if ($useSession) {
$array[] = Session::getId();
}
// can work without any additional setup
if ($useBrowser) {
$array[] = md5($this->request->ip().$this->request->header('User-Agent', 'no-browser'));
}
// Add additional name for more unique chunk name
if (!is_null($additionalName)) {
$array[] = $additionalName;
}
// Build the final name - parts separated by dot
$namesSeparatedByDot = [
implode('-', $array),
];
// Add the chunk index for parallel upload
if (null !== $currentChunkIndex) {
$namesSeparatedByDot[] = $currentChunkIndex;
}
// Add extension
$namesSeparatedByDot[] = ChunkStorage::CHUNK_EXTENSION;
// build name
return implode('.', $namesSeparatedByDot);
}
/**
* Creates save instance and starts saving the uploaded file.
*
* @param ChunkStorage $chunkStorage the chunk storage
*
* @return AbstractSave
*/
abstract public function startSaving($chunkStorage);
/**
* Returns the chunk file name for a storing the tmp file.
*
* @return string
*/
abstract public function getChunkFileName();
/**
* Checks if the request has first chunk.
*
* @return bool
*/
abstract public function isFirstChunk();
/**
* Checks if the current request has the last chunk.
*
* @return bool
*/
abstract public function isLastChunk();
/**
* Checks if the current request is chunked upload.
*
* @return bool
*/
abstract public function isChunkedUpload();
/**
* Returns the percentage of the upload file.
*
* @return int
*/
abstract public function getPercentageDone();
}