4
* Copyright (C) 2008 10gen Inc.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License, version 3,
8
* as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Affero General Public License for more details.
15
* You should have received a copy of the GNU Affero General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "client/gridfs.h"
21
#include "client/dbclient.h"
28
#include <boost/program_options.hpp>
30
using namespace mongo;
32
namespace po = boost::program_options;
34
class Files : public Tool {
36
Files() : Tool( "files" ){
38
( "local,l", po::value<string>(), "local filename for put|get (default is to use the same name as 'gridfs filename')")
39
( "type,t", po::value<string>(), "MIME type for put (default is to omit)")
40
( "replace,r", "Remove other files with same name after PUT")
43
( "command" , po::value<string>() , "command (list|search|put|get)" )
44
( "file" , po::value<string>() , "filename for get|put" )
46
addPositionArg( "command" , 1 );
47
addPositionArg( "file" , 2 );
50
virtual void printExtraHelp( ostream & out ){
51
out << "usage: " << _name << " [options] command [gridfs filename]" << endl;
52
out << "command:" << endl;
53
out << " one of (list|search|put|get)" << endl;
54
out << " list - list all files. 'gridfs filename' is an optional prefix " << endl;
55
out << " which listed filenames must begin with." << endl;
56
out << " search - search all files. 'gridfs filename' is a substring " << endl;
57
out << " which listed filenames must contain." << endl;
58
out << " put - add a file with filename 'gridfs filename'" << endl;
59
out << " get - get a file with filename 'gridfs filename'" << endl;
60
out << " delete - delete all files with filename 'gridfs filename'" << endl;
63
void display( GridFS * grid , BSONObj obj ){
64
auto_ptr<DBClientCursor> c = grid->list( obj );
66
BSONObj obj = c->next();
68
<< obj["filename"].str() << "\t"
69
<< (long)obj["length"].number()
75
string cmd = getParam( "command" );
76
if ( cmd.size() == 0 ){
77
cerr << "ERROR: need command" << endl << endl;
82
GridFS g( conn() , _db );
85
string filename = getParam( "file" );
89
if ( filename.size() )
90
b.appendRegex( "filename" , ( (string)"^" + filename ).c_str() );
91
display( &g , b.obj() );
95
if ( filename.size() == 0 ){
96
cerr << "ERROR: need a filename" << endl << endl;
101
if ( cmd == "search" ){
103
b.appendRegex( "filename" , filename.c_str() );
104
display( &g , b.obj() );
109
GridFile f = g.findFile( filename );
111
cerr << "ERROR: file not found" << endl;
115
string out = getParam("local", f.getFilename());
119
cout << "done write to: " << out << endl;
125
const string& infile = getParam("local", filename);
126
const string& type = getParam("type", "");
128
BSONObj file = g.storeFile(infile, filename, type);
129
cout << "added file: " << file << endl;
131
if (hasParam("replace")){
132
auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] ));
133
while (cursor->more()){
134
BSONObj o = cursor->nextSafe();
135
conn().remove(_db+".fs.files", BSON("_id" << o["_id"]));
136
conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"]));
137
cout << "removed file: " << o << endl;
146
if ( cmd == "delete" ){
147
g.removeFile(filename);
152
cerr << "ERROR: unknown command '" << cmd << "'" << endl << endl;
158
int main( int argc , char ** argv ) {
160
return f.main( argc , argv );