public static OAuth20Token FromJson(String json) { var parser = new JsonHelper(); if (!parser.ParseJsonMessage(json)) return null; var accessToken = parser.GetProperty("access_token"); var refreshToken = parser.GetProperty("refresh_token"); if (String.IsNullOrEmpty(accessToken) || String.IsNullOrEmpty(refreshToken)) return null; var token = new OAuth20Token { AccessToken = accessToken, RefreshToken = refreshToken, ClientID = parser.GetProperty("client_id"), ClientSecret = parser.GetProperty("client_secret"), RedirectUri = parser.GetProperty("redirect_uri"), }; double expiresIn; if (double.TryParse(parser.GetProperty("expires_in"), out expiresIn)) token.ExpiresIn = expiresIn; DateTime timestamp; if (DateTime.TryParse(parser.GetProperty("timestamp"), out timestamp)) token.Timestamp = timestamp; return token; }
internal DropBoxQuotaInfo(String jmstext) { var jh = new JsonHelper(); if (jh.ParseJsonMessage(jmstext)) { SharedBytes = Convert.ToUInt64(jh.GetProperty("shared")); QuotaBytes = Convert.ToUInt64(jh.GetProperty("quota")); NormalBytes = Convert.ToUInt64(jh.GetProperty("normal")); } }
public DropBoxToken(String jsonString) : base("", "") { var jh = new JsonHelper(); if (jh.ParseJsonMessage(jsonString)) { TokenSecret = jh.GetProperty("secret"); TokenKey = jh.GetProperty("token"); } }
public static BaseFileEntry UpdateObjectFromJsonString(String jsonMessage, BaseFileEntry objectToUpdate, IStorageProviderService service, IStorageProviderSession session) { // verify if we have a directory or a file var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonMessage)) throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName); var isDir = jc.GetBooleanProperty("is_dir"); // create the entry BaseFileEntry dbentry; Boolean bEntryOk; if (isDir) { if (objectToUpdate == null) dbentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate as BaseDirectoryEntry; bEntryOk = BuildDirectyEntry(dbentry as BaseDirectoryEntry, jc, service, session); } else { if (objectToUpdate == null) dbentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate; bEntryOk = BuildFileEntry(dbentry, jc); } // parse the childs and fill the entry as self if (!bEntryOk) throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService); // set the is deleted flag try { // try to read the is_deleted property dbentry.IsDeleted = jc.GetBooleanProperty("is_deleted"); } catch (Exception) { // the is_deleted proprty is missing (so it's not a deleted file or folder) dbentry.IsDeleted = false; } // return the child return dbentry; }
private static ICloudFileSystemEntry ParseSingleEntry(IStorageProviderSession session, String json, JsonHelper parser) { if (json == null) return null; if (parser == null) parser = CreateParser(json); if (ContainsError(json, false, parser)) return null; BaseFileEntry entry; var type = parser.GetProperty("type"); if (!IsFolderType(type) && !IsFileType(type)) return null; var id = parser.GetProperty("id"); var name = parser.GetProperty("name"); var parentID = parser.GetProperty("parent_id"); var uploadLocation = parser.GetProperty("upload_location"); var updatedTime = Convert.ToDateTime(parser.GetProperty("updated_time")).ToUniversalTime(); if (IsFolderType(type)) { int count = parser.GetPropertyInt("count"); entry = new BaseDirectoryEntry(name, count, updatedTime, session.Service, session) {Id = id}; } else { var size = Convert.ToInt64(parser.GetProperty("size")); entry = new BaseFileEntry(name, size, updatedTime, session.Service, session) {Id = id}; } entry[SkyDriveConstants.UploadLocationKey] = uploadLocation; if (!String.IsNullOrEmpty(parentID)) { entry.ParentID = SkyDriveConstants.RootIDRegex.IsMatch(parentID) ? "/" : parentID; } else { entry.Name = "/"; entry.Id = "/"; } entry[SkyDriveConstants.InnerIDKey] = id; entry[SkyDriveConstants.TimestampKey] = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); return entry; }
internal DropBoxAccountInfo(String jmstext) { var jh = new JsonHelper(); if (!jh.ParseJsonMessage(jmstext)) return; Country = jh.GetProperty("country"); DisplayName = jh.GetProperty("display_name"); UserId = jh.GetPropertyInt("uid"); var quotainfo = jh.GetSubObjectString("quota_info"); if (quotainfo != null) QuotaInfo = new DropBoxQuotaInfo(quotainfo.ToString()); }
private static Boolean BuildDirectyEntry(BaseDirectoryEntry dirEntry, JsonHelper jh, IStorageProviderService service, IStorageProviderSession session) { // build the file entry part if (!BuildFileEntry(dirEntry, jh)) return false; // now take the content var content = jh.GetListProperty("contents"); if (content.Count == 0) return true; // remove all childs dirEntry.ClearChilds(); // add the childs foreach (var jsonContent in content) { // parse the item var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonContent)) continue; // check if we have a directory var isDir = jc.GetBooleanProperty("is_dir"); BaseFileEntry fentry; if (isDir) { fentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); } else { fentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); } // build the file attributes BuildFileEntry(fentry, jc); // establish parent child realtionship dirEntry.AddChild(fentry); } // set the length dirEntry.Length = dirEntry.Count; // go ahead return true; }
private static Boolean BuildFileEntry(BaseFileEntry fileEntry, JsonHelper jh) { /* * "revision": 29251, "thumb_exists": false, "bytes": 37941660, "modified": "Tue, 01 Jun 2010 14:45:09 +0000", "path": "/Public/2010_06_01 15_53_48_336.nvl", "is_dir": false, "icon": "page_white", "mime_type": "application/octet-stream", "size": "36.2MB" * */ // set the size fileEntry.Length = Convert.ToInt64(jh.GetProperty("bytes")); // set the modified time fileEntry.Modified = jh.GetDateTimeProperty("modified"); // build the displayname var DropBoxPath = jh.GetProperty("path"); var arr = DropBoxPath.Split('/'); fileEntry.Name = arr.Length > 0 ? arr[arr.Length - 1] : DropBoxPath; if (DropBoxPath.Equals("/")) { fileEntry.Id = "/"; fileEntry.ParentID = null; } else { fileEntry.Id = DropBoxPath.Trim('/'); fileEntry.ParentID = DropBoxResourceIDHelpers.GetParentID(DropBoxPath); } // set the hash property if possible var hashValue = jh.GetProperty("hash"); if (hashValue.Length > 0) fileEntry.SetPropertyValue("hash", hashValue); // set the path property fileEntry.SetPropertyValue("path", DropBoxPath.Equals("/") ? "" : DropBoxPath); // set the revision value if possible var revValue = jh.GetProperty("rev"); if (revValue.Length > 0) { fileEntry.SetPropertyValue("rev", revValue); } // go ahead return true; }
private static JsonHelper CreateParser(String json) { var parser = new JsonHelper(); if (!parser.ParseJsonMessage(json)) throw new SkyDriveParserException("Can not parse this JSON message"); return parser; }
private static bool ContainsError(String json, bool throwIfError, JsonHelper parser) { if (String.IsNullOrEmpty(json)) return false; if (parser == null) parser = CreateParser(json); var error = parser.GetProperty("error"); if (!String.IsNullOrEmpty(error)) { if (throwIfError) throw new SkyDriveParserException( String.Format("The returned JSON message is describing the error. The message contained the following: {0}", error)); return true; } return false; }
private static IEnumerable<ICloudFileSystemEntry> ParseListOfEntries(IStorageProviderSession session, String json, JsonHelper parser) { if (json == null) return null; if (parser == null) parser = CreateParser(json); if (ContainsError(json, false, parser)) return null; return parser.GetListProperty("data").Select(jsonEntry => ParseSingleEntry(session, jsonEntry)).Where(entry => entry != null); }
public override void UploadChunk(IStorageProviderSession session, IResumableUploadSession uploadSession, Stream stream, long chunkLength) { if (uploadSession.Status == ResumableUploadSessionStatus.Completed || uploadSession.Status == ResumableUploadSessionStatus.Aborted) throw new InvalidOperationException("Upload session was either completed or aborted."); if (stream == null) throw new ArgumentNullException("stream"); var requestParams = new Dictionary<string, string>(); if (uploadSession.Status == ResumableUploadSessionStatus.Started) { requestParams.Add("upload_id", uploadSession.GetItem<string>("UploadId")); requestParams.Add("offset", uploadSession.BytesTransfered.ToString()); } var request = new OAuthService().CreateWebRequest(GetUrlString(DropBoxChunkedUpload, session.ServiceConfiguration), "PUT", null, null, ((DropBoxStorageProviderSession)session).Context, (DropBoxToken)session.SessionToken, requestParams); request.ContentLength = chunkLength; using (var requestStream = request.GetRequestStream()) { stream.CopyTo(requestStream); } using (var response = request.GetResponse()) using (var responseStream = response.GetResponseStream()) { if (responseStream == null) return; var json = new JsonHelper(); json.ParseJsonMessage(new StreamReader(responseStream).ReadToEnd()); var uplSession = (ResumableUploadSession)uploadSession; uplSession["UploadId"] = json.GetProperty("upload_id"); uplSession["Expired"] = json.GetDateTimeProperty("expired"); uplSession.BytesTransfered += chunkLength; uplSession.Status = ResumableUploadSessionStatus.Started; if (uplSession.BytesToTransfer == uploadSession.BytesTransfered) { CommitUploadSession(session, uploadSession); } } }