bit more cleaning up and tweaks to multithreading · 18c8183d97 - SVN.BY: Go Git Service
浏览代码

bit more cleaning up and tweaks to multithreading

Related Work Items: #21, #23, #26
Tiernan OToole 10 年之前
父节点
当前提交
18c8183d97
共有 1 个文件被更改,包括 68 次插入64 次删除
  1. 68 64
      B2Uploader/Program.cs

+ 68 - 64
B2Uploader/Program.cs

20
         [Option('a', "appkey", HelpText = "Application Key", Required=true)]
20
         [Option('a', "appkey", HelpText = "Application Key", Required=true)]
21
         public string ApplicationKey { get; set; }
21
         public string ApplicationKey { get; set; }
22
 
22
 
23
-        [Option('d', "directory", HelpText="Directory you want to upload", Required=true)]
24
-        public string Directory {get;set;}
25
-
23
+        [Option('d', "directory", HelpText = "Directory you want to upload", Required = true)]
24
+        public string Directory { get; set; }
25
+        
26
         [Option('v', "verbose", HelpText="Verbose Output")]
26
         [Option('v', "verbose", HelpText="Verbose Output")]
27
         public bool Verbose{get;set;}
27
         public bool Verbose{get;set;}
28
     }
28
     }
51
                 {
51
                 {
52
                     //check if file already exists
52
                     //check if file already exists
53
 
53
 
54
-                    string fileName = s.Replace('\\', '_');
54
+                    string fileName = s.Replace('\\', '/');
55
 
55
 
56
                     var existingFiles = ListFileNames(new ListFileNamesRequest() { bucketId = bucket.bucketId, startFileName = fileName }, auth.apiUrl, auth.authorizationToken);
56
                     var existingFiles = ListFileNames(new ListFileNamesRequest() { bucketId = bucket.bucketId, startFileName = fileName }, auth.apiUrl, auth.authorizationToken);
57
                     bool found = false;
57
                     bool found = false;
61
                         {
61
                         {
62
                             //check the file size
62
                             //check the file size
63
                             System.IO.FileInfo fi = new System.IO.FileInfo(s);
63
                             System.IO.FileInfo fi = new System.IO.FileInfo(s);
64
+                            
64
                             if (fi.Length == x.size)
65
                             if (fi.Length == x.size)
65
                             {
66
                             {
66
                                 found = true;
67
                                 found = true;
92
         }
93
         }
93
 
94
 
94
         static AuthorizeResponse AuthorizeUser(string accountId, string applicationKey)
95
         static AuthorizeResponse AuthorizeUser(string accountId, string applicationKey)
95
-        {            
96
+        {
96
             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.backblaze.com/b2api/v1/b2_authorize_account");
97
             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.backblaze.com/b2api/v1/b2_authorize_account");
97
             string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(accountId + ":" + applicationKey));
98
             string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(accountId + ":" + applicationKey));
98
             webRequest.Headers.Add("Authorization", "Basic " + credentials);
99
             webRequest.Headers.Add("Authorization", "Basic " + credentials);
103
             return JsonConvert.DeserializeObject<AuthorizeResponse>(responseString);
104
             return JsonConvert.DeserializeObject<AuthorizeResponse>(responseString);
104
         }
105
         }
105
 
106
 
106
-        static ListBucketsResponse ListBuckets(ListBucketsRequest request, string authToken, string apiURL)
107
-        {            
108
-            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiURL + "/b2api/v1/b2_list_buckets");
109
-            string body = JsonConvert.SerializeObject(request);
110
-            var data = Encoding.UTF8.GetBytes(body);
111
-            webRequest.Method = "POST";
112
-            webRequest.Headers.Add("Authorization", authToken);
113
-            webRequest.ContentType = "application/json; charset=utf-8";
114
-            webRequest.ContentLength = data.Length;
115
-            using (var stream = webRequest.GetRequestStream())
116
-            {
117
-                stream.Write(data, 0, data.Length);
118
-                stream.Close();
119
-            }
120
-            WebResponse response = (HttpWebResponse)webRequest.GetResponse();
121
-            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
122
-            response.Close();
107
+        static ListBucketsResponse ListBuckets(ListBucketsRequest request, string authToken, string apiUrl)
108
+        {
109
+            var headers = GetAuthHeaders(authToken);
110
+
111
+            string responseString = MakeWebRequest(apiUrl + "/b2api/v1/b2_list_buckets", headers, request);
112
+
123
 
113
 
124
             return JsonConvert.DeserializeObject<ListBucketsResponse>(responseString);
114
             return JsonConvert.DeserializeObject<ListBucketsResponse>(responseString);
125
         }
115
         }
126
 
116
 
117
+        static List<Tuple<string,string>> GetAuthHeaders(string authToken)
118
+        {
119
+            List<Tuple<string, string>> headers = new List<Tuple<string, string>>();
120
+            headers.Add(new Tuple<string, string>("Authorization", authToken));
121
+            return headers;
122
+        }
123
+
127
         static GetUploadURLResponse GetUploadURL(GetUploadURLRequest request, string apiUrl, string authToken)
124
         static GetUploadURLResponse GetUploadURL(GetUploadURLRequest request, string apiUrl, string authToken)
128
-        {          
129
-            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_get_upload_url");
130
-            string body = JsonConvert.SerializeObject(request);
131
-            var data = Encoding.UTF8.GetBytes(body);
132
-            webRequest.Method = "POST";
133
-            webRequest.Headers.Add("Authorization", authToken);
134
-            webRequest.ContentType = "application/json; charset=utf-8";
135
-            webRequest.ContentLength = data.Length;
136
-            using (var stream = webRequest.GetRequestStream())
137
-            {
138
-                stream.Write(data, 0, data.Length);
139
-                stream.Close();
140
-            }
141
-            WebResponse response = (HttpWebResponse)webRequest.GetResponse();
142
-            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
143
-            response.Close();
125
+        {
126
+
127
+            var headers = GetAuthHeaders(authToken); 
128
+            string responseString = MakeWebRequest(apiUrl + "/b2api/v1/b2_get_upload_url", headers, request);
129
+            
144
             return JsonConvert.DeserializeObject<GetUploadURLResponse>(responseString);
130
             return JsonConvert.DeserializeObject<GetUploadURLResponse>(responseString);
145
         }
131
         }
146
 
132
 
148
         {
134
         {
149
 
135
 
150
             byte[] bytes = System.IO.File.ReadAllBytes(filePath);
136
             byte[] bytes = System.IO.File.ReadAllBytes(filePath);
151
-
152
             String sha1 = GetSha1(bytes);
137
             String sha1 = GetSha1(bytes);
153
-            
154
-            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
155
-            webRequest.Method = "POST";
156
-            webRequest.Headers.Add("Authorization", authToken);
157
-            webRequest.Headers.Add("X-Bz-File-Name", filePath.Replace('\\','_'));
158
-            webRequest.Headers.Add("X-Bz-Content-Sha1", sha1);
159
-            webRequest.ContentType = contentType;
160
-            using (var stream = webRequest.GetRequestStream())
161
-            {
162
-                stream.Write(bytes, 0, bytes.Length);
163
-                stream.Close();
164
-            }
165
-            WebResponse response = (HttpWebResponse)webRequest.GetResponse();
166
-            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
167
-            response.Close();
138
+
139
+            var headers = GetAuthHeaders(authToken);
140
+
141
+            headers.Add(new Tuple<string, string>("X-Bz-File-Name", filePath.Replace('\\', '/')));
142
+            headers.Add(new Tuple<string, string>("X-Bz-Content-Sha1", sha1));
143
+
144
+            string responseString = MakeWebRequest(uploadUrl, headers, bytes, contentType);
145
+
168
             var resp = JsonConvert.DeserializeObject<UploadFileResponse>(responseString);
146
             var resp = JsonConvert.DeserializeObject<UploadFileResponse>(responseString);
169
 
147
 
170
             if(resp.contentSha1 == sha1)
148
             if(resp.contentSha1 == sha1)
181
 
159
 
182
         static ListFileNamesResponse ListFileNames(ListFileNamesRequest request, string apiUrl, string authToken)
160
         static ListFileNamesResponse ListFileNames(ListFileNamesRequest request, string apiUrl, string authToken)
183
         {
161
         {
162
+            var headers = GetAuthHeaders(authToken);
163
+            string responseString =  MakeWebRequest(apiUrl + "/b2api/v1/b2_list_file_names", headers, request);
164
+
165
+            return JsonConvert.DeserializeObject<ListFileNamesResponse>(responseString);
166
+        }
167
+
168
+        static string MakeWebRequest<T>(string url, List<Tuple<string,string>> headers, T item, string contentType = "application/json; charset=utf-8")
169
+        {
170
+            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
171
+            byte[] data;
172
+
173
+            if (typeof(T) == typeof(byte[]))
174
+            {
175
+                data = (byte[])(object) item;
176
+            }
177
+            else {
178
+                string body = JsonConvert.SerializeObject(item);
179
+
180
+                data = Encoding.UTF8.GetBytes(body);
181
+            }
184
             
182
             
185
-            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_list_file_names");
186
-            string body = JsonConvert.SerializeObject(request);
187
-            var data = Encoding.UTF8.GetBytes(body);
188
-            webRequest.Method = "POST";
189
-            webRequest.Headers.Add("Authorization", authToken);
190
-            webRequest.ContentType = "application/json; charset=utf-8";
191
-            webRequest.ContentLength = data.Length;
192
-            using (var stream = webRequest.GetRequestStream())
183
+            req.Method = "POST";
184
+
185
+            foreach(var head in headers)
186
+            {
187
+                req.Headers.Add(head.Item1, head.Item2);
188
+            }
189
+
190
+            req.ContentType = contentType;
191
+            req.ContentLength = data.Length;
192
+            using (var stream = req.GetRequestStream())
193
             {
193
             {
194
                 stream.Write(data, 0, data.Length);
194
                 stream.Write(data, 0, data.Length);
195
                 stream.Close();
195
                 stream.Close();
196
             }
196
             }
197
-            WebResponse response = (HttpWebResponse)webRequest.GetResponse();
197
+            WebResponse response = (HttpWebResponse)req.GetResponse();
198
             var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
198
             var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
199
             response.Close();
199
             response.Close();
200
-            return JsonConvert.DeserializeObject<ListFileNamesResponse>(responseString);
200
+
201
+            return responseString;
202
+
203
+
204
+
201
         }
205
         }
202
 
206
 
203
         private static string GetSha1(byte[] bytes)
207
         private static string GetSha1(byte[] bytes)