/* * success,err = gridfile:write(filename) */ static int gridfile_write(lua_State *L) { GridFile *gridfile = userdata_to_gridfile(L, 1); const char *where = luaL_checkstring(L, 2); try { gridfile->write(lua_tostring(L, 2)); } catch (std::exception &e) { lua_pushboolean(L, 0); lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILE, "write", e.what()); return 2; } lua_pushboolean(L, 1); return 1; }
/* * string = gridfile:data() */ static int gridfile_data(lua_State *L) { GridFile *gridfile = userdata_to_gridfile(L, 1); std::stringstream data(std::stringstream::out | std::stringstream::binary); try { gridfile->write(data); } catch (std::exception &e) { lua_pushboolean(L, 0); lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILE, "data", e.what()); return 2; } std::string data_str = data.str(); lua_pushlstring (L, data_str.c_str(), data_str.length()); return 1; }
BOOL CMongoDB::GetFileFromMongoDB(const string& strFileName, const string&strSavePath, string& strErr) { BOOL bRet = TRUE; string strTemp, strTempPath; int nStart(0); if (strFileName.length() <= 0) strErr = "FileName is empty!"; gridfs_offset size(0), afsize(0); string strPath(strSavePath); if (connect.isStillConnected() && m_bConnect) { GridFS fs(connect, m_strDBName); auto pos = strFileName.find(";", nStart); while ((pos > 0) && (pos != strFileName.npos)) { strTemp = strFileName.substr(nStart, pos - nStart); nStart = pos + 1; if (strTemp.length() > 0) { GridFile file = fs.findFileByName(strTemp); if (file.exists()) { strPath = strSavePath; strPath.append("\\"); strPath.append(strTemp); size = file.getContentLength(); afsize = file.write(strPath); } else strErr = "File not exists!"; } if (size > 0 && size == afsize) bRet = bRet ? TRUE : FALSE; else bRet = FALSE; pos = strFileName.find(";", nStart); } } return bRet; }
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; }
int run() { if (mongoFilesGlobalParams.command.size() == 0) { cerr << "ERROR: need command" << endl << endl; printHelp(cout); return -1; } GridFS g(conn(), toolGlobalParams.db); if (mongoFilesGlobalParams.command == "list") { BSONObjBuilder b; if (mongoFilesGlobalParams.gridFSFilename.size()) { b.appendRegex( "filename" , (string)"^" + pcrecpp::RE::QuoteMeta(mongoFilesGlobalParams.gridFSFilename) ); } display( &g , b.obj() ); return 0; } if (mongoFilesGlobalParams.gridFSFilename.size() == 0) { cerr << "ERROR: need a filename" << endl << endl; printHelp(cout); return -1; } if (mongoFilesGlobalParams.command == "search") { BSONObjBuilder b; b.appendRegex("filename", mongoFilesGlobalParams.gridFSFilename); display( &g , b.obj() ); return 0; } if (mongoFilesGlobalParams.command == "get") { GridFile f = g.findFile(mongoFilesGlobalParams.gridFSFilename); if ( ! f.exists() ) { cerr << "ERROR: file not found" << endl; return -2; } f.write(mongoFilesGlobalParams.localFile); if (mongoFilesGlobalParams.localFile != "-") { toolInfoOutput() << "done write to: " << mongoFilesGlobalParams.localFile << std::endl; } return 0; } if (mongoFilesGlobalParams.command == "put") { BSONObj file = g.storeFile(mongoFilesGlobalParams.localFile, mongoFilesGlobalParams.gridFSFilename, mongoFilesGlobalParams.contentType); toolInfoOutput() << "added file: " << file << std::endl; if (mongoFilesGlobalParams.replace) { auto_ptr<DBClientCursor> cursor = conn().query(toolGlobalParams.db + ".fs.files", BSON("filename" << mongoFilesGlobalParams.gridFSFilename << "_id" << NE << file["_id"] )); while (cursor->more()) { BSONObj o = cursor->nextSafe(); conn().remove(toolGlobalParams.db + ".fs.files", BSON("_id" << o["_id"])); conn().remove(toolGlobalParams.db + ".fs.chunks", BSON("_id" << o["_id"])); toolInfoOutput() << "removed file: " << o << std::endl; } } conn().getLastError(); toolInfoOutput() << "done!" << std::endl; return 0; } if (mongoFilesGlobalParams.command == "delete") { g.removeFile(mongoFilesGlobalParams.gridFSFilename); conn().getLastError(); toolInfoOutput() << "done!" << std::endl; return 0; } cerr << "ERROR: unknown command '" << mongoFilesGlobalParams.command << "'" << endl << endl; printHelp(cout); return -1; }