Skip to content

Commit caf9db9

Browse files
committed
PHPLIB-249: GridFS stat should report modified and create timestamps
1 parent 81b866c commit caf9db9

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
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\GridFS;
44

5+
use MongoDB\BSON\UTCDateTime;
56
use Exception;
67

78
/**
@@ -137,6 +138,12 @@ public function stream_stat()
137138

138139
$file = $this->stream->getFile();
139140

141+
if (isset($file->uploadDate) && $file->uploadDate instanceof UTCDateTime) {
142+
$timestamp = $file->uploadDate->toDateTime()->getTimestamp();
143+
$stat[9] = $stat['mtime'] = $timestamp;
144+
$stat[10] = $stat['ctime'] = $timestamp;
145+
}
146+
140147
if (isset($file->chunkSize) && is_integer($file->chunkSize)) {
141148
$stat[11] = $stat['blksize'] = $file->chunkSize;
142149
}

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)