@@ -9,6 +9,7 @@ class FileFetcher extends Job
9
9
{
10
10
private $ temporaryDirectory ;
11
11
private $ chunkSizeInBytes = (1024 * 100 );
12
+ private $ compatibleServer = true ;
12
13
13
14
public function __construct ($ filePath , $ temporaryDirectory = "/tmp " )
14
15
{
@@ -27,22 +28,36 @@ public function __construct($filePath, $temporaryDirectory = "/tmp")
27
28
$ state ['destination ' ] = $ file ->isFile () ? $ filePath : $ this ->getTemporaryFilePath ($ filePath );
28
29
29
30
if (!$ file ->isFile () && $ this ->serverIsNotCompatible ($ filePath )) {
30
- throw new \Exception ("The server hosting the file does not support ranged requests. " );
31
+ $ this ->compatibleServer = false ;
32
+ $ state ['total_bytes ' ] = PHP_INT_MAX ;
33
+ $ this ->deleteFile ($ state ['destination ' ]);
34
+ } else {
35
+ $ state ['total_bytes ' ] = $ file ->isFile () ? $ file ->getSize () : $ this ->getRemoteFileSize ($ filePath );
31
36
}
32
37
33
- $ state ['total_bytes ' ] = $ file ->isFile () ? $ file ->getSize () : $ this ->getRemoteFileSize ($ filePath );
34
-
35
38
if (file_exists ($ state ['destination ' ])) {
36
39
$ state ['total_bytes_copied ' ] = filesize ($ state ['destination ' ]);
37
40
}
38
41
39
42
$ this ->setState ($ state );
40
43
}
41
44
45
+ public function setTimeLimit (int $ seconds ): bool
46
+ {
47
+ if (!$ this ->compatibleServer ) {
48
+ return false ;
49
+ }
50
+ return parent ::setTimeLimit ($ seconds );
51
+ }
52
+
42
53
protected function runIt ()
43
54
{
44
55
try {
45
- $ this ->copy ();
56
+ if ($ this ->compatibleServer ) {
57
+ $ this ->copy ();
58
+ } else {
59
+ $ this ->copyIncompatible ();
60
+ }
46
61
$ result = $ this ->getResult ();
47
62
$ result ->setStatus (Result::DONE );
48
63
} catch (FileCopyInterruptedException $ e ) {
@@ -149,6 +164,23 @@ private function copy()
149
164
}
150
165
}
151
166
167
+ private function copyIncompatible ()
168
+ {
169
+ $ from = $ this ->getStateProperty ('source ' );
170
+ $ to = $ this ->getStateProperty ('destination ' );
171
+
172
+ $ bufferSize = 1048576 ;
173
+ $ bytesCopied = 0 ;
174
+ $ fin = fopen ($ from , "rb " );
175
+ $ fout = fopen ($ to , "w " );
176
+ while (!feof ($ fin )) {
177
+ $ bytesCopied += fwrite ($ fout , fread ($ fin , $ bufferSize ));
178
+ }
179
+ fclose ($ fin );
180
+ fclose ($ fout );
181
+ $ this ->setStateProperty ('total_bytes_copied ' , $ bytesCopied );
182
+ }
183
+
152
184
private function getChunk ()
153
185
{
154
186
$ url = $ this ->getStateProperty ('source ' );
@@ -175,8 +207,8 @@ private function getChunk()
175
207
176
208
private function getTemporaryFilePath ($ sourceFileUrl )
177
209
{
178
- $ pieces = explode ( " / " , $ sourceFileUrl );
179
- $ file_name = end ( $ pieces );
210
+ $ info = parse_url ( $ sourceFileUrl );
211
+ $ file_name = str_replace ( " . " , " _ " , $ info [ " host " ]) . str_replace ( " / " , " _ " , $ info [ ' path ' ] );
180
212
return $ this ->getTemporaryFile ($ file_name );
181
213
}
182
214
@@ -218,6 +250,13 @@ private function setState($state)
218
250
$ this ->getResult ()->setData (json_encode ($ state ));
219
251
}
220
252
253
+ private function deleteFile ($ file )
254
+ {
255
+ if (file_exists ($ file )) {
256
+ unlink ($ file );
257
+ }
258
+ }
259
+
221
260
public function setStateProperty ($ property , $ value )
222
261
{
223
262
$ state = $ this ->getState ();
0 commit comments