Ejemplo n.º 1
0
        private BackgroundTransferRequest PrepareTransfer(Uri remoteUri, string destination,
                                                          Action <BackgroundTransferRequest> onFinished)
        {
            if (TransferCountExceeded())
            {
                throw new BackgroundTransferException("The maximum number of background file transfer requests for this application has been exceeded.");
            }
            BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(remoteUri);

            // GET and POST are supported.
            transferRequest.Method = "GET";
            //var inProgressRelativePath = destination.Length > 150 ? Path.GetFileName(destination) : destination;
            //var inProgressSubPath = Path.GetDirectoryName(destination);
            //// too long file paths throw a "DirectoryNotFoundException"
            //var nameCandidate = Path.GetFileNameWithoutExtension(destination)
            //    .Replace(".", "")
            //    .Replace("(", "")
            //    .Replace(")", "")
            //    .Replace(" ", "");
            //var name = nameCandidate.Length > 0 ? nameCandidate : "temp";
            //var inProgressTempPath = tempDownloadDir + "\\" + inProgressSubPath + "\\" + name + Path.GetExtension(destination);
            var inProgressTempPath = tempDownloadDir + TailIfNeeded(destination);
            var inProgressUri      = new Uri(inProgressTempPath, UriKind.RelativeOrAbsolute);

            transferRequest.DownloadLocation = inProgressUri;
            // DownloadLocation does not always equal inProgressUri, encoding takes place, apparently in the setter.
            var inProgressDir = Path.GetDirectoryName(transferRequest.DownloadLocation.OriginalString);

            FileUtils.CreateDirIfNotExists(inProgressDir);
            transferRequest.Tag = destination;
            transferRequest.TransferPreferences    = RequestSettings.TransferPrefs();
            transferRequest.TransferStatusChanged += async(sender, e) => {
                await OnTransferStatusUpdate(e.Request, onFinished);

                OnDownloadStatusUpdate(transferRequest);
            };
            transferRequest.TransferProgressChanged += (sender, e) => {
                OnDownloadStatusUpdate(transferRequest);
            };
            return(transferRequest);
        }