src.internal.dependencies.file_validator module#

Module for validating file uploads.

exception src.internal.dependencies.file_validator.MaxFileSizeException(fs: int)[source]#

Bases: Exception

Exception raised when file size exceeds upload limit.

class src.internal.dependencies.file_validator.MaxFileSizeValidator(max_size: int)[source]#

Bases: object

Validates that file size does not exceed max size.

class src.internal.dependencies.file_validator.ValidateFileUpload(max_upload_size: Optional[int] = None, accepted_content_types: Optional[List[str]] = None)[source]#

Bases: object

Validates incoming request to check if file upload is valid.

src.internal.dependencies.file_validator.clean_filename(filename: str, whitelist: str = '-_.() abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', replace: str = ' ') str[source]#

Clean filename to ensure it is safe for filesystem

Taken from: https://gist.github.com/wassname/1393c4a57cfcbf03641dbc31886123b8

Parameters:
  • filename (str) – Filename

  • whitelist (str, optional) – Chars to ignore. Defaults to VALID_FILENAME_CHARS.

  • replace (str, optional) – What to replace invalid chars with. Defaults to “ “.

Returns:

Cleaned URL.

Return type:

str

src.internal.dependencies.file_validator.determine_safe_file_size(path: str = '/', clearance: Union[int, float] = 5) int[source]#

Determine the safe file size for a given path. This is done to determine if there is enough space for decompressing a file.

Parameters:
  • path (str, optional) – Path to check disk usage for. Defaults to “/”.

  • clearance (Union[int, float], optional) – Expected compression ratio. Defaults to 5.

Returns:

How many bytes can be safely written to disk.

Return type:

int