Skip to content

Commit 9768b42

Browse files
author
Tomas Rutkauskas
committed
Refactor form-data processing and improve code readability
Adjusted logic to handle empty input files gracefully and refactored nested conditions for better clarity. Improved formatting and comments, ensuring consistent styling and reducing redundant whitespace. These changes enhance maintainability and code readability.
1 parent 64a7098 commit 9768b42

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

ConvertApi.Cli/Program.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public static async Task Main(string[] args)
5252
? args[^1] // Last argument
5353
: Path.GetExtension(outputFile).Trim('.').ToLower(); // Infer from output file
5454

55-
56-
// Second to last argument
57-
// Last argument
55+
56+
// Second to last argument
57+
// Last argument
5858

5959
// Validate input files
6060
foreach (var file in inputFiles)
@@ -115,23 +115,29 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
115115
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
116116
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/mixed"));
117117

118-
if (inputFiles.Length == 1)
118+
if (inputFiles.Any())
119119
{
120-
form.Add(new StreamContent(File.OpenRead(inputFiles[0]))
120+
if (inputFiles.Length == 1)
121121
{
122-
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = Path.GetFileName(inputFiles[0]) } }
123-
});
124-
}
125-
else
126-
{
127-
int fileIndex = 0;
128-
foreach (var inputFile in inputFiles)
129-
{
130-
form.Add(new StreamContent(File.OpenRead(inputFile))
122+
form.Add(new StreamContent(File.OpenRead(inputFiles[0]))
131123
{
132-
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = $"files[{fileIndex}]", FileName = Path.GetFileName(inputFile) } }
124+
Headers = { ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = Path.GetFileName(inputFiles[0]) } }
133125
});
134-
fileIndex++;
126+
}
127+
else
128+
{
129+
int fileIndex = 0;
130+
foreach (var inputFile in inputFiles)
131+
{
132+
form.Add(new StreamContent(File.OpenRead(inputFile))
133+
{
134+
Headers =
135+
{
136+
ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = $"files[{fileIndex}]", FileName = Path.GetFileName(inputFile) }
137+
}
138+
});
139+
fileIndex++;
140+
}
135141
}
136142
}
137143

@@ -155,7 +161,8 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
155161

156162
while ((section = await multipartReader.ReadNextSectionAsync()) != null)
157163
{
158-
if (Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition) && (contentDisposition.FileName != null || contentDisposition.FileNameStar.HasValue))
164+
if (Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition) &&
165+
(contentDisposition.FileName != null || contentDisposition.FileNameStar.HasValue))
159166
{
160167
var fileName = contentDisposition.FileNameStar.HasValue ? contentDisposition.FileNameStar.Value : contentDisposition.FileName.Value.Trim('"');
161168
var filePath = Directory.Exists(outputPath) ? Path.Combine(outputPath, fileName) : Path.Combine(Path.GetDirectoryName(outputPath), fileName);
@@ -175,12 +182,12 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
175182
var errorContent = await response.Content.ReadAsStringAsync();
176183
Console.WriteLine($"Error: {response.StatusCode}. Response message: {errorContent}");
177184
}
178-
}
179-
}
180-
181-
static string GetVersion()
185+
}
186+
}
187+
188+
static string GetVersion()
182189
{
183190
var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
184191
return version ?? "unknown";
185192
}
186-
}
193+
}

0 commit comments

Comments
 (0)