@@ -8,77 +8,66 @@ namespace NpgsqlRest.UploadHandlers;
88
99public class CsvUploadHandler ( NpgsqlRestUploadOptions options , ILogger ? logger ) : IUploadHandler
1010{
11- public bool RequiresTransaction => true ;
12- public string [ ] Parameters => _parameters ;
13-
14- private readonly string [ ] _parameters = [
15- "included_mime_types" ,
16- "excluded_mime_types" ,
17- "check_file" ,
18- "test_buffer_size" ,
19- "non_printable_threshold" ,
20- "delimiters" ,
21- "has_fields_enclosed_in_quotes" ,
22- "set_white_space_to_null" ,
23- "row_command" ,
24- ] ;
11+ private const string CheckFileParam = "check_csv" ;
12+ private const string DelimitersParam = "delimiters" ;
13+ private const string HasFieldsEnclosedInQuotesParam = "has_fields_enclosed_in_quotes" ;
14+ private const string SetWhiteSpaceToNullParam = "set_white_space_to_null" ;
15+ private const string RowCommandParam = "row_command" ;
2516
2617 private string ? _type = null ;
2718
19+ public bool RequiresTransaction => true ;
20+ public string [ ] Parameters => [
21+ UploadExtensions . IncludedMimeTypeParam , UploadExtensions . ExcludedMimeTypeParam ,
22+ CheckFileParam , FileCheckExtensions . TestBufferSizeParam , FileCheckExtensions . NonPrintableThresholdParam , DelimitersParam ,
23+ HasFieldsEnclosedInQuotesParam , SetWhiteSpaceToNullParam , RowCommandParam
24+ ] ;
25+
2826 public IUploadHandler SetType ( string type )
2927 {
3028 _type = type ;
3129 return this ;
3230 }
3331
34- public async Task < object > UploadAsync ( NpgsqlConnection connection , HttpContext context , Dictionary < string , string > ? parameters )
32+ public async Task < string > UploadAsync ( NpgsqlConnection connection , HttpContext context , Dictionary < string , string > ? parameters )
3533 {
36- string [ ] ? includedMimeTypePatterns = options . DefaultUploadHandlerOptions . CsvUploadIncludedMimeTypePatterns ;
37- string [ ] ? excludedMimeTypePatterns = options . DefaultUploadHandlerOptions . CsvUploadExcludedMimeTypePatterns ;
34+ var ( includedMimeTypePatterns , excludedMimeTypePatterns , _) = options . ParseSharedParameters ( parameters ) ;
3835
3936 bool checkFileStatus = options . DefaultUploadHandlerOptions . CsvUploadCheckFileStatus ;
40- int testBufferSize = options . DefaultUploadHandlerOptions . CsvUploadTestBufferSize ;
41- int nonPrintableThreshold = options . DefaultUploadHandlerOptions . CsvUploadNonPrintableThreshold ;
37+ int testBufferSize = options . DefaultUploadHandlerOptions . TextTestBufferSize ;
38+ int nonPrintableThreshold = options . DefaultUploadHandlerOptions . TextNonPrintableThreshold ;
4239 string delimiters = options . DefaultUploadHandlerOptions . CsvUploadDelimiterChars ;
4340 bool hasFieldsEnclosedInQuotes = options . DefaultUploadHandlerOptions . CsvUploadHasFieldsEnclosedInQuotes ;
4441 bool setWhiteSpaceToNull = options . DefaultUploadHandlerOptions . CsvUploadSetWhiteSpaceToNull ;
4542 string rowCommand = options . DefaultUploadHandlerOptions . CsvUploadRowCommand ;
4643
4744 if ( parameters is not null )
4845 {
49- if ( parameters . TryGetValue ( _parameters [ 0 ] , out var includedMimeTypeStr ) && includedMimeTypeStr is not null )
50- {
51- includedMimeTypePatterns = includedMimeTypeStr . SplitParameter ( ) ;
52- }
53- if ( parameters . TryGetValue ( _parameters [ 1 ] , out var excludedMimeTypeStr ) && excludedMimeTypeStr is not null )
54- {
55- excludedMimeTypePatterns = excludedMimeTypeStr . SplitParameter ( ) ;
56- }
57- if ( parameters . TryGetValue ( _parameters [ 2 ] , out var checkFileStatusStr ) && bool . TryParse ( checkFileStatusStr , out var checkFileStatusParsed ) )
46+ if ( parameters . TryGetValue ( CheckFileParam , out var checkFileStatusStr ) && bool . TryParse ( checkFileStatusStr , out var checkFileStatusParsed ) )
5847 {
5948 checkFileStatus = checkFileStatusParsed ;
6049 }
61- if ( parameters . TryGetValue ( _parameters [ 3 ] , out var testBufferSizeStr ) && int . TryParse ( testBufferSizeStr , out var testBufferSizeParsed ) )
50+ if ( parameters . TryGetValue ( FileCheckExtensions . TestBufferSizeParam , out var testBufferSizeStr ) && int . TryParse ( testBufferSizeStr , out var testBufferSizeParsed ) )
6251 {
6352 testBufferSize = testBufferSizeParsed ;
6453 }
65- if ( parameters . TryGetValue ( _parameters [ 4 ] , out var nonPrintableThresholdStr ) && int . TryParse ( nonPrintableThresholdStr , out var nonPrintableThresholdParsed ) )
54+ if ( parameters . TryGetValue ( FileCheckExtensions . NonPrintableThresholdParam , out var nonPrintableThresholdStr ) && int . TryParse ( nonPrintableThresholdStr , out var nonPrintableThresholdParsed ) )
6655 {
6756 nonPrintableThreshold = nonPrintableThresholdParsed ;
6857 }
69- if ( parameters . TryGetValue ( _parameters [ 5 ] , out var delimitersStr ) && delimitersStr is not null )
58+ if ( parameters . TryGetValue ( DelimitersParam , out var delimitersStr ) && delimitersStr is not null )
7059 {
7160 delimiters = delimitersStr ! ;
7261 }
73- if ( parameters . TryGetValue ( _parameters [ 6 ] , out var hasFieldsEnclosedInQuotesStr ) && bool . TryParse ( hasFieldsEnclosedInQuotesStr , out var hasFieldsEnclosedInQuotesParsed ) )
62+ if ( parameters . TryGetValue ( HasFieldsEnclosedInQuotesParam , out var hasFieldsEnclosedInQuotesStr ) && bool . TryParse ( hasFieldsEnclosedInQuotesStr , out var hasFieldsEnclosedInQuotesParsed ) )
7463 {
7564 hasFieldsEnclosedInQuotes = hasFieldsEnclosedInQuotesParsed ;
7665 }
77- if ( parameters . TryGetValue ( _parameters [ 7 ] , out var setWhiteSpaceToNullStr ) && bool . TryParse ( setWhiteSpaceToNullStr , out var setWhiteSpaceToNullParsed ) )
66+ if ( parameters . TryGetValue ( SetWhiteSpaceToNullParam , out var setWhiteSpaceToNullStr ) && bool . TryParse ( setWhiteSpaceToNullStr , out var setWhiteSpaceToNullParsed ) )
7867 {
7968 setWhiteSpaceToNull = setWhiteSpaceToNullParsed ;
8069 }
81- if ( parameters . TryGetValue ( _parameters [ 8 ] , out var rowCommandStr ) && rowCommandStr is not null )
70+ if ( parameters . TryGetValue ( RowCommandParam , out var rowCommandStr ) && rowCommandStr is not null )
8271 {
8372 rowCommand = rowCommandStr ;
8473 }
0 commit comments