From 4791b139b83032dd5a404674f48489cb9b1c1f1f Mon Sep 17 00:00:00 2001 From: ig Date: Fri, 8 Sep 2023 15:22:39 +0200 Subject: [PATCH] Do not crash when file is not available --- csharp/App/Backend/S3/S3Cmd.cs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/csharp/App/Backend/S3/S3Cmd.cs b/csharp/App/Backend/S3/S3Cmd.cs index 53305e963..29cc42fe6 100644 --- a/csharp/App/Backend/S3/S3Cmd.cs +++ b/csharp/App/Backend/S3/S3Cmd.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Drawing; using CliWrap; using CliWrap.Buffered; using InnovEnergy.Lib.Utils; @@ -11,11 +10,11 @@ public class S3Cmd { private static readonly Command Python = Cli.Wrap("python3"); - private const String? S3CmdPath = "Resources/s3cmd.py"; - private const String S3Prefix = "s3://"; - - public String Key { get; init; } - public String Secret { get; init; } + private const String? S3CmdPath = "Resources/s3cmd.py"; + private const String S3Prefix = "s3://"; + + public String Key { get; init;} + public String Secret { get; init;} public String Region { get; init; } = "sos-ch-dk-2.exo.io"; // private String?[] DefaultArgs { get; } @@ -50,7 +49,7 @@ public class S3Cmd { const String cors = "./Resources/CORS"; - var result = await Run(bucketName, "mb"); + var result = await Run(bucketName, "mb"); var setCors = await Run(bucketName, "setcors", cors); return result.ExitCode == 0 && setCors.ExitCode == 0; @@ -62,10 +61,20 @@ public class S3Cmd return result.StandardOutput; } - public async Task GetFileText(String bucketName, String filename) + public async Task?> GetFileLines(String bucketName, String filename) { - var result = await Run(bucketName + "/" + filename, "get", "--force"); - return File.ReadAllLines("./" + filename); + try + { + await Run(bucketName + "/" + filename, "get", "--force"); + } + catch + { + return null; + } + + var lines = File.ReadAllLines($"./{filename}"); + File.Delete(filename); + return lines; } public async Task DeleteBucket(String bucketName) @@ -76,7 +85,7 @@ public class S3Cmd private Task Run(String bucketName, String operation, params String[] optionalArgs) { - var credentials = new String[] + var credentials = new[] { S3CmdPath, "--access_key", Key,