Commented S3 code and assemble in diffenrent function for better reading

This commit is contained in:
atef 2024-06-21 10:50:00 +02:00
parent 194aacb232
commit 907fa27180
2 changed files with 55 additions and 27 deletions

View File

@ -683,24 +683,30 @@ internal static class Program
private static async Task<Boolean> UploadCsv(StatusRecord status, DateTime timeStamp)
{
var csv = status.ToCsv().LogInfo();
await RestApiSavingfile(csv);
var s3Config = status.Config.S3;
var csv = status.ToCsv().LogInfo();
// This is temporary for Wittman, but now it's for all Installation
//await File.WriteAllTextAsync("/var/www/html/status.csv", csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines());
if (s3Config is null)
return false;
//this for concatenating in one file
//Concatenating 15 files in one file
return await ConcatinatingAndCompressingFiles(timeStamp, s3Config);
}
private static async Task<Boolean> ConcatinatingAndCompressingFiles(DateTime timeStamp, S3Config s3Config)
{
if (_counterOfFile >= NbrOfFileToConcatenate)
{
_counterOfFile = 0;
var logFileConcatenator = new LogFileConcatenator();
var csvToSend = logFileConcatenator.ConcatenateFiles(NbrOfFileToConcatenate);
File.WriteAllText("test.csv",csvToSend);
var s3Path = timeStamp.ToUnixTime() + ".csv";
var request = s3Config.CreatePutRequest(s3Path);
@ -708,25 +714,8 @@ internal static class Program
//Use this for no compression
//var response = await request.PutAsync(new StringContent(csv));
//Compress CSV data to a byte array
byte[] compressedBytes;
using (var memoryStream = new MemoryStream())
{
//Create a zip directory and put the compressed file inside
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var entry = archive.CreateEntry("data.csv", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
using (var entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream))
{
writer.Write(csvToSend);
}
}
compressedBytes = memoryStream.ToArray();
}
var compressedBytes = CompresseBytes(csvToSend);
// Encode the compressed byte array as a Base64 string
string base64String = Convert.ToBase64String(compressedBytes);
@ -746,7 +735,46 @@ internal static class Program
}
_counterOfFile++;
return true;
return true;
}
private static Byte[] CompresseBytes(String csvToSend)
{
//Compress CSV data to a byte array
byte[] compressedBytes;
using (var memoryStream = new MemoryStream())
{
//Create a zip directory and put the compressed file inside
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var entry = archive.CreateEntry("data.csv", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
using (var entryStream = entry.Open())
using (var writer = new StreamWriter(entryStream))
{
writer.Write(csvToSend);
}
}
compressedBytes = memoryStream.ToArray();
}
return compressedBytes;
}
private static async Task RestApiSavingfile(String csv)
{
// This is for the Rest API
// Check if the directory exists, and create it if it doesn't
const String directoryPath = "/var/www/html";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string filePath = Path.Combine(directoryPath, "status.csv");
await File.WriteAllTextAsync(filePath, csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines());
}
private static Boolean IsPowerOfTwo(Int32 n)

View File

@ -64,7 +64,7 @@ public record S3Config
{
contentType = "application/base64; charset=utf-8";
//contentType = "text/plain; charset=utf-8";
//contentType = "text/plain; charset=utf-8"; //this to use when sending plain csv to S3
var payload = $"{method}\n{md5Hash}\n{contentType}\n{date}\n/{bucket.Trim('/')}/{s3Path.Trim('/')}";
using var hmacSha1 = new HMACSHA1(UTF8.GetBytes(s3Secret));