File: MultipartData.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (239 lines) | stat: -rw-r--r-- 5,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*!********************************************************************

 Audacity: A Digital Audio Editor

 @file MultipartData.cpp

 Dmitry Vedenko
 **********************************************************************/

#include "MultipartData.h"

#include <algorithm>
#include <cstdint>
#include <cstring>

#include "AudacityException.h"

namespace audacity
{
namespace network_manager
{
namespace
{
class ByteBufferPart final : public MultipartData::Part
{
public:
   ByteBufferPart(const void* data, size_t size)
   {
      mData.resize(size);
      std::memcpy(mData.data(), data, size);
   }

   int64_t GetSize() const override
   {
      return static_cast<int64_t>(mData.size());
   }

   size_t GetOffset() const override
   {
      return mOffset;
   }

   size_t Read(void* buffer, size_t maxBytes) override
   {
      const size_t availableBytes = mData.size() - mOffset;

      maxBytes = std::min(maxBytes, availableBytes);

      std::memcpy(buffer, mData.data() + mOffset, maxBytes);

      mOffset += maxBytes;

      return maxBytes;
   }

   bool Seek(int64_t offset, int origin) override
   {
      switch (origin)
      {
      case SEEK_SET:
         if (offset <= GetSize())
         {
            mOffset = offset;
            return true;
         }
         break;
      case SEEK_CUR:
      {
         const int64_t targetOffset = offset + mOffset;

         if (targetOffset >= 0 && targetOffset < GetSize())
         {
            mOffset = targetOffset;
            return true;
         }
      }
      break;
      case SEEK_END:
      {
         const int64_t targetOffset = GetSize() + offset;

         if (targetOffset >= 0 && targetOffset < GetSize())
         {
            mOffset = targetOffset;
            return true;
         }
      }
      break;
      default:
         break;
      }

      return false;
   }

private:
   std::vector<uint8_t> mData;
   size_t mOffset { 0 };
};

class FilePart final : public MultipartData::Part
{
public:
   explicit FilePart(const wxFileName& fileName)
       : mFileName(fileName)
   {
      if (!fileName.FileExists())
      {
         throw SimpleMessageBoxException(ExceptionType::BadUserAction,
            XO("Failed to open the file for upload: %s")
               .Format(fileName.GetFullPath()));
      }

      if (!mFile.Open(fileName.GetFullPath()))
      {
         throw SimpleMessageBoxException(
            ExceptionType::BadUserAction,
            XO("Failed to open the file for upload: %s")
               .Format(fileName.GetFullPath()));
      }
   }

   int64_t GetSize() const override
   {
      return static_cast<int64_t>(mFileName.GetSize().GetValue());
   }

   size_t GetOffset() const override
   {
      return static_cast<size_t>(mFile.Tell());
   }

   size_t Read(void* buffer, size_t maxBytes) override
   {
      const auto bytesRead = mFile.Read(buffer, maxBytes);

      if (bytesRead == wxInvalidOffset)
         return 0;

      return static_cast<size_t>(bytesRead);
   }

   bool Seek(int64_t offset, int origin = SEEK_SET) override
   {
      static constexpr wxSeekMode map[] = { wxFromStart, wxFromCurrent,
                                            wxFromEnd };

      if (origin < SEEK_SET || origin > SEEK_END)
         return false;

      return wxInvalidOffset != mFile.Seek(offset, map[origin]);
   }

private:
   wxFile mFile;
   wxFileName mFileName;
};
}

void MultipartData::Part::SetHeader(
   const std::string& headerName, const std::string& headerValue)
{
   mHeaders.setHeader(headerName, headerValue);
}

void MultipartData::Part::SetContentType(const std::string& mimeType)
{
   SetHeader(common_headers::ContentType, mimeType);
}

void MultipartData::Part::SetContentDisposition(const std::string& disposition)
{
   SetHeader(common_headers::ContentDisposition, disposition);
}

const HeadersList& MultipartData::Part::GetHeaders() const
{
   return mHeaders;
}

void MultipartData::Add(std::string_view name, std::string_view value)
{
   Add(name, {}, value.data(), value.length());
}

void MultipartData::Add(
   std::string_view name, std::string_view contentType, const void* value,
   size_t size)
{
   mParts.emplace_back(std::make_unique<ByteBufferPart>(value, size));

   mParts.back()->SetContentDisposition("form-data; name=\"" + std::string(name) + "\"");

   if (!contentType.empty())
      mParts.back()->SetContentType(std::string(contentType));
}

void MultipartData::AddFile(
   std::string_view name, std::string_view contentType,
   const wxFileName& fileName)
{
   mParts.emplace_back(std::make_unique<FilePart>(fileName));

   mParts.back()->SetContentDisposition(
      "form-data; name=\"" + std::string(name) + "\"; filename=\"" +
      fileName.GetFullName().ToUTF8().data() + "\"");

   if (!contentType.empty())
      mParts.back()->SetContentType(std::string(contentType));
}

size_t MultipartData::GetPartsCount() const
{
   return mParts.size();
}

MultipartData::Part* MultipartData::GetPart(size_t idx)
{
   if (idx < mParts.size())
      return mParts[idx].get();

   return nullptr;
}

const MultipartData::Part* MultipartData::GetPart(size_t idx) const
{
   if (idx < mParts.size())
      return mParts[idx].get();

   return nullptr;
}

bool MultipartData::IsEmpty() const
{
   return mParts.empty();
}

}
}