From 21b62d1b7905bbafdaba30e4b833efe1bbec8022 Mon Sep 17 00:00:00 2001 From: Syping Date: Sat, 25 Oct 2025 02:09:53 +0200 Subject: [PATCH] impl. set output stdout and improve get file safety --- Program.cs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Program.cs b/Program.cs index 593f530..4237466 100644 --- a/Program.cs +++ b/Program.cs @@ -47,8 +47,19 @@ internal static class Program { break; } - using Stream output = outputFile == "-" ? Console.OpenStandardOutput() : File.Create(outputFile); - output.Write(content); + if (outputFile == "-" || outputFile == String.Empty) { + using MemoryStream contentStream = new(content); + using Stream output = Console.OpenStandardOutput(); + contentStream.CopyTo(output); + } + else { + String tempFile = Path.GetTempFileName(); + using (MemoryStream contentStream = new(content)) { + using FileStream output = File.Create(tempFile); + contentStream.CopyTo(output); + } + File.Move(tempFile, outputFile, true); + } } catch (RagePhotoException exception) { Console.Error.WriteLine(exception.Message); @@ -60,7 +71,7 @@ internal static class Program { } } - private static void Set(FileInfo photoFile, String? format, String? jpegFile, String? description, String? json, String? title, FileInfo? outputFile) { + private static void Set(FileInfo photoFile, String? format, String? jpegFile, String? description, String? json, String? title, String? outputFile) { if (format == null && jpegFile == null && description == null && json == null && title == null) { Console.Error.WriteLine("No value has being set"); @@ -88,7 +99,7 @@ internal static class Program { if (title != null) photo.Title = title; - if (jpegFile == string.Empty) { + if (jpegFile == String.Empty) { photo.Jpeg = Properties.Resources.EmptyJpeg; } else if (jpegFile != null) { @@ -98,9 +109,16 @@ internal static class Program { photo.Jpeg = jpegStream.ToArray(); } - String tempFile = Path.GetTempFileName(); - photo.SaveFile(tempFile); - File.Move(tempFile, outputFile != null ? outputFile.FullName : photoFile.FullName, true); + if (outputFile == "-") { + using MemoryStream photoStream = new(photo.Save()); + using Stream output = Console.OpenStandardOutput(); + photoStream.CopyTo(output); + } + else { + String tempFile = Path.GetTempFileName(); + photo.SaveFile(tempFile); + File.Move(tempFile, !String.IsNullOrEmpty(outputFile) ? outputFile : photoFile.FullName, true); + } } catch (RagePhotoException exception) { Console.Error.WriteLine(exception.Message); @@ -160,7 +178,7 @@ internal static class Program { Option titleOption = new("--title", "-t") { Description = "Photo Title" }; - Option outputOption = new("--output", "-o") { + Option outputOption = new("--output", "-o") { Description = "Output File" }; Command setCommand = new("set", "Set Photo Data") {