Skip to content

Commit

Permalink
refactor: change the isDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Dec 14, 2024
1 parent 9cb52e3 commit 4ac97d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
FTP_USER: username
FTP_PASSWORD: password
FTP_PORT: 21
FTP_ROOT: /home/vsftpd/username

jobs:
lunix-tests:
Expand Down Expand Up @@ -41,7 +42,7 @@ jobs:
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, mysql, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, redis
coverage: none

- run: docker run --net=host -p 21:21 -e USER=$FTP_USER -e PASS=$FTP_PASSWORD -d --name ftp papacdev/vsftpd
- run: docker run --net=host -p 21:21 -p 20:20 -p 12020:12020 -p 12021:12021 -p 12022:12022 -p 12023:12023 -p 12024:12024 -p 12025:12025 -e USER=$FTP_USER -e PASS=$FTP_PASSWORD -d --name ftp -v $(pwd)/:/ftp/$FTP_USER papacdev/vsftpd
- run: docker run -p 1080:1080 -p 1025:1025 -d --name maildev soulteary/maildev
- run: docker run -p 6379:6379 -d --name redis redis
- run: docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -e POSTGRES_PASSWORD=postgres -d postgis/postgis
Expand Down
17 changes: 12 additions & 5 deletions src/Storage/Service/FTPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Config/stubs/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
'ftp' => [
"driver" => "ftp",
'hostname' => app_env('FTP_HOST', '127.0.0.1'),
'hostname' => app_env('FTP_HOST', 'localhost'),
'password' => app_env('FTP_PASSWORD', 'password'),
'username' => app_env('FTP_USERNAME', 'username'),
'port' => app_env('FTP_PORT', 21),
Expand Down

0 comments on commit 4ac97d2

Please sign in to comment.