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"));
     }
 }
Esempio n. 2
0
 public DropBoxToken(String jsonString)
     : base("", "")
 {
     var jh = new JsonHelper();
     if (jh.ParseJsonMessage(jsonString))
     {
         TokenSecret = jh.GetProperty("secret");
         TokenKey = jh.GetProperty("token");
     }
 }
        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());
        }
Esempio n. 4
0
        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;
        }
        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;
        }
        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 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;
        }
        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);
                }
            }
        }