Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add GridFS:store_data and GridFile:data

Contributed by Paulo Pizarro


git-svn-id: http://luamongo.googlecode.com/svn/trunk@64 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information...
commit 700bb9c6a7a450bf8648978974e643bcdb41ff94 1 parent ed332df
neomantra authored
Showing with 50 additions and 0 deletions.
  1. +22 −0 mongo_gridfile.cpp
  2. +28 −0 mongo_gridfs.cpp
View
22 mongo_gridfile.cpp
@@ -195,6 +195,27 @@ static int gridfile_write(lua_State *L) {
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;
+}
+
/*
* __gc
*/
@@ -230,6 +251,7 @@ int mongo_gridfile_register(lua_State *L) {
{"num_chunks", gridfile_num_chunks},
{"upload_date", gridfile_upload_date},
{"write", gridfile_write},
+ {"data", gridfile_data},
{NULL, NULL}
};
View
28 mongo_gridfs.cpp
@@ -151,6 +151,33 @@ static int gridfs_store_file(lua_State *L) {
/*
+ * gridfile, err = gridfs:store_data(data[, remote_file], content_type]])
+ * puts the file represented by data into the db
+ */
+static int gridfs_store_data(lua_State *L) {
+ int resultcount = 1;
+
+ GridFS *gridfs = userdata_to_gridfs(L, 1);
+
+ size_t length = 0;
+ const char *data = luaL_checklstring(L, 2, &length);
+ const char *remote = luaL_optstring(L, 3, "");
+ const char *content_type = luaL_optstring(L, 4, "");
+
+ try {
+ BSONObj res = gridfs->storeFile(data, length, remote, content_type);
+ bson_to_lua(L, res);
+ } catch (std::exception &e) {
+ lua_pushnil(L);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "store_data", e.what());
+ resultcount = 2;
+ }
+
+ return resultcount;
+}
+
+
+/*
* __gc
*/
static int gridfs_gc(lua_State *L) {
@@ -178,6 +205,7 @@ int mongo_gridfs_register(lua_State *L) {
{"list", gridfs_list},
{"remove_file", gridfs_remove_file},
{"store_file", gridfs_store_file},
+ {"store_data", gridfs_store_data},
{NULL, NULL}
};
Please sign in to comment.
Something went wrong with that request. Please try again.