starting work on 20 · de5ce69ea0 - SVN.BY: Go Git Service
Преглед изворни кода

starting work on 20

Related Work Items: #20
Tiernan OToole пре 8 година
родитељ
комит
de5ce69ea0
2 измењених фајлова са 65 додато и 1 уклоњено
  1. 22 0
      B2Classes/Classes.cs
  2. 43 1
      B2Uploader/Program.cs

+ 22 - 0
B2Classes/Classes.cs

75
         public string contentType { get; set; }
75
         public string contentType { get; set; }
76
         public FileInfo fileInfo { get; set; }
76
         public FileInfo fileInfo { get; set; }
77
     }
77
     }
78
+
79
+    public class ListFileNamesRequest
80
+    {
81
+        public string bucketId { get; set; }
82
+        public string startFileName { get; set; }
83
+        public int maxFileCount { get; set; }
84
+    }
85
+
86
+    public class File
87
+    {
88
+        public string action { get; set; }
89
+        public string fileId { get; set; }
90
+        public string fileName { get; set; }
91
+        public int size { get; set; }
92
+        public object uploadTimestamp { get; set; }
93
+    }
94
+
95
+    public class ListFileNamesResponse 
96
+    {
97
+        public List<File> files { get; set; }
98
+        public string nextFileName { get; set; }
99
+    }
78
 }
100
 }

+ 43 - 1
B2Uploader/Program.cs

47
 
47
 
48
                 foreach(string s in Directory.GetFiles(options.Directory))
48
                 foreach(string s in Directory.GetFiles(options.Directory))
49
                 {
49
                 {
50
+                    //check if file already exists
51
+
52
+                    string fileName = s.Replace('\\', '_');
53
+
54
+                    var existingFiles = ListFileNames(new ListFileNamesRequest() { bucketId = bucket.bucketId, startFileName = fileName }, auth.apiUrl, auth.authorizationToken);
55
+                    bool found = false;
56
+                    foreach (var x in existingFiles.files)
57
+                    {
58
+                        if (x.fileName == fileName)
59
+                        {
60
+                            found = true;
61
+                            break;
62
+                        }
63
+                    }
64
+                    if (found)
65
+                    {
66
+                        Console.WriteLine("File exists already, skipping");
67
+                        continue;
68
+                    }
69
+
70
+
50
                     var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
71
                     var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
51
                     var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
72
                     var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
52
                 }
73
                 }
114
         static UploadFileResponse UploadFile(string authToken, string contentType, string filePath, string uploadUrl)
135
         static UploadFileResponse UploadFile(string authToken, string contentType, string filePath, string uploadUrl)
115
         {
136
         {
116
 
137
 
117
-            byte[] bytes = File.ReadAllBytes(filePath);
138
+            byte[] bytes = System.IO.File.ReadAllBytes(filePath);
118
 
139
 
119
             String sha1 = GetSha1(bytes);
140
             String sha1 = GetSha1(bytes);
120
             
141
             
146
             }
167
             }
147
         }
168
         }
148
 
169
 
170
+        static ListFileNamesResponse ListFileNames(ListFileNamesRequest request, string apiUrl, string authToken)
171
+        {
172
+            
173
+            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_list_file_names");
174
+            string body = JsonConvert.SerializeObject(request);
175
+            var data = Encoding.UTF8.GetBytes(body);
176
+            webRequest.Method = "POST";
177
+            webRequest.Headers.Add("Authorization", authToken);
178
+            webRequest.ContentType = "application/json; charset=utf-8";
179
+            webRequest.ContentLength = data.Length;
180
+            using (var stream = webRequest.GetRequestStream())
181
+            {
182
+                stream.Write(data, 0, data.Length);
183
+                stream.Close();
184
+            }
185
+            WebResponse response = (HttpWebResponse)webRequest.GetResponse();
186
+            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
187
+            response.Close();
188
+            return JsonConvert.DeserializeObject<ListFileNamesResponse>(responseString);
189
+        }
190
+
149
         private static string GetSha1(byte[] bytes)
191
         private static string GetSha1(byte[] bytes)
150
         {
192
         {
151
             using (SHA1Managed sha1 = new SHA1Managed())
193
             using (SHA1Managed sha1 = new SHA1Managed())