duck.http.fileuploads.handlersยถ

Module containing classes for storing uploaded file data.

Package Contentsยถ

Classesยถ

BaseFileUpload

Base class for storing file uploads data.

PersistentFileUpload

Class for persistent storage of file uploads on disk in a specified directory.

TemporaryFileUpload

Class for temporarily storing file upload data in memory.

APIยถ

class duck.http.fileuploads.handlers.BaseFileUpload(filename: str, initial_bytes: bytes = b'', **kw)ยถ

Bases: io.BytesIO

Base class for storing file uploads data.

Variables:
  • initial_bytes โ€“ The initial byte content of the file.

  • filename โ€“ The name of the file.

  • name โ€“ Name of the form field for this File. (optional)

  • content_type โ€“ The mimetype of the uploaded content. (optional)

  • content_disposition โ€“ The file upload content disposition. (optional)

Initialization

Initialize self. See help(type(self)) for accurate signature.

__repr__()ยถ
getsize()ยถ

Returns the file IO object total bytes.

guess_mimetype() โ†’ Optional[str]ยถ

Returns the guessed mimetype for the uploaded file.

Notes

  • This method guesses the mimetype for the file upload using the provided bytes rather than the the filename, because it bypasses altered filenames thereby increasing security.

abstractmethod save()ยถ

Save the uploaded data. This method should be implemented by subclasses.

Raises:

NotImplementedError โ€“ If the method is not implemented by subclasses.

save_to_file(filepath: str)ยถ

Save the uploaded data to a file.

Parameters:

filepath โ€“ The path where the file will be saved.

Returns:

The number of bytes written.

Return type:

int

verify()ยถ

Verifies the uploaded file if the content type matches the guessed mimetype.

Raises:

FileVerificationError โ€“ If the specified content type doesnโ€™t match the guessed mimetype.

Notes:

  • This method guesses the mimetype for the file upload using the provided bytes rather than the the filename, because it bypasses altered filenames thereby increasing security.

exception duck.http.fileuploads.handlers.FileUploadErrorยถ

Bases: Exception

Base exception class for file upload errors

Initialization

Initialize self. See help(type(self)) for accurate signature.

exception duck.http.fileuploads.handlers.FileVerificationErrorยถ

Bases: duck.http.fileuploads.handlers.FileUploadError

Raised on verification failure for uploaded files.

Initialization

Initialize self. See help(type(self)) for accurate signature.

class duck.http.fileuploads.handlers.PersistentFileUpload(filename: str, initial_bytes: bytes = b'', directory: str = SETTINGS['FILE_UPLOAD_DIR'], overwrite_existing_file=True, **kw)ยถ

Bases: duck.http.fileuploads.handlers.BaseFileUpload

Class for persistent storage of file uploads on disk in a specified directory.

Variables:
  • filename โ€“ The name of the file.

  • initial_bytes โ€“ The initial byte content of the file.

  • directory โ€“ The directory where the file will be saved.

  • overwrite_existing_file โ€“ Whether to overwrite existing file in directory when saving

Initialization

Initialize self. See help(type(self)) for accurate signature.

save()ยถ

Save the uploaded data by writing it to the specified file path.

save_to_file()ยถ

Save the uploaded data to the specified file path.

Returns:

The number of bytes written.

Return type:

int

class duck.http.fileuploads.handlers.TemporaryFileUpload(filename: str, initial_bytes: bytes = b'', **kw)ยถ

Bases: duck.http.fileuploads.handlers.BaseFileUpload

Class for temporarily storing file upload data in memory.

Initialization

Initialize self. See help(type(self)) for accurate signature.

save()ยถ

Save the uploaded data. In this case, the method does nothing.