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
f418ac21
Commit
f418ac21
authored
Jan 06, 2016
by
Will Banfield
Committed by
Jeremy Mikola
Mar 14, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added 'Abort' to clean up according to spec
parent
6e84d2ff
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
8 deletions
+33
-8
Bucket.php
src/GridFS/Bucket.php
+0
-4
GridFsUpload.php
src/GridFS/GridFsUpload.php
+33
-4
No files found.
src/GridFS/Bucket.php
View file @
f418ac21
...
@@ -186,10 +186,6 @@ class Bucket
...
@@ -186,10 +186,6 @@ class Bucket
'file'
=>
$file
'file'
=>
$file
];
];
$context
=
stream_context_create
([
'gridfs'
=>
$options
]);
$context
=
stream_context_create
([
'gridfs'
=>
$options
]);
//db/prefix/(filter criteria as BSON}
// find criteria being MongoDB\BSON\fromPHP(['_id' => $file['_id']])
// stream wrapper can explode('/', 3), which returns array of db, prefix, and BSON blob
// MongoDB\BSON\toPHP(bson blob) yields find() criteria
return
fopen
(
sprintf
(
'gridfs://%s/%s'
,
$this
->
databaseName
,
$file
->
filename
),
'r'
,
false
,
$context
);
return
fopen
(
sprintf
(
'gridfs://%s/%s'
,
$this
->
databaseName
,
$file
->
filename
),
'r'
,
false
,
$context
);
}
}
private
function
findFileRevision
(
$filename
,
$revision
)
private
function
findFileRevision
(
$filename
,
$revision
)
...
...
src/GridFS/GridFsUpload.php
View file @
f418ac21
...
@@ -21,6 +21,7 @@ class GridFsUpload
...
@@ -21,6 +21,7 @@ class GridFsUpload
private
$chunkSize
;
private
$chunkSize
;
private
$buffer
;
private
$buffer
;
private
$file
;
private
$file
;
private
$isClosed
=
false
;
/**
/**
* Constructs a GridFS upload stream
* Constructs a GridFS upload stream
*
*
...
@@ -123,6 +124,9 @@ class GridFsUpload
...
@@ -123,6 +124,9 @@ class GridFsUpload
*/
*/
public
function
insertChunks
(
$toWrite
)
public
function
insertChunks
(
$toWrite
)
{
{
if
(
$this
->
isClosed
){
return
;
}
$readBytes
=
0
;
$readBytes
=
0
;
while
(
$readBytes
!=
strlen
(
$toWrite
))
{
while
(
$readBytes
!=
strlen
(
$toWrite
))
{
$addToBuffer
=
substr
(
$toWrite
,
$readBytes
,
$this
->
chunkSize
-
$this
->
bufferLength
);
$addToBuffer
=
substr
(
$toWrite
,
$readBytes
,
$this
->
chunkSize
-
$this
->
bufferLength
);
...
@@ -144,6 +148,9 @@ class GridFsUpload
...
@@ -144,6 +148,9 @@ class GridFsUpload
*/
*/
public
function
close
()
public
function
close
()
{
{
if
(
$this
->
isClosed
){
return
;
}
rewind
(
$this
->
buffer
);
rewind
(
$this
->
buffer
);
$cached
=
stream_get_contents
(
$this
->
buffer
);
$cached
=
stream_get_contents
(
$this
->
buffer
);
...
@@ -152,6 +159,7 @@ class GridFsUpload
...
@@ -152,6 +159,7 @@ class GridFsUpload
}
}
fclose
(
$this
->
buffer
);
fclose
(
$this
->
buffer
);
$this
->
fileCollectionInsert
();
$this
->
fileCollectionInsert
();
$this
->
isClosed
=
true
;
}
}
public
function
getSize
()
public
function
getSize
()
{
{
...
@@ -175,22 +183,43 @@ class GridFsUpload
...
@@ -175,22 +183,43 @@ class GridFsUpload
}
}
public
function
isEOF
()
public
function
isEOF
()
{
{
return
false
;
return
$this
->
isClosed
;
}
private
function
abort
()
{
$this
->
collectionsWrapper
->
getChunksCollection
()
->
deleteMany
([
"files_id"
=>
$this
->
file
[
"_id"
]]);
$this
->
collectionsWrapper
->
getFilesCollection
()
->
deleteOne
([
"_id"
=>
$this
->
file
[
'_id'
]]);
$this
->
isClosed
=
true
;
}
}
private
function
insertChunk
(
$data
)
private
function
insertChunk
(
$data
)
{
{
if
(
$this
->
isClosed
){
return
;
}
$toUpload
=
[
"files_id"
=>
$this
->
file
[
'_id'
],
"n"
=>
$this
->
chunkOffset
,
"data"
=>
new
\MongoDB\BSON\Binary
(
$data
,
\MongoDB\BSON\Binary
::
TYPE_GENERIC
)];
$toUpload
=
[
"files_id"
=>
$this
->
file
[
'_id'
],
"n"
=>
$this
->
chunkOffset
,
"data"
=>
new
\MongoDB\BSON\Binary
(
$data
,
\MongoDB\BSON\Binary
::
TYPE_GENERIC
)];
hash_update
(
$this
->
ctx
,
$data
);
hash_update
(
$this
->
ctx
,
$data
);
$this
->
collectionsWrapper
->
chunkInsert
(
$toUpload
);
try
{
$this
->
collectionsWrapper
->
chunkInsert
(
$toUpload
);
}
catch
(
\MongoDB\Exception
$e
){
$this
->
abort
();
throw
$e
;
}
$this
->
length
+=
strlen
(
$data
);
$this
->
length
+=
strlen
(
$data
);
$this
->
chunkOffset
++
;
$this
->
chunkOffset
++
;
}
}
private
function
fileCollectionInsert
()
private
function
fileCollectionInsert
()
{
{
if
(
$this
->
isClosed
){
return
;
}
$md5
=
hash_final
(
$this
->
ctx
);
$md5
=
hash_final
(
$this
->
ctx
);
$this
->
file
=
array_merge
(
$this
->
file
,
[
'length'
=>
$this
->
length
,
'md5'
=>
$md5
]);
$this
->
file
=
array_merge
(
$this
->
file
,
[
'length'
=>
$this
->
length
,
'md5'
=>
$md5
]);
$this
->
collectionsWrapper
->
fileInsert
(
$this
->
file
);
try
{
$this
->
collectionsWrapper
->
fileInsert
(
$this
->
file
);
}
catch
(
\MongoDB\Exception
$e
){
$this
->
abort
();
throw
$e
;
}
return
$this
->
file
[
'_id'
];
return
$this
->
file
[
'_id'
];
}
}
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
...
...
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