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
6e636cb8
Commit
6e636cb8
authored
Dec 16, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #304
parents
9dfb2c5f
049a77c4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
74 additions
and
45 deletions
+74
-45
Bucket.php
src/GridFS/Bucket.php
+4
-0
StreamWrapper.php
src/GridFS/StreamWrapper.php
+1
-1
WritableStream.php
src/GridFS/WritableStream.php
+48
-39
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+9
-0
SpecFunctionalTest.php
tests/GridFS/SpecFunctionalTest.php
+1
-3
WritableStreamFunctionalTest.php
tests/GridFS/WritableStreamFunctionalTest.php
+11
-2
No files found.
src/GridFS/Bucket.php
View file @
6e636cb8
...
...
@@ -80,6 +80,10 @@ class Bucket
throw
InvalidArgumentException
::
invalidType
(
'"chunkSizeBytes" option'
,
$options
[
'chunkSizeBytes'
],
'integer'
);
}
if
(
isset
(
$options
[
'chunkSizeBytes'
])
&&
$options
[
'chunkSizeBytes'
]
<
1
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Expected "chunkSizeBytes" option to be >= 1, %d given'
,
$options
[
'chunkSizeBytes'
]));
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
'MongoDB\Driver\ReadConcern'
);
}
...
...
src/GridFS/StreamWrapper.php
View file @
6e636cb8
...
...
@@ -150,7 +150,7 @@ class StreamWrapper
}
try
{
return
$this
->
stream
->
insertChunk
s
(
$data
);
return
$this
->
stream
->
writeByte
s
(
$data
);
}
catch
(
Exception
$e
)
{
trigger_error
(
sprintf
(
'%s: %s'
,
get_class
(
$e
),
$e
->
getMessage
()),
\E_USER_WARNING
);
return
false
;
...
...
src/GridFS/WritableStream.php
View file @
6e636cb8
...
...
@@ -5,7 +5,9 @@ namespace MongoDB\GridFS;
use
MongoDB\BSON\Binary
;
use
MongoDB\BSON\ObjectId
;
use
MongoDB\BSON\UTCDateTime
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\RuntimeException
;
/**
* WritableStream abstracts the process of writing a GridFS file.
...
...
@@ -16,8 +18,7 @@ class WritableStream
{
private
static
$defaultChunkSizeBytes
=
261120
;
private
$buffer
;
private
$bufferLength
=
0
;
private
$buffer
=
''
;
private
$chunkOffset
=
0
;
private
$chunkSize
;
private
$collectionWrapper
;
...
...
@@ -66,6 +67,10 @@ class WritableStream
throw
InvalidArgumentException
::
invalidType
(
'"chunkSizeBytes" option'
,
$options
[
'chunkSizeBytes'
],
'integer'
);
}
if
(
isset
(
$options
[
'chunkSizeBytes'
])
&&
$options
[
'chunkSizeBytes'
]
<
1
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Expected "chunkSizeBytes" option to be >= 1, %d given'
,
$options
[
'chunkSizeBytes'
]));
}
if
(
isset
(
$options
[
'contentType'
])
&&
!
is_string
(
$options
[
'contentType'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"contentType" option'
,
$options
[
'contentType'
],
'string'
);
}
...
...
@@ -76,15 +81,13 @@ class WritableStream
$this
->
chunkSize
=
$options
[
'chunkSizeBytes'
];
$this
->
collectionWrapper
=
$collectionWrapper
;
$this
->
buffer
=
fopen
(
'php://memory'
,
'w+b'
);
$this
->
ctx
=
hash_init
(
'md5'
);
$this
->
file
=
[
'_id'
=>
$options
[
'_id'
],
'chunkSize'
=>
$this
->
chunkSize
,
'filename'
=>
(
string
)
$filename
,
// TODO: This is necessary until PHPC-536 is implemented
'uploadDate'
=>
new
UTCDateTime
((
int
)
floor
(
microtime
(
true
)
*
1000
)),
'uploadDate'
=>
new
UTCDateTime
,
]
+
array_intersect_key
(
$options
,
[
'aliases'
=>
1
,
'contentType'
=>
1
,
'metadata'
=>
1
]);
}
...
...
@@ -113,14 +116,10 @@ class WritableStream
return
;
}
rewind
(
$this
->
buffer
);
$cached
=
stream_get_contents
(
$this
->
buffer
);
if
(
strlen
(
$cached
)
>
0
)
{
$this
->
insertChunk
(
$cached
);
if
(
strlen
(
$this
->
buffer
)
>
0
)
{
$this
->
insertChunkFromBuffer
();
}
fclose
(
$this
->
buffer
);
$this
->
fileCollectionInsert
();
$this
->
isClosed
=
true
;
}
...
...
@@ -151,69 +150,72 @@ class WritableStream
* Inserts binary data into GridFS via chunks.
*
* Data will be buffered internally until chunkSizeBytes are accumulated, at
* which point a chunk's worth of data will be inserted and the buffer
* reset.
* which point a chunk document will be inserted and the buffer reset.
*
* @param string $
toWrite
Binary data to write
* @param string $
data
Binary data to write
* @return integer
*/
public
function
insertChunks
(
$toWrite
)
public
function
writeBytes
(
$data
)
{
if
(
$this
->
isClosed
)
{
// TODO: Should this be an error condition? e.g. BadMethodCallException
return
;
}
$
readBytes
=
0
;
$
bytesRead
=
0
;
while
(
$readBytes
!=
strlen
(
$toWrite
))
{
$addToBuffer
=
substr
(
$toWrite
,
$readBytes
,
$this
->
chunkSize
-
$this
->
bufferLength
);
fwrite
(
$this
->
buffer
,
$addToBuffer
);
$readBytes
+=
strlen
(
$addToBuffer
);
$this
->
bufferLength
+=
strlen
(
$addToBuffer
);
while
(
$bytesRead
!=
strlen
(
$data
))
{
$initialBufferLength
=
strlen
(
$this
->
buffer
);
$this
->
buffer
.=
substr
(
$data
,
$bytesRead
,
$this
->
chunkSize
-
$initialBufferLength
);
$bytesRead
+=
strlen
(
$this
->
buffer
)
-
$initialBufferLength
;
if
(
$this
->
bufferLength
==
$this
->
chunkSize
)
{
rewind
(
$this
->
buffer
);
$this
->
insertChunk
(
stream_get_contents
(
$this
->
buffer
));
ftruncate
(
$this
->
buffer
,
0
);
$this
->
bufferLength
=
0
;
if
(
strlen
(
$this
->
buffer
)
==
$this
->
chunkSize
)
{
$this
->
insertChunkFromBuffer
();
}
}
return
$
readBytes
;
return
$
bytesRead
;
}
private
function
abort
()
{
$this
->
collectionWrapper
->
deleteChunksByFilesId
(
$this
->
file
[
'_id'
]);
try
{
$this
->
collectionWrapper
->
deleteChunksByFilesId
(
$this
->
file
[
'_id'
]);
}
catch
(
DriverRuntimeException
$e
)
{
// We are already handling an error if abort() is called, so suppress this
}
$this
->
isClosed
=
true
;
}
private
function
fileCollectionInsert
()
{
if
(
$this
->
isClosed
)
{
// TODO: Should this be an error condition? e.g. BadMethodCallException
return
;
}
$md5
=
hash_final
(
$this
->
ctx
);
$this
->
file
[
'length'
]
=
$this
->
length
;
$this
->
file
[
'md5'
]
=
$md5
;
$this
->
collectionWrapper
->
insertFile
(
$this
->
file
);
try
{
$this
->
collectionWrapper
->
insertFile
(
$this
->
file
);
}
catch
(
DriverRuntimeException
$e
)
{
$this
->
abort
();
throw
$e
;
}
return
$this
->
file
[
'_id'
];
}
private
function
insertChunk
(
$data
)
private
function
insertChunk
FromBuffer
(
)
{
if
(
$this
->
isClosed
)
{
// TODO: Should this be an error condition? e.g. BadMethodCallException
if
(
strlen
(
$this
->
buffer
)
==
0
)
{
return
;
}
$toUpload
=
[
$data
=
$this
->
buffer
;
$this
->
buffer
=
''
;
$chunk
=
[
'files_id'
=>
$this
->
file
[
'_id'
],
'n'
=>
$this
->
chunkOffset
,
'data'
=>
new
Binary
(
$data
,
Binary
::
TYPE_GENERIC
),
...
...
@@ -221,7 +223,14 @@ class WritableStream
hash_update
(
$this
->
ctx
,
$data
);
$this
->
collectionWrapper
->
insertChunk
(
$toUpload
);
try
{
$this
->
collectionWrapper
->
insertChunk
(
$chunk
);
}
catch
(
DriverRuntimeException
$e
)
{
$this
->
abort
();
throw
$e
;
}
$this
->
length
+=
strlen
(
$data
);
$this
->
chunkOffset
++
;
}
...
...
tests/GridFS/BucketFunctionalTest.php
View file @
6e636cb8
...
...
@@ -68,6 +68,15 @@ class BucketFunctionalTest extends FunctionalTestCase
return
$options
;
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected "chunkSizeBytes" option to be >= 1, 0 given
*/
public
function
testConstructorShouldRequireChunkSizeBytesOptionToBePositive
()
{
new
Bucket
(
$this
->
manager
,
$this
->
getDatabaseName
(),
[
'chunkSizeBytes'
=>
0
]);
}
/**
* @dataProvider provideInputDataAndExpectedChunks
*/
...
...
tests/GridFS/SpecFunctionalTest.php
View file @
6e636cb8
...
...
@@ -150,9 +150,7 @@ class SpecFunctionalTest extends FunctionalTestCase
}
if
(
isset
(
$value
[
'$date'
]))
{
// TODO: This is necessary until PHPC-536 is implemented
$milliseconds
=
floor
((
new
DateTime
(
$value
[
'$date'
]))
->
format
(
'U.u'
)
*
1000
);
$value
=
new
UTCDateTime
((
int
)
$milliseconds
);
$value
=
new
UTCDateTime
(
new
DateTime
(
$value
[
'$date'
]));
return
;
}
...
...
tests/GridFS/WritableStreamFunctionalTest.php
View file @
6e636cb8
...
...
@@ -52,13 +52,22 @@ class WritableStreamFunctionalTest extends FunctionalTestCase
return
$options
;
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected "chunkSizeBytes" option to be >= 1, 0 given
*/
public
function
testConstructorShouldRequireChunkSizeBytesOptionToBePositive
()
{
new
WritableStream
(
$this
->
collectionWrapper
,
'filename'
,
[
'chunkSizeBytes'
=>
0
]);
}
/**
* @dataProvider provideInputDataAndExpectedMD5
*/
public
function
test
InsertChunk
sCalculatesMD5
(
$input
,
$expectedMD5
)
public
function
test
WriteByte
sCalculatesMD5
(
$input
,
$expectedMD5
)
{
$stream
=
new
WritableStream
(
$this
->
collectionWrapper
,
'filename'
);
$stream
->
insertChunk
s
(
$input
);
$stream
->
writeByte
s
(
$input
);
$stream
->
close
();
$fileDocument
=
$this
->
filesCollection
->
findOne
(
...
...
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