move databases to the user profile config folders

This commit is contained in:
13xforever 2019-03-06 19:51:41 +05:00
parent 26b58ae912
commit 6eebd91247
6 changed files with 68 additions and 39 deletions

View File

@ -104,6 +104,9 @@ namespace CompatBot.Commands
Description = $"Rating: {score}",
ThumbnailUrl = thumb.url,
};
#if DEBUG
result.WithFooter("Test instance");
#endif
hasResults = true;
await ctx.RespondAsync(embed: result).ConfigureAwait(false);
}

View File

@ -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 @@ namespace CompatBot.Database
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)

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Database.Migrations;
@ -106,5 +107,48 @@ namespace CompatBot.Database
}
}
}
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;
}
}
}

View File

@ -31,7 +31,6 @@ namespace CompatBot.Database.Providers
return false;
}
public static bool IsFresh(string locale, DateTime dataTimestamp)
{
using (var db = new ThumbnailDb())
@ -43,7 +42,6 @@ namespace CompatBot.Database.Providers
return false;
}
public static async Task SetLastRunTimestampAsync(string locale, string containerId = null)
{
if (string.IsNullOrEmpty(locale))

View File

@ -24,7 +24,7 @@ namespace CompatBot.Database.Providers
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 @@ namespace CompatBot.Database.Providers
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 @@ namespace CompatBot.Database.Providers
{
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 @@ namespace CompatBot.Database.Providers
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());
}
}

View File

@ -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 @@ namespace CompatBot.Database
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)