Esempio n. 1
0
        /// <summary>
        /// Uploads a file to the given path using the Windows Phone BackgroundTransferService.
        /// </summary>
        /// <param name="path">The path to the folder to upload the file to.</param>
        /// <param name="uploadLocation">The location of the file on the device to upload.</param>
        /// <param name="option">an enum to specify the overwrite behavior if a file with the same name already exists.</param>
        /// <param name="ct">a token that is used to cancel the background upload operation.</param>
        /// <param name="progress">an object that is called to report the background upload's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public Task <LiveOperationResult> BackgroundUploadAsync(
            string path,
            Uri uploadLocation,
            OverwriteOption option,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            if (path == null)
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UrlInvalid"),
                                               "path");
                throw new ArgumentNullException("path", message);
            }

            if (uploadLocation == null)
            {
                throw new ArgumentNullException("uploadLocation");
            }

            string filename = Path.GetFileName(uploadLocation.OriginalString);

            if (string.IsNullOrEmpty(filename))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMissingFileName"),
                                               "uploadLocation");
                throw new ArgumentException(message, "uploadLocation");
            }

            if (!BackgroundTransferHelper.IsRootedInSharedTransfers(uploadLocation))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMustBeRootedInSharedTransfers"),
                                               "uploadLocation");
                throw new ArgumentException(message, "uploadLocation");
            }

            if (this.Session == null)
            {
                throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn"));
            }

            var builder = new BackgroundUploadOperation.Builder
            {
                BackgroundTransferService = this.BackgroundTransferService,
                Client = this,
                Path   = path,
                UploadLocationOnDevice = uploadLocation,
                OverwriteOption        = option,
                Progress = progress,
                BackgroundTransferPreferences = this.BackgroundTransferPreferences
            };

            BackgroundUploadOperation operation = builder.Build();

            ct.Register(operation.Cancel);

            return(operation.ExecuteAsync());
        }
        public void TestSharedTransfersDirectoryWithBackwardSlashesAndNoFileName()
        {
            var location = new Uri(@"\shared\transfers\", UriKind.Relative);

            Assert.IsTrue(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Did not accept valid location: " + location);
        }
        public void TestSharedTransfersInSubPath()
        {
            var location = new Uri(@"/blah/shared/transfers/myFile.txt", UriKind.Relative);

            Assert.IsFalse(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Accepted an invalid location: " + location);
        }
        public void TestSharedTransfersDirectoryWithForwardSlashesAndNoLeadingSlash()
        {
            var location = new Uri("shared/transfers/myFile.txt", UriKind.Relative);

            Assert.IsTrue(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Did not accept valid location: " + location);
        }
 public void TestAccept2xxStatusCode()
 {
     for (long statusCode = 200; statusCode < 300; statusCode++)
     {
         Assert.IsTrue(
             BackgroundTransferHelper.IsSuccessfulStatusCode(statusCode),
             "Failed to accept a failed statusCode: " + statusCode);
     }
 }
Esempio n. 6
0
        internal LivePendingDownload(
            IBackgroundTransferService backgroundTransferService,
            BackgroundTransferRequest request)
        {
            Debug.Assert(backgroundTransferService != null);
            Debug.Assert(request != null);
            Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));

            this.request = request;
            this.backgroundTransferService = backgroundTransferService;
        }
 private BackgroundDownloadOperation(Builder builder)
 {
     this.requestUri  = builder.RequestUri;
     this.accessToken = builder.AccessToken;
     this.downloadLocationOnDevice  = builder.DownloadLocationOnDevice;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.progress            = builder.Progress;
     this.tcs                 = new TaskCompletionSource <LiveOperationResult>();
     this.status              = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
        public void TestNotInSharedTransfersDirectory()
        {
            var location = new Uri(@"/notshared/transfers/myFile.txt", UriKind.Relative);

            Assert.IsFalse(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Accepted an invalid location: " + location);

            location = new Uri(@"/shared/nottransfers/myFile.txt", UriKind.Relative);
            Assert.IsFalse(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Accepted an invalid location: " + location);
        }
        public void TestSharedTransfersDirectoryWithMixedSlashes()
        {
            var location = new Uri(@"\shared/transfers\myFile.txt", UriKind.Relative);

            Assert.IsTrue(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Did not accept valid location: " + location);

            location = new Uri(@"/shared\transfers/myFile.txt", UriKind.Relative);
            Assert.IsTrue(
                BackgroundTransferHelper.IsRootedInSharedTransfers(location),
                "Did not accept valid location: " + location);
        }
 private BackgroundUploadOperation(Builder builder)
 {
     this.path = builder.Path;
     this.uploadLocationOnDevice = builder.UploadLocationOnDevice;
     this.client = builder.Client;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.overwriteOption           = builder.OverwriteOption;
     this.progress = builder.Progress;
     this.tcs      = new TaskCompletionSource <LiveOperationResult>();
     this.requestAddedToService = false;
     this.status = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
        public void TestRejectNon2xxStatusCode()
        {
            for (long statusCode = 0; statusCode < 600; statusCode++)
            {
                if (statusCode >= 200 && statusCode < 300)
                {
                    statusCode = 300;
                    continue;
                }

                Assert.IsFalse(
                    BackgroundTransferHelper.IsSuccessfulStatusCode(statusCode),
                    "Accepted an invalid statusCode: " + statusCode);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Retrieves all the pending background uploads created by the Live SDK.
        /// Useful for when the application comes back from a tombstoned or out of memory state.
        /// </summary>
        /// <returns>All the pending background uploads created by the Live SDK.</returns>
        public IEnumerable <LivePendingUpload> GetPendingBackgroundUploads()
        {
            var requests = new List <BackgroundTransferRequest>();

            this.BackgroundTransferService.FindAllLiveSdkRequests(requests);
            var uploads = new List <LivePendingUpload>();

            foreach (BackgroundTransferRequest request in requests)
            {
                if (BackgroundTransferHelper.IsUploadRequest(request))
                {
                    uploads.Add(new LivePendingUpload(this.backgroundTransferService, request));
                }
            }

            return(uploads);
        }
        public void TestIsUploadRequest()
        {
            var request = new BackgroundTransferRequest(new Uri("/shared/transfers/file.txt", UriKind.Relative))
            {
                Method = "post"
            };

            Assert.IsTrue(
                BackgroundTransferHelper.IsUploadRequest(request),
                "Did not recognize a valid upload request");


            request.Method = "get";
            Assert.IsFalse(
                BackgroundTransferHelper.IsUploadRequest(request),
                "Thought a download request was a upload request.");
        }
Esempio n. 14
0
        /// <summary>
        /// Downloads the resource with the given path to the downloadLocation using the Windows Phone
        /// BackgroundTransferService.
        /// </summary>
        /// <param name="path">Path to the resource to download</param>
        /// <param name="downloadLocation">
        ///     The path to the file that will contain the download resource.
        ///     The downloadLocation must exist in /shared/transfers.
        /// </param>
        /// <param name="ct">a token that is used to cancel the background download operation.</param>
        /// <param name="progress">an object that is called to report the background download's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public async Task <LiveOperationResult> BackgroundDownloadAsync(
            string path,
            Uri downloadLocation,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UrlInvalid"),
                                               "path");
                if (path == null)
                {
                    throw new ArgumentNullException("path", message);
                }

                throw new ArgumentException(message, "path");
            }

            if (downloadLocation == null)
            {
                throw new ArgumentNullException("downloadLocation");
            }

            string filename = Path.GetFileName(downloadLocation.OriginalString);

            if (string.IsNullOrEmpty(filename))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMissingFileName"),
                                               "downloadLocation");
                throw new ArgumentException(message, "downloadLocation");
            }

            if (!BackgroundTransferHelper.IsRootedInSharedTransfers(downloadLocation))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMustBeRootedInSharedTransfers"),
                                               "downloadLocation");
                throw new ArgumentException(message, "downloadLocation");
            }

            if (this.Session == null)
            {
                throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn"));
            }

            Uri requestUri = this.GetResourceUri(path, ApiMethod.Download);

            var builder = new BackgroundDownloadOperation.Builder
            {
                RequestUri = requestUri,
                DownloadLocationOnDevice  = downloadLocation,
                BackgroundTransferService = this.BackgroundTransferService,
                Progress = progress,
                BackgroundTransferPreferences = this.BackgroundTransferPreferences
            };

            if (!this.Session.IsValid)
            {
                LiveLoginResult result = await this.Session.AuthClient.RefreshTokenAsync();

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    this.Session = result.Session;
                }
            }

            builder.AccessToken = this.Session.AccessToken;

            BackgroundDownloadOperation operation = builder.Build();

            ct.Register(operation.Cancel);

            return(await operation.ExecuteAsync());
        }
 public void TestGetTransferPreferencesAllowCellularAndBattery()
 {
     Assert.AreEqual(
         TransferPreferences.AllowCellularAndBattery,
         BackgroundTransferHelper.GetTransferPreferences(BackgroundTransferPreferences.AllowCellularAndBattery));
 }
 public void TestGetTransferPreferencesNone()
 {
     Assert.AreEqual(
         TransferPreferences.None,
         BackgroundTransferHelper.GetTransferPreferences(BackgroundTransferPreferences.None));
 }