From d1835f2c7e6a7a29bed4e41fdc17fa3a9e50384e Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Fri, 13 Dec 2024 23:46:11 +0000 Subject: [PATCH] refactor: change the isDirectory method --- .github/workflows/tests.yml | 1 + src/Storage/Service/FTPService.php | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5c08f719..b334cfae 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,6 +7,7 @@ env: FTP_USER: username FTP_PASSWORD: password FTP_PORT: 21 + FTP_ROOT: /home/vsftpd/username jobs: lunix-tests: diff --git a/src/Storage/Service/FTPService.php b/src/Storage/Service/FTPService.php index c0f79790..7fc5026b 100644 --- a/src/Storage/Service/FTPService.php +++ b/src/Storage/Service/FTPService.php @@ -177,6 +177,8 @@ public function changePath(?string $path = null) if ($base_path && (!@ftp_chdir($this->connection, $base_path))) { throw new RuntimeException('Root is invalid or does not exist: ' . $base_path); } + + ftp_pwd($this->connection); } /** @@ -483,13 +485,18 @@ public function isFile(string $filename): bool */ public function isDirectory(string $dirname): bool { - $listing = $this->listDirectoryContents(); + $original_directory = ftp_pwd($this->connection); - $dirname_info = array_filter($listing, function ($item) use ($dirname) { - return $item['type'] === 'directory' && $item['name'] === $dirname; - }); + // Test if you can change directory to $dirname + // suppress errors in case $dir is not a file or not a directory + if (!@ftp_chdir($this->connection, $dirname)) { + return false; + } - return count($dirname_info) !== 0; + // If it is a directory, then change the directory back to the original directory + ftp_chdir($this->connection, $original_directory); + + return true; } /**