/* * filename_str = gridfile:filename() */ static int gridfile_filename(lua_State *L) { GridFile *gridfile = userdata_to_gridfile(L, 1); lua_pushstring(L, gridfile->getFilename().c_str()); return 1; }
int run(){ string cmd = getParam( "command" ); if ( cmd.size() == 0 ){ cerr << "ERROR: need command" << endl << endl; printHelp(cout); return -1; } GridFS g( conn() , _db ); auth(); string filename = getParam( "file" ); if ( cmd == "list" ){ BSONObjBuilder b; if ( filename.size() ) b.appendRegex( "filename" , ( (string)"^" + filename ).c_str() ); display( &g , b.obj() ); return 0; } if ( filename.size() == 0 ){ cerr << "ERROR: need a filename" << endl << endl; printHelp(cout); return -1; } if ( cmd == "search" ){ BSONObjBuilder b; b.appendRegex( "filename" , filename.c_str() ); display( &g , b.obj() ); return 0; } if ( cmd == "get" ){ GridFile f = g.findFile( filename ); if ( ! f.exists() ){ cerr << "ERROR: file not found" << endl; return -2; } string out = getParam("local", f.getFilename()); f.write( out ); if (out != "-") cout << "done write to: " << out << endl; return 0; } if ( cmd == "put" ){ const string& infile = getParam("local", filename); const string& type = getParam("type", ""); BSONObj file = g.storeFile(infile, filename, type); cout << "added file: " << file << endl; if (hasParam("replace")){ auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] )); while (cursor->more()){ BSONObj o = cursor->nextSafe(); conn().remove(_db+".fs.files", BSON("_id" << o["_id"])); conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"])); cout << "removed file: " << o << endl; } } conn().getLastError(); cout << "done!"; return 0; } if ( cmd == "delete" ){ g.removeFile(filename); conn().getLastError(); cout << "done!"; return 0; } cerr << "ERROR: unknown command '" << cmd << "'" << endl << endl; printHelp(cout); return -1; }