AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::rest::Part Class Reference

#include <form.hpp>

Collaboration diagram for anch::rest::Part:

Public Member Functions

 Part ()
 Part (std::string_view name, std::string content)
 Part (std::string_view name, std::string_view filename, anch::codec::MediaType mediaType, std::string content)
std::string_view content () const
std::unique_ptr< std::istream > open () const
std::size_t size () const noexcept
bool isFile () const noexcept
bool isInMemory () const noexcept
std::optional< std::filesystem::path > getPath () const
bool operator== (const Part &other) const
const std::string & getName () const noexcept
void setName (std::string_view name)
std::optional< std::string_view > getFilename () const noexcept
const anch::codec::MediaType & getMediaType () const noexcept
void setMediaType (anch::codec::MediaType mediaType)
anch::rest::Headers & getHeaders () noexcept
const anch::rest::Headers & getHeaders () const noexcept

Static Public Member Functions

static Part fromFile (std::string_view name, const std::filesystem::path &path, anch::codec::MediaType mediaType, std::optional< std::string_view > filename=std::nullopt)
static Part fromTemporaryFile (std::string_view name, const std::filesystem::path &path, anch::codec::MediaType mediaType, std::optional< std::string_view > filename)

Detailed Description

Form part: a field ( text/plain by default) or a file (with a file name, application/octet-stream by default).

Content is in memory or in a file ( fromFile , fromTemporaryFile , large parts read by Multipart ). It is read with open (stream) or with content , which loads a file content in memory on first call: use open for files. Copies of a part share its file.

Author
Vincent Lachenal
Since
0.2

Constructor & Destructor Documentation

◆ Part() [1/3]

anch::rest::Part::Part ( )

Part default constructor: empty field without name ( text/plain ), to be assigned (model members)

◆ Part() [2/3]

anch::rest::Part::Part ( std::string_view name,
std::string content )

Field Part constructor ( text/plain )

Parameters
namethe part name
contentthe content
Here is the call graph for this function:

◆ Part() [3/3]

anch::rest::Part::Part ( std::string_view name,
std::string_view filename,
anch::codec::MediaType mediaType,
std::string content )

File Part constructor

Parameters
namethe part name
filenamethe file name
mediaTypethe content media type
contentthe content
Here is the call graph for this function:

Member Function Documentation

◆ content()

std::string_view anch::rest::Part::content ( ) const

Get content: file content is loaded in memory on first call

Returns
the content
Exceptions
std::runtime_errorwhen file can not be read

◆ fromFile()

Part anch::rest::Part::fromFile ( std::string_view name,
const std::filesystem::path & path,
anch::codec::MediaType mediaType,
std::optional< std::string_view > filename = std::nullopt )
static

Create part from a file: content is read from the file when it is used (the file is not deleted)

Parameters
namethe part name
paththe file path
mediaTypethe content media type
filenamethe file name (default: path file name)
Returns
the part
Exceptions
std::filesystem::filesystem_errorwhen file size can not be read
Here is the call graph for this function:

◆ fromTemporaryFile()

Part anch::rest::Part::fromTemporaryFile ( std::string_view name,
const std::filesystem::path & path,
anch::codec::MediaType mediaType,
std::optional< std::string_view > filename )
static

Create part from a temporary file: the file is deleted with the last part copy

Parameters
namethe part name
paththe file path
mediaTypethe content media type
filenamethe file name (empty for a field)
Returns
the part
Exceptions
std::filesystem::filesystem_errorwhen file size can not be read
Here is the call graph for this function:

◆ getFilename()

std::optional< std::string_view > anch::rest::Part::getFilename ( ) const
noexcept

Get file name

Returns
the file name, empty for fields
Here is the call graph for this function:

◆ getHeaders() [1/2]

const anch::rest::Headers & anch::rest::Part::getHeaders ( ) const
noexcept

Get other part headers

Returns
the headers
Here is the call graph for this function:

◆ getHeaders() [2/2]

anch::rest::Headers & anch::rest::Part::getHeaders ( )
noexcept

Get other part headers ( Content-Disposition and Content-Type are given by name, file name and media type)

Returns
the headers
Here is the call graph for this function:

◆ getMediaType()

const anch::codec::MediaType & anch::rest::Part::getMediaType ( ) const
noexcept

Get content media type

Returns
the media type
Here is the call graph for this function:

◆ getName()

const std::string & anch::rest::Part::getName ( ) const
noexcept

Get part name

Returns
the name
Here is the call graph for this function:

◆ getPath()

std::optional< std::filesystem::path > anch::rest::Part::getPath ( ) const

Get content file path

Returns
the path, empty for content in memory
Here is the call graph for this function:

◆ isFile()

bool anch::rest::Part::isFile ( ) const
noexcept

Check if part is a file (it has a file name)

Returns
true when part is a file
Here is the call graph for this function:

◆ isInMemory()

bool anch::rest::Part::isInMemory ( ) const
noexcept

Check if content is in memory (not in a file)

Returns
true when content is in memory
Here is the call graph for this function:

◆ open()

std::unique_ptr< std::istream > anch::rest::Part::open ( ) const

Open content stream

Returns
the content stream

◆ operator==()

bool anch::rest::Part::operator== ( const Part & other) const

Equality operator: name, file name, media type, headers and content are compared (files are read)

Parameters
otherthe part to compare
Returns
true when parts are equal
Here is the call graph for this function:

◆ setMediaType()

void anch::rest::Part::setMediaType ( anch::codec::MediaType mediaType)

Set content media type

Parameters
mediaTypethe media type
Here is the call graph for this function:

◆ setName()

void anch::rest::Part::setName ( std::string_view name)

Set part name

Parameters
namethe name
Here is the call graph for this function:

◆ size()

std::size_t anch::rest::Part::size ( ) const
noexcept

Get content size

Returns
the content size in bytes

The documentation for this class was generated from the following file: