public int StartDownload(string address)
        {
            // put a record in the database.
            int id;

            using (IDownloadItemRepository repository = createRepository())
            {
                var item = new DownloadItem()
                {
                    Address = address,
                    Status  = DownloadStatus.Requested
                };
                repository.Add(item);
                repository.SaveChanges();

                // get the database generated id.
                id = item.Id;
            }

            eventBus.Notify(this, EventBusToken.DownlaodRequested);

            var channelFactory  = new ChannelFactory <IDownloadService>("DLS");
            var downloadService = channelFactory.CreateChannel();

            using (downloadService as IDisposable)
                downloadService.Download(id);

            return(id);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        /// <summary>
        /// Starts a download
        /// </summary>
        /// <param name="item"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool StartDownload(DownloadItem item, out DownloadItemActionError error)
        {
            error = DownloadItemActionError.DownloadabilityFailure;

            var downloadMethod = ResolveDownloadMethod(item.FileUrl);

            if (downloadMethod == null)
            {
                logger.LogWarning("URL not handled: " + item.FileUrl);
                error = DownloadItemActionError.UrlNotHandled;
                return(false);
            }

            try
            {
                downloadMethod.Start(item);
                item.DownloadedSize = 0;
                item.StartedAt      = DateTime.Now;
                item.Archived       = false;
                item.State          = DownloadState.Downloading;

                repository.Add(item);
                _ = dispatcher.Broadcast(new ItemStarted(item));
            }
            catch (FileNotDownloadableException ex)
            {
                logger.LogWarning(ex.Message);
                error = DownloadItemActionError.DownloadabilityFailure;
                return(false);
            }
            catch (StartDownloadException ex)
            {
                logger.LogWarning(ex.Message);
                error = DownloadItemActionError.StartDownloadFailure;
                return(false);
            }
            return(true);
        }