mirror of https://github.com/ErsatzTV/ErsatzTV.git
7 changed files with 122 additions and 57 deletions
@ -0,0 +1,3 @@ |
|||||||
|
namespace ErsatzTV.Application.Streaming; |
||||||
|
|
||||||
|
public record GetNextVersion : IRequest<string>; |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using System.Text; |
||||||
|
using CliWrap; |
||||||
|
using ErsatzTV.Core; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Streaming; |
||||||
|
|
||||||
|
public class GetNextVersionHandler(IFileSystem fileSystem) |
||||||
|
: NextChannelHandlerBase(fileSystem), IRequestHandler<GetNextVersion, string> |
||||||
|
{ |
||||||
|
public async Task<string> Handle(GetNextVersion request, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
Validation<BaseError, string> validation = await ChannelBinaryMustExist(); |
||||||
|
foreach (string channelBinary in validation.SuccessToSeq()) |
||||||
|
{ |
||||||
|
var stdOutBuffer = new StringBuilder(); |
||||||
|
CommandResult command = await Cli.Wrap(channelBinary) |
||||||
|
.WithArguments(["--version"]) |
||||||
|
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer)) |
||||||
|
.WithValidation(CommandResultValidation.None) |
||||||
|
.ExecuteAsync(cancellationToken); |
||||||
|
|
||||||
|
if (command.IsSuccess) |
||||||
|
{ |
||||||
|
return stdOutBuffer.ToString().Replace("ersatztv-channel", string.Empty).Trim(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception) |
||||||
|
{ |
||||||
|
// do nothing
|
||||||
|
} |
||||||
|
|
||||||
|
return "n/a"; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using ErsatzTV.Core; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Streaming; |
||||||
|
|
||||||
|
public abstract class NextChannelHandlerBase(IFileSystem fileSystem) |
||||||
|
{ |
||||||
|
protected Task<Validation<BaseError, string>> ChannelBinaryMustExist() |
||||||
|
{ |
||||||
|
string nextFolder = SystemEnvironment.NextFolder; |
||||||
|
if (string.IsNullOrWhiteSpace(nextFolder)) |
||||||
|
{ |
||||||
|
string processFileName = Environment.ProcessPath ?? string.Empty; |
||||||
|
string processExecutable = Path.GetFileNameWithoutExtension(processFileName); |
||||||
|
nextFolder = Path.GetDirectoryName(processFileName); |
||||||
|
if ("dotnet".Equals(processExecutable, StringComparison.OrdinalIgnoreCase)) |
||||||
|
{ |
||||||
|
nextFolder = AppContext.BaseDirectory; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
string executable = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) |
||||||
|
? "ersatztv-channel.exe" |
||||||
|
: "ersatztv-channel"; |
||||||
|
|
||||||
|
string channelBinary = fileSystem.Path.Combine(ReplaceTilde(nextFolder), executable); |
||||||
|
if (!fileSystem.Path.Exists(channelBinary)) |
||||||
|
{ |
||||||
|
return Task.FromResult<Validation<BaseError, string>>( |
||||||
|
BaseError.New("ersatztv-channel binary does not exist!")); |
||||||
|
} |
||||||
|
|
||||||
|
return Task.FromResult<Validation<BaseError, string>>(channelBinary); |
||||||
|
} |
||||||
|
|
||||||
|
private string ReplaceTilde(string path) |
||||||
|
{ |
||||||
|
if (!path.StartsWith('~')) |
||||||
|
{ |
||||||
|
return path; |
||||||
|
} |
||||||
|
|
||||||
|
string userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
||||||
|
|
||||||
|
switch (path) |
||||||
|
{ |
||||||
|
case "~": |
||||||
|
return userFolder; |
||||||
|
case not null |
||||||
|
when path.Length == 2 && |
||||||
|
(path[1] == fileSystem.Path.DirectorySeparatorChar || |
||||||
|
path[1] == fileSystem.Path.AltDirectorySeparatorChar): |
||||||
|
return userFolder + fileSystem.Path.DirectorySeparatorChar; |
||||||
|
default: |
||||||
|
return fileSystem.Path.Combine(userFolder, path[2..]); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,6 @@ |
|||||||
|
namespace ErsatzTV; |
||||||
|
|
||||||
|
public class NextVersion |
||||||
|
{ |
||||||
|
public static string Version { get; set; } |
||||||
|
} |
||||||
Loading…
Reference in new issue