0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00

support creating GridFS files from buffers in addition to files. SERVER-312

This commit is contained in:
Mathias Stearn 2009-10-16 11:23:09 -04:00
parent 166c722630
commit 6880a8788d
2 changed files with 43 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#include "../stdafx.h"
#include <fcntl.h>
#include <utility>
#include "gridfs.h"
@ -43,6 +44,28 @@ namespace mongo {
}
BSONObj GridFS::storeFile( const char* data , size_t length , const string& remoteName , const string& contentType){
massert("large files not yet implemented", length <= 0xffffffff);
char const * const end = data + length;
OID id;
id.init();
BSONObj idObj = BSON("_id" << id);
int chunkNumber = 0;
while (data < end){
int chunkLen = MIN(DEFAULT_CHUNK_SIZE, end-data);
Chunk c(idObj, chunkNumber, data, chunkLen);
_client.insert( _chunksNS.c_str() , c._data );
chunkNumber++;
data += chunkLen;
}
return insertFile(remoteName, id, length, contentType);
}
BSONObj GridFS::storeFile( const string& fileName , const string& remoteName , const string& contentType){
uassert( "file doesn't exist" , fileName == "-" || boost::filesystem::exists( fileName ) );
@ -83,13 +106,18 @@ namespace mongo {
massert("large files not yet implemented", length <= 0xffffffff);
return insertFile((remoteName.empty() ? fileName : remoteName), id, length, contentType);
}
BSONObj GridFS::insertFile(const string& name, const OID& id, unsigned length, const string& contentType){
BSONObj res;
if ( ! _client.runCommand( _dbName.c_str() , BSON( "filemd5" << id << "root" << _prefix ) , res ) )
throw UserException( "filemd5 failed" );
BSONObjBuilder file;
file << "_id" << id
<< "filename" << (remoteName.empty() ? fileName : remoteName)
<< "filename" << name
<< "length" << (unsigned) length
<< "chunkSize" << DEFAULT_CHUNK_SIZE
<< "uploadDate" << DATENOW

View File

@ -63,6 +63,17 @@ namespace mongo {
*/
BSONObj storeFile( const string& fileName , const string& remoteName="" , const string& contentType="");
/**
* puts the file represented by data into the db
* @param data pointer to buffer to store in GridFS
* @param length length of buffer
* @param remoteName optional filename to use for file stored in GridFS
* (default is to use fileName parameter)
* @param contentType optional MIME type for this object.
* (default is to omit)
* @return the file object
*/
BSONObj storeFile( const char* data , size_t length , const string& remoteName , const string& contentType);
/**
* removes file referenced by fileName from the db
* @param fileName filename (in GridFS) of the file to remove
@ -97,6 +108,9 @@ namespace mongo {
string _filesNS;
string _chunksNS;
// insert fileobject. All chunks must be in DB.
BSONObj insertFile(const string& name, const OID& id, unsigned length, const string& contentType);
friend class GridFile;
};