Skip to content

Commit 50f6e59

Browse files
author
Steeven Andrian
committed
update Ftp and Downloader handler
1 parent 58f6515 commit 50f6e59

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"require": {
3333
"php": "^7.2.0",
34+
"ext-fileinfo": "*",
3435
"o2system/kernel": "*"
3536
},
3637
"autoload": {

src/Handlers/Downloader.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)
173173

174174
$this->fileinfo = pathinfo($filePath);
175175
$this->filesize = filesize($filePath);
176-
$this->filemime = $this->getMime($filePath);
176+
$this->filemime = mime_content_type($filePath);
177177
$this->lastModified = filemtime($filePath);
178178

179179
} elseif ($mode === self::MODE_DATASTREAM) {
@@ -191,7 +191,7 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)
191191
}
192192

193193
$this->filesize = strlen($this->filedata);
194-
$this->filemime = $this->getMime($this->fileinfo[ 'filename' ]);
194+
$this->filemime = mime_content_type($this->fileinfo[ 'filename' ]);
195195
$this->lastModified = time();
196196

197197
} else {
@@ -225,27 +225,6 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)
225225

226226
// ------------------------------------------------------------------------
227227

228-
/**
229-
* Downloader::getMime
230-
*
231-
* @param string $filePath
232-
*
233-
* @return string
234-
*/
235-
private function getMime($filePath)
236-
{
237-
$file = new File($filePath);
238-
$mime = $file->getMime();
239-
240-
if (is_array($mime)) {
241-
$mime = reset($mime);
242-
}
243-
244-
return $mime;
245-
}
246-
247-
// ------------------------------------------------------------------------
248-
249228
/**
250229
* Downloader::forceDownload
251230
*

src/Handlers/Ftp.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// ------------------------------------------------------------------------
1717

1818
use O2System\Spl\Exceptions\RuntimeException;
19+
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
20+
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;
1921

2022
/**
2123
* Class Ftp
@@ -24,6 +26,9 @@
2426
*/
2527
class Ftp
2628
{
29+
use ConfigCollectorTrait;
30+
use ErrorCollectorTrait;
31+
2732
/**
2833
* Passive mode flag
2934
*
@@ -62,8 +67,16 @@ class Ftp
6267
/**
6368
* Ftp::__construct
6469
*/
65-
public function __construct()
70+
public function __construct(array $config = [])
6671
{
72+
$this->setConfig($config);
73+
74+
// Prep the port
75+
$this->config[ 'port' ] = empty($this->config[ 'port' ]) ? 21 : (int)$this->config[ 'port' ];
76+
77+
// Prep the hostname
78+
$this->config[ 'hostname' ] = preg_replace('|.+?://|', '', $this->config[ 'hostname' ]);
79+
6780
language()
6881
->addFilePath(str_replace('Handlers', '', __DIR__) . DIRECTORY_SEPARATOR)
6982
->loadFile('ftp');
@@ -76,32 +89,28 @@ public function __construct()
7689
*
7790
* Connect to FTP server.
7891
*
79-
* @param array $config Ftp configuration.
80-
*
8192
* @return bool Returns TRUE on success or FALSE on failure.
8293
* @throws RuntimeException
8394
*/
84-
public function connect(array $config = [])
95+
public function connect()
8596
{
86-
// Prep the port
87-
$config[ 'port' ] = empty($config[ 'port' ]) ? 21 : (int)$config[ 'port' ];
88-
89-
// Prep the hostname
90-
$config[ 'hostname' ] = preg_replace('|.+?://|', '', $config[ 'hostname' ]);
91-
92-
if (false === ($this->handle = @ftp_connect($config[ 'hostname' ], $config[ 'port' ]))) {
97+
if (false === ($this->handle = @ftp_connect($this->config[ 'hostname' ], $this->config[ 'port' ]))) {
9398
if ($this->debugMode === true) {
9499
throw new RuntimeException('FTP_E_UNABLE_TO_CONNECT');
95100
}
96101

102+
$this->addError(1, 'FTP_E_UNABLE_TO_CONNECT');
103+
97104
return false;
98105
}
99106

100-
if (false !== (@ftp_login($this->handle, $config[ 'username' ], $config[ 'password' ]))) {
107+
if (false !== (@ftp_login($this->handle, $this->config[ 'username' ], $this->config[ 'password' ]))) {
101108
if ($this->debugMode === true) {
102109
throw new RuntimeException('FTP_E_UNABLE_TO_LOGIN');
103110
}
104111

112+
$this->addError(2, 'FTP_E_UNABLE_TO_LOGIN');
113+
105114
return false;
106115
}
107116

@@ -110,8 +119,6 @@ public function connect(array $config = [])
110119
ftp_pasv($this->handle, true);
111120
}
112121

113-
$this->config = $config;
114-
115122
return true;
116123
}
117124

@@ -151,6 +158,8 @@ public function download($remoteFilePath, $localFilePath, $mode = 'auto')
151158
throw new RuntimeException('FTP_E_UNABLE_TO_DOWNLOAD');
152159
}
153160

161+
$this->addError(3, 'FTP_E_UNABLE_TO_DOWNLOAD');
162+
154163
return false;
155164
}
156165

@@ -174,6 +183,8 @@ protected function isConnected()
174183
throw new RuntimeException('FTP_E_NO_CONNECTION');
175184
}
176185

186+
$this->addError(4, 'FTP_E_NO_CONNECTION');
187+
177188
return false;
178189
}
179190

@@ -246,6 +257,8 @@ public function rename($oldFilename, $newFilename)
246257
throw new RuntimeException('FTP_UNABLE_TO_RENAME');
247258
}
248259

260+
$this->addError(5, 'FTP_UNABLE_TO_RENAME');
261+
249262
return false;
250263
}
251264

@@ -278,6 +291,8 @@ public function move($oldRemoteFilePath, $newRemoteFilePath)
278291
throw new RuntimeException('FTP_UNABLE_TO_MOVE');
279292
}
280293

294+
$this->addError(6, 'FTP_UNABLE_TO_MOVE');
295+
281296
return false;
282297
}
283298

@@ -309,6 +324,8 @@ public function deleteFile($filePath)
309324
throw new RuntimeException('FTP_E_UNABLE_TO_DELETE');
310325
}
311326

327+
$this->addError(7, 'FTP_E_UNABLE_TO_DELETE');
328+
312329
return false;
313330
}
314331

@@ -353,6 +370,8 @@ public function deleteDir($remotePath)
353370
throw new RuntimeException('FTP_E_UNABLE_TO_DELETE_DIRECTORY');
354371
}
355372

373+
$this->addError(8, 'FTP_E_UNABLE_TO_DELETE_DIRECTORY');
374+
356375
return false;
357376
}
358377

@@ -461,6 +480,8 @@ public function changeDir($remotePath, $suppressDebug = false)
461480
throw new RuntimeException('FTP_E_UNABLE_TO_CHANGE_DIRECTORY');
462481
}
463482

483+
$this->addError(9, 'FTP_E_UNABLE_TO_CHANGE_DIRECTORY');
484+
464485
return false;
465486
}
466487

@@ -493,6 +514,8 @@ public function makeDir($remotePath, $permissions = null)
493514
throw new RuntimeException('FTP_E_UNABLE_TO_MAKE_DIRECTORY');
494515
}
495516

517+
$this->addError(10, 'FTP_E_UNABLE_TO_MAKE_DIRECTORY');
518+
496519
return false;
497520
}
498521

@@ -528,6 +551,8 @@ public function setChmod($remotePath, $mode)
528551
throw new RuntimeException('FTP_E_UNABLE_TO_CHMOD');
529552
}
530553

554+
$this->addError(11, 'FTP_E_UNABLE_TO_CHMOD');
555+
531556
return false;
532557
}
533558

@@ -572,6 +597,8 @@ public function upload($localFilePath, $remoteFilePath, $mode = 'auto', $permiss
572597
throw new RuntimeException('FTP_E_UNABLE_TO_UPLOAD');
573598
}
574599

600+
$this->addError(12, 'FTP_E_UNABLE_TO_UPLOAD');
601+
575602
return false;
576603
}
577604

0 commit comments

Comments
 (0)