mirror of
https://github.com/frosch95/K8sFileBrowser.git
synced 2026-04-11 21:08:22 +02:00
First draft of kubernetes file browser
This commit is contained in:
68
K8sFileBrowser/Services/KubernetesFileInformationResult.cs
Normal file
68
K8sFileBrowser/Services/KubernetesFileInformationResult.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using K8sFileBrowser.Models;
|
||||
using Serilog;
|
||||
|
||||
namespace K8sFileBrowser.Services;
|
||||
|
||||
public class KubernetesFileInformationResult
|
||||
{
|
||||
private readonly string _parent;
|
||||
public IList<FileInformation> FileInformations { get; set; } = new List<FileInformation>();
|
||||
|
||||
public KubernetesFileInformationResult(string parent)
|
||||
{
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
public Task ParseFileInformationCallback(Stream stdIn, Stream stdOut, Stream stdErr)
|
||||
{
|
||||
using (var stdOutReader = new StreamReader(stdOut))
|
||||
{
|
||||
var output = stdOutReader.ReadToEnd();
|
||||
|
||||
output.Split("\n").ToList().ForEach(line =>
|
||||
{
|
||||
if (line.Length <= 0) return;
|
||||
line = line.TrimEnd('\n');
|
||||
|
||||
var fileInformation = line.Split("|").ToList();
|
||||
FileInformations.Add(new FileInformation
|
||||
{
|
||||
Parent = _parent,
|
||||
Type = GetFileType(fileInformation[0]),
|
||||
Name = fileInformation[1],
|
||||
Size = fileInformation[2],
|
||||
Date = GetDate(fileInformation[3])
|
||||
});
|
||||
});
|
||||
Log.Information(output);
|
||||
}
|
||||
|
||||
using (var stdErrReader = new StreamReader(stdErr))
|
||||
{
|
||||
var output = stdErrReader.ReadToEnd();
|
||||
Log.Warning(output);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static FileType GetFileType(string type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
"directory" => FileType.Directory,
|
||||
"regular file" => FileType.File,
|
||||
_ => FileType.Unknown
|
||||
};
|
||||
}
|
||||
|
||||
private static DateTimeOffset GetDate(string date)
|
||||
{
|
||||
var unixTime = long.Parse(date);
|
||||
return DateTimeOffset.FromUnixTimeSeconds(unixTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user