Example #1
0
        private ResourceDownload DownloadForResourceAtUrlLocalBasePathLocalFileNameCredentialsModificationDate(Uri url, string localBasePath, string localFileName, NetworkCredential credentials, DateTime?modificationDate)
        {
            var localUrl = this.LocalPathForResourceAtUrl(url, localBasePath, localFileName);
            ResourceDownload download = new ResourceDownload(url, localUrl, credentials, modificationDate);

            return(download);
        }
        private void ContinueQueue()
        {
            this.ResourceDownloads.Remove(this.currentResourceDownload);
            this.currentResourceDownload.Connection = null;
            this.currentResourceDownload            = null;

            this.StartDownloadForFrontMostFile();
        }
        /// <summary>
        /// Adds given resource to download list
        /// </summary>
        /// <param name="resourceDownload">Resource to download</param>
        public void AddResourceDownload(ResourceDownload resourceDownload)
        {
            resourceDownload.DownloadManager = this;
            if (this.ResourceDownloads.Contains(resourceDownload))
            {
                return;
            }

            this.ResourceDownloads.Add(resourceDownload);
            this.StartDownloadForFrontMostFile();
        }
        private async void StartDownloadForFrontMostFile()
        {
            if (this.ResourceDownloads.Count == 0 || this.currentResourceDownload != null)
            {
                return;
            }

            this.StopProcessingDownloadQueue();

            this.currentResourceDownload = this.ResourceDownloads[0];
            ResourceDownload downloadToStart = this.currentResourceDownload;
            var remoteData = new RemoteData(this.currentResourceDownload.DownloadUrl, this.remoteSession, this);

            var userAgent = ServerSession.CurrentSession.CrmServer.UserAgent;

            if (userAgent?.Length > 0)
            {
                remoteData.CustomHttpHeaderFields["User-Agent"] = userAgent;
            }

            ulong?downloadOffset = null;
            var   path           = this.currentResourceDownload.LocalUrl;

            if (this.StorageService.FileExists(path) && this.currentResourceDownload.ShouldAutoResume && this.serverSupportRangeHeader)
            {
                this.currentDownloadFileHandle = await this.StorageService.GetFileStreamForWrite(path);

                this.currentDownloadFileHandle.Seek(this.currentDownloadFileHandle.Length, SeekOrigin.Begin);
                downloadOffset = await this.StorageService.GetFileSize(path);
            }
            else
            {
                if (this.StorageService.FileExists(path))
                {
                    Exception error;
                    this.StorageService.TryDelete(path, out error);
                }

                await this.StorageService.CreateFile(path);

                this.currentDownloadFileHandle = await this.StorageService.GetFileStreamForWrite(path);
            }

            if (downloadOffset.HasValue)
            {
                remoteData.CustomHttpHeaderFields["Range"]           = $"bytes={downloadOffset}-";
                this.currentResourceDownload.NumberOfBytesDownloaded = (uint)downloadOffset.Value;
            }

            if (downloadToStart != null)
            {
                this.StartRequest(remoteData);
            }
        }
        /// <summary>
        /// Moves given resource download list to front of the queue
        /// </summary>
        /// <param name="resourceDownloadsToMove">Resource download list</param>
        /// <returns>Returns true if succeeds</returns>
        public bool MoveResourceDownloadsToFrontOfQueue(List <ResourceDownload> resourceDownloadsToMove)
        {
            if (resourceDownloadsToMove.Count == 0)
            {
                return(false);
            }

            var firstDownload = resourceDownloadsToMove[0];

            if (this.currentResourceDownload == firstDownload)
            {
                return(false);
            }

            foreach (var download in resourceDownloadsToMove)
            {
                this.ResourceDownloads.Remove(download);
            }

            this.ResourceDownloads.InsertRange(0, resourceDownloadsToMove);

            if (this.currentDownloadFileHandle != null)
            {
                this.currentDownloadFileHandle.Flush();
                this.currentDownloadFileHandle.Dispose();
                this.currentDownloadFileHandle = null;
            }

            ResourceDownload cancelledDownload = this.currentResourceDownload;

            if (this.currentResourceDownload != null)
            {
                this.currentResourceDownload.Connection?.Cancel();
                this.currentResourceDownload.Connection = null;
                this.currentResourceDownload            = null;
            }

            if (cancelledDownload != null)
            {
                Messenger.Default.Send(SyncManagerMessage.Create(
                                           SyncManagerMessageKey.WillCancelRunningDownloadNotification,
                                           new Dictionary <string, object> {
                    { "TheObject", cancelledDownload }
                }));
            }

            this.StartDownloadForFrontMostFile();

            return(true);
        }
Example #6
0
        private ResourceDownload AttemptToQueueDownloadForResourceAtUrl(Uri url, ResourceDownloadManager downloadManager, string localBasePath, string localFileName, NetworkCredential credentials, DateTime?modificationDate, bool shouldAutoResume)
        {
            ResourceDownload existingDownload = this.HasRunningDownloadForResourceAtUrl(url);

            if (existingDownload != null)
            {
                return(existingDownload);
            }

            ResourceDownload download = this.DownloadForResourceAtUrlLocalBasePathLocalFileNameCredentialsModificationDate(url, localBasePath, localFileName, credentials, modificationDate);

            download.ShouldAutoResume = shouldAutoResume;
            downloadManager.AddResourceDownload(download);
            return(download);
        }
        /// <summary>
        /// Returns resource download object for given uri
        /// </summary>
        /// <param name="downloadUrl">Download uri</param>
        /// <returns><see cref="ResourceDownload"/></returns>
        public ResourceDownload ResourceDownloadForUrl(Uri downloadUrl)
        {
            ResourceDownload download = null;

            foreach (var downloadItem in this.ResourceDownloads)
            {
                if (downloadItem.DownloadUrl.AbsoluteUri == downloadUrl.AbsoluteUri)
                {
                    download = downloadItem;
                    break;
                }
            }

            return(download);
        }
        /// <summary>
        /// Cancels resource download
        /// </summary>
        /// <param name="resourceDownload">Resource download</param>
        /// <returns>Returns true if succeeds</returns>
        public bool CancelResourceDownload(ResourceDownload resourceDownload)
        {
            var cancelledDownload = false;

            if (this.currentDownloadFileHandle != null)
            {
                this.currentDownloadFileHandle.Flush();
                this.currentDownloadFileHandle.Dispose();
                this.currentDownloadFileHandle = null;
            }

            if (this.currentResourceDownload == resourceDownload)
            {
                if (this.serverSupportRangeHeader == false)
                {
                    Exception error;
                    this.StorageService.TryDelete(this.currentResourceDownload.LocalUrl, out error);
                }

                this.currentResourceDownload.Connection?.Cancel();
                this.currentResourceDownload.Connection = null;
                this.currentResourceDownload            = null;
            }

            if (this.ResourceDownloads.Contains(resourceDownload))
            {
                cancelledDownload = true;
            }

            this.ResourceDownloads.Remove(resourceDownload);
            Messenger.Default.Send(SyncManagerMessage.Create(
                                       SyncManagerMessageKey.WillCancelRunningDownloadNotification,
                                       new Dictionary <string, object> {
                { "TheObject", resourceDownload }
            }));

            return(cancelledDownload);
        }
        /// <summary>
        /// Stop processing download queue
        /// </summary>
        public void StopProcessingDownloadQueue()
        {
            this.currentDownloadFileHandle?.Dispose();
            this.currentDownloadFileHandle = null;

            var currentDownload = this.currentResourceDownload;

            if (this.currentResourceDownload != null)
            {
                this.currentResourceDownload.Connection?.Cancel();
                this.currentResourceDownload.Connection = null;
                this.currentResourceDownload            = null;
            }

            if (currentDownload != null)
            {
                Messenger.Default.Send(SyncManagerMessage.Create(
                                           SyncManagerMessageKey.WillCancelRunningDownloadNotification,
                                           new Dictionary <string, object> {
                    { "TheObject", currentDownload }
                }));
            }
        }
Example #10
0
 /// <summary>
 /// Prioritizes given download
 /// </summary>
 /// <param name="resourceDownload">Resource Download instance</param>
 /// <returns>Returns true if succeeds</returns>
 public bool PrioritizeDownload(ResourceDownload resourceDownload)
 {
     return(resourceDownload.DownloadManager.MoveResourceDownloadToFrontOfQueue(resourceDownload));
 }
 /// <summary>
 /// Moves given resource download to front of the queue
 /// </summary>
 /// <param name="resourceDownload">Resource download</param>
 /// <returns>Returns true if succeeds</returns>
 public bool MoveResourceDownloadToFrontOfQueue(ResourceDownload resourceDownload)
 {
     return(this.MoveResourceDownloadsToFrontOfQueue(new List <ResourceDownload> {
         resourceDownload
     }));
 }