Storing in DB now
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.IsolatedStorage;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using fxl.codes.kisekae.Entities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace fxl.codes.kisekae.Services
|
||||
@@ -12,28 +17,103 @@ namespace fxl.codes.kisekae.Services
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly FileParserService _fileParserService;
|
||||
private readonly ILogger<DatabaseService> _logger;
|
||||
private readonly IsolatedStorageFile _storage;
|
||||
|
||||
public DatabaseService(IConfiguration configuration, FileParserService fileParserService)
|
||||
public DatabaseService(ILogger<DatabaseService> logger, IConfiguration configuration, FileParserService fileParserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileParserService = fileParserService;
|
||||
_connectionString = configuration.GetConnectionString("kisekae");
|
||||
_storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
}
|
||||
|
||||
public async void StoreToDatabase(IFormFile file)
|
||||
public async Task<KisekaeDto> StoreToDatabase(IFormFile file)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var existing = connection.QuerySingle<Kisekae>("select * from kisekae where kisekae.filename = @Filename", file);
|
||||
if (existing != null) return;
|
||||
var existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.filename = @Filename", new { Filename = file.FileName });
|
||||
if (existing != null)
|
||||
{
|
||||
_logger.LogInformation($"Already existing upload {file.FileName} under id {existing.Id}");
|
||||
return existing;
|
||||
}
|
||||
|
||||
_fileParserService.UnzipLzh(file);
|
||||
await using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream);
|
||||
|
||||
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;
|
||||
|
||||
_fileParserService.UnzipLzh(file, memoryStream);
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
|
||||
var kisekae = new KisekaeDto
|
||||
{
|
||||
Filename = file.FileName,
|
||||
Name = directory,
|
||||
Checksum = checksum
|
||||
};
|
||||
|
||||
var id = connection.QueryFirst<int>("insert into kisekae(name, filename, checksum) values (@Name, @Filename, @Checksum) returning id", kisekae);
|
||||
kisekae.Id = id;
|
||||
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
var cels = new List<CelDto>();
|
||||
var palettes = new List<PaletteDto>();
|
||||
var configurations = new List<ConfigurationDto>();
|
||||
foreach (var filename in filenames)
|
||||
{
|
||||
await using var reader = _storage.OpenFile(Path.Combine(directory, filename), FileMode.Open);
|
||||
if (!reader.CanRead) throw new InvalidDataException();
|
||||
|
||||
var bytes = new byte[reader.Length];
|
||||
reader.Read(bytes, 0, bytes.Length);
|
||||
|
||||
switch (Path.GetExtension(filename).ToLower())
|
||||
{
|
||||
case ".cel":
|
||||
cels.Add(new CelDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
KisekaeId = id
|
||||
});
|
||||
break;
|
||||
case ".cnf":
|
||||
configurations.Add(new ConfigurationDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = BitConverter.ToString(bytes),
|
||||
KisekaeId = id
|
||||
});
|
||||
break;
|
||||
case ".kcf":
|
||||
palettes.Add(new PaletteDto
|
||||
{
|
||||
Filename = filename,
|
||||
Data = bytes,
|
||||
KisekaeId = id
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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 connection.CloseAsync();
|
||||
|
||||
return kisekae;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,12 +28,17 @@ namespace fxl.codes.kisekae.Services
|
||||
_storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
}
|
||||
|
||||
public void UnzipLzh(IFormFile file)
|
||||
public void UnzipLzh(IFormFile file, MemoryStream memoryStream = null)
|
||||
{
|
||||
if (_storage.FileExists(file.FileName)) _storage.DeleteFile(file.FileName);
|
||||
|
||||
|
||||
using var writer = _storage.CreateFile(file.FileName);
|
||||
file.CopyTo(writer);
|
||||
|
||||
if (memoryStream != null)
|
||||
memoryStream.CopyTo(writer);
|
||||
else
|
||||
file.CopyTo(writer);
|
||||
|
||||
writer.Flush();
|
||||
writer.Close();
|
||||
|
||||
@@ -43,7 +48,14 @@ namespace fxl.codes.kisekae.Services
|
||||
.ToString() ?? "";
|
||||
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
if (_storage.DirectoryExists(directory)) _storage.DeleteDirectory(directory);
|
||||
if (_storage.DirectoryExists(directory))
|
||||
{
|
||||
var allFiles = _storage.GetFileNames(Path.Combine(directory, "*"));
|
||||
foreach (var existingFile in allFiles) _storage.DeleteFile(Path.Combine(directory, existingFile));
|
||||
|
||||
_storage.DeleteDirectory(directory);
|
||||
}
|
||||
|
||||
_storage.CreateDirectory(directory);
|
||||
|
||||
var process = new Process
|
||||
@@ -59,9 +71,11 @@ namespace fxl.codes.kisekae.Services
|
||||
};
|
||||
|
||||
if (!process.Start()) throw new InvalidDataException("Unable to call p7zip to extract archive");
|
||||
|
||||
|
||||
while (!process.StandardOutput.EndOfStream) _logger.LogTrace(process.StandardOutput.ReadLine());
|
||||
process.WaitForExit();
|
||||
|
||||
_storage.DeleteFile(file.FileName);
|
||||
}
|
||||
|
||||
public void ParsePalette(string directory, PaletteModel palette)
|
||||
|
||||
Reference in New Issue
Block a user