Audacity 3.2.0
ServiceConfig.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 ServiceConfig.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#include "ServiceConfig.h"
12#include "Languages.h"
13
14#include <cassert>
15#include <stdexcept>
16#include <string_view>
17
18#include <rapidjson/document.h>
19
20#include "Prefs.h"
21#include "CodeConversions.h"
22
23
24
25namespace cloud::audiocom
26{
27namespace
28{
30 L"/CloudServices/AudioCom/ApiEndpoint",
31 L"https://api.audio.com"
32};
33
35 L"/CloudServices/AudioCom/OAuthClientID",
36 L"1741964426607541"
37};
38
40 L"/CloudServices/AudioCom/OAuthClientSecret",
41 L"shKqnY2sLTfRK7hztwzNEVxnmhJfOy1i"
42};
43
45 L"/CloudServices/AudioCom/OAuthRedirectURL",
46 L"https://audio.com/auth/sign-in/success"
47};
48
50 L"/CloudServices/AudioCom/OAuthLoginPage",
51 L"https://audio.com/audacity/link?clientId={auth_client_id}"
52};
53
55 L"/CloudServices/AudioCom/FinishUploadPage",
56 L"https://audio.com/audacity/upload?audioId={audio_id}&token={auth_token}&clientId={auth_client_id}"
57};
58
60 L"/CloudServices/AudioCom/AudioURL",
61 L"https://audio.com/{user_slug}/audio/{audio_slug}/edit"
62};
63
65 L"/CloudServices/AudioCom/DownloadMimeType",
66 L"audio/x-wav"
67};
68
69std::string Substitute(std::string pattern, std::initializer_list<std::pair<std::string_view, std::string_view>> substitutions)
70{
71 for(auto& [key, value] : substitutions)
72 {
73 auto pos = pattern.find(key);
74
75 if (pos > 0 && pos != std::string::npos)
76 {
77 // There is no need to check that pos + key.size() is valid, there
78 // will be a zero terminator in the worst case.
79 if (pattern[pos - 1] == '{' && pattern[pos + key.size()] == '}')
80 pattern.replace(pos - 1, key.size() + 2, value);
81 }
82 }
83
84 return std::move(pattern);
85}
86
87} // namespace
88
90{
99}
100
102{
103 return mApiEndpoint;
104}
105
107{
108 return Substitute(
109 mOAuthLoginPage, { { "auth_client_id", GetOAuthClientID() } });
110}
111
113{
114 return mOAuthClientID;
115}
116
118{
119 return mOAuthClientSecret;
120}
121
123{
124 return mOAuthRedirectURL;
125}
126
127std::string ServiceConfig::GetAPIUrl(std::string_view apiURI) const
128{
129 return mApiEndpoint + std::string(apiURI);
130}
131
133 std::string_view audioID, std::string_view token) const
134{
135 return Substitute(
136 mFinishUploadPage, { { "audio_id", audioID },
137 { "auth_token", token },
138 { "auth_client_id", mOAuthClientID } });
139}
140
142 std::string_view userSlug, std::string_view audioSlug) const
143{
144 return Substitute(
145 mAudioURL, { { "user_slug", userSlug }, { "audio_slug", audioSlug } });
146}
147
148std::chrono::milliseconds ServiceConfig::GetProgressCallbackTimeout() const
149{
150 return std::chrono::seconds(3);
151}
152
153std::vector<std::string> ServiceConfig::GetPreferredAudioFormats() const
154{
155 return { "audio/x-wavpack", "audio/x-flac", "audio/x-wav" };
156}
157
158rapidjson::Document ServiceConfig::GetExportConfig(const std::string& mimeType) const
159{
160 if(mimeType == "audio/x-wavpack")
161 {
162 rapidjson::Document config;
163 config.SetObject();
164 config.AddMember("quality", rapidjson::Value(2), config.GetAllocator());
165 config.AddMember("bit_rate", rapidjson::Value(40), config.GetAllocator());
166 config.AddMember("bit_depth", 24, config.GetAllocator());
167 config.AddMember("hybrid_mode", false, config.GetAllocator());
168 return config;
169 }
170 if(mimeType == "audio/x-flac")
171 {
172 rapidjson::Document config;
173 config.SetObject();
174 config.AddMember("bit_depth", rapidjson::Value(24), config.GetAllocator());
175 config.AddMember("level", rapidjson::Value(5), config.GetAllocator());
176 }
177 if(mimeType == "audio/x-wav")
178 {
179 return {};
180 }
181 throw std::invalid_argument("unknown mime-type");
182}
183
184
186{
187 return mPreferredMimeType;
188}
189
191{
192 auto language = Languages::GetLang();
193
194 if (language.Contains(L"-") && language.Length() > 2)
195 return wxString::Format("%s;q=1.0, %s;q=0.7, *;q=0.5", language, language.Left(2)).ToStdString();
196 else
197 return wxString::Format("%s;q=1.0, *;q=0.5", language).ToStdString();
198}
199
201{
202 static ServiceConfig config;
203 return config;
204}
205
206} // namespace cloud::audiocom
Declare functions to perform UTF-8 to std::wstring conversions.
static const AudacityProject::AttachedObjects::RegisteredFactory key
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:205
Specialization of Setting for strings.
Definition: Prefs.h:368
Configuration for the audio.com.
Definition: ServiceConfig.h:23
std::string GetDownloadMime() const
std::string GetAcceptLanguageValue() const
Returns the preferred language.
std::string GetOAuthClientID() const
OAuth2 client ID.
rapidjson::Document GetExportConfig(const std::string &exporterName) const
Export configuration suitable for the mime type provided.
std::string GetOAuthRedirectURL() const
OAuth2 redirect URL. Only used to satisfy the protocol.
std::string GetAPIUrl(std::string_view apiURI) const
Helper to construct the full URLs for the API.
std::string GetAPIEndpoint() const
API endpoint.
std::chrono::milliseconds GetProgressCallbackTimeout() const
Timeout between progress callbacks.
std::string GetOAuthClientSecret() const
OAuth2 client secret.
std::string GetAudioURL(std::string_view userSlug, std::string_view audioSlug) const
Helper to construct the page URL for the authorised upload.
std::string GetFinishUploadPage(std::string_view audioID, std::string_view token) const
Helper to construct the page URL for the anonymous upload last stage.
std::string GetOAuthLoginPage() const
Page to open in browser to initiate OAuth.
std::vector< std::string > GetPreferredAudioFormats() const
Preferred audio format.
wxString GetLang()
Definition: Languages.cpp:395
constexpr size_t npos(-1)
std::string ToUTF8(const std::wstring &wstr)
std::string Substitute(std::string pattern, std::initializer_list< std::pair< std::string_view, std::string_view > > substitutions)
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.