func getMongoFileContent(ctx *goproxy.ProxyCtx, db mgo.Database, objId bson.ObjectId) (file *mgo.GridFile, err error) { ctx.Logf("db: %+v", db) file, err = db.GridFS("fs").OpenId(objId) if err != nil { return file, err if err == mgo.ErrNotFound { } } //defer file.Close() return file, err }
// Store file in MongoDB GridFS func saveFileToMongo(db mgo.Database, objId bson.ObjectId, contentType string, openFile io.Reader, fileName string, ctx *goproxy.ProxyCtx) { ctx.Logf("db: %+v", db) mdbfile, err := db.GridFS("fs").Create(fileName) if err == nil { mdbfile.SetContentType(contentType) mdbfile.SetId(objId) ctx.Logf("Copying to: %s", fileName) _, err = io.Copy(mdbfile, openFile) if err != nil { ctx.Logf("Unable to copy to mongo: %s - %v", fileName, err) } ctx.Logf("Done copying, closing") err = mdbfile.Close() if err != nil { ctx.Logf("Unable to close copy to mongo") } ctx.Logf("MongoDB body file saved") } }