コード例 #1
0
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
        {
            // build the url
            string url = GetDownloadFileUrlInternal(session, fileSystemEntry.Parent);

            // get the session
            DropBoxStorageProviderSession dbSession = session as DropBoxStorageProviderSession;

            // build service
            OAuthService svc = new OAuthService();

            // encode the filename
            String fileName = fileSystemEntry.Name;

            // build oAuth parameter
            var param = new Dictionary <string, string>();

            param.Add("file", fileName);

            // sign the url
            String signedUrl = svc.GetSignedUrl(url, dbSession.Context, dbSession.SessionToken as DropBoxToken, param);

            // build upload web request
            WebRequest uploadRequest = svc.CreateWebRequestMultiPartUpload(signedUrl, null);

            // get the network stream
            WebRequestStream ws = svc.GetRequestStreamMultiPartUpload(uploadRequest, fileName, uploadSize);

            // register the post dispose opp
            ws.PushPostDisposeOperation(CommitUploadStream, svc, uploadRequest, uploadSize, fileSystemEntry, ws);

            // go ahead
            return(ws);
        }
コード例 #2
0
        private bool MoveOrRenameOrCopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath, bool copy)
        {
            // build the path for resource
            var resourcePath = DropBoxResourceIDHelpers.GetResourcePath(orgEntry);

            // request the json object via oauth
            var parameters = new Dictionary <string, string>
            {
                { "from_path", resourcePath },
                { "root", GetRootToken(session) },
                { "to_path", toPath }
            };

            try
            {
                // move or rename the entry
                int code;
                var res = DropBoxRequestParser.RequestResourceByUrl(GetUrlString(copy ? DropBoxCopyItem : DropBoxMoveItem, session.ServiceConfiguration), parameters, this, session, out code);

                // update the entry
                DropBoxRequestParser.UpdateObjectFromJsonString(res, orgEntry, this, session);
            }
            catch (Exception)
            {
                return(false);
            }

            orgEntry.Id = toPath;
            return(true);
        }
コード例 #3
0
        private ICloudDirectoryEntry GetRootBySession(DropBoxStorageProviderSession session)
        {
            // now check if we have application keys and secrets from a sandbox or a fullbox
            // try to load the root of the full box if this fails we have only sandbox access
            ICloudDirectoryEntry root;

            try
            {
                root = RequestResource(session, "/", null) as ICloudDirectoryEntry;
            }
            catch (SharpBoxException ex)
            {
                if (session.SandBoxMode)
                {
                    throw ex;
                }
                else
                {
                    // enable sandbox mode
                    session.SandBoxMode = true;

                    // retry to get root object
                    root = RequestResource(session, "/", null) as ICloudDirectoryEntry;
                }
            }

            // go ahead
            return(root);
        }
コード例 #4
0
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            // build the url
            string url = GetDownloadFileUrlInternal(session, fileSystemEntry);

            // build the service
            OAuthService svc = new OAuthService();

            // get the dropbox session
            DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

            // create webrequst
            WebRequest requestProtected = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, null, null, dropBoxSession.Context, (DropBoxToken)dropBoxSession.SessionToken, null);

            // get the response
            WebResponse response = svc.GetWebResponse(requestProtected);

            // get the data stream
            Stream orgStream = svc.GetResponseStream(response);

            // build the download stream
            BaseFileEntryDownloadStream dStream = new BaseFileEntryDownloadStream(orgStream, fileSystemEntry);

            // put the disposable on the stack
            dStream._DisposableObjects.Push(response);

            // go ahead
            return(dStream);
        }
コード例 #5
0
        private string GetResourceUrlInternal(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            // cast varibales
            DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

            // build the path for resource
            String resourcePath = GenericHelper.GetResourcePath(fileSystemEntry);

            // trim heading and trailing slashes
            resourcePath = resourcePath.TrimStart('/');
            resourcePath = resourcePath.TrimEnd('/');

            // build the metadata url
            String getMetaData;

            // get url
            if (dropBoxSession.SandBoxMode)
            {
                getMetaData = PathHelper.Combine(GetUrlString(DropBoxSandboxRoot, session.ServiceConfiguration), HttpUtilityEx.UrlEncodeUTF8(resourcePath));
            }
            else
            {
                getMetaData = PathHelper.Combine(GetUrlString(DropBoxDropBoxRoot, session.ServiceConfiguration), HttpUtilityEx.UrlEncodeUTF8(resourcePath));
            }

            return(getMetaData);
        }
コード例 #6
0
        public static String RequestResourceByUrl(String url, Dictionary <String, String> parameters, IStorageProviderService service, IStorageProviderSession session, out int netErrorCode)
        {
            // cast the dropbox session
            DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

            // instance the oAuthServer
            OAuthService svc = new OAuthService();

            // build the webrequest to protected resource
            WebRequest request = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, null, null, dropBoxSession.Context, (DropBoxToken)dropBoxSession.SessionToken, parameters);

            // get the error code
            WebException ex;

            // perform a simple webrequest
            Stream s = svc.PerformWebRequest(request, null, out netErrorCode, out ex);

            if (s == null)
            {
                return("");
            }

            // read the memory stream and convert to string
            return(new StreamReader(s).ReadToEnd());
        }
コード例 #7
0
        private DropBoxStorageProviderSession Authorize(DropBoxToken token, DropBoxConfiguration configuration)
        {
            // Get a valid dropbox session through oAuth authorization
            DropBoxStorageProviderSession session = BuildSessionFromAccessToken(token, configuration);

            //( Get a valid root object
            return(GetRootBySessionExceptionHandled(session));
        }
コード例 #8
0
        public DropBoxStorageProviderSession BuildSessionFromAccessToken(DropBoxToken token, DropBoxConfiguration configuration)
        {
            // build the consumer context
            var consumerContext = new OAuthConsumerContext(token.BaseTokenInformation.ConsumerKey, token.BaseTokenInformation.ConsumerSecret);

            // build the session
            var session = new DropBoxStorageProviderSession(token, configuration, consumerContext, this);

            // go aahead
            return(session);
        }
コード例 #9
0
        public override string GetResourceUrl(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, String additionalPath)
        {
            // get the dropbox session
            DropBoxStorageProviderSession dbSession = session as DropBoxStorageProviderSession;

            // build the internal url
            String url = GetResourceUrlInternal(session, fileSystemEntry);

            // add the optional path
            if (additionalPath != null)
            {
                url = PathHelper.Combine(url, HttpUtilityEx.UrlEncodeUTF8(additionalPath));
            }

            // generate the oauth url
            OAuthService svc = new OAuthService();

            return(svc.GetProtectedResourceUrl(url, dbSession.Context, dbSession.SessionToken as DropBoxToken, null, WebRequestMethodsEx.Http.Get));
        }
コード例 #10
0
        public static String GetDownloadFileUrlInternal(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            // cast varibales
            DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

            // gather information
            String rootToken   = GetRootToken(dropBoxSession);
            String dropboxPath = GenericHelper.GetResourcePath(entry);

            // add all information to url;
            String url = GetUrlString(DropBoxUploadDownloadFile, session.ServiceConfiguration) + "/" + rootToken;

            if (dropboxPath.Length > 0 && dropboxPath[0] != '/')
            {
                url += "/";
            }

            url += HttpUtilityEx.UrlEncodeUTF8(dropboxPath);

            return(url);
        }
コード例 #11
0
        private DropBoxStorageProviderSession GetRootBySessionExceptionHandled(DropBoxStorageProviderSession session)
        {
            try
            {
                var root = GetRootBySession(session);

                // return the infos
                return(root == null ? null : session);
            }
            catch (SharpBoxException ex)
            {
                // check if the exception an http error 403
                if (ex.InnerException is HttpException)
                {
                    if ((((HttpException)ex.InnerException).GetHttpCode() == 403) || (((HttpException)ex.InnerException).GetHttpCode() == 401))
                    {
                        throw new UnauthorizedAccessException();
                    }
                }

                // otherwise rethrow the old exception
                throw ex;
            }
        }
コード例 #12
0
 private static String GetRootToken(DropBoxStorageProviderSession session)
 {
     return(session.SandBoxMode ? "sandbox" : "dropbox");
 }
コード例 #13
0
        public DropBoxStorageProviderSession BuildSessionFromAccessToken(DropBoxToken token, DropBoxConfiguration configuration)
        {
            // build the consumer context
            var consumerContext = new OAuthConsumerContext(token.BaseTokenInformation.ConsumerKey, token.BaseTokenInformation.ConsumerSecret);

            // build the session
            var session = new DropBoxStorageProviderSession(token, configuration, consumerContext, this);

            // go aahead
            return session;
        }
コード例 #14
0
        private DropBoxStorageProviderSession GetRootBySessionExceptionHandled(DropBoxStorageProviderSession session)
        {
            try
            {
                var root = GetRootBySession(session);

                // return the infos
                return root == null ? null : session;
            }
            catch (SharpBoxException ex)
            {
                // check if the exception an http error 403
                if (ex.InnerException is HttpException)
                {
                    if ((((HttpException) ex.InnerException).GetHttpCode() == 403) || (((HttpException) ex.InnerException).GetHttpCode() == 401))
                        throw new UnauthorizedAccessException();
                }

                // otherwise rethrow the old exception
                throw ex;
            }
        }
コード例 #15
0
        private bool MoveOrRenameOrCopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath, bool copy)
        {
            // build the path for resource
            var resourcePath = DropBoxResourceIDHelpers.GetResourcePath(orgEntry);

            // request the json object via oauth
            var parameters = new Dictionary<string, string>
                                 {
                                     {"from_path", resourcePath},
                                     {"root", GetRootToken(session)},
                                     {"to_path", toPath}
                                 };

            try
            {
                // move or rename the entry
                int code;
                var res = DropBoxRequestParser.RequestResourceByUrl(GetUrlString(copy ? DropBoxCopyItem : DropBoxMoveItem, session.ServiceConfiguration), parameters, this, session, out code);

                // update the entry
                DropBoxRequestParser.UpdateObjectFromJsonString(res, orgEntry, this, session);
            }
            catch (Exception)
            {
                return false;
            }

            orgEntry.Id = toPath;
            return true;
        }
コード例 #16
0
 private bool CopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath)
 {
     return MoveOrRenameOrCopyItem(session, orgEntry, toPath, true);
 }
コード例 #17
0
 private static String GetRootToken(DropBoxStorageProviderSession session)
 {
     return session.SandBoxMode ? "sandbox" : "dropbox";
 }
コード例 #18
0
 private bool CopyItem(DropBoxStorageProviderSession session, BaseFileEntry orgEntry, String toPath)
 {
     return(MoveOrRenameOrCopyItem(session, orgEntry, toPath, true));
 }
コード例 #19
0
        /// <summary>
        /// This method offers the mobile login api of dropbox for users who are migrating from version 0 of the 
        /// dropbox API because version 1 supports token based logins only
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="appkey"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static ICloudStorageAccessToken LoginWithMobileAPI(String username, String password, String appkey, String appsecret)
        {
            // get the configuration
            var configuration = DropBoxConfiguration.GetStandardConfiguration();

            // build the consumer context
            var consumerContext = new OAuthConsumerContext(appkey, appsecret);

            // build up the oauth session
            var serviceContext = new OAuthServiceContext(configuration.RequestTokenUrl.ToString(),
                                                         configuration.AuthorizationTokenUrl.ToString(), configuration.AuthorizationCallBack.ToString(),
                                                         configuration.AccessTokenUrl.ToString());

            // get a request token from the provider      
            var svc = new OAuthService();
            var oAuthRequestToken = svc.GetRequestToken(serviceContext, consumerContext);
            if (oAuthRequestToken == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidConsumerKeySecret);
            }
            var DropBoxRequestToken = new DropBoxToken(oAuthRequestToken, new DropBoxBaseTokenInformation {ConsumerKey = appkey, ConsumerSecret = appsecret});

            // generate the dropbox service
            var service = new DropBoxStorageProviderService();

            // build up a request Token Session
            var requestSession = new DropBoxStorageProviderSession(DropBoxRequestToken, configuration, consumerContext, service);

            // build up the parameters
            var param = new Dictionary<String, String>
                            {
                                {"email", username},
                                {"password", password}
                            };

            // call the mobile login api 
            var result = "";

            try
            {
                int code;
                result = DropBoxRequestParser.RequestResourceByUrl(DropBoxMobileLogin, param, service, requestSession, out code);
                if (result.Length == 0)
                    throw new UnauthorizedAccessException();
            }

#if MONOTOUCH || WINDOWS_PHONE || MONODROID
            catch (Exception ex)
            {
                if (ex is UnauthorizedAccessException)
                    throw ex;
                else
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService, ex);
            }
#else
            catch (System.Web.HttpException netex)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService, netex);
            }
#endif

            // exchange a request token for an access token
            var accessToken = new DropBoxToken(result);

            // adjust the token 
            if (accessToken.BaseTokenInformation == null)
            {
                accessToken.BaseTokenInformation = new DropBoxBaseTokenInformation
                                                       {
                                                           ConsumerKey = appkey,
                                                           ConsumerSecret = appsecret
                                                       };
            }


            // go ahead
            return accessToken;
        }
コード例 #20
0
        private ICloudDirectoryEntry GetRootBySession(DropBoxStorageProviderSession session)
        {
            // now check if we have application keys and secrets from a sandbox or a fullbox
            // try to load the root of the full box if this fails we have only sandbox access
            ICloudDirectoryEntry root;

            try
            {
                root = RequestResource(session, "/", null) as ICloudDirectoryEntry;
            }
            catch (SharpBoxException ex)
            {
                if (session.SandBoxMode)
                    throw ex;
                else
                {
                    // enable sandbox mode
                    session.SandBoxMode = true;

                    // retry to get root object
                    root = RequestResource(session, "/", null) as ICloudDirectoryEntry;
                }
            }

            // go ahead
            return root;
        }