Skip to content

Commit fcdc117

Browse files
committed
PHPLIB-249: GridFS stat should report modified and created timestamps
1 parent eab912f commit fcdc117

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace MongoDB\GridFS;
1919

20+
use MongoDB\BSON\UTCDateTime;
2021
use Exception;
2122

2223
/**
@@ -152,6 +153,12 @@ public function stream_stat()
152153

153154
$file = $this->stream->getFile();
154155

156+
if (isset($file->uploadDate) && $file->uploadDate instanceof UTCDateTime) {
157+
$timestamp = $file->uploadDate->toDateTime()->getTimestamp();
158+
$stat[9] = $stat['mtime'] = $timestamp;
159+
$stat[10] = $stat['ctime'] = $timestamp;
160+
}
161+
155162
if (isset($file->chunkSize) && is_integer($file->chunkSize)) {
156163
$stat[11] = $stat['blksize'] = $file->chunkSize;
157164
}

tests/GridFS/StreamWrapperFunctionalTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Tests\GridFS;
44

55
use MongoDB\BSON\Binary;
6+
use MongoDB\BSON\UTCDateTime;
67

78
/**
89
* Functional tests for the internal StreamWrapper class.
@@ -14,7 +15,7 @@ public function setUp()
1415
parent::setUp();
1516

1617
$this->filesCollection->insertMany([
17-
['_id' => 'length-10', 'length' => 10, 'chunkSize' => 4],
18+
['_id' => 'length-10', 'length' => 10, 'chunkSize' => 4, 'uploadDate' => new UTCDateTime('1484202200000')],
1819
]);
1920

2021
$this->chunksCollection->insertMany([
@@ -58,6 +59,10 @@ public function testReadableStreamStat()
5859
$this->assertSame(0100444, $stat['mode']);
5960
$this->assertSame(10, $stat[7]);
6061
$this->assertSame(10, $stat['size']);
62+
$this->assertSame(1484202200, $stat[9]);
63+
$this->assertSame(1484202200, $stat['mtime']);
64+
$this->assertSame(1484202200, $stat[10]);
65+
$this->assertSame(1484202200, $stat['ctime']);
6166
$this->assertSame(4, $stat[11]);
6267
$this->assertSame(4, $stat['blksize']);
6368
}
@@ -99,13 +104,18 @@ public function testWritableStreamRead()
99104

100105
public function testWritableStreamStat()
101106
{
107+
$currentTimestamp = time();
102108
$stream = $this->bucket->openUploadStream('filename', ['chunkSizeBytes' => 1024]);
103109

104110
$stat = fstat($stream);
105111
$this->assertSame(0100222, $stat[2]);
106112
$this->assertSame(0100222, $stat['mode']);
107113
$this->assertSame(0, $stat[7]);
108114
$this->assertSame(0, $stat['size']);
115+
$this->assertGreaterThanOrEqual($currentTimestamp, $stat[9]);
116+
$this->assertGreaterThanOrEqual($currentTimestamp, $stat['mtime']);
117+
$this->assertGreaterThanOrEqual($currentTimestamp, $stat[10]);
118+
$this->assertGreaterThanOrEqual($currentTimestamp, $stat['ctime']);
109119
$this->assertSame(1024, $stat[11]);
110120
$this->assertSame(1024, $stat['blksize']);
111121

0 commit comments

Comments
 (0)