@@ -17,7 +17,7 @@ public static async Task Main(string[] args)
1717 DisplayHelp ( ) ;
1818 return ;
1919 }
20-
20+
2121 // Replacing null values with empty string to avoid unexpected behaviour.
2222 args = args . Select ( s => s ?? "" ) . ToArray ( ) ;
2323
@@ -31,15 +31,15 @@ public static async Task Main(string[] args)
3131 : args . Length - parametersCount ;
3232
3333 string [ ] inputFiles = args . Skip ( 2 ) . Take ( inputFilesEndIndex - 2 ) . ToArray ( ) ;
34-
34+
3535 string fromFormat = args . Length > inputFilesEndIndex
3636 ? args [ ^ ( 2 + parametersCount ) ] // Second-to-last argument - parameters with '='
3737 : Path . GetExtension ( inputFiles [ 0 ] ) . Trim ( '.' ) . ToLower ( ) ; // Infer from first input file
3838
3939 string toFormat = args . Length > inputFilesEndIndex + 1
4040 ? args [ ^ ( 1 + parametersCount ) ] // Last argument - parameters with '='
4141 : Path . GetExtension ( outputDirectoryOrFile ) . Trim ( '.' ) . ToLower ( ) ; // Infer from output file
42-
42+
4343 ValidateInputFiles ( inputFiles , fromFormat ) ;
4444
4545 // Extract dynamic properties
@@ -59,13 +59,13 @@ private static void ValidateInputFiles(string[] inputFiles, string fromFormat)
5959 {
6060 if ( fromFormat == "web" )
6161 return ;
62-
62+
6363 if ( inputFiles . Length == 0 )
6464 {
6565 Console . WriteLine ( "Error: At least one input file is required." ) ;
6666 return ;
6767 }
68-
68+
6969 foreach ( var file in inputFiles )
7070 {
7171 if ( ! File . Exists ( file ) )
@@ -120,29 +120,25 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
120120 {
121121 if ( inputFiles . Length == 1 )
122122 {
123- form . Add ( new StreamContent ( File . OpenRead ( inputFiles [ 0 ] ) )
124- {
125- Headers = { ContentDisposition = new System . Net . Http . Headers . ContentDispositionHeaderValue ( "form-data" ) { Name = "file" , FileName = Path . GetFileName ( inputFiles [ 0 ] ) } }
126- } ) ;
123+ AddFilesToFormParameters ( form , "file" , inputFiles [ 0 ] ) ;
127124 }
128125 else
129126 {
130127 int fileIndex = 0 ;
131128 foreach ( var inputFile in inputFiles )
132129 {
133- form . Add ( new StreamContent ( File . OpenRead ( inputFile ) )
134- {
135- Headers =
136- {
137- ContentDisposition = new System . Net . Http . Headers . ContentDispositionHeaderValue ( "form-data" ) { Name = $ "files[{ fileIndex } ]", FileName = Path . GetFileName ( inputFile ) }
138- }
139- } ) ;
130+ AddFilesToFormParameters ( form , $ "files[{ fileIndex } ]", inputFile ) ;
140131 fileIndex ++ ;
141132 }
142133 }
143134 }
144135
145- foreach ( var property in properties )
136+ foreach ( var ( filePropertyName , filePath ) in properties . Where ( x => x . Key . ToLower ( ) . EndsWith ( "file" ) ) )
137+ {
138+ AddFilesToFormParameters ( form , filePropertyName , filePath ) ;
139+ }
140+
141+ foreach ( var property in properties . Where ( x => ! x . Key . ToLower ( ) . EndsWith ( "file" ) ) )
146142 {
147143 form . Add ( new StringContent ( property . Value ) , property . Key ) ;
148144 }
@@ -186,13 +182,21 @@ static async Task ConvertFiles(string apiToken, string fromFormat, string toForm
186182 }
187183 }
188184
185+ private static void AddFilesToFormParameters ( MultipartFormDataContent form , string parameterName , string filePath )
186+ {
187+ form . Add ( new StreamContent ( File . OpenRead ( filePath ) )
188+ {
189+ Headers = { ContentDisposition = new System . Net . Http . Headers . ContentDispositionHeaderValue ( "form-data" ) { Name = parameterName , FileName = Path . GetFileName ( filePath ) } }
190+ } ) ;
191+ }
192+
189193 static string GetVersion ( )
190194 {
191195 var version = Assembly . GetExecutingAssembly ( )
192196 . GetName ( )
193197 . Version ?
194198 . ToString ( ) ?? "unknown" ;
195-
199+
196200 return version ;
197201 }
198202}
0 commit comments