Commits

Aristarkh Zagorodnikov committed f585eb1

Added support for custom filename field (default is "filename") via GridFSFilenameField directive, designed and implemented by ggrundik (https://bitbucket.org/ggrundik)

Comments (0)

Files changed (3)

 Contributors:
 Stafford Brunk (https://bitbucket.org/wingrunr21)
 Radoslav Hampartsumyan (https://bitbucket.org/rado_h)
+ggrundik (https://bitbucket.org/ggrundik)
 Ubuntu pre-build instructions:
 sudo apt-get -y install wget g++ scons apache2 apache2-threaded-dev libboost-filesystem-dev libboost-system-dev libboost-thread-dev
 
-Ubuntu build instructions (MongoDB C++ driver v2.2.2):
+Ubuntu build instructions (MongoDB C++ driver v2.2.3):
 wget --no-check-certificate https://bitbucket.org/onyxmaster/mod_gridfs/get/release.tar.gz
 tar xzf release.tar.gz
 rm release.tar.gz
 mv onyxmaster-mod_gridfs-* mod_gridfs
 cd mod_gridfs
-wget http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-2.2.2.tgz
-tar xzf mongodb-linux-x86_64-2.2.2.tgz
+wget http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-2.2.3.tgz
+tar xzf mongodb-linux-x86_64-2.2.3.tgz
 rm mongodb-linux-x86_64-2.2.2.tgz
 mv mongo-cxx-driver-v2.2 mongo-cxx-driver
 echo 'env.Append(CCFLAGS="-fPIC")' >> mongo-cxx-driver/SConstruct
 GridFSConnection -- sets connection string ("host[:port]" for single hosts, "replicaSetName/host[:port],[host[:port], ...]" for replica sets)
 GridFSDatabase <database> -- sets database name
 GridFSCollectionPrefix <collection prefix> -- optional, sets collection prefix (default is "fs")
+GridFSFilenameField <filename field> -- optional, sets filename field (default is "filename")
 GridFSCacheMaxAge <max age> -- optional, sets cache max age in seconds (default is 1 week, maximum is 10 years), set to 0 to disable expiration caching (see Notes below)
 GridFSConnectTimeout <timeout> -- optional, sets MongoDB connection timeout in seconds (default is 5 seconds)
 GridFSReadPrefMode <mode> -- optional, sets MongoDB read preference mode (MongoDB v2.2+, see http://docs.mongodb.org/manual/applications/replication/#read-preference-modes)
 Written by Aristarkh Zagorodnikov
 Initial context (location) awareness suggestion and implementation by Stafford Brunk (https://bitbucket.org/wingrunr21)
 Authentication support implementation by Radoslav Hampartsumyan (https://bitbucket.org/rado_h)
+Custom filename field by ggrundik (https://bitbucket.org/ggrundik)

gridfs/mod_gridfs.cpp

 //	Default prefix
 static const std::string DEFAULT_COLLECTION_PREFIX = "fs";
 
+//	Default filename field
+static const std::string DEFAULT_FILENAME_FIELD = "filename";
+
+
 //	Module configuration
 struct gridfs_config
 {
 	const std::string *connection_string;
 	const std::string *database;
 	const std::string *collection_prefix;
+	const std::string *filename_field;
 	const std::string *username;
 	const std::string *password;
 	int cache_max_age;
 	config->connection_string = add->connection_string != 0 ? add->connection_string : base->connection_string;
 	config->database = add->database != 0 ? add->database : base->database;
 	config->collection_prefix = add->collection_prefix != 0 ? add->collection_prefix : base->collection_prefix;
+	config->filename_field = add->filename_field != 0 ? add->filename_field : base->filename_field;
 	config->username = add->username != 0 ? add->username : base->username;
 	config->password = add->password != 0 ? add->password : base->password;
 	config->cache_max_age = add->cache_max_age_set ? add->cache_max_age : base->cache_max_age;
 	return 0;
 }
 
+//	Handles "GridFSFilenameField <filename field>" command
+static const char *gridfs_filename_field_command(cmd_parms *const command, void *const module_config, const char *const argument)
+{
+	gridfs_config *const config = static_cast<gridfs_config *>(module_config);
+	void *const filename_field_data = apr_palloc(command->pool, sizeof(std::string));
+	if (filename_field_data == 0)
+		return "GridFSFilenameField failed to allocate data.";
+	std::string *filename_field;
+	try
+	{
+		filename_field = new (filename_field_data) std::string(argument);
+	}
+	catch (...)
+	{
+		return "GridFSFilenameField exception.";
+	}
+	config->filename_field = filename_field;
+	return 0;
+}
+
 //	Handles "GridFSUsername <username>" command
 static const char *gridfs_username_command(cmd_parms *const command, void *const module_config, const char *const argument)
 {
 				}
 
 				const std::string& collection_prefix = config->collection_prefix != 0 ? *config->collection_prefix : DEFAULT_COLLECTION_PREFIX;
+				const std::string& filename_field = config->filename_field != 0 ? *config->filename_field : DEFAULT_FILENAME_FIELD;
 				const mongo::GridFS gridfs(client, database, collection_prefix, false);
+				const mongo::BSONObj query = BSON(filename_field << filename);
 				while (true)
 				{
 					try
 					{
 						mongo::GridFile gridfile = custom_read_pref_mode ?
-							gridfs.findFile(filename, *config->read_pref_mode) :
-							gridfs.findFile(filename);
+							gridfs.findFile(query, *config->read_pref_mode) :
+							gridfs.findFile(query);
 						if (!gridfile.exists())
 						{
 							if (custom_read_pref_mode)
 	AP_INIT_TAKE1("GridFSConnection", reinterpret_cast<cmd_func>(gridfs_connection_command), 0, OR_FILEINFO, "GridFS connection string."),
 	AP_INIT_TAKE1("GridFSDatabase", reinterpret_cast<cmd_func>(gridfs_database_command), 0, OR_FILEINFO, "GridFS database name."),
 	AP_INIT_TAKE1("GridFSCollectionPrefix", reinterpret_cast<cmd_func>(gridfs_collection_prefix_command), 0, OR_FILEINFO, "GridFS collection prefix."),
+	AP_INIT_TAKE1("GridFSFilenameField", reinterpret_cast<cmd_func>(gridfs_filename_field_command), 0, OR_FILEINFO, "GridFS file name field."),
 	AP_INIT_TAKE1("GridFSUsername", reinterpret_cast<cmd_func>(gridfs_username_command), 0, OR_FILEINFO, "GridFS database authentication username."),
 	AP_INIT_TAKE1("GridFSPassword", reinterpret_cast<cmd_func>(gridfs_password_command), 0, OR_FILEINFO, "GridFS database authentication password."),	
 	AP_INIT_TAKE1("GridFSCacheMaxAge", reinterpret_cast<cmd_func>(gridfs_cache_max_age_command), 0, OR_FILEINFO, "GridFS cache max age (seconds, 0 to disable expiration)."),