Skip to content

Commit

Permalink
move databases to the user profile config folders
Browse files Browse the repository at this point in the history
  • Loading branch information
13xforever committed Mar 6, 2019
1 parent 26b58ae commit 6eebd91
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 39 deletions.
3 changes: 3 additions & 0 deletions CompatBot/Commands/Psn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public static async Task SearchForGame(CommandContext ctx, [RemainingText] strin
Description = $"Rating: {score}",
ThumbnailUrl = thumb.url,
};
#if DEBUG
result.WithFooter("Test instance");
#endif
hasResults = true;
await ctx.RespondAsync(embed: result).ConfigureAwait(false);
}
Expand Down
6 changes: 4 additions & 2 deletions CompatBot/Database/BotDb.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using CompatApiClient;
using Microsoft.EntityFrameworkCore;
Expand All @@ -19,7 +20,8 @@ internal class BotDb: DbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=bot.db");
var dbPath = DbImporter.GetDbPath("bot.db", Environment.SpecialFolder.ApplicationData);
optionsBuilder.UseSqlite($"Data Source=\"{dbPath}\"");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
44 changes: 44 additions & 0 deletions CompatBot/Database/DbImporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Database.Migrations;
Expand Down Expand Up @@ -106,5 +107,48 @@ await db.ExecuteSqlCommandAsync(@"CREATE TABLE `temp_new_explanation` (
}
}
}

internal static string GetDbPath(string dbName, Environment.SpecialFolder desiredFolder)
{
var settingsFolder = Path.Combine(Environment.GetFolderPath(desiredFolder), "compat-bot");
try
{
if (!Directory.Exists(settingsFolder))
Directory.CreateDirectory(settingsFolder);
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to create settings folder " + settingsFolder);
settingsFolder = "";
}

var dbPath = Path.Combine(settingsFolder, dbName);
if (settingsFolder != "")
try
{
if (File.Exists(dbName))
{
Config.Log.Info($"Found local {dbName}, moving...");
if (File.Exists(dbPath))
{
Config.Log.Error($"{dbPath} already exists, please reslove the conflict manually");
throw new InvalidOperationException($"Failed to move local {dbName} to {dbPath}");
}
else
{
var dbFiles = Directory.GetFiles(".", Path.GetFileNameWithoutExtension(dbName) + ".*");
foreach (var file in dbFiles)
File.Move(file, Path.Combine(settingsFolder, Path.GetFileName(file)));
Config.Log.Info($"Using {dbPath}");
}
}
}
catch (Exception e)
{
Config.Log.Error(e, $"Failed to move local {dbName} to {dbPath}");
throw e;
}
return dbPath;
}
}
}
2 changes: 0 additions & 2 deletions CompatBot/Database/Providers/ScrapeStateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static bool IsFresh(string locale, string containerId = null)
return false;
}


public static bool IsFresh(string locale, DateTime dataTimestamp)
{
using (var db = new ThumbnailDb())
Expand All @@ -43,7 +42,6 @@ public static bool IsFresh(string locale, DateTime dataTimestamp)
return false;
}


public static async Task SetLastRunTimestampAsync(string locale, string containerId = null)
{
if (string.IsNullOrEmpty(locale))
Expand Down
45 changes: 12 additions & 33 deletions CompatBot/Database/Providers/ThumbnailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static async Task<string> GetThumbnailUrlAsync(this DiscordClient client,

using (var db = new ThumbnailDb())
{
var thumb = await db.Thumbnail.FirstOrDefaultAsync(t => t.ProductCode == productCode.ToUpperInvariant()).ConfigureAwait(false);
var thumb = await db.Thumbnail.FirstOrDefaultAsync(t => t.ProductCode == productCode).ConfigureAwait(false);
//todo: add search task if not found
if (thumb?.EmbeddableUrl is string embeddableUrl && !string.IsNullOrEmpty(embeddableUrl))
return embeddableUrl;
Expand All @@ -48,35 +48,14 @@ public static async Task<string> GetThumbnailUrlAsync(this DiscordClient client,

if (thumb?.Url is string url && !string.IsNullOrEmpty(url))
{
if (!string.IsNullOrEmpty(Path.GetExtension(url)))
{
thumb.EmbeddableUrl = url;
await db.SaveChangesAsync().ConfigureAwait(false);
return url;
}
var contentName = (thumb.ContentId ?? thumb.ProductCode);
var embed = await GetEmbeddableUrlAsync(client, contentName, url).ConfigureAwait(false);

try
if (embed.url != null)
{
using (var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false))
using (var memStream = new MemoryStream())
{
await imgStream.CopyToAsync(memStream).ConfigureAwait(false);
// minimum jpg size is 119 bytes, png is 67 bytes
if (memStream.Length < 64)
return null;
memStream.Seek(0, SeekOrigin.Begin);
var spam = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);
//var message = await spam.SendFileAsync(memStream, (thumb.ContentId ?? thumb.ProductCode) + ".jpg").ConfigureAwait(false);
var contentName = (thumb.ContentId ?? thumb.ProductCode);
var message = await spam.SendFileAsync(contentName + ".jpg", memStream, contentName).ConfigureAwait(false);
thumb.EmbeddableUrl = message.Attachments.First().Url;
await db.SaveChangesAsync().ConfigureAwait(false);
return thumb.EmbeddableUrl;
}
}
catch (Exception e)
{
Config.Log.Warn(e);
thumb.EmbeddableUrl = embed.url;
await db.SaveChangesAsync().ConfigureAwait(false);
return embed.url;
}
}
}
Expand Down Expand Up @@ -130,6 +109,9 @@ await db.Thumbnail.AddAsync(new Thumbnail
{
try
{
if (!string.IsNullOrEmpty(Path.GetExtension(url)))
return (url, null);

using (var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false))
using (var memStream = new MemoryStream())
{
Expand All @@ -140,11 +122,8 @@ await db.Thumbnail.AddAsync(new Thumbnail

memStream.Seek(0, SeekOrigin.Begin);
var spam = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);
if (string.IsNullOrEmpty(Path.GetExtension(url)))
{
var message = await spam.SendFileAsync(contentId + ".jpg", memStream, contentId).ConfigureAwait(false);
url = message.Attachments.First().Url;
}
var message = await spam.SendFileAsync(contentId + ".jpg", memStream, contentId).ConfigureAwait(false);
url = message.Attachments.First().Url;
return (url, memStream.ToArray());
}
}
Expand Down
7 changes: 5 additions & 2 deletions CompatBot/Database/ThumbnailDb.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using CompatApiClient;
using Microsoft.EntityFrameworkCore;

Expand All @@ -11,7 +13,8 @@ internal class ThumbnailDb: DbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=thumbs.db");
var dbPath = DbImporter.GetDbPath("thumbs.db", Environment.SpecialFolder.LocalApplicationData);
optionsBuilder.UseSqlite($"Data Source=\"{dbPath}\"");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down

0 comments on commit 6eebd91

Please sign in to comment.