public DropboxFile UploadFile(string path, byte[] buffer) { path = "/" + path.TrimStart('/'); var uri = new Uri(ApiContentServer + "files/upload"); var webRequest = WebRequest.Create(uri); webRequest.ContentType = "application/octet-stream"; webRequest.Headers.Add("Authorization", "Bearer " + Accesstoken.access_token); var arg = "{\"path\": \"" + path + "\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}"; webRequest.Headers.Add("Dropbox-API-Arg", arg); var request = (HttpWebRequest)webRequest; request.Method = WebRequestMethods.Http.Post; request.KeepAlive = true; request.ContentLength = buffer.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(buffer, 0, buffer.Length); } var response = request.GetResponse(); var reader = new StreamReader(response.GetResponseStream()); var json = reader.ReadToEnd(); Hashtable o = (Hashtable)Nikse.Json.JSON.JsonDecode(json); DropboxFile fsi = new DropboxFile { Bytes = Convert.ToInt64(o["size"]), Path = o["path_display"].ToString().TrimStart('/'), IsDirectory = false }; fsi.Description = FormatBytesToDisplayFileSize(fsi.Bytes); return(fsi); }
//public Account GetAccountInfo() //{ // var uri = new Uri(new Uri(DropboxRestApi.BaseUri), "account/info"); // var json = GetResponse(uri); // return ParseJson<Account>(json); //} public DropboxFile GetFiles(string root, string path) { var uri = new Uri(new Uri(DropboxRestApi.BaseUri), String.Format("metadata/{0}/{1}", root, path)); var json = GetResponse(uri); Hashtable o = (Hashtable)Nikse.Json.JSON.JsonDecode(json); DropboxFile f = new DropboxFile(); f.Size = o["size"].ToString(); List <DropboxFile> list = new List <DropboxFile>(); foreach (Hashtable ht in (o["contents"] as System.Collections.ArrayList)) { DropboxFile file = new DropboxFile(); if (Convert.ToBoolean(ht["is_dir"]) == false) { file.Path = ht["path"].ToString().TrimStart('/').Trim(); file.IsDirectory = Convert.ToBoolean(ht["is_dir"]); file.Bytes = Convert.ToInt64(ht["bytes"]); file.Size = ht["size"].ToString().Trim(); file.Modified = DateTime.Parse(ht["modified"].ToString()); list.Add(file); } } f.Contents = list; return(f); }
public DropboxFile UploadFile(string root, string path, byte[] buffer) { var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer), String.Format("files_put/{0}/{1}", root, UpperCaseUrlEncode(path))); var oauth = new OAuth(); var requestUri = oauth.SignRequest(uri, _consumerKey, _consumerSecret, _accessToken, "PUT"); var request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = WebRequestMethods.Http.Put; request.KeepAlive = true; request.ContentLength = buffer.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(buffer, 0, buffer.Length); } var response = request.GetResponse(); var reader = new StreamReader(response.GetResponseStream()); var json = reader.ReadToEnd(); //return ParseJson<FileSystemInfo>(json); Hashtable o = (Hashtable)Nikse.Json.JSON.JsonDecode(json); DropboxFile fsi = new DropboxFile(); fsi.Bytes = Convert.ToInt64(o["bytes"]); fsi.Path = o["path"].ToString().TrimStart('/'); fsi.IsDirectory = Convert.ToBoolean(o["is_dir"]); fsi.Size = o["size"].ToString().Trim(); return(fsi); }
public DropboxFile DownloadFile(DropboxFile target) { var uri = new Uri(new Uri(ApiContentServer), "files/download"); using (Stream responseStream = GetResponseStream(uri, "{\"path\":\"/" + target.Path.TrimStart('/') + "\"}")) using (MemoryStream memoryStream = new MemoryStream()) { byte[] buffer = new byte[1024]; int bytesRead; do { bytesRead = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); target.Data = memoryStream.ToArray(); } return(target); }
//public FileSystemInfo CreateFolder(string root, string path) //{ // var uri = new Uri(new Uri(DropboxRestApi.BaseUri), // String.Format("fileops/create_folder?root={0}&path={1}", // root, UpperCaseUrlEncode(path))); // var json = GetResponse(uri); // return ParseJson<FileSystemInfo>(json); //} public DropboxFile DownloadFile(string root, string path) { var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer), String.Format("files?root={0}&path={1}", root, UpperCaseUrlEncode(path))); var oauth = new OAuth(); var requestUri = oauth.SignRequest(uri, _consumerKey, _consumerSecret, _accessToken); var request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = WebRequestMethods.Http.Get; var response = request.GetResponse(); var metadata = response.Headers["x-dropbox-metadata"]; //var file = ParseJson<FileSystemInfo>(metadata); Hashtable o = (Hashtable)Nikse.Json.JSON.JsonDecode(metadata); DropboxFile file = new DropboxFile(); file.Bytes = Convert.ToInt64(o["bytes"]); file.Path = o["path"].ToString().TrimStart('/').Trim(); file.IsDirectory = Convert.ToBoolean(o["is_dir"]); file.Size = o["size"].ToString().Trim(); using (Stream responseStream = response.GetResponseStream()) using (MemoryStream memoryStream = new MemoryStream()) { byte[] buffer = new byte[1024]; int bytesRead; do { bytesRead = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); file.Data = memoryStream.ToArray(); } return(file); }
public DropboxFile GetFiles(string path) { if (path.Length > 0) { path = "/" + path.TrimStart('/'); } var uri = new Uri(new Uri(DropboxApi.BaseUri), "files/list_folder"); var json = GetResponse(uri, "{\"path\":\"" + path + "\"}"); var o = (Hashtable)Nikse.Json.JSON.JsonDecode(json); var f = new DropboxFile(); List <DropboxFile> list = new List <DropboxFile>(); foreach (Hashtable ht in (o["entries"] as ArrayList)) { var file = new DropboxFile(); var tag = ht[".tag"].ToString().ToLowerInvariant(); if (tag == "file" || tag == "folder") { file.Path = ht["path_display"].ToString().TrimStart('/').Trim(); file.IsDirectory = tag == "folder"; if (tag == "file") { file.Bytes = Convert.ToInt64(ht["size"].ToString().Trim(), CultureInfo.InvariantCulture); file.Description = FormatBytesToDisplayFileSize(file.Bytes); file.Modified = DateTime.Parse(ht["client_modified"].ToString()); } else { file.Description = "<Folder>"; } list.Add(file); } } f.Contents = list; return(f); }