-
Notifications
You must be signed in to change notification settings - Fork 1
/
Global.asax.cs
209 lines (150 loc) · 6.15 KB
/
Global.asax.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Newtonsoft.Json;
using Octokit;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace GithubImageLite
{
public class Global : System.Web.HttpApplication
{
public static No2DB.Base.DRole _Role { get; set; }
public static ConcurrentBag<Models.GitToken> _GitTokens { get; set; }
public static string ServerToken { get; set; }
public static string UploadPath { get; set; }
public static decimal PerRepoLimit { get; set; }
public static string ImageNotFound = "https://raw.githubusercontent.com/ctimggit/storage1/master/404.png";
public static bool IsWriting { get; set; }
protected void Application_Start(object sender, EventArgs e)
{
_Role = new No2DB.Base.DRole("GITHUBIMAGELITE");
var settingStr = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "site.configsettings");
var obj = JsonConvert.DeserializeObject<Models.ServerSetting>(settingStr);
PerRepoLimit = obj.LimitMB;
UploadPath = obj.UplpadPath;
ServerToken = obj.ServerToken;
GetAllGitTokens();
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
public static void UpdateGitToken(Models.GitToken token, decimal byteCount)
{
foreach (var c in _GitTokens)
{
if (c.Id == token.Id)
{
c.UsedMB += byteCount;
}
}
if (!IsWriting)
{
IsWriting = true;
token.UsedMB += byteCount;
Global._Role.GetOp("TOKENS").Update(token.Id, token);
IsWriting = false;
}
}
public static ConcurrentBag<Models.GitToken> GetAllGitTokens()
{
if (!IsWriting)
{
var tmp = Global._Role.GetQ<Models.GitToken>("TOKENS").AllDatasList();
tmp = tmp.Where(x => x.UsedMB <= PerRepoLimit).OrderBy(x => x.UsedMB).ThenBy(x => x.RepoName).ThenBy(x => x.CreateDate).ToList();
var t = new ConcurrentBag<Models.GitToken>();
foreach (var c in tmp)
{
t.Add(c);
}
_GitTokens = t;
}
return _GitTokens;
}
public static void ReCountTokenId(string tokeId)
{
var tokenInfo = Global._Role.GetQ<Models.GitToken>("TOKENS").DataByKey(tokeId);
if (tokenInfo == null)
{
return;
}
decimal count = 0;
//這 DONMATEST 可以任意都可以
var client = new GitHubClient(new ProductHeaderValue("GISL"));
//從網站上取得的 personal access token https://github.com/settings/tokens
var tokenAuth = new Credentials(tokenInfo.Token);
client.Credentials = tokenAuth;
try
{
var existingFiles = client.Repository.Content.GetAllContentsByRef(tokenInfo.UserName, tokenInfo.RepoName, "imgs", "master").Result;
if (existingFiles.Count > 0)
{
foreach (var f in existingFiles)
{
count += (f.Size / (1024m * 1024m));
}
}
}
catch
{
}
tokenInfo.UsedMB = count;
Global._Role.GetOp("TOKENS").Update(tokenInfo.Id, tokenInfo);
}
//a16e3baf8fa8784dc45ec4f49a9856c239992818
//
public static void UploadFile(string token, string userName, string reponame, string repoId, string localPath, string ownerId)
{
//這 DONMATEST 可以任意都可以
var client = new GitHubClient(new ProductHeaderValue("GISL"));
//從網站上取得的 personal access token https://github.com/settings/tokens
var tokenAuth = new Credentials(token);
client.Credentials = tokenAuth;
//try
//{
// var existingFiles = client.Repository.Content.GetAllContentsByRef(userName, reponame, "imgs", "master").Result;
// //如果有找到已存在就刪除
// foreach (var f in existingFiles)
// {
// if (f.Name == Path.GetFileName(localPath))
// {
// await client.Repository.Content.DeleteFile(long.Parse(repoId), "imgs/" + Path.GetFileName(localPath), new DeleteFileRequest("delete file", f.Sha));
// break;
// }
// }
//}
//catch
//{
//}
//最後一個參數是否要轉成 base64
var updateRequest = new UpdateFileRequest("Hi , Upload by GISL Tool. " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Convert.ToBase64String(File.ReadAllBytes(localPath)), "SHA", false);
var res = client.Repository.Content.UpdateFile(long.Parse(repoId), "imgs/" + Path.GetFileName(localPath), updateRequest).Result;
var img = new Models.ImageInfo();
img.Id = Path.GetFileNameWithoutExtension(localPath);
img.LastUpdate = DateTime.Now;
img.GitPath = res.Content.DownloadUrl;
img.Length = Convert.ToDecimal(res.Content.Size) / ((1024m * 1024m));
img.Owner = ownerId;
img.TokenId = userName + repoId;
Global._Role.GetOp("IMAGES").Update(img.Id, img);
}
}
}