diff --git a/CompatBot/Commands/Psn.cs b/CompatBot/Commands/Psn.cs index cb48f5a0..3851b693 100644 --- a/CompatBot/Commands/Psn.cs +++ b/CompatBot/Commands/Psn.cs @@ -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); } diff --git a/CompatBot/Database/BotDb.cs b/CompatBot/Database/BotDb.cs index c94555f0..41180c9c 100644 --- a/CompatBot/Database/BotDb.cs +++ b/CompatBot/Database/BotDb.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using CompatApiClient; using Microsoft.EntityFrameworkCore; @@ -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) diff --git a/CompatBot/Database/DbImporter.cs b/CompatBot/Database/DbImporter.cs index 23a8bd4a..4bb2b74b 100644 --- a/CompatBot/Database/DbImporter.cs +++ b/CompatBot/Database/DbImporter.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Threading; using System.Threading.Tasks; using CompatBot.Database.Migrations; @@ -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; + } } } \ No newline at end of file diff --git a/CompatBot/Database/Providers/ScrapeStateProvider.cs b/CompatBot/Database/Providers/ScrapeStateProvider.cs index 3d3370a9..4246ee6f 100644 --- a/CompatBot/Database/Providers/ScrapeStateProvider.cs +++ b/CompatBot/Database/Providers/ScrapeStateProvider.cs @@ -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()) @@ -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)) diff --git a/CompatBot/Database/Providers/ThumbnailProvider.cs b/CompatBot/Database/Providers/ThumbnailProvider.cs index 54f038e2..de3a8084 100644 --- a/CompatBot/Database/Providers/ThumbnailProvider.cs +++ b/CompatBot/Database/Providers/ThumbnailProvider.cs @@ -24,7 +24,7 @@ public static async Task 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; @@ -48,35 +48,14 @@ public static async Task 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; } } } @@ -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()) { @@ -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()); } } diff --git a/CompatBot/Database/ThumbnailDb.cs b/CompatBot/Database/ThumbnailDb.cs index 87107fb4..aa5d2acc 100644 --- a/CompatBot/Database/ThumbnailDb.cs +++ b/CompatBot/Database/ThumbnailDb.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.ComponentModel.DataAnnotations; +using System.IO; using CompatApiClient; using Microsoft.EntityFrameworkCore; @@ -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)