Skip to content

Commit

Permalink
Merge pull request #558 from iceljc/bugfix/get-mongo-db-name
Browse files Browse the repository at this point in the history
Bugfix/get mongo db name
  • Loading branch information
Oceania2018 authored Jul 22, 2024
2 parents 5637a41 + bf253a8 commit 46d537e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public DbConnectionSetting()
{
Slavers = new string[0];
}
}
}
25 changes: 21 additions & 4 deletions src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Web;

namespace BotSharp.Plugin.MongoStorage;

public class MongoDbContext
Expand All @@ -6,6 +8,8 @@ public class MongoDbContext
private readonly string _mongoDbDatabaseName;
private readonly string _collectionPrefix;

private const string DB_NAME_INDEX = "authSource";

public MongoDbContext(BotSharpDatabaseSettings dbSettings)
{
var mongoDbConnectionString = dbSettings.BotSharpMongoDb;
Expand All @@ -16,14 +20,27 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings)

private string GetDatabaseName(string mongoDbConnectionString)
{
var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
if (databaseName.Contains("?"))
var dbName = string.Empty;
if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn))
{
databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase));
return dbName;
}
return databaseName;

var query = HttpUtility.ParseQueryString(conn.Query);
var keys = query.AllKeys ?? [];

if (keys.Contains(DB_NAME_INDEX))
{
dbName = query[DB_NAME_INDEX];
}
else
{
dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty;
}
return dbName;
}


private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }

#region Indexes
Expand Down

0 comments on commit 46d537e

Please sign in to comment.