/* * bson, err = gridfs:store_file(filename[, remote_file[, content_type]]) */ static int gridfs_store_file(lua_State *L) { int resultcount = 1; GridFS *gridfs = userdata_to_gridfs(L, 1); const char *filename = luaL_checkstring(L, 2); const char *remote = luaL_optstring(L, 3, ""); const char *content_type = luaL_optstring(L, 4, ""); try { BSONObj res = gridfs->storeFile(filename, remote, content_type); bson_to_lua(L, res); } catch (std::exception &e) { lua_pushnil(L); lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "store_file", e.what()); resultcount = 2; } return resultcount; }
int main() { try { DBClientBase *conn = NULL; string err_msg; ConnectionString cs = ConnectionString::parse("localhost", err_msg); if (!cs.isValid()) { throw "bad: " + err_msg; } try { conn = cs.connect(err_msg); } catch (DBException &e) { cout << "caught " << err_msg << endl; return 1; } if (!conn){ cout<<"Unable to connect to DB"<<endl; return 1; } // BSONObjBuilder b; // b.append("name", "Joe"); // b.append("age", 33); // BSONObj p = b.obj(); // // conn->insert("db.coll",p,0); GridFS * fs; fs = new GridFS(*conn,"test"); BSONObj p = fs->storeFile("pic.JPG"); conn->insert("db.coll",p,0); } catch( DBException &e ) { cout << "caught " << e.what() << endl; } return 0; }