public void Rename(string oldname, string newname) { try { try { var id = GetFileID(oldname); var url = string.Format("{0}/{1}?access_token={2}", WLID_SERVER, id, Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "PUT"; var updateData = new WLID_FolderItem() { name = newname }; var data = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(updateData)); req.ContentLength = data.Length; req.ContentType = "application/json; charset=UTF-8"; using (var requestStream = req.GetRequestStream()) requestStream.Write(data, 0, data.Length); var areq = new Utility.AsyncHttpRequest(req); using (var resp = (HttpWebResponse)areq.GetResponse()) { if (resp.StatusCode == System.Net.HttpStatusCode.NotFound) { throw new FileMissingException(); } if ((int)resp.StatusCode < 200 || (int)resp.StatusCode > 299) { throw new ProtocolViolationException(Strings.OneDrive.UnexpectedError(resp.StatusCode, resp.StatusDescription)); } m_fileidCache[newname] = id; m_fileidCache.Remove(oldname); } } catch { // Since we don't know the state of file IDs, clear the cache m_fileidCache.Clear(); throw; } } catch (System.Net.WebException wex) { if (wex.Response is System.Net.HttpWebResponse && ((System.Net.HttpWebResponse)wex.Response).StatusCode == System.Net.HttpStatusCode.NotFound) { throw new FileMissingException(wex); } else { throw; } } }
private WLID_FolderItem FindFolders(bool autocreate) { var folders = (m_rootfolder + '/' + m_prefix).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (folders.Length == 0) { var url = string.Format("{0}/{1}?access_token={2}", WLID_SERVER, ROOT_FOLDER_ID, Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); return(m_oauth.GetJSONData <WLID_FolderItem>(url, x => x.UserAgent = USER_AGENT)); } WLID_FolderItem cur = null; foreach (var f in folders) { var n = FindFolder(f, cur == null ? null : cur.id); if (n == null) { if (autocreate) { var url = string.Format("{0}/{1}?access_token={2}", WLID_SERVER, cur == null ? ROOT_FOLDER_ID : cur.id, Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "POST"; var areq = new Utility.AsyncHttpRequest(req); using (var ms = new System.IO.MemoryStream()) using (var sw = new System.IO.StreamWriter(ms)) { new Newtonsoft.Json.JsonSerializer().Serialize(sw, new WLID_CreateFolderData() { name = f, description = Strings.OneDrive.AutoCreatedFolderLabel }); sw.Flush(); ms.Position = 0; req.ContentLength = ms.Length; req.ContentType = "application/json"; using (var reqs = areq.GetRequestStream()) Utility.Utility.CopyStream(ms, reqs, true, m_copybuffer); } using (var resp = (HttpWebResponse)areq.GetResponse()) using (var rs = areq.GetResponseStream()) using (var tr = new System.IO.StreamReader(rs)) using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) { if ((int)resp.StatusCode < 200 || (int)resp.StatusCode > 299) { throw new ProtocolViolationException(Strings.OneDrive.UnexpectedError(resp.StatusCode, resp.StatusDescription)); } cur = new Newtonsoft.Json.JsonSerializer().Deserialize <WLID_FolderItem>(jr); } } else { throw new FolderMissingException(Strings.OneDrive.MissingFolderError(f)); } } else { cur = n; } } return(cur); }