Skip to content

Commit

Permalink
Use file index in the specified order
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed May 20, 2024
1 parent 86d94c5 commit f4a0e6c
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/GridFS/GridFSAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,13 @@ public function listContents(string $path, bool $deep): iterable
$path = $this->prefixer->prefixDirectoryPath($path);

$pathdeep = 0;
$pipeline = [];
// Get the last revision of each file, using the index on the files collection
$pipeline = [['$sort' => ['filename' => 1, 'uploadDate' => 1]]];
if ($path !== '') {
$pathdeep = substr_count($path, '/');
// Exclude files that do not start with the expected path
$pipeline[] = ['$match' => ['filename' => new Regex('^' . preg_quote($path))]];
}
// Get the last revision of each file
$pipeline[] = ['$sort' => ['filename' => 1, 'uploadDate' => -1]];

if ($deep === false) {
$pipeline[] = ['$addFields' => ['splitpath' => ['$split' => ['$filename', '/']]]];
Expand All @@ -300,7 +299,7 @@ public function listContents(string $path, bool $deep): iterable
'isDir' => ['$ne' => [['$size' => '$splitpath'], $pathdeep + 1]],
],
// Get the metadata of the last revision of each file
'file' => ['$first' => '$$ROOT'],
'file' => ['$last' => '$$ROOT'],
// The "lastModified" date is the date of the last uploaded file in the directory
'uploadDate' => ['$max' => '$uploadDate'],
]];
Expand Down

0 comments on commit f4a0e6c

Please sign in to comment.