Do not crash when file is not available

This commit is contained in:
ig 2023-09-08 15:22:39 +02:00
parent f98a149318
commit 4791b139b8
1 changed files with 20 additions and 11 deletions

View File

@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using CliWrap; using CliWrap;
using CliWrap.Buffered; using CliWrap.Buffered;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
@ -11,11 +10,11 @@ public class S3Cmd
{ {
private static readonly Command Python = Cli.Wrap("python3"); private static readonly Command Python = Cli.Wrap("python3");
private const String? S3CmdPath = "Resources/s3cmd.py"; private const String? S3CmdPath = "Resources/s3cmd.py";
private const String S3Prefix = "s3://"; private const String S3Prefix = "s3://";
public String Key { get; init; } public String Key { get; init;}
public String Secret { get; init; } public String Secret { get; init;}
public String Region { get; init; } = "sos-ch-dk-2.exo.io"; public String Region { get; init; } = "sos-ch-dk-2.exo.io";
// private String?[] DefaultArgs { get; } // private String?[] DefaultArgs { get; }
@ -50,7 +49,7 @@ public class S3Cmd
{ {
const String cors = "./Resources/CORS"; const String cors = "./Resources/CORS";
var result = await Run(bucketName, "mb"); var result = await Run(bucketName, "mb");
var setCors = await Run(bucketName, "setcors", cors); var setCors = await Run(bucketName, "setcors", cors);
return result.ExitCode == 0 && setCors.ExitCode == 0; return result.ExitCode == 0 && setCors.ExitCode == 0;
@ -62,10 +61,20 @@ public class S3Cmd
return result.StandardOutput; return result.StandardOutput;
} }
public async Task<String[]> GetFileText(String bucketName, String filename) public async Task<IReadOnlyList<String>?> GetFileLines(String bucketName, String filename)
{ {
var result = await Run(bucketName + "/" + filename, "get", "--force"); try
return File.ReadAllLines("./" + filename); {
await Run(bucketName + "/" + filename, "get", "--force");
}
catch
{
return null;
}
var lines = File.ReadAllLines($"./{filename}");
File.Delete(filename);
return lines;
} }
public async Task<Boolean> DeleteBucket(String bucketName) public async Task<Boolean> DeleteBucket(String bucketName)
@ -76,7 +85,7 @@ public class S3Cmd
private Task<BufferedCommandResult> Run(String bucketName, String operation, params String[] optionalArgs) private Task<BufferedCommandResult> Run(String bucketName, String operation, params String[] optionalArgs)
{ {
var credentials = new String[] var credentials = new[]
{ {
S3CmdPath, S3CmdPath,
"--access_key", Key, "--access_key", Key,