-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrain.cs
More file actions
148 lines (147 loc) · 5.78 KB
/
Copy pathBrain.cs
File metadata and controls
148 lines (147 loc) · 5.78 KB
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
using System.Text.Json;
namespace TTMC.Mixdrop
{
public class Mixdrop
{
private string email;
private string key;
private string domain;
private HttpClient client;
public Mixdrop(string email, string key, string domain = "mixdrop.ag")
{
this.email = email;
this.key = key;
this.domain = domain;
client = new();
}
public string Upload(string fileName, byte[] data, int? folderID = null)
{
MultipartFormDataContent content = new();
ByteArrayContent bytes = new(data);
content.Add(bytes, "file", Path.GetFileName(fileName));
content.Add(new StringContent(email), "email");
content.Add(new StringContent(key), "key");
if (folderID != null)
{
content.Add(new StringContent(folderID.Value.ToString()), "folder");
}
HttpResponseMessage response = client.PostAsync($"https://ul.{domain}/api", content).Result;
string resp = response.Content.ReadAsStringAsync().Result;
FileStatus fileStatus = ErrorCheck<FileStatus>(resp);
return fileStatus.fileref ?? string.Empty;
}
public string Upload(string fileName, Stream stream, int? folderID = null)
{
MultipartFormDataContent content = new();
StreamContent bytes = new(stream);
content.Add(bytes, "file", Path.GetFileName(fileName));
content.Add(new StringContent(email), "email");
content.Add(new StringContent(key), "key");
if (folderID != null)
{
content.Add(new StringContent(folderID.Value.ToString()), "folder");
}
HttpResponseMessage response = client.PostAsync($"https://ul.{domain}/api", content).Result;
string resp = response.Content.ReadAsStringAsync().Result;
FileStatus fileStatus = ErrorCheck<FileStatus>(resp);
return fileStatus.fileref ?? string.Empty;
}
public string AddSubtitle(string path, string language)
{
MultipartFormDataContent content = new();
byte[] file = System.IO.File.ReadAllBytes(path);
ByteArrayContent bytes = new(file);
content.Add(bytes, "file", Path.GetFileName(path));
content.Add(new StringContent(language), "lang");
HttpResponseMessage response = client.PostAsync($"https://api.{domain}/addsubtitle?email=" + email + "&key=" + key, content).Result;
string tmp = response.Content.ReadAsStringAsync().Result;
return ErrorCheck<string>(tmp);
}
public FileStatus RemoteUpload(string url, string? name = null, int? folderID = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/remoteupload?email=" + email + "&key=" + key + "&url=" + url + (name == null ? string.Empty : "&name=" + name) + (folderID == null ? string.Empty : "&folder=" + folderID.Value)).Result;
return ErrorCheck<FileStatus>(tmp);
}
public FileStatus RemoteStatus(int? ID = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/remotestatus?email=" + email + "&key=" + key + (ID == null ? string.Empty : "&id=" + ID)).Result;
return ErrorCheck<FileStatus>(tmp);
}
public Dictionary<string, CollectionFile> FileInfo(params string[] fileref)
{
string url = $"https://api.{domain}/fileinfo2?email=" + email + "&key=" + key;
foreach (string value in fileref)
{
url += "&ref[]=" + value;
}
string tmp = client.GetStringAsync(url).Result;
return ErrorCheck<Dictionary<string, CollectionFile>>(tmp);
}
public Collection FolderList(int? folderID = null, int? page = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/folderlist?email=" + email + "&key=" + key + (folderID == null ? string.Empty : "&id=" + folderID.Value) + (page == null ? string.Empty : "&page=" + page.Value)).Result;
try
{
return ErrorCheck<Collection>(tmp);
}
catch
{
return new Collection() { files = new(), folders = new() };
}
}
public int CreateFolder(string title, int? parent = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/foldercreate?email=" + email + "&key=" + key + "&title=" + title + (parent == null ? string.Empty : "&parent=" + parent.Value)).Result;
ID id = ErrorCheck<ID>(tmp);
return id.id;
}
public IEnumerable<CollectionFile> RemovedFiles(int? page = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/removed?email=" + email + "&key=" + key + (page == null ? string.Empty : "&page=" + page.Value)).Result;
return ErrorCheck<IEnumerable<CollectionFile>>(tmp);
}
public string FileDuplicate(string fileref, int? folder = null)
{
string tmp = client.GetStringAsync($"https://api.{domain}/fileduplicate?email=" + email + "&key=" + key + "&ref=" + fileref + (folder == null ? string.Empty : "&folder=" + folder)).Result;
FileStatus fileStatus = ErrorCheck<FileStatus>(tmp);
return fileStatus.fileref ?? string.Empty;
}
public string FileRename(string fileref, string title)
{
string tmp = client.GetStringAsync($"https://api.{domain}/filerename?email=" + email + "&key=" + key + "&ref=" + fileref + "&title=" + title).Result;
Title fileStatus = ErrorCheck<Title>(tmp);
return fileStatus.title ?? string.Empty;
}
public string FolderRename(int id, string title)
{
string tmp = client.GetStringAsync($"https://api.{domain}/folderrename?email=" + email + "&key=" + key + "&id=" + id + "&title=" + title).Result;
Title temp = ErrorCheck<Title>(tmp);
return temp.title ?? string.Empty;
}
public T ErrorCheck<T>(string json)
{
if (json.StartsWith('{') && json.EndsWith('}'))
{
Item? item = JsonSerializer.Deserialize<Item>(json);
if (item != null && item.result != null)
{
JsonElement result = (JsonElement)item.result;
string raw = result.GetRawText();
if (item.success)
{
T? response = JsonSerializer.Deserialize<T>(raw);
if (response != null)
{
return response;
}
}
else if (result.TryGetProperty("msg", out JsonElement error))
{
throw new(error.GetString());
}
}
}
throw new(json);
}
}
}