Skip to content

Commit 7bc8287

Browse files
author
Tomas Rutkauskas
committed
Validate input arguments and improve file upload handling
Added validation to ensure at least one input file is provided. Simplified format argument parsing and modified upload logic to include indexed file names in form data. Minor code formatting adjustments for readability.
1 parent b40db6a commit 7bc8287

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

ConvertApi.Cli/Program.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ public static async Task Main(string[] args)
3131
{
3232
pipeInput.Add(line);
3333
}
34+
3435
inputFiles = pipeInput.ToArray();
3536
}
3637
else
3738
{
38-
inputFiles = args.Skip(2).TakeWhile(arg => !arg.Contains('=')).ToArray();
39+
int inputFilesCount = args.Length - 4; // Exclude API token, output file, from-format, and to-format
40+
if (inputFilesCount < 1)
41+
{
42+
Console.WriteLine("Error: At least one input file is required.");
43+
return;
44+
}
45+
46+
inputFiles = args.Skip(2).Take(inputFilesCount).ToArray();
3947
}
4048

4149
// Validate input files
@@ -48,8 +56,8 @@ public static async Task Main(string[] args)
4856
}
4957
}
5058

51-
string fromFormat = Directory.Exists(outputFile) ? (args.Length > 3 ? args[^2] : throw new ArgumentException("[from-format] is required when output path is a folder.")) : Path.GetExtension(inputFiles[0]).Trim('.').ToLower();
52-
string toFormat = Directory.Exists(outputFile) ? (args.Length > 4 ? args[^1] : throw new ArgumentException("[to-format] is required when output path is a folder.")) : Path.GetExtension(outputFile).Trim('.').ToLower();
59+
string fromFormat = args[^2];
60+
string toFormat = args[^1];
5361

5462
var dynamicProperties = args.Skip(2 + inputFiles.Length).Where(arg => arg.Contains('=')).ToDictionary(
5563
arg => arg.Split('=')[0],
@@ -108,12 +116,17 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
108116
}
109117
else
110118
{
119+
int fileIndex = 0;
111120
foreach (var inputFile in inputFiles)
112121
{
113122
form.Add(new StreamContent(File.OpenRead(inputFile))
114123
{
115-
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "files", FileName = Path.GetFileName(inputFile) } }
124+
Headers =
125+
{
126+
ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = $"files[{fileIndex}]", FileName = Path.GetFileName(inputFile) }
127+
}
116128
});
129+
fileIndex++;
117130
}
118131
}
119132

@@ -137,7 +150,8 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
137150

138151
while ((section = await multipartReader.ReadNextSectionAsync()) != null)
139152
{
140-
if (Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition) && (contentDisposition.FileName != null || contentDisposition.FileNameStar.HasValue))
153+
if (Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition) &&
154+
(contentDisposition.FileName != null || contentDisposition.FileNameStar.HasValue))
141155
{
142156
var fileName = contentDisposition.FileNameStar.HasValue ? contentDisposition.FileNameStar.Value : contentDisposition.FileName.Value.Trim('"');
143157
var filePath = Directory.Exists(outputPath) ? Path.Combine(outputPath, fileName) : Path.Combine(Path.GetDirectoryName(outputPath), fileName);
@@ -157,12 +171,12 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
157171
var errorContent = await response.Content.ReadAsStringAsync();
158172
Console.WriteLine($"Error: {response.StatusCode}. Response message: {errorContent}");
159173
}
160-
}
161-
}
162-
163-
static string GetVersion()
174+
}
175+
}
176+
177+
static string GetVersion()
164178
{
165179
var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
166180
return version ?? "unknown";
167181
}
168-
}
182+
}

0 commit comments

Comments
 (0)