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
8f2aaeab
Commit
8f2aaeab
authored
Dec 10, 2015
by
Will Banfield
Committed by
Jeremy Mikola
Mar 14, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-148: Implement file deletion
parent
76f89eae
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
33 deletions
+57
-33
Bucket.php
src/GridFS/Bucket.php
+18
-16
BucketReadWriter.php
src/GridFS/BucketReadWriter.php
+20
-0
GridFsDownload.php
src/GridFS/GridFsDownload.php
+1
-2
SpecificationTests.php
tests/GridFS/SpecificationTests.php
+18
-15
No files found.
src/GridFS/Bucket.php
View file @
8f2aaeab
...
@@ -111,21 +111,30 @@ class Bucket
...
@@ -111,21 +111,30 @@ class Bucket
{
{
return
$this
->
options
[
'bucketName'
];
return
$this
->
options
[
'bucketName'
];
}
}
public
function
find
(
$filter
,
array
$options
=
[]
)
public
function
getReadConcern
(
)
{
{
//add proper validation for the filter and for the options
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
return
$this
->
filesCollection
->
find
(
$filter
);
return
$this
->
options
[
'readPreference'
];
}
else
{
return
null
;
}
}
}
public
function
getWriteConcern
()
public
function
chunkInsert
(
$toUpload
)
{
{
$this
->
chunksCollection
->
insertOne
(
$toUpload
);
if
(
isset
(
$this
->
options
[
'writeConcern'
]))
{
return
$this
->
options
[
'writeConcern'
];
}
else
{
return
null
;
}
}
}
public
function
fileInsert
(
$toUpload
)
{
public
function
find
(
$filter
,
array
$options
=
[])
$this
->
filesCollection
->
insertOne
(
$toUpload
);
{
//add proper validation for the filter and for the options
return
$this
->
filesCollection
->
find
(
$filter
);
}
}
p
ublic
function
ensureIndexes
()
p
rivate
function
ensureIndexes
()
{
{
if
(
$this
->
ensuredIndexes
)
{
if
(
$this
->
ensuredIndexes
)
{
return
;
return
;
...
@@ -162,11 +171,4 @@ class Bucket
...
@@ -162,11 +171,4 @@ class Bucket
'projection'
=>
[
'_id'
=>
1
],
'projection'
=>
[
'_id'
=>
1
],
]);
]);
}
}
public
function
delete
(
ObjectId
$id
)
{
$options
=
[
'writeConcern'
=>
$this
->
writeConcern
];
$this
->
chunksCollection
->
deleteMany
([
'file_id'
=>
$id
],
$options
);
$this
->
filesCollection
->
deleteOne
([
'_id'
=>
$id
],
$options
);
}
}
}
src/GridFS/BucketReadWriter.php
View file @
8f2aaeab
...
@@ -63,5 +63,25 @@ class BucketReadWriter
...
@@ -63,5 +63,25 @@ class BucketReadWriter
$gridFsStream
=
new
GridFsDownload
(
$this
->
bucket
,
$id
);
$gridFsStream
=
new
GridFsDownload
(
$this
->
bucket
,
$id
);
$gridFsStream
->
downloadToStream
(
$destination
);
$gridFsStream
->
downloadToStream
(
$destination
);
}
}
/**
* Delete a file from the GridFS bucket. If the file collection entry is not found, still attempts to delete orphaned chunks
*
* @param ObjectId $id file id
* @throws GridFSFileNotFoundException
*/
public
function
delete
(
\MongoDB\BSON\ObjectId
$id
)
{
$options
=
[];
$writeConcern
=
$this
->
bucket
->
getWriteConcern
();
if
(
!
is_null
(
$writeConcern
))
{
$options
[
'writeConcern'
]
=
$writeConcern
;
}
$file
=
$this
->
bucket
->
getFilesCollection
()
->
findOne
([
'_id'
=>
$id
]);
$this
->
bucket
->
getChunksCollection
()
->
deleteMany
([
'files_id'
=>
$id
],
$options
);
if
(
is_null
(
$file
))
{
throw
new
\MongoDB\Exception\GridFSFileNotFoundException
(
$id
,
$this
->
bucket
->
getDatabaseName
(),
$this
->
bucket
->
getBucketName
());
}
$this
->
bucket
->
getFilesCollection
()
->
deleteOne
([
'_id'
=>
$id
],
$options
);
}
}
}
src/GridFS/GridFsDownload.php
View file @
8f2aaeab
...
@@ -19,7 +19,7 @@ class GridFsDownload extends GridFsStream
...
@@ -19,7 +19,7 @@ class GridFsDownload extends GridFsStream
private
$bufferFresh
=
true
;
private
$bufferFresh
=
true
;
private
$bufferEmpty
=
true
;
private
$bufferEmpty
=
true
;
/**
/**
* Constructs a GridFS
up
load stream
* Constructs a GridFS
down
load stream
*
*
* Supported options:
* Supported options:
*
*
...
@@ -45,7 +45,6 @@ class GridFsDownload extends GridFsStream
...
@@ -45,7 +45,6 @@ class GridFsDownload extends GridFsStream
{
{
$this
->
file
=
$bucket
->
getFilesCollection
()
->
findOne
([
'_id'
=>
$objectId
]);
$this
->
file
=
$bucket
->
getFilesCollection
()
->
findOne
([
'_id'
=>
$objectId
]);
if
(
is_null
(
$this
->
file
))
{
if
(
is_null
(
$this
->
file
))
{
//MUST RAISE AN ERROR ! (WHICH ONE I DON'T)
throw
new
\MongoDB\Exception\GridFSFileNotFoundException
(
$objectId
,
$bucket
->
getBucketName
(),
$bucket
->
getDatabaseName
());
throw
new
\MongoDB\Exception\GridFSFileNotFoundException
(
$objectId
,
$bucket
->
getBucketName
(),
$bucket
->
getDatabaseName
());
}
}
if
(
$this
->
file
->
length
>
0
)
{
if
(
$this
->
file
->
length
>
0
)
{
...
...
tests/GridFS/SpecificationTests.php
View file @
8f2aaeab
...
@@ -75,12 +75,12 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -75,12 +75,12 @@ class SpecificationTests extends FunctionalTestCase
$test
[
'assert'
][
'result'
]
=
$result
;
$test
[
'assert'
][
'result'
]
=
$result
;
}
}
if
(
$testResult
==
"void"
)
{
if
(
$testResult
==
"void"
)
{
$
test
[
'assert'
]
[
'result'
]
=
null
;
$
fixedAssert
[
'result'
]
=
null
;
}
}
$this
->
assertEquals
(
$result
,
$fixedAssert
[
'result'
]);
$this
->
assertEquals
(
$result
,
$fixedAssert
[
'result'
]);
}
}
if
(
isset
(
$test
[
'assert'
][
'data'
]))
{
if
(
isset
(
$test
[
'assert'
][
'data'
]))
{
$this
->
runCommands
(
$fixedAssert
,
$result
);
$this
->
runCommands
(
$fixedAssert
[
'data'
]
,
$result
);
$this
->
collectionsEqual
(
$this
->
collections
[
'expected.files'
],
$this
->
bucket
->
getFilesCollection
());
$this
->
collectionsEqual
(
$this
->
collections
[
'expected.files'
],
$this
->
bucket
->
getFilesCollection
());
if
(
isset
(
$this
->
collections
[
'expected.chunks'
]))
{
if
(
isset
(
$this
->
collections
[
'expected.chunks'
]))
{
$this
->
collectionsEqual
(
$this
->
collections
[
'expected.chunks'
],
$this
->
bucket
->
getChunksCollection
());
$this
->
collectionsEqual
(
$this
->
collections
[
'expected.chunks'
],
$this
->
bucket
->
getChunksCollection
());
...
@@ -91,7 +91,7 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -91,7 +91,7 @@ class SpecificationTests extends FunctionalTestCase
public
function
provideSpecificationTests
()
public
function
provideSpecificationTests
()
{
{
$testPath
=
getcwd
()
.
'/tests/GridFS/Specification/tests/d
ownload
.json'
;
$testPath
=
getcwd
()
.
'/tests/GridFS/Specification/tests/d
elete
.json'
;
$testArgs
=
[];
$testArgs
=
[];
foreach
(
glob
(
$testPath
)
as
$filename
)
{
foreach
(
glob
(
$testPath
)
as
$filename
)
{
...
@@ -148,22 +148,22 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -148,22 +148,22 @@ class SpecificationTests extends FunctionalTestCase
public
function
runCommands
(
$cmds
,
$result
)
public
function
runCommands
(
$cmds
,
$result
)
{
{
$cmds
=
$this
->
fixTypes
(
$cmds
,
true
);
foreach
(
$cmds
as
$cmd
){
foreach
(
$cmds
as
$cmd
)
{
foreach
(
$cmd
as
$key
=>
$value
)
{
foreach
(
$cmd
as
$key
=>
$value
)
{
if
(
isset
(
$this
->
commands
[
$key
]))
{
if
(
isset
(
$this
->
commands
[
$key
]))
{
$cmdName
=
$key
;
$cmdName
=
$key
;
$collectionName
=
$value
;
$collectionName
=
$value
;
if
(
isset
(
$cmd
[
'documents'
])){
foreach
(
$cmd
[
'documents'
]
as
$docIndex
=>
$doc
)
{
foreach
(
$cmd
[
'documents'
]
as
$docIndex
=>
$doc
)
{
foreach
(
$doc
as
$docKey
=>
$docVal
){
foreach
(
$doc
as
$docKey
=>
$docVal
){
if
(
is_string
(
$docVal
))
{
if
(
is_string
(
$docVal
))
{
if
(
$docVal
==
'*result'
)
{
if
(
$docVal
==
'*result'
)
{
$doc
[
$docKey
]
=
$result
;
$doc
[
$docKey
]
=
$result
;
}
}
}
}
}
$cmd
[
'documents'
][
$docIndex
]
=
$doc
;
}
}
$cmd
[
'documents'
][
$docIndex
]
=
$doc
;
}
}
$collection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
$collectionName
));
$collection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
$collectionName
));
$this
->
commands
[
$key
](
$collection
,
$this
->
fixTypes
(
$cmd
,
true
));
$this
->
commands
[
$key
](
$collection
,
$this
->
fixTypes
(
$cmd
,
true
));
...
@@ -171,6 +171,7 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -171,6 +171,7 @@ class SpecificationTests extends FunctionalTestCase
}
}
}
}
}
}
}
}
public
function
initializeDatabases
(
$data
,
$test
)
public
function
initializeDatabases
(
$data
,
$test
)
...
@@ -186,12 +187,15 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -186,12 +187,15 @@ class SpecificationTests extends FunctionalTestCase
$filesCollection
->
insertMany
(
$data
[
'files'
]);
$filesCollection
->
insertMany
(
$data
[
'files'
]);
$expectedFilesCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"expected.files"
));
$expectedFilesCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"expected.files"
));
$expectedFilesCollection
->
insertMany
(
$data
[
'files'
]);
$expectedFilesCollection
->
insertMany
(
$data
[
'files'
]);
$this
->
collections
[
'expected.files'
]
=
$expectedFilesCollection
;
}
}
if
(
isset
(
$data
[
'chunks'
])
&&
count
(
$data
[
'chunks'
])
>
0
)
{
if
(
isset
(
$data
[
'chunks'
])
&&
count
(
$data
[
'chunks'
])
>
0
)
{
$chunksCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"fs.chunks"
));
$chunksCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"fs.chunks"
));
$chunksCollection
->
insertMany
(
$data
[
'chunks'
]);
$chunksCollection
->
insertMany
(
$data
[
'chunks'
]);
$expectedChunksCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"expected.chunks"
));
$expectedChunksCollection
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
"expected.chunks"
));
$expectedChunksCollection
->
insertMany
(
$data
[
'chunks'
]);
$expectedChunksCollection
->
insertMany
(
$data
[
'chunks'
]);
$this
->
collections
[
'expected.chunks'
]
=
$expectedChunksCollection
;
}
}
if
(
isset
(
$test
[
'arrange'
]))
{
if
(
isset
(
$test
[
'arrange'
]))
{
foreach
(
$test
[
'arrange'
][
'data'
]
as
$cmd
)
{
foreach
(
$test
[
'arrange'
][
'data'
]
as
$cmd
)
{
...
@@ -203,9 +207,7 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -203,9 +207,7 @@ class SpecificationTests extends FunctionalTestCase
}
}
}
}
}
}
}
}
public
function
uploadCommand
(
$args
)
public
function
uploadCommand
(
$args
)
{
{
$args
=
$this
->
fixTypes
(
$args
,
false
);
$args
=
$this
->
fixTypes
(
$args
,
false
);
...
@@ -230,7 +232,8 @@ class SpecificationTests extends FunctionalTestCase
...
@@ -230,7 +232,8 @@ class SpecificationTests extends FunctionalTestCase
}
}
function
deleteCommand
(
$args
)
function
deleteCommand
(
$args
)
{
{
$args
=
$this
->
fixTypes
(
$args
,
false
);
$this
->
bucketReadWriter
->
delete
(
$args
[
'id'
]);
}
}
function
download_by_nameCommand
(
$args
)
function
download_by_nameCommand
(
$args
)
{
{
...
...
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