Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
0195906b
Commit
0195906b
authored
Sep 12, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #239
parents
1e3be59e
57b40124
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
105 additions
and
27 deletions
+105
-27
Bucket.php
src/GridFS/Bucket.php
+31
-7
ReadableStream.php
src/GridFS/ReadableStream.php
+9
-11
StreamWrapper.php
src/GridFS/StreamWrapper.php
+7
-2
WritableStream.php
src/GridFS/WritableStream.php
+4
-4
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+53
-2
WritableStreamFunctionalTest.php
tests/GridFS/WritableStreamFunctionalTest.php
+1
-1
No files found.
src/GridFS/Bucket.php
View file @
0195906b
...
...
@@ -218,20 +218,44 @@ class Bucket
}
/**
* Gets the
ID
of the GridFS file associated with a stream.
* Gets the
file document
of the GridFS file associated with a stream.
*
* @param resource $stream GridFS stream
* @return mixed
* @return stdClass
* @throws InvalidArgumentException
*/
public
function
get
IdFrom
Stream
(
$stream
)
public
function
get
FileDocumentFor
Stream
(
$stream
)
{
if
(
!
is_resource
(
$stream
)
||
get_resource_type
(
$stream
)
!=
"stream"
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream'
,
$stream
,
'resource'
);
}
$metadata
=
stream_get_meta_data
(
$stream
);
if
(
$metadata
[
'wrapper_data'
]
instanceof
StreamWrapper
)
{
return
$metadata
[
'wrapper_data'
]
->
getId
();
if
(
!
$metadata
[
'wrapper_data'
]
instanceof
StreamWrapper
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream wrapper data'
,
$metadata
[
'wrapper_data'
],
'MongoDB\Driver\GridFS\StreamWrapper'
);
}
return
$metadata
[
'wrapper_data'
]
->
getFile
();
}
/**
* Gets the file document's ID of the GridFS file associated with a stream.
*
* @param resource $stream GridFS stream
* @return stdClass
* @throws CorruptFileException
* @throws InvalidArgumentException
*/
public
function
getFileIdForStream
(
$stream
)
{
$file
=
$this
->
getFileDocumentForStream
(
$stream
);
if
(
!
isset
(
$file
->
_id
)
&&
!
property_exists
(
$file
,
'_id'
))
{
throw
new
CorruptFileException
(
'file._id does not exist'
);
}
// TODO: Throw if we cannot access the ID
return
$file
->
_id
;
}
/**
...
...
@@ -379,7 +403,7 @@ class Bucket
$destination
=
$this
->
openUploadStream
(
$filename
,
$options
);
stream_copy_to_stream
(
$source
,
$destination
);
return
$this
->
get
IdFrom
Stream
(
$destination
);
return
$this
->
get
FileIdFor
Stream
(
$destination
);
}
/**
...
...
src/GridFS/ReadableStream.php
View file @
0195906b
...
...
@@ -21,8 +21,8 @@ class ReadableStream
private
$chunkOffset
=
0
;
private
$chunksIterator
;
private
$collectionWrapper
;
private
$file
;
private
$firstCheck
=
true
;
private
$id
;
private
$iteratorEmpty
=
false
;
private
$length
;
private
$numChunks
;
...
...
@@ -44,15 +44,15 @@ class ReadableStream
throw
new
CorruptFileException
(
'file.length is not an integer > 0'
);
}
if
(
!
isset
(
$file
->
_id
)
&&
!
array_key_exists
(
'_id'
,
(
array
)
$file
))
{
if
(
!
isset
(
$file
->
_id
)
&&
!
property_exists
(
$file
,
'_id'
))
{
throw
new
CorruptFileException
(
'file._id does not exist'
);
}
$this
->
id
=
$file
->
_id
;
$this
->
file
=
$file
;
$this
->
chunkSize
=
$file
->
chunkSize
;
$this
->
length
=
$file
->
length
;
$this
->
chunksIterator
=
$collectionWrapper
->
getChunksIteratorByFilesId
(
$
this
->
id
);
$this
->
chunksIterator
=
$collectionWrapper
->
getChunksIteratorByFilesId
(
$
file
->
_
id
);
$this
->
collectionWrapper
=
$collectionWrapper
;
$this
->
numChunks
=
ceil
(
$this
->
length
/
$this
->
chunkSize
);
$this
->
initEmptyBuffer
();
...
...
@@ -69,9 +69,7 @@ class ReadableStream
return
[
'bucketName'
=>
$this
->
collectionWrapper
->
getBucketName
(),
'databaseName'
=>
$this
->
collectionWrapper
->
getDatabaseName
(),
'id'
=>
$this
->
id
,
'chunkSize'
=>
$this
->
chunkSize
,
'length'
=>
$this
->
length
,
'file'
=>
$this
->
file
,
];
}
...
...
@@ -130,13 +128,13 @@ class ReadableStream
}
/**
* Return the stream's
ID (i.e. file document identifier)
.
* Return the stream's
file document
.
*
* @return
integer
* @return
stdClass
*/
public
function
get
Id
()
public
function
get
File
()
{
return
$this
->
id
;
return
$this
->
file
;
}
/**
...
...
src/GridFS/StreamWrapper.php
View file @
0195906b
...
...
@@ -22,9 +22,14 @@ class StreamWrapper
private
$protocol
;
private
$stream
;
public
function
getId
()
/**
* Return the stream's file document.
*
* @return stdClass
*/
public
function
getFile
()
{
return
$this
->
stream
->
get
Id
();
return
$this
->
stream
->
get
File
();
}
/**
...
...
src/GridFS/WritableStream.php
View file @
0195906b
...
...
@@ -127,13 +127,13 @@ class WritableStream
}
/**
* Return the stream's
ID (i.e. file document identifier)
.
* Return the stream's
file document
.
*
* @return
integer
* @return
stdClass
*/
public
function
get
Id
()
public
function
get
File
()
{
return
$this
->
file
[
'_id'
]
;
return
(
object
)
$this
->
file
;
}
/**
...
...
tests/GridFS/BucketFunctionalTest.php
View file @
0195906b
...
...
@@ -323,12 +323,63 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
assertEquals
(
$this
->
getDatabaseName
(),
$this
->
bucket
->
getDatabaseName
());
}
public
function
testGetIdFromStream
()
public
function
testGetFileDocumentForStreamWithReadableStream
()
{
$metadata
=
[
'foo'
=>
'bar'
];
$id
=
$this
->
bucket
->
uploadFromStream
(
'filename'
,
$this
->
createStream
(
'foobar'
),
[
'metadata'
=>
$metadata
]);
$stream
=
$this
->
bucket
->
openDownloadStream
(
$id
);
$fileDocument
=
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
$this
->
assertEquals
(
$id
,
$fileDocument
->
_id
);
$this
->
assertSame
(
'filename'
,
$fileDocument
->
filename
);
$this
->
assertSame
(
6
,
$fileDocument
->
length
);
$this
->
assertSameDocument
(
$metadata
,
$fileDocument
->
metadata
);
}
public
function
testGetFileDocumentForStreamWithWritableStream
()
{
$metadata
=
[
'foo'
=>
'bar'
];
$stream
=
$this
->
bucket
->
openUploadStream
(
'filename'
,
[
'_id'
=>
1
,
'metadata'
=>
$metadata
]);
$fileDocument
=
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
$this
->
assertEquals
(
1
,
$fileDocument
->
_id
);
$this
->
assertSame
(
'filename'
,
$fileDocument
->
filename
);
$this
->
assertSameDocument
(
$metadata
,
$fileDocument
->
metadata
);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
*/
public
function
testGetFileDocumentForStreamShouldRequireStreamResource
(
$stream
)
{
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
}
public
function
testGetFileIdForStreamWithReadableStream
()
{
$id
=
$this
->
bucket
->
uploadFromStream
(
'filename'
,
$this
->
createStream
(
'foobar'
));
$stream
=
$this
->
bucket
->
openDownloadStream
(
$id
);
$this
->
assertEquals
(
$id
,
$this
->
bucket
->
getIdFromStream
(
$stream
));
$this
->
assertEquals
(
$id
,
$this
->
bucket
->
getFileIdForStream
(
$stream
));
}
public
function
testGetFileIdForStreamWithWritableStream
()
{
$stream
=
$this
->
bucket
->
openUploadStream
(
'filename'
,
[
'_id'
=>
1
]);
$this
->
assertEquals
(
1
,
$this
->
bucket
->
getFileIdForStream
(
$stream
));
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
*/
public
function
testGetFileIdForStreamShouldRequireStreamResource
(
$stream
)
{
$this
->
bucket
->
getFileIdForStream
(
$stream
);
}
/**
...
...
tests/GridFS/WritableStreamFunctionalTest.php
View file @
0195906b
...
...
@@ -62,7 +62,7 @@ class WritableStreamFunctionalTest extends FunctionalTestCase
$stream
->
close
();
$fileDocument
=
$this
->
filesCollection
->
findOne
(
[
'_id'
=>
$stream
->
get
Id
()
],
[
'_id'
=>
$stream
->
get
File
()
->
_id
],
[
'projection'
=>
[
'md5'
=>
1
,
'_id'
=>
0
]]
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment