Audacity 3.2.0
UploadService.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 UploadService.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11
12#include "UploadService.h"
13
14#include <cassert>
15#include <mutex>
16
17#include <wx/filefn.h>
18#include <wx/filename.h>
19
20#include <rapidjson/document.h>
21#include <rapidjson/writer.h>
22
23#include "AudacityException.h"
24
25#include "OAuthService.h"
26#include "ServiceConfig.h"
27
28#include "NetworkManager.h"
29#include "Request.h"
30#include "IResponse.h"
31#include "MultipartData.h"
32
33#include "CodeConversions.h"
34
35#include "TempDirectory.h"
36#include "FileNames.h"
37
38namespace cloud::audiocom
39{
40namespace
41{
42std::string_view DeduceMimeType(const wxString& ext)
43{
44 if (ext == "wv")
45 return "audio/x-wavpack";
46 else if (ext == "flac")
47 return "audio/x-flac";
48 else if (ext == "mp3")
49 return "audio/mpeg";
50 else
51 return "audio/x-wav";
52}
53
55 const wxString& filePath, const wxString& projectName, bool isPublic)
56{
57 rapidjson::Document document;
58 document.SetObject();
59
60 const wxFileName fileName(filePath);
61 const auto mimeType = DeduceMimeType(fileName.GetExt());
62
63 document.AddMember(
64 "mime",
65 rapidjson::Value(
66 mimeType.data(), mimeType.length(), document.GetAllocator()),
67 document.GetAllocator());
68
69 const auto downloadMime = GetServiceConfig().GetDownloadMime();
70
71 if (!downloadMime.empty())
72 {
73 document.AddMember(
74 "download_mime",
75 rapidjson::Value(
76 downloadMime.data(), downloadMime.length(),
77 document.GetAllocator()),
78 document.GetAllocator());
79 }
80
81 const auto name = audacity::ToUTF8(projectName.empty() ? fileName.GetFullName() : projectName);
82
83 document.AddMember(
84 "name",
85 rapidjson::Value(name.data(), name.length(), document.GetAllocator()),
86 document.GetAllocator());
87
88 document.AddMember(
89 "size",
90 rapidjson::Value(static_cast<int64_t>(fileName.GetSize().GetValue())),
91 document.GetAllocator());
92
93 document.AddMember(
94 "public", rapidjson::Value(isPublic), document.GetAllocator());
95
96 rapidjson::StringBuffer buffer;
97 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
98 document.Accept(writer);
99
100 return std::string(buffer.GetString());
101}
102
103std::string GetProgressPayload(uint64_t current, uint64_t total)
104{
105 rapidjson::Document document;
106 document.SetObject();
107
108 document.AddMember(
109 "progress", rapidjson::Value(current / static_cast<double>(total) * 100.0),
110 document.GetAllocator());
111
112 rapidjson::StringBuffer buffer;
113 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
114 document.Accept(writer);
115
116 return std::string(buffer.GetString());
117}
118
119UploadFailedPayload ParseUploadFailedMessage(const std::string& payloadText)
120{
121 rapidjson::StringStream stream(payloadText.c_str());
122 rapidjson::Document document;
123
124 document.ParseStream(stream);
125
126 if (!document.IsObject())
127 {
128 // This is unexpected, just return an empty object
129 assert(document.IsObject());
130 return {};
131 }
132
133 UploadFailedPayload payload;
134
135 auto readInt = [&document](const char* name) {
136 return document.HasMember(name) && document[name].IsInt() ?
137 document[name].GetInt() :
138 0;
139 };
140
141 auto readString = [&document](const char* name) -> const char*
142 {
143 return document.HasMember(name) && document[name].IsString() ?
144 document[name].GetString() :
145 "";
146 };
147
148 payload.code = readInt("code");
149 payload.status = readInt("status");
150
151 payload.name = readString("name");
152 payload.message = readString("message");
153
154 if (document.HasMember("errors") && document["errors"].IsObject())
155 {
156 for (auto& err : document["errors"].GetObject ())
157 {
158 if (!err.value.IsString())
159 continue;
160
161 payload.additionalErrors.emplace_back(
162 err.name.GetString(), err.value.GetString());
163 }
164 }
165
166 return payload;
167}
168
169
170// This class will capture itself inside the request handlers
171// by a strong reference. This way we ensure that it outlives all
172// the outstanding requests.
175 std::enable_shared_from_this<UploadOperation>
176{
178 const ServiceConfig& serviceConfig, wxString fileName,
179 wxString projectName, bool isPublic,
180 UploadService::CompletedCallback completedCallback,
181 UploadService::ProgressCallback progressCallback)
182 : mServiceConfig(serviceConfig)
183 , mFileName(std::move(fileName))
184 , mProjectName(std::move(projectName))
185 , mIsPublic(isPublic)
186 , mCompletedCallback(std::move(completedCallback))
187 , mProgressCallback(std::move(progressCallback))
188 {
189 }
190
192
193 const wxString mFileName;
194 const wxString mProjectName;
195
196 const bool mIsPublic;
197
200
201 std::string mAuthToken;
202
203 std::string mSuccessUrl;
204 std::string mFailureUrl;
205 std::string mProgressUrl;
206
207 std::string mAudioID;
208 std::string mUploadToken;
209
210 std::string mAudioSlug;
211
212 using Clock = std::chrono::steady_clock;
213
214 Clock::time_point mLastProgressReportTime;
215
216 mutable std::mutex mStatusMutex;
217 mutable std::mutex mCallbacksMutex;
218
219 std::weak_ptr<audacity::network_manager::IResponse> mActiveResponse;
220 bool mCompleted {};
221 bool mAborted {};
222
224 {
225 if (!mAuthToken.empty())
226 request.setHeader(
228
229 const auto language = mServiceConfig.GetAcceptLanguageValue();
230
231 if (!language.empty())
232 request.setHeader(
234 language);
235 }
236
237 void FailPromise(UploadOperationCompleted::Result result, std::string errorMessage)
238 {
239 {
240 std::lock_guard<std::mutex> lock(mStatusMutex);
241 mCompleted = true;
242 }
243
244 std::lock_guard<std::mutex> callbacksLock(mCallbacksMutex);
245
246 if (mCompletedCallback)
247 {
248 mCompletedCallback(
249 UploadOperationCompleted { result, ParseUploadFailedMessage(errorMessage) });
250 }
251
252 mProgressCallback = {};
253 mCompletedCallback = {};
254 }
255
257 {
258 {
259 std::lock_guard<std::mutex> lock(mStatusMutex);
260 mCompleted = true;
261 }
262
263 std::lock_guard<std::mutex> callbacksLock(mCallbacksMutex);
264
265 if (mCompletedCallback)
266 {
267
268 mCompletedCallback(
270 UploadSuccessfulPayload { mAudioID, mAudioSlug } });
271 }
272
273 mProgressCallback = {};
274 mCompletedCallback = {};
275 }
276
277 void InitiateUpload(std::string_view authToken)
278 {
279 using namespace audacity::network_manager;
280
281 Request request(mServiceConfig.GetAPIUrl("/audio"));
282
283 request.setHeader(
285
286 request.setHeader(
288
289 mAuthToken = std::string(authToken);
290 SetRequiredHeaders(request);
291
292 const auto payload = GetUploadRequestPayload(mFileName, mProjectName, mIsPublic);
293
294 std::lock_guard<std::mutex> lock(mStatusMutex);
295
296 // User has already aborted? Do not send the request.
297 if (mAborted)
298 return;
299
300 auto response = NetworkManager::GetInstance().doPost(
301 request, payload.data(), payload.size());
302
303 mActiveResponse = response;
304
305 response->setRequestFinishedCallback(
306 [response, sharedThis = shared_from_this(), this](auto) {
307 auto responseCode = response->getHTTPCode();
308
309 if (responseCode == 201)
310 {
311 HandleUploadPolicy(response->readAll<std::string>());
312 }
313 else if (responseCode == 401)
314 {
315 FailPromise(
317 response->readAll<std::string>());
318 }
319 else if (responseCode == 422)
320 {
321 FailPromise(
323 response->readAll<std::string>());
324 }
325 else
326 {
327 FailPromise(
329 response->readAll<std::string>());
330 }
331 });
332 }
333
334 void HandleUploadPolicy(std::string uploadPolicyJSON)
335 {
336 using namespace audacity::network_manager;
337
338 rapidjson::Document document;
339 document.Parse(uploadPolicyJSON.data(), uploadPolicyJSON.length());
340
341 if (
342 !document.HasMember("url") || !document.HasMember("success") ||
343 !document.HasMember("fail") || !document.HasMember("progress"))
344 {
345 FailPromise(
347 uploadPolicyJSON);
348
349 return;
350 }
351
352 auto form = std::make_unique<MultipartData>();
353
354 if (document.HasMember("fields"))
355 {
356 const auto& fields = document["fields"];
357
358 for (auto it = fields.MemberBegin(); it != fields.MemberEnd(); ++it)
359 form->Add(it->name.GetString(), it->value.GetString());
360 }
361
362 const auto fileField =
363 document.HasMember("field") ? document["field"].GetString() : "file";
364
365 const wxFileName name { mFileName };
366
367 try
368 {
369 // We have checked for the file existence on the main thread
370 // already. For safety sake check for any exception thrown by AddFile
371 // anyway
372 form->AddFile(fileField, DeduceMimeType(name.GetExt()), name);
373 }
374 catch (...)
375 {
376 // Just fail the promise in case if any exception was thrown
377 // UploadService user is responsible to display an appropriate dialog
379 return;
380 }
381
382
383 const auto url = document["url"].GetString();
384
385 mSuccessUrl = document["success"].GetString();
386 mFailureUrl = document["fail"].GetString();
387 mProgressUrl = document["progress"].GetString();
388
389 if (document.HasMember("extra"))
390 {
391 const auto& extra = document["extra"];
392
393 mAudioID = extra["audio"]["id"].GetString();
394 mAudioSlug = extra["audio"]["slug"].GetString();
395
396 if (extra.HasMember("token"))
397 mUploadToken = extra["token"].GetString();
398 }
399
400 const auto encType = document.HasMember("enctype") ?
401 document["enctype"].GetString() :
402 "multipart/form-data";
403
404 Request request(url);
405
406 request.setHeader(common_headers::ContentType, encType);
407 request.setHeader(
409
410 // We only lock late and for very short time
411 std::lock_guard<std::mutex> lock(mStatusMutex);
412
413 if (mAborted)
414 return;
415
416 auto response =
417 NetworkManager::GetInstance().doPost(request, std::move(form));
418
419 mActiveResponse = response;
420
421 response->setRequestFinishedCallback(
422 [response, sharedThis = shared_from_this(), this](auto)
423 {
424 HandleS3UploadCompleted(response);
425 });
426
427 response->setUploadProgressCallback(
428 [response, sharedThis = shared_from_this(),
429 this](auto current, auto total)
430 { HandleUploadProgress(current, total); });
431 }
432
433 void HandleUploadProgress(uint64_t current, uint64_t total)
434 {
435 {
436 std::lock_guard<std::mutex> callbacksLock(mCallbacksMutex);
437
438 if (mProgressCallback)
439 mProgressCallback(current, total);
440 }
441
442 const auto now = Clock::now();
443
444 if ((now - mLastProgressReportTime) > mServiceConfig.GetProgressCallbackTimeout())
445 {
446 mLastProgressReportTime = now;
447
448 using namespace audacity::network_manager;
449 Request request(mProgressUrl);
450
451 request.setHeader(
453 request.setHeader(
455
456 auto payload = GetProgressPayload(current, total);
457
458 std::lock_guard<std::mutex> lock(mStatusMutex);
459
460 if (mAborted)
461 return;
462
463 auto response = NetworkManager::GetInstance().doPatch(
464 request, payload.data(), payload.size());
465
466 response->setRequestFinishedCallback([response](auto) {});
467 }
468 }
469
470 void HandleS3UploadCompleted(std::shared_ptr<audacity::network_manager::IResponse> response)
471 {
472 using namespace audacity::network_manager;
473
474 const auto responseCode = response->getHTTPCode();
475
476 const bool success =
477 responseCode == 200 || responseCode == 201 || responseCode == 204;
478
479 Request request(success ? mSuccessUrl : mFailureUrl);
480 SetRequiredHeaders(request);
481
482 std::lock_guard<std::mutex> lock(mStatusMutex);
483
484 if (mAborted)
485 return;
486
487 auto finalResponse = success ? NetworkManager::GetInstance().doPost(request, nullptr, 0) :
488 NetworkManager::GetInstance().doDelete(request);
489
490 mActiveResponse = finalResponse;
491
492 finalResponse->setRequestFinishedCallback(
493 [finalResponse, sharedThis = shared_from_this(), this, success](auto)
494 {
495 const auto httpCode = finalResponse->getHTTPCode();
496 if (success && httpCode >= 200 && httpCode < 300)
497 {
498 CompletePromise();
499 return;
500 }
501
502 FailPromise(
504 finalResponse->readAll<std::string>());
505 });
506 }
507
508 bool IsCompleted() override
509 {
510 std::lock_guard<std::mutex> lock(mStatusMutex);
511 return mCompleted;
512 }
513
514 void Abort() override
515 {
516 {
517 std::lock_guard<std::mutex> lock(mStatusMutex);
518
519 if (mCompleted)
520 return;
521
522 mCompleted = true;
523 mAborted = true;
524
525 if (auto activeResponse = mActiveResponse.lock())
526 activeResponse->abort();
527 }
528
529 std::lock_guard<std::mutex> callbacksLock(mCallbacksMutex);
530
531 if (mCompletedCallback)
532 mCompletedCallback({ UploadOperationCompleted::Result::Aborted });
533
534 mCompletedCallback = {};
535 mProgressCallback = {};
536 }
537
538
539 void DiscardResult() override
540 {
541 using namespace audacity::network_manager;
542
543 Abort();
544
545 auto url = mServiceConfig.GetAPIUrl("/audio");
546 url += "/" + mAudioID + "?token=" + mUploadToken;
547
548 Request request(url);
549 auto response = NetworkManager::GetInstance().doDelete(request);
550
551 response->setRequestFinishedCallback(
552 [response](auto)
553 {
554 // Do nothing
555 });
556 }
557}; // struct UploadOperation
558} // namespace
559
561 : mServiceConfig(config), mOAuthService(service)
562{
563}
564
566 const wxString& fileName, const wxString& projectName, bool isPublic,
567 CompletedCallback completedCallback, ProgressCallback progressCallback)
568{
569 if (!wxFileExists(fileName))
570 {
571 if (completedCallback)
572 completedCallback(UploadOperationCompleted {
574
575 return {};
576 }
577
578 auto operation = std::make_shared<AudiocomUploadOperation>(
579 mServiceConfig, fileName, projectName, isPublic,
580 std::move(completedCallback), std::move(progressCallback));
581
582 mOAuthService.ValidateAuth([operation](std::string_view authToken)
583 { operation->InitiateUpload(authToken); });
584
585 return UploadOperationHandle { operation };
586}
587
589
591 std::shared_ptr<UploadOperation> operation)
592 : mOperation(std::move(operation))
593{
594}
595
597{
598 if (mOperation)
599 // It is safe to call Abort on completed operations
600 mOperation->Abort();
601}
602
603UploadOperationHandle::operator bool() const noexcept
604{
605 return mOperation != nullptr;
606}
607
609{
610 return mOperation.operator->();
611}
612
614{
615 const auto tempPath = TempDirectory::DefaultTempDir();
616
617 if (!wxDirExists(tempPath))
618 {
619 // Temp directory was not created yet.
620 // Is it a first run of Audacity?
621 // In any case, let's wait for some better time
622 return {};
623 }
624
626 tempPath, XO("Cannot proceed to upload.")))
627 return {};
628
629 return tempPath + "/cloud/";
630}
631
632namespace
633{
635 const auto tempPath = GetUploadTempPath();
636
637 if (!wxDirExists(tempPath))
638 return;
639
640 wxArrayString files;
641
642 wxDir::GetAllFiles(tempPath, &files, {}, wxDIR_FILES);
643
644 for (const auto& file : files)
645 wxRemoveFile(file);
646
647 return;
648});
649}
650
651} // namespace cloud::audiocom
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
Declare functions to perform UTF-8 to std::wstring conversions.
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
Declare an interface for HTTP response.
Declare a class for performing HTTP requests.
Declare a class for constructing HTTP requests.
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
Request & setHeader(const std::string &name, std::string value)
Definition: Request.cpp:46
Service responsible for OAuth authentication against the audio.com service.
Definition: OAuthService.h:38
void ValidateAuth(std::function< void(std::string_view)> completedHandler)
Attempt to authorize the user.
Configuration for the audio.com.
Definition: ServiceConfig.h:24
std::string GetAcceptLanguageValue() const
Returns the preferred language.
std::string GetAPIUrl(std::string_view apiURI) const
Helper to construct the full URLs for the API.
std::chrono::milliseconds GetProgressCallbackTimeout() const
Timeout between progress callbacks.
MimeType GetDownloadMime() const
Return the mime type server should store the file. This is a requirement from audiocom.
A unique_ptr like class that holds a pointer to UploadOperation.
UploadOperation * operator->() const noexcept
std::shared_ptr< UploadOperation > mOperation
Class used to track the upload operation.
Definition: UploadService.h:86
std::function< void(uint64_t current, uint64_t total)> ProgressCallback
UploadOperationHandle Upload(const wxString &fileName, const wxString &projectName, bool isPublic, CompletedCallback completedCallback, ProgressCallback progressCallback)
Uploads the file to audio.com.
const ServiceConfig & mServiceConfig
UploadService(const ServiceConfig &config, OAuthService &service)
std::function< void(const UploadOperationCompleted &)> CompletedCallback
FILES_API bool WritableLocationCheck(const FilePath &path, const TranslatableString &message)
Check location on writable access and return true if checked successfully.
FILES_API const FilePath & DefaultTempDir()
FILES_API Observer::Publisher< FilePath > & GetTempPathObserver()
FrameStatistics & GetInstance() noexcept
std::string ToUTF8(const std::wstring &wstr)
std::string GetProgressPayload(uint64_t current, uint64_t total)
UploadFailedPayload ParseUploadFailedMessage(const std::string &payloadText)
std::string_view DeduceMimeType(const wxString &ext)
std::string GetUploadRequestPayload(const wxString &filePath, const wxString &projectName, bool isPublic)
wxString GetUploadTempPath()
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
STL namespace.
This structure represents an upload error as returned by the server.
Definition: UploadService.h:28
std::vector< AdditionalError > additionalErrors
Definition: UploadService.h:36
@ FileNotFound
Specified file is not found.
@ InvalidData
audio.com has failed to understand what Audacity wants
@ Aborted
Upload was aborted by the user.
@ UnexpectedResponse
Audacity has failed to understand audio.com response.
@ UploadFailed
Upload failed for some other reason.
This structure represents the payload associated with successful upload.
Definition: UploadService.h:41
void HandleS3UploadCompleted(std::shared_ptr< audacity::network_manager::IResponse > response)
AudiocomUploadOperation(const ServiceConfig &serviceConfig, wxString fileName, wxString projectName, bool isPublic, UploadService::CompletedCallback completedCallback, UploadService::ProgressCallback progressCallback)
void SetRequiredHeaders(audacity::network_manager::Request &request) const
void FailPromise(UploadOperationCompleted::Result result, std::string errorMessage)
bool IsCompleted() override
Returns true if the upload is finished.
std::weak_ptr< audacity::network_manager::IResponse > mActiveResponse