thephpleague / flysystem

Abstraction for local and remote filesystems
https://flysystem.thephpleague.com
MIT License
13.23k stars 825 forks source link

How can i get one element on listContent? #1777

Open ElUtku opened 2 months ago

ElUtku commented 2 months ago

Question

Q A
Flysystem Version 3.23.0
Adapter Name All
Adapter version -

Hello, is there any way to get the json of a single item? example:

League\Flysystem\DirectoryAttributes {#594 ▼
  -type: "dir"
  -path: "Test\Test2"
  -visibility: "private"
  -lastModified: 1702292652
  -extraMetadata: array:3 [▼
    "id" => ""
    "virtual_path" => ""
    "display_path" => "Test2"
  ]
}

Currently I have to get all elements of a path and check if the original path matches the path of the current $item. This is inefficient and I don't know if there is a way to get the file or directory directly through a listContent($path).

$contents=$filesystem->listContents(dirname($ruta))->toArray();

foreach ($contents as $item) {
       if ($item['path']==$ruta || str_replace('\\', '/',$item['path']) == $ruta) {  // /a/b/c.txt == /a/b/c.txt

            if ($item instanceof FileAttributes) {
                        return new FileAttributes($ruta,$item->fileSize(),$item->visibility(),$item->lastModified(),$item->mimeType(),$item->extraMetadata());
            } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) {
                        return new DirectoryAttributes($ruta,$item->visibility(),$item->lastModified(),$item->extraMetadata());
            }

       }
}