Skip to content

Upload Options

File upload configuration for handling uploads via PostgreSQL Large Objects, file system, CSV, and Excel handlers.

Overview

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "Enabled": false,
      "LogUploadEvent": true,
      "LogUploadParameters": false,
      "DefaultUploadHandler": "large_object",
      "UseDefaultUploadMetadataParameter": false,
      "DefaultUploadMetadataParameterName": "_upload_metadata",
      "UseDefaultUploadMetadataContextKey": false,
      "DefaultUploadMetadataContextKey": "request.upload_metadata",
      "UploadHandlers": {
        "StopAfterFirstSuccess": false,
        "IncludedMimeTypePatterns": null,
        "ExcludedMimeTypePatterns": null,
        "BufferSize": 8192,
        "TextTestBufferSize": 4096,
        "TextNonPrintableThreshold": 5,
        "AllowedImageTypes": "jpeg, png, gif, bmp, tiff, webp",
        "LargeObjectEnabled": true,
        "LargeObjectKey": "large_object",
        "LargeObjectCheckText": false,
        "LargeObjectCheckImage": false,
        "FileSystemEnabled": true,
        "FileSystemKey": "file_system",
        "FileSystemPath": "/tmp/uploads",
        "FileSystemUseUniqueFileName": true,
        "FileSystemCreatePathIfNotExists": true,
        "FileSystemCheckText": false,
        "FileSystemCheckImage": false,
        "CsvUploadEnabled": true,
        "CsvUploadCheckFileStatus": true,
        "CsvUploadDelimiterChars": ",",
        "CsvUploadHasFieldsEnclosedInQuotes": true,
        "CsvUploadSetWhiteSpaceToNull": true,
        "CsvUploadRowCommand": "call process_csv_row($1,$2,$3,$4)",
        "ExcelUploadEnabled": true,
        "ExcelKey": "excel",
        "ExcelSheetName": null,
        "ExcelAllSheets": false,
        "ExcelTimeFormat": "HH:mm:ss",
        "ExcelDateFormat": "yyyy-MM-dd",
        "ExcelDateTimeFormat": "yyyy-MM-dd HH:mm:ss",
        "ExcelRowDataAsJson": false,
        "ExcelUploadRowCommand": "call process_excel_row($1,$2,$3,$4)"
      }
    }
  }
}

General Settings

SettingTypeDefaultDescription
EnabledboolfalseEnable file upload handling.
LogUploadEventbooltrueLog upload events.
LogUploadParametersboolfalseLog upload parameters (file names, sizes, etc.).
DefaultUploadHandlerstring"large_object"Default handler when not specified.
UseDefaultUploadMetadataParameterboolfalsePass upload metadata via parameter.
DefaultUploadMetadataParameterNamestring"_upload_metadata"Parameter name for upload metadata JSON.
UseDefaultUploadMetadataContextKeyboolfalsePass upload metadata via context key.
DefaultUploadMetadataContextKeystring"request.upload_metadata"Context key for upload metadata JSON.

Upload Handlers Common Settings

Settings that apply to all upload handlers.

SettingTypeDefaultDescription
StopAfterFirstSuccessboolfalseStop processing after first successful handler.
IncludedMimeTypePatternsstringnullCSV of MIME type patterns to include. null to allow all.
ExcludedMimeTypePatternsstringnullCSV of MIME type patterns to exclude. null to exclude none.
BufferSizeint8192Buffer size in bytes for file_system and large_object handlers (8 KB).
TextTestBufferSizeint4096Buffer sample size for testing textual content (4 KB).
TextNonPrintableThresholdint5Maximum non-printable characters allowed in text buffer.
AllowedImageTypesstring"jpeg, png, gif, bmp, tiff, webp"Comma-separated list of allowed image types.

Large Object Handler

Uploads files using PostgreSQL Large Objects API.

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "UploadHandlers": {
        "LargeObjectEnabled": true,
        "LargeObjectKey": "large_object",
        "LargeObjectCheckText": false,
        "LargeObjectCheckImage": false
      }
    }
  }
}
SettingTypeDefaultDescription
LargeObjectEnabledbooltrueEnable Large Object upload handler.
LargeObjectKeystring"large_object"Handler key name.
LargeObjectCheckTextboolfalseValidate uploaded content is text.
LargeObjectCheckImageboolfalseValidate uploaded content is an allowed image type.

File System Handler

Uploads files to the server file system.

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "UploadHandlers": {
        "FileSystemEnabled": true,
        "FileSystemKey": "file_system",
        "FileSystemPath": "/tmp/uploads",
        "FileSystemUseUniqueFileName": true,
        "FileSystemCreatePathIfNotExists": true,
        "FileSystemCheckText": false,
        "FileSystemCheckImage": false
      }
    }
  }
}
SettingTypeDefaultDescription
FileSystemEnabledbooltrueEnable file system upload handler.
FileSystemKeystring"file_system"Handler key name.
FileSystemPathstring"/tmp/uploads"Directory path for uploaded files.
FileSystemUseUniqueFileNamebooltrueGenerate unique file names to prevent overwrites.
FileSystemCreatePathIfNotExistsbooltrueCreate upload directory if it doesn't exist.
FileSystemCheckTextboolfalseValidate uploaded content is text.
FileSystemCheckImageboolfalseValidate uploaded content is an allowed image type.

CSV Upload Handler

Uploads CSV files and processes rows via a PostgreSQL command.

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "UploadHandlers": {
        "CsvUploadEnabled": true,
        "CsvUploadCheckFileStatus": true,
        "CsvUploadDelimiterChars": ",",
        "CsvUploadHasFieldsEnclosedInQuotes": true,
        "CsvUploadSetWhiteSpaceToNull": true,
        "CsvUploadRowCommand": "call process_csv_row($1,$2,$3,$4)"
      }
    }
  }
}
SettingTypeDefaultDescription
CsvUploadEnabledbooltrueEnable CSV upload handler.
CsvUploadCheckFileStatusbooltrueCheck file status before processing.
CsvUploadDelimiterCharsstring","CSV field delimiter character(s).
CsvUploadHasFieldsEnclosedInQuotesbooltrueFields may be enclosed in quotes.
CsvUploadSetWhiteSpaceToNullbooltrueConvert whitespace-only values to NULL.
CsvUploadRowCommandstring"call process_csv_row($1,$2,$3,$4)"PostgreSQL command to process each row.

CSV Row Command Parameters

ParameterTypeDescription
$1intRow index (1-based).
$2text[]Parsed values as text array.
$3textResult of previous row command.
$4jsonUpload metadata JSON.

Excel Upload Handler

Uploads Excel files and processes rows via a PostgreSQL command.

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "UploadHandlers": {
        "ExcelUploadEnabled": true,
        "ExcelKey": "excel",
        "ExcelSheetName": null,
        "ExcelAllSheets": false,
        "ExcelTimeFormat": "HH:mm:ss",
        "ExcelDateFormat": "yyyy-MM-dd",
        "ExcelDateTimeFormat": "yyyy-MM-dd HH:mm:ss",
        "ExcelRowDataAsJson": false,
        "ExcelUploadRowCommand": "call process_excel_row($1,$2,$3,$4)"
      }
    }
  }
}
SettingTypeDefaultDescription
ExcelUploadEnabledbooltrueEnable Excel upload handler.
ExcelKeystring"excel"Handler key name.
ExcelSheetNamestringnullSheet name to process. null for first available sheet.
ExcelAllSheetsboolfalseProcess all sheets in the workbook.
ExcelTimeFormatstring"HH:mm:ss"Format for time values.
ExcelDateFormatstring"yyyy-MM-dd"Format for date values.
ExcelDateTimeFormatstring"yyyy-MM-dd HH:mm:ss"Format for datetime values.
ExcelRowDataAsJsonboolfalsePass row data as JSON instead of text array.
ExcelUploadRowCommandstring"call process_excel_row($1,$2,$3,$4)"PostgreSQL command to process each row.

Excel Row Command Parameters

ParameterTypeDescription
$1intRow index (1-based).
$2text[] or jsonParsed values as text array (or JSON if ExcelRowDataAsJson is true).
$3textResult of previous row command.
$4jsonUpload metadata JSON.

Complete Example

Production configuration with file system and CSV uploads:

json
{
  "NpgsqlRest": {
    "UploadOptions": {
      "Enabled": true,
      "LogUploadEvent": true,
      "LogUploadParameters": false,
      "DefaultUploadHandler": "file_system",
      "UseDefaultUploadMetadataParameter": true,
      "DefaultUploadMetadataParameterName": "_upload_metadata",
      "UploadHandlers": {
        "StopAfterFirstSuccess": true,
        "IncludedMimeTypePatterns": "image/*,text/*,application/pdf",
        "ExcludedMimeTypePatterns": null,
        "BufferSize": 16384,
        "LargeObjectEnabled": false,
        "FileSystemEnabled": true,
        "FileSystemPath": "/var/uploads",
        "FileSystemUseUniqueFileName": true,
        "FileSystemCreatePathIfNotExists": true,
        "FileSystemCheckImage": true,
        "CsvUploadEnabled": true,
        "CsvUploadDelimiterChars": ",",
        "CsvUploadRowCommand": "call import_csv_row($1,$2,$3,$4)",
        "ExcelUploadEnabled": true,
        "ExcelUploadRowCommand": "call import_excel_row($1,$2,$3,$4)"
      }
    }
  }
}

Next Steps

Released under the MIT License.