From 8ab8732ab1fb0534073d8fd663e1ad72dc0f24b5 Mon Sep 17 00:00:00 2001 From: vasilportey Date: Fri, 24 Jul 2015 15:39:17 +0300 Subject: [PATCH 1/4] fix bug in detecting file name (when there are two spaces in filename) --- lib/Touki/FTP/FilesystemFactory.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Touki/FTP/FilesystemFactory.php b/lib/Touki/FTP/FilesystemFactory.php index 56f4056..cef992a 100644 --- a/lib/Touki/FTP/FilesystemFactory.php +++ b/lib/Touki/FTP/FilesystemFactory.php @@ -52,7 +52,10 @@ public function build($input, $prefix = '') $filesystem = $this->resolveFile($type); $permParts = str_split(substr($parts[0], 1, 9), 3); $hours = sscanf($parts[7], "%d:%d"); - $name = implode(' ', array_slice($parts, 8)); + + $nameStart = strpos($input, $parts[7]); + $nameStart += strlen($parts[7]) + 1; + $name = trim(substr($input, $nameStart)); if (null === $hours[1]) { $year = $hours[0]; From 2abcfa65262fb0535a266eff90d6295e87b82179 Mon Sep 17 00:00:00 2001 From: Portey Vasil Date: Tue, 29 Sep 2015 14:47:05 +0300 Subject: [PATCH 2/4] Fixes rawlist --- .../FTP/Manager/FTPFilesystemManager.php | 69 ++++++++++++++----- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/lib/Touki/FTP/Manager/FTPFilesystemManager.php b/lib/Touki/FTP/Manager/FTPFilesystemManager.php index b38080d..d12c964 100644 --- a/lib/Touki/FTP/Manager/FTPFilesystemManager.php +++ b/lib/Touki/FTP/Manager/FTPFilesystemManager.php @@ -71,6 +71,7 @@ public function findBy($directory, $callable) } $directory = '/'.ltrim($directory, '/'); + $raw = $this->wrapper->rawlist($directory); $list = array(); @@ -78,15 +79,29 @@ public function findBy($directory, $callable) throw new DirectoryException(sprintf("Directory %s not found", $directory)); } - foreach ($raw as $item) { - $fs = $this->factory->build($item, $directory); - - if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { - continue; - } - - if (true === call_user_func_array($callable, array($fs))) { - $list[] = $fs; + if(is_array($raw)){ + if(count($raw)){ + foreach ($raw as $item) { + $fs = $this->factory->build($item, $directory); + + if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { + continue; + } + + if (true === call_user_func_array($callable, array($fs))) { + $list[] = $fs; + } + } + }else{ + $raw = $this->wrapper->nlist($directory); + + if ($raw && is_array($raw) && count($raw)) { + foreach ($raw as $item) { + if (substr($item, -4) == '.mp3') { + $list[] = new \Touki\FTP\Model\File($directory . DIRECTORY_SEPARATOR . $item); + } + } + } } } @@ -158,15 +173,33 @@ public function findOneBy($directory, $callable) throw new DirectoryException(sprintf("Directory %s not found", $directory)); } - foreach ($raw as $item) { - $fs = $this->factory->build($item, $directory); - - if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { - continue; - } - - if (true === call_user_func_array($callable, array($fs))) { - return $fs; + if(is_array($raw)){ + if(count($raw)){ + foreach ($raw as $item) { + $fs = $this->factory->build($item, $directory); + + if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { + continue; + } + + if (true === call_user_func_array($callable, array($fs))) { + return $fs; + } + } + }else{ + $raw = $this->wrapper->nlist($directory); + + if ($raw && is_array($raw) && count($raw)) { + foreach ($raw as $item) { + if (substr($item, -4) == '.mp3') { + $fs = new \Touki\FTP\Model\File($directory . DIRECTORY_SEPARATOR . $item); + + if (true === call_user_func_array($callable, array($fs))) { + return $fs; + } + } + } + } } } From 27f98260bb6647ff6d5eda5f049f3a7afffa5dfe Mon Sep 17 00:00:00 2001 From: Portey Vasil Date: Thu, 8 Oct 2015 13:44:37 +0300 Subject: [PATCH 3/4] Update FTPFilesystemManager.php --- .../FTP/Manager/FTPFilesystemManager.php | 78 +++++++------------ 1 file changed, 28 insertions(+), 50 deletions(-) diff --git a/lib/Touki/FTP/Manager/FTPFilesystemManager.php b/lib/Touki/FTP/Manager/FTPFilesystemManager.php index d12c964..bc48201 100644 --- a/lib/Touki/FTP/Manager/FTPFilesystemManager.php +++ b/lib/Touki/FTP/Manager/FTPFilesystemManager.php @@ -79,29 +79,19 @@ public function findBy($directory, $callable) throw new DirectoryException(sprintf("Directory %s not found", $directory)); } - if(is_array($raw)){ - if(count($raw)){ - foreach ($raw as $item) { - $fs = $this->factory->build($item, $directory); - - if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { - continue; - } - - if (true === call_user_func_array($callable, array($fs))) { - $list[] = $fs; - } - } - }else{ - $raw = $this->wrapper->nlist($directory); - - if ($raw && is_array($raw) && count($raw)) { - foreach ($raw as $item) { - if (substr($item, -4) == '.mp3') { - $list[] = new \Touki\FTP\Model\File($directory . DIRECTORY_SEPARATOR . $item); - } - } - } + if(is_array($raw) && empty($raw)){ + $raw = $this->wrapper->rawlist(str_replace(' ', '\\ ', $directory)); + } + + foreach ($raw as $item) { + $fs = $this->factory->build($item, $directory); + + if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { + continue; + } + + if (true === call_user_func_array($callable, array($fs))) { + $list[] = $fs; } } @@ -173,36 +163,24 @@ public function findOneBy($directory, $callable) throw new DirectoryException(sprintf("Directory %s not found", $directory)); } - if(is_array($raw)){ - if(count($raw)){ - foreach ($raw as $item) { - $fs = $this->factory->build($item, $directory); - - if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { - continue; - } - - if (true === call_user_func_array($callable, array($fs))) { - return $fs; - } - } - }else{ - $raw = $this->wrapper->nlist($directory); - - if ($raw && is_array($raw) && count($raw)) { - foreach ($raw as $item) { - if (substr($item, -4) == '.mp3') { - $fs = new \Touki\FTP\Model\File($directory . DIRECTORY_SEPARATOR . $item); - - if (true === call_user_func_array($callable, array($fs))) { - return $fs; - } - } - } - } + if(is_array($raw) && empty($raw)){ + $raw = $this->wrapper->rawlist(str_replace(' ', '\\ ', $directory)); + } + + + foreach ($raw as $item) { + $fs = $this->factory->build($item, $directory); + + if ('/.' === substr($fs->getRealpath(), -2) || '/..' === substr($fs->getRealpath(), -3)) { + continue; + } + + if (true === call_user_func_array($callable, array($fs))) { + return $fs; } } + return null; } From a9c1d11ee1ead9277fa2b1d183584be3cc8b8251 Mon Sep 17 00:00:00 2001 From: Portey Vasil Date: Thu, 8 Oct 2015 13:45:23 +0300 Subject: [PATCH 4/4] Update FTPWrapper.php --- lib/Touki/FTP/FTPWrapper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Touki/FTP/FTPWrapper.php b/lib/Touki/FTP/FTPWrapper.php index cd5317f..4db2d20 100644 --- a/lib/Touki/FTP/FTPWrapper.php +++ b/lib/Touki/FTP/FTPWrapper.php @@ -79,7 +79,11 @@ public function setConnection(ConnectionInterface $connection) */ public function get($localFile, $remoteFile, $mode = self::BINARY, $resumepos = 0) { - return ftp_get($this->connection->getStream(), $localFile, $remoteFile, $mode, $resumepos); + try{ + return ftp_get($this->connection->getStream(), $localFile, $remoteFile, $mode, $resumepos); + }catch (\Exception $e){ + return ftp_get($this->connection->getStream(), $localFile, str_replace(' ', '\\ ', $remoteFile), $mode, $resumepos); + } } /**