forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadService.h
152 lines (122 loc) · 4.05 KB
/
UploadService.h
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
UploadService.h
Dmitry Vedenko
**********************************************************************/
#pragma once
#include <functional>
#include <memory>
#include <utility>
#include <variant>
#include <vector>
#include <wx/string.h>
namespace cloud::audiocom
{
class ServiceConfig;
class OAuthService;
//! This structure represents an upload error as returned by the server.
struct CLOUD_AUDIOCOM_API UploadFailedPayload final
{
int32_t code {};
int32_t status {};
std::string name;
std::string message;
using AdditionalError = std::pair<std::string, std::string>;
std::vector<AdditionalError> additionalErrors;
};
//! This structure represents the payload associated with successful upload
struct CLOUD_AUDIOCOM_API UploadSuccessfulPayload final
{
//! ID of the uploaded audio
std::string audioId;
//! "Slug" to be used for shareable URL construction
std::string audioSlug;
//! Upload token, if any
std::string uploadToken;
//! URL to the uploaded audio
std::string audioUrl;
};
//! Message that is sent when upload is finished.
//! This message is sent from the network thread.
struct CLOUD_AUDIOCOM_API UploadOperationCompleted final
{
//! Result of the upload
enum class Result
{
//! Upload was successful
Success,
//! Upload was aborted by the user
Aborted,
//! Specified file is not found
FileNotFound,
//! Authorization is required
Unauthorized,
//! audio.com has failed to understand what Audacity wants
InvalidData,
//! Audacity has failed to understand audio.com response
UnexpectedResponse,
//! Upload failed for some other reason
UploadFailed
};
//! Upload result
Result result;
using Payload = std::variant<
std::monostate,
UploadSuccessfulPayload,
UploadFailedPayload
>;
//! Operation payload
Payload payload;
};
//! Class used to track the upload operation
class CLOUD_AUDIOCOM_API UploadOperation
{
public:
virtual ~UploadOperation();
//! Returns true if the upload is finished
virtual bool IsCompleted() = 0;
//! Abort the upload, if running
virtual void Abort() = 0;
//! Abort the upload, if running,
//! notify audio.com that the uploaded file is no longer needed otherwise
virtual void DiscardResult() = 0;
};
//! A unique_ptr like class that holds a pointer to UploadOperation
class CLOUD_AUDIOCOM_API UploadOperationHandle final
{
public:
UploadOperationHandle() = default;
explicit UploadOperationHandle(std::shared_ptr<UploadOperation> operation);
UploadOperationHandle(const UploadOperationHandle&) = delete;
UploadOperationHandle(UploadOperationHandle&&) = default;
UploadOperationHandle& operator=(const UploadOperationHandle&) = delete;
UploadOperationHandle& operator=(UploadOperationHandle&&) = default;
~UploadOperationHandle();
explicit operator bool() const noexcept;
UploadOperation* operator->() const noexcept;
private:
std::shared_ptr<UploadOperation> mOperation {};
};
//! Service, responsible for uploading audio files to audio.com
class CLOUD_AUDIOCOM_API UploadService final
{
public:
UploadService(const ServiceConfig& config, OAuthService& service);
using CompletedCallback =
std::function<void(const UploadOperationCompleted&)>;
using ProgressCallback = std::function<void(uint64_t current, uint64_t total)>;
//! Uploads the file to audio.com
/*
Both callbacks are invoked from the network thread.
If projectName is empty, the name for the uploaded file is deduced from the fileName.
*/
UploadOperationHandle Upload(
const wxString& fileName, const wxString& projectName, bool isPublic,
CompletedCallback completedCallback, ProgressCallback progressCallback);
private:
const ServiceConfig& mServiceConfig;
OAuthService& mOAuthService;
};
CLOUD_AUDIOCOM_API wxString GetUploadTempPath();
} // namespace cloud::audiocom