Thinking about shunting most of the file parsing into db services since it should be done once
This commit is contained in:
+112
-29
@@ -1,10 +1,15 @@
|
||||
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.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
using fxl.codes.kisekae.Entities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -15,47 +20,96 @@ namespace fxl.codes.kisekae.Services
|
||||
{
|
||||
public class DatabaseService
|
||||
{
|
||||
private const string ResolutionRegexPattern = @"\((?<Width>[0-9]*).(?<Height>[0-9]*)\)";
|
||||
private readonly ConfigurationReaderService _configurationReaderService;
|
||||
|
||||
private readonly string _connectionString;
|
||||
private readonly FileParserService _fileParserService;
|
||||
private readonly ILogger<DatabaseService> _logger;
|
||||
private readonly IsolatedStorageFile _storage;
|
||||
|
||||
public DatabaseService(ILogger<DatabaseService> logger, IConfiguration configuration, FileParserService fileParserService)
|
||||
public DatabaseService(ILogger<DatabaseService> logger,
|
||||
IConfiguration configuration,
|
||||
ConfigurationReaderService configurationReaderService,
|
||||
FileParserService fileParserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_configurationReaderService = configurationReaderService;
|
||||
_fileParserService = fileParserService;
|
||||
_connectionString = configuration.GetConnectionString("kisekae");
|
||||
_storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
}
|
||||
|
||||
public async Task<KisekaeDto> StoreToDatabase(IFormFile file)
|
||||
public async Task<IEnumerable<KisekaeDto>> GetAll()
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.filename = @Filename", new { Filename = file.FileName });
|
||||
if (existing != null)
|
||||
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 async void GetKisekaeConfig(int id, int configId)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var queryParams = new { Id = id, ConfigId = configId };
|
||||
var cels = connection.Query<CelConfigDto>("select * from cel_config where configId = @ConfigId", queryParams);
|
||||
if (cels == null)
|
||||
{
|
||||
_logger.LogInformation($"Already existing upload {file.FileName} under id {existing.Id}");
|
||||
return existing;
|
||||
var config = connection.QuerySingle<ConfigurationDto>(
|
||||
"select * from configuration where kisekae_id = @Id and id = @ConfigId",
|
||||
queryParams);
|
||||
|
||||
foreach (var line in config.Data.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
|
||||
switch (line.ToCharArray()[0])
|
||||
{
|
||||
case '(':
|
||||
var resolutionRegex = new Regex(ResolutionRegexPattern);
|
||||
var resolutionMatch = resolutionRegex.Match(line);
|
||||
config.Width = int.Parse(resolutionMatch.Groups["Width"].Value);
|
||||
config.Height = int.Parse(resolutionMatch.Groups["Height"].Value);
|
||||
break;
|
||||
case '[':
|
||||
var borderValue = line[1..];
|
||||
if (borderValue.Contains(';')) borderValue = borderValue.Split(';')[0].Trim();
|
||||
config.BorderIndex = int.Parse(borderValue);
|
||||
break;
|
||||
case '%':
|
||||
break;
|
||||
case '#':
|
||||
break;
|
||||
case '$':
|
||||
case ' ':
|
||||
break;
|
||||
}
|
||||
|
||||
await connection.UpdateAsync(config);
|
||||
}
|
||||
|
||||
await using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream);
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async void StoreToDatabase(IFormFile file)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
var memoryStream = await GetAsMemoryStream(file);
|
||||
var checksum = Convert.ToBase64String(await new SHA256Managed().ComputeHashAsync(memoryStream));
|
||||
existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.checksum = @Checksum", new { Checksum = checksum });
|
||||
if (existing != null)
|
||||
{
|
||||
_logger.LogInformation($"Already existing upload (checksum: {checksum}) under id {existing.Id} and filename {existing.Filename}");
|
||||
return existing;
|
||||
}
|
||||
memoryStream.Position = 0; // Reset for re-read
|
||||
|
||||
memoryStream.Position = 0;
|
||||
var existing = GetExisting(connection, file.FileName, checksum);
|
||||
if (existing != null) return;
|
||||
|
||||
_fileParserService.UnzipLzh(file, memoryStream);
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
|
||||
var kisekae = new KisekaeDto
|
||||
{
|
||||
Filename = file.FileName,
|
||||
@@ -63,13 +117,33 @@ namespace fxl.codes.kisekae.Services
|
||||
Checksum = checksum
|
||||
};
|
||||
|
||||
var id = connection.QueryFirst<int>("insert into kisekae(name, filename, checksum) values (@Name, @Filename, @Checksum) returning id", kisekae);
|
||||
kisekae.Id = id;
|
||||
kisekae.Id = await connection.InsertAsync(kisekae);
|
||||
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
var cels = new List<CelDto>();
|
||||
var palettes = new List<PaletteDto>();
|
||||
var configurations = new List<ConfigurationDto>();
|
||||
SetInnerFiles(directory, filenames, kisekae.Id);
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
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 void SetInnerFiles(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);
|
||||
@@ -81,7 +155,7 @@ namespace fxl.codes.kisekae.Services
|
||||
switch (Path.GetExtension(filename).ToLower())
|
||||
{
|
||||
case ".cel":
|
||||
cels.Add(new CelDto
|
||||
files.Add(new CelDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
@@ -89,15 +163,15 @@ namespace fxl.codes.kisekae.Services
|
||||
});
|
||||
break;
|
||||
case ".cnf":
|
||||
configurations.Add(new ConfigurationDto
|
||||
files.Add(new ConfigurationDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = BitConverter.ToString(bytes),
|
||||
Data = Encoding.ASCII.GetString(bytes),
|
||||
KisekaeId = id
|
||||
});
|
||||
break;
|
||||
case ".kcf":
|
||||
palettes.Add(new PaletteDto
|
||||
files.Add(new PaletteDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
@@ -107,13 +181,22 @@ namespace fxl.codes.kisekae.Services
|
||||
}
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync("insert into cel (filename, data, kisekae_id) values (@Filename, @Data, @KisekaeId)", cels);
|
||||
await connection.ExecuteAsync("insert into configuration (filename, data, kisekae_id) values (@Filename, @Data, @KisekaeId)", configurations);
|
||||
await connection.ExecuteAsync("insert into palette (filename, data, kisekae_id) values (@Filename, @Data, @KisekaeId)", palettes);
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
await connection.InsertAsync(files.OfType<CelDto>());
|
||||
await connection.InsertAsync(files.OfType<ConfigurationDto>());
|
||||
await connection.InsertAsync(files.OfType<PaletteDto>());
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
return kisekae;
|
||||
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