Skip to content

Commit 9689861

Browse files
authored
Merge pull request #11 from verseles/add-metadata-flag-to-exists-13038786992094857972
feat: Allow passing flags to file existence checks
2 parents 915f77b + eafa605 commit 9689861

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/Rclone.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,39 +913,42 @@ public function ls(string $path, array $flags = []) : array
913913
* Checks if a path exists and is a file.
914914
*
915915
* @param string $path Path to check.
916+
* @param array $flags Additional flags (e.g. ['metadata' => true]).
916917
*
917918
* @return object Object with 'exists' (bool), 'details' (object|array), and 'error' (string|\Exception) properties.
918919
*/
919-
public function is_file(string $path) : object
920+
public function is_file(string $path, array $flags = []) : object
920921
{
921-
return $this->exists($path, 'file');
922+
return $this->exists($path, 'file', $flags);
922923
}
923924

924925
/**
925926
* Checks if a path exists and is a directory.
926927
*
927928
* @param string $path Path to check.
929+
* @param array $flags Additional flags.
928930
*
929931
* @return object Object with 'exists' (bool), 'details' (object|array), and 'error' (string|\Exception) properties.
930932
*/
931-
public function is_dir(string $path) : object
933+
public function is_dir(string $path, array $flags = []) : object
932934
{
933-
return $this->exists($path, 'dir');
935+
return $this->exists($path, 'dir', $flags);
934936
}
935937

936938
/**
937939
* Checks if a path exists and is of the specified type ('file' or 'dir').
938940
* This method lists the parent directory and then filters for the specific item.
939941
*
940-
* @param string $path The path to check.
941-
* @param string $type The type to check for ('file' or 'dir').
942+
* @param string $path The path to check.
943+
* @param string $type The type to check for ('file' or 'dir').
944+
* @param array $flags Additional flags for the ls operation.
942945
*
943946
* @return object An object with properties:
944947
* - bool 'exists': True if the item exists and matches the type.
945948
* - mixed 'details': The item's details from 'lsjson' if it exists, else empty array.
946949
* - mixed 'error': The Exception object if an error occurred during 'ls', else empty string.
947950
*/
948-
public function exists(string $path, string $type) : object
951+
public function exists(string $path, string $type, array $flags = []) : object
949952
{
950953
$dirname = dirname($path);
951954
// If dirname is '.', it means the path is at the remote's root.
@@ -956,7 +959,7 @@ public function exists(string $path, string $type) : object
956959
$basename = basename($path);
957960

958961
try {
959-
$listing = $this->ls($dirname); // List parent directory contents.
962+
$listing = $this->ls($dirname, $flags); // List parent directory contents.
960963
$found_item = array_filter($listing, static fn($item) => isset($item->Name) && $item->Name === $basename &&
961964
isset($item->IsDir) && $item->IsDir === ($type === 'dir'),
962965
);

tests/Unit/LocalProviderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,22 @@ final public function write_to_a_file($params) : array
4646

4747
return [$left_side, $temp_filepath, $content];
4848
}
49+
50+
#[Test]
51+
#[Depends('write_to_a_file')]
52+
public function is_file_respects_flags(array $params) : void
53+
{
54+
[$left_side, $temp_filepath] = $params;
55+
56+
// We verify that flags are passed by using a filtering flag that should exclude the file.
57+
// If the flag was ignored, the file would be found and this assertion would fail.
58+
59+
// 1G is definitely larger than the test file (approx 46 bytes).
60+
$result = $left_side->is_file($temp_filepath, ['min-size' => '1G']);
61+
62+
self::assertFalse($result->exists, "File should NOT be found when min-size is large (flag respected).");
63+
// Ensure it returned false due to filtering (empty list), not an error/crash
64+
self::assertEmpty($result->error, "Error occurred during check: " . print_r($result->error, TRUE));
65+
}
4966

5067
}

0 commit comments

Comments
 (0)