Converting to EF, need to work on TS
This commit is contained in:
+54
-129
@@ -1,161 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.IO.IsolatedStorage;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using fxl.codes.kisekae.Entities;
|
||||
using fxl.codes.kisekae.Extensions;
|
||||
using fxl.codes.kisekae.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace fxl.codes.kisekae.Services
|
||||
{
|
||||
public class DatabaseService
|
||||
{
|
||||
private const string ResolutionRegexPattern = @"\((?<Width>[0-9]*).(?<Height>[0-9]*)\)";
|
||||
private readonly ConfigurationReaderService _configurationReaderService;
|
||||
private readonly IDbContextFactory<KisekaeContext> _contextFactory;
|
||||
|
||||
private readonly string _connectionString;
|
||||
private readonly FileParserService _fileParserService;
|
||||
private readonly ILogger<DatabaseService> _logger;
|
||||
private readonly IsolatedStorageFile _storage;
|
||||
|
||||
public DatabaseService(ILogger<DatabaseService> logger,
|
||||
IConfiguration configuration,
|
||||
ConfigurationReaderService configurationReaderService,
|
||||
FileParserService fileParserService)
|
||||
FileParserService fileParserService,
|
||||
IDbContextFactory<KisekaeContext> contextFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_configurationReaderService = configurationReaderService;
|
||||
_fileParserService = fileParserService;
|
||||
_connectionString = configuration.GetConnectionString("kisekae");
|
||||
_contextFactory = contextFactory;
|
||||
_storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<KisekaeDto>> GetAll()
|
||||
public IEnumerable<Kisekae> GetAll()
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.KisekaeSets
|
||||
.Include(x => x.Configurations)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
using var multi = await connection.QueryMultipleAsync("select * from kisekae; select * from configuration");
|
||||
var files = multi.Read<KisekaeDto>();
|
||||
var configs = multi.Read<ConfigurationDto>();
|
||||
var dictionary = configs.ToLookup(x => x.KisekaeId);
|
||||
await connection.CloseAsync();
|
||||
|
||||
foreach (var kiss in files) kiss.Configurations = dictionary[kiss.Id];
|
||||
return files;
|
||||
public Configuration GetConfig(int id)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.Configurations
|
||||
.Include(x => x.Cels).ThenInclude(x => x.Cel)
|
||||
.Include(x => x.Kisekae).ThenInclude(x => x.Palettes).ThenInclude(x => x.Colors)
|
||||
.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
|
||||
public async void StoreToDatabase(IFormFile file)
|
||||
{
|
||||
var memoryStream = await GetAsMemoryStream(file);
|
||||
var checksum = Convert.ToBase64String(await new SHA256Managed().ComputeHashAsync(memoryStream));
|
||||
var checksum = Convert.ToBase64String(await SHA256.Create().ComputeHashAsync(memoryStream));
|
||||
memoryStream.Position = 0; // Reset for re-read
|
||||
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
var transaction = await connection.BeginTransactionAsync();
|
||||
await using var context = await _contextFactory.CreateDbContextAsync();
|
||||
var existing = await context.KisekaeSets
|
||||
.FirstOrDefaultAsync(x => string.Equals(x.FileName, file.FileName)
|
||||
|| string.Equals(x.CheckSum, checksum));
|
||||
if (existing != null) return;
|
||||
|
||||
try
|
||||
_fileParserService.UnzipLzh(file, memoryStream);
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
|
||||
var kisekae = new Kisekae
|
||||
{
|
||||
var existing = GetExisting(connection, file.FileName, checksum);
|
||||
if (existing != null) return;
|
||||
FileName = file.FileName,
|
||||
CheckSum = checksum
|
||||
};
|
||||
|
||||
_fileParserService.UnzipLzh(file, memoryStream);
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
var kisekae = new KisekaeDto
|
||||
{
|
||||
Filename = file.FileName,
|
||||
Name = directory,
|
||||
Checksum = checksum
|
||||
};
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
await SetInnerFiles(kisekae, directory, filenames);
|
||||
|
||||
kisekae.Id = await connection.InsertAsync(kisekae);
|
||||
foreach (var config in kisekae.Configurations)
|
||||
_configurationReaderService.ReadConfigurationToDto(config,
|
||||
kisekae.Cels.ToDictionary(x => x.FileName.ToLowerInvariant()),
|
||||
kisekae.Palettes.ToDictionary(x => x.FileName.ToLowerInvariant()));
|
||||
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
await SetInnerFiles(connection, directory, filenames, kisekae.Id);
|
||||
SetPaletteColors(kisekae.Palettes);
|
||||
|
||||
using var saved = await connection.QueryMultipleAsync(
|
||||
"select * from configuration where kisekae_id = @id; select * from cel where kisekae_id = @id; select * from palette where kisekae_id = @id",
|
||||
new { id = kisekae.Id });
|
||||
var configs = saved.Read<ConfigurationDto>().ToList();
|
||||
var cels = saved.Read<CelDto>().ToDictionary(x => x.Filename.ToLowerInvariant());
|
||||
var palettes = saved.Read<PaletteDto>().ToDictionary(x => x.Filename.ToLowerInvariant());
|
||||
|
||||
foreach (var config in configs)
|
||||
{
|
||||
_configurationReaderService.ReadConfigurationToDto(config, cels, palettes);
|
||||
await connection.InsertAsync<CelConfigDto>(config.Cels);
|
||||
await connection.UpdateAsync<PaletteDto>(config.Palettes);
|
||||
await connection.UpdateAsync(config);
|
||||
}
|
||||
|
||||
var paletteColors = SetPaletteColors(palettes.Values);
|
||||
await connection.InsertAsync<PaletteColorDto>(paletteColors);
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
foreach (var celConfig in kisekae.Configurations.SelectMany(config => config.Cels))
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
_logger.LogError(e, $"Error saving file {file.FileName}");
|
||||
_fileParserService.RenderCel(celConfig);
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
await context.AddAsync(kisekae);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<PlaysetModel> LoadConfig(int configId)
|
||||
private async Task SetInnerFiles(Kisekae kisekae, string directory, IEnumerable<string> filenames)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var config = await connection.QuerySingleAsync<ConfigurationDto>("select * from configuration where id = @configId", new { configId });
|
||||
|
||||
var celIds = await connection.QueryAsync<int>("select cel_id from cel_config where config_id = @configId", new { configId });
|
||||
var renders = await connection.QueryAsync<CelRenderDto>($"select * from cel_render where cel_id in ({string.Join(",", celIds)})");
|
||||
if (!renders.Any())
|
||||
{
|
||||
using var reader = await connection.QueryMultipleAsync($"select * from cel where id in ({string.Join(",", celIds)});"
|
||||
+ "select * from palette where kisekae_id = @kisekaeId", new { kisekaeId = config.KisekaeId });
|
||||
|
||||
var cels = reader.Read<CelDto>();
|
||||
var palettes = reader.Read<PaletteDto>().ToDictionary(x => x.Id);
|
||||
|
||||
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
return null;
|
||||
}
|
||||
|
||||
private KisekaeDto GetExisting(IDbConnection connection, string filename, string checksum)
|
||||
{
|
||||
var existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.filename = @Filename", new { Filename = filename });
|
||||
if (existing != null)
|
||||
{
|
||||
_logger.LogInformation($"Already existing upload {filename} under id {existing.Id}");
|
||||
return existing;
|
||||
}
|
||||
|
||||
existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.checksum = @Checksum", new { Checksum = checksum });
|
||||
if (existing == null) return null;
|
||||
|
||||
_logger.LogInformation($"Already existing upload (checksum: {checksum}) under id {existing.Id} and filename {existing.Filename}");
|
||||
return existing;
|
||||
}
|
||||
|
||||
private async Task SetInnerFiles(IDbConnection connection, string directory, IEnumerable<string> filenames, int id)
|
||||
{
|
||||
var files = new List<IKisekaeFile>();
|
||||
foreach (var filename in filenames)
|
||||
{
|
||||
await using var reader = _storage.OpenFile(Path.Combine(directory, filename), FileMode.Open);
|
||||
@@ -167,40 +104,32 @@ namespace fxl.codes.kisekae.Services
|
||||
switch (Path.GetExtension(filename).ToLower())
|
||||
{
|
||||
case ".cel":
|
||||
files.Add(new CelDto
|
||||
kisekae.Cels.Add(new Cel
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
KisekaeId = id
|
||||
FileName = filename,
|
||||
Data = bytes
|
||||
});
|
||||
break;
|
||||
case ".cnf":
|
||||
files.Add(new ConfigurationDto
|
||||
kisekae.Configurations.Add(new Configuration
|
||||
{
|
||||
Filename = filename,
|
||||
Data = Encoding.ASCII.GetString(bytes),
|
||||
KisekaeId = id
|
||||
Name = filename,
|
||||
Data = Encoding.ASCII.GetString(bytes)
|
||||
});
|
||||
break;
|
||||
case ".kcf":
|
||||
files.Add(new PaletteDto
|
||||
kisekae.Palettes.Add(new Palette
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
KisekaeId = id
|
||||
FileName = filename,
|
||||
Data = bytes
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await connection.InsertAsync(files.OfType<CelDto>());
|
||||
await connection.InsertAsync(files.OfType<ConfigurationDto>());
|
||||
await connection.InsertAsync(files.OfType<PaletteDto>());
|
||||
}
|
||||
|
||||
private List<PaletteColorDto> SetPaletteColors(IEnumerable<PaletteDto> palettes)
|
||||
private void SetPaletteColors(IEnumerable<Palette> palettes)
|
||||
{
|
||||
var paletteColors = new List<PaletteColorDto>();
|
||||
foreach (var palette in palettes)
|
||||
{
|
||||
var colors = _fileParserService.ParsePalette(palette, out var groups, out var colorsPerGroup);
|
||||
@@ -209,23 +138,19 @@ namespace fxl.codes.kisekae.Services
|
||||
for (var colorIndex = 0; colorIndex < colorsPerGroup; colorIndex++)
|
||||
{
|
||||
var color = colors[groupIndex * colorsPerGroup + colorIndex];
|
||||
paletteColors.Add(new PaletteColorDto
|
||||
palette.Colors.Add(new PaletteColor
|
||||
{
|
||||
Group = groupIndex,
|
||||
PaletteId = palette.Id,
|
||||
Hex = color.ToHex()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return paletteColors;
|
||||
}
|
||||
|
||||
private static async Task<MemoryStream> GetAsMemoryStream(IFormFile file)
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
await file.CopyToAsync(stream);
|
||||
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user