20namespace network_manager
30 std::memcpy(mData.data(), data,
size);
35 return static_cast<int64_t
>(mData.size());
43 size_t Read(
void* buffer,
size_t maxBytes)
override
45 const size_t availableBytes = mData.size() - mOffset;
47 maxBytes =
std::min(maxBytes, availableBytes);
49 std::memcpy(buffer, mData.data() + mOffset, maxBytes);
56 bool Seek(int64_t offset,
int origin)
override
61 if (offset <= GetSize())
69 const int64_t targetOffset = offset + mOffset;
71 if (targetOffset >= 0 && targetOffset < GetSize())
73 mOffset = targetOffset;
80 const int64_t targetOffset = GetSize() + offset;
82 if (targetOffset >= 0 && targetOffset < GetSize())
84 mOffset = targetOffset;
105 : mFileName(fileName)
107 if (!fileName.FileExists())
110 XO(
"Failed to open the file for upload: %s")
111 .
Format(fileName.GetFullPath()));
114 if (!mFile.Open(fileName.GetFullPath()))
118 XO(
"Failed to open the file for upload: %s")
119 .
Format(fileName.GetFullPath()));
125 return static_cast<int64_t
>(mFileName.GetSize().GetValue());
130 return static_cast<size_t>(mFile.Tell());
133 size_t Read(
void* buffer,
size_t maxBytes)
override
135 const auto bytesRead = mFile.Read(buffer, maxBytes);
137 if (bytesRead == wxInvalidOffset)
140 return static_cast<size_t>(bytesRead);
143 bool Seek(int64_t offset,
int origin = SEEK_SET)
override
145 static constexpr wxSeekMode map[] = { wxFromStart, wxFromCurrent,
148 if (origin < SEEK_SET || origin > SEEK_END)
151 return wxInvalidOffset != mFile.Seek(offset, map[origin]);
161 const std::string& headerName,
const std::string& headerValue)
183 Add(
name, {}, value.data(), value.length());
187 std::string_view
name, std::string_view contentType,
const void* value,
190 mParts.emplace_back(std::make_unique<ByteBufferPart>(value,
size));
192 mParts.back()->SetContentDisposition(
"form-data; name=\"" + std::string(
name) +
"\"");
194 if (!contentType.empty())
195 mParts.back()->SetContentType(std::string(contentType));
199 std::string_view
name, std::string_view contentType,
200 const wxFileName& fileName)
202 mParts.emplace_back(std::make_unique<FilePart>(fileName));
204 mParts.back()->SetContentDisposition(
205 "form-data; name=\"" + std::string(
name) +
"\"; filename=\"" +
206 fileName.GetFullName().ToUTF8().data() +
"\"");
208 if (!contentType.empty())
209 mParts.back()->SetContentType(std::string(contentType));
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
@ BadUserAction
Indicates that the user performed an action that is not allowed.
A MessageBoxException that shows a given, unvarying string.
void SetContentType(const std::string &mimeType)
const HeadersList & GetHeaders() const
void SetContentDisposition(const std::string &disposition)
void SetHeader(const std::string &headerName, const std::string &headerValue)
void Add(std::string_view name, std::string_view value)
void AddFile(std::string_view name, std::string_view contentType, const wxFileName &fileName)
std::vector< std::unique_ptr< Part > > mParts
Part * GetPart(size_t idx)
size_t GetPartsCount() const
size_t Read(void *buffer, size_t maxBytes) override
std::vector< uint8_t > mData
ByteBufferPart(const void *data, size_t size)
int64_t GetSize() const override
size_t GetOffset() const override
bool Seek(int64_t offset, int origin) override
bool Seek(int64_t offset, int origin=SEEK_SET) override
int64_t GetSize() const override
FilePart(const wxFileName &fileName)
size_t GetOffset() const override
size_t Read(void *buffer, size_t maxBytes) override