1using System.Globalization;
4using System.Text.Json.Nodes;
20 provider.m_loginProcessData =
null;
25 provider.m_loginProcessData =
null;
70 throw new InvalidOperationException(
"登录已经在进程中");
73 throw new InvalidOperationException(
"网络连接错误");
76 progress.Cancelled += delegate {
80 loginProcessData.
Fail(
this,
null);
86 catch (Exception obj) {
97 public void List(
string path,
CancellableProgress progress, Action<ExternalContentEntry> success, Action<Exception> failure) {
100 Dictionary<string, string> dictionary =
new() {
101 {
"Authorization", $
"Bearer {SettingsManager.ScpboxAccessToken}" }, {
"Content-Type",
"application/json" }
103 JsonObject jsonObject =
new() {
105 {
"recursive",
false },
106 {
"include_media_info",
false },
107 {
"include_deleted",
false },
108 {
"include_has_explicit_shared_members",
false }
110 MemoryStream data =
new(Encoding.UTF8.GetBytes(jsonObject.ToJsonString()));
112 $
"{CommunityServerManager.CurrentChineseInfo.ApiUrl}/com/files/list_folder",
117 delegate(
byte[] result) {
121 catch (Exception obj2) {
128 catch (Exception obj) {
136 JsonObject jsonObject =
new() { {
"path",
NormalizePath(path) } };
137 Dictionary<string, string> dictionary =
new() {
138 {
"Authorization", $
"Bearer {SettingsManager.ScpboxAccessToken}" }, {
"Dropbox-API-Arg", jsonObject.ToJsonString() }
141 $
"{CommunityServerManager.CurrentChineseInfo.ApiUrl}/com/files/download",
145 delegate(
byte[] result) { success(
new MemoryStream(result)); },
149 catch (Exception obj) {
157 JsonObject jsonObject =
new() { {
"path",
NormalizePath(path) }, {
"mode",
"add" }, {
"autorename",
true }, {
"mute",
false } };
158 Dictionary<string, string> dictionary =
new() {
159 {
"Authorization", $
"Bearer {SettingsManager.ScpboxAccessToken}" },
160 {
"Content-Type",
"application/octet-stream" },
161 {
"Dropbox-API-Arg", jsonObject.ToJsonString() }
164 $
"{CommunityServerManager.CurrentChineseInfo.ApiUrl}/com/files/upload",
169 delegate { success(
null); },
173 catch (Exception obj) {
181 Dictionary<string, string> dictionary =
new() {
182 {
"Authorization", $
"Bearer {SettingsManager.ScpboxAccessToken}" }, {
"Content-Type",
"application/json" }
184 JsonObject jsonObject =
new() { {
"path",
NormalizePath(path) }, {
"short_url",
false } };
185 MemoryStream data =
new(Encoding.UTF8.GetBytes(jsonObject.ToJsonString()));
187 $
"{CommunityServerManager.CurrentChineseInfo.ApiUrl}/com/sharing/create_shared_link",
192 delegate(
byte[] result) {
196 catch (Exception obj2) {
203 catch (Exception obj) {
211 login.succ = delegate(
byte[] a) {
212 StreamReader streamReader =
new(
new MemoryStream(a));
214 if (json.GetProperty(
"code").GetInt32() == 200) {
215 JsonElement data = json.GetProperty(
"data");
218 string nickName = data.GetProperty(
"nickName").GetString() ??
string.Empty;
230 ).AddSeconds(data.GetProperty("loginTime
").GetInt64()),
241 m_loginProcessData = null;
242 DialogsManager.HideAllDialogs();
248 login.tip.Text = json.GetProperty(
"msg").GetString();
253 $
"登录失败:{login.tip.Text}",
257 m_loginProcessData = null;
258 DialogsManager.HideAllDialogs();
264 login.fail = delegate(Exception e) {
265 login.tip.Text = e.ToString();
274 m_loginProcessData = null;
275 DialogsManager.HideAllDialogs();
280 login.cancel = delegate {
286 catch (Exception error) {
352 if (loginProcessData.IsTokenFlow) {
355 ||
string.IsNullOrEmpty(uri.Fragment)) {
356 throw new Exception(
"不能接收来自Schub的身份验证信息");
358 Dictionary<string, string> dictionary = WebManager.UrlParametersFromString(uri.Fragment.TrimStart(
'#'));
359 if (!dictionary.TryGetValue(
"access_token", out
string value)) {
360 if (dictionary.TryGetValue(
"error", out
string value1)) {
361 throw new Exception(value1);
363 throw new Exception(
"不能接收来自Schub的身份验证信息");
366 loginProcessData.Succeed(
this);
368 catch (Exception error) {
369 loginProcessData.Fail(
this, error);
376 throw new InvalidOperationException(
"这个应用未登录到Schub中国社区");
381 ExternalContentEntry externalContentEntry =
new();
382 if (jsonElement.TryGetProperty(
"entries", out JsonElement entries)) {
383 foreach (JsonProperty item
in entries.EnumerateObject()) {
384 ExternalContentEntry externalContentEntry2 =
new() { Path = item.Value.GetProperty(
"path_display").GetString() };
385 externalContentEntry2.Type = item.Value.GetProperty(
".tag").GetString() ==
"folder"
386 ? ExternalContentType.Directory
387 : ExternalContentManager.ExtensionToType(Storage.GetExtension(externalContentEntry2.
Path));
389 externalContentEntry2.Time = item.Value.TryGetProperty(
"server_modified", out JsonElement server_modified)
390 ? DateTime.Parse(server_modified.GetString(), CultureInfo.InvariantCulture)
391 :
new DateTime(2000, 1, 1);
392 externalContentEntry2.Size = item.Value.TryGetProperty(
"size", out JsonElement size) ? size.GetInt64() : 0;
394 externalContentEntry.
ChildEntries.Add(externalContentEntry2);
397 return externalContentEntry;
402 if (jsonElement.TryGetProperty(
"url", out JsonElement url)) {
403 return $
"{url.GetString()?.Replace("www.dropbox.
", "dl.dropbox.
").Replace("?dl=0
", "")}?dl=1";
405 throw new InvalidOperationException(
"Share information not found.");
static void HideAllDialogs()
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
List< ExternalContentEntry > ChildEntries
static readonly JsonDocumentOptions DefaultJsonOptions
static Action< Uri > HandleUri
CancellableProgress Progress
Action< Exception > Failure
void Fail(SchubExternalContentProvider provider, Exception error)
void Succeed(SchubExternalContentProvider provider)
static ExternalContentEntry JsonElementToEntry(JsonElement jsonElement)
void LoginLaunchBrowser()
void Upload(string path, Stream stream, CancellableProgress progress, Action< string > success, Action< Exception > failure)
LoginProcessData m_loginProcessData
void Link(string path, CancellableProgress progress, Action< string > success, Action< Exception > failure)
static string JsonElementToLinkAddress(JsonElement jsonElement)
void List(string path, CancellableProgress progress, Action< ExternalContentEntry > success, Action< Exception > failure)
SchubExternalContentProvider()
void Download(string path, CancellableProgress progress, Action< Stream > success, Action< Exception > failure)
static string NormalizePath(string path)
void Login(CancellableProgress progress, Action success, Action< Exception > failure)
static string ScpboxUserInfo
static string ScpboxAccessToken
static void Post(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, Stream data, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static bool IsInternetConnectionAvailable()
static void Get(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)