public override IHttpTask Download(TaskConfiguration config)
        {
            var task = new BackgroundDownloader
            {
                Method     = config.HttpMethod,
                CostPolicy = config.UseMeteredConnection
                    ? BackgroundTransferCostPolicy.Default
                    : BackgroundTransferCostPolicy.UnrestrictedOnly
            };

            foreach (var header in config.Headers)
            {
                task.SetRequestHeader(header.Key, header.Value);
            }

            var filePath  = config.LocalFilePath ?? Path.Combine(ApplicationData.Current.LocalFolder.Path, Path.GetRandomFileName());
            var fileName  = Path.GetFileName(filePath);
            var directory = Path.GetDirectoryName(filePath);

            // why are these async??
            var folder = StorageFolder.GetFolderFromPathAsync(directory).AsTask().Result;
            var file   = folder.CreateFileAsync(fileName).AsTask().Result;

            var operation = task.CreateDownload(new Uri(config.Uri), file);
            var httpTask  = new DownloadHttpTask(config, operation, false);

            this.Add(httpTask);

            return(httpTask);
        }
        public override IHttpTask Upload(TaskConfiguration config)
        {
            if (String.IsNullOrWhiteSpace(config.LocalFilePath))
            {
                throw new ArgumentException("You must set the local file path when uploading");
            }

            if (!File.Exists(config.LocalFilePath))
            {
                throw new ArgumentException($"File '{config.LocalFilePath}' does not exist");
            }

            var task = new BackgroundUploader
            {
                Method     = config.HttpMethod,
                CostPolicy = config.UseMeteredConnection
                    ? BackgroundTransferCostPolicy.Default
                    : BackgroundTransferCostPolicy.UnrestrictedOnly
            };

            foreach (var header in config.Headers)
            {
                task.SetRequestHeader(header.Key, header.Value);
            }

            // seriously - this should not be async!
            var file      = StorageFile.GetFileFromPathAsync(config.LocalFilePath).AsTask().Result;
            var operation = task.CreateUpload(new Uri(config.Uri), file);
            var httpTask  = new UploadHttpTask(config, operation, false);

            this.Add(httpTask);

            return(httpTask);
        }
        public HttpTransferTasks()
        {
            BackgroundUploader
            .GetCurrentUploadsAsync()
            .AsTask()
            .ContinueWith(result =>
            {
                foreach (var task in result.Result)
                {
                    var config = new TaskConfiguration(task.RequestedUri.ToString(), task.SourceFile.Path)
                    {
                        HttpMethod           = task.Method,
                        UseMeteredConnection = task.CostPolicy != BackgroundTransferCostPolicy.UnrestrictedOnly
                    };
                    this.Add(new UploadHttpTask(config, task, true));
                }
            });

            BackgroundDownloader
            .GetCurrentDownloadsAsync()
            .AsTask()
            .ContinueWith(result =>
            {
                foreach (var task in result.Result)
                {
                    var config = new TaskConfiguration(task.RequestedUri.ToString())
                    {
                        HttpMethod           = task.Method,
                        UseMeteredConnection = task.CostPolicy != BackgroundTransferCostPolicy.UnrestrictedOnly
                    };
                    this.Add(new DownloadHttpTask(config, task, true));
                }
            });
        }
Beispiel #4
0
        public override IHttpTask Download(TaskConfiguration config)
        {
            if (String.IsNullOrWhiteSpace(config.LocalFilePath))
            {
                config.LocalFilePath = Path.GetTempFileName();
            }

            return(this.CreateTaskAndPersist(config, false));
        }
Beispiel #5
0
        IHttpTask CreateTask(TaskConfiguration config, bool upload, string identifier)
        {
            var task = new HttpTask(config, upload, identifier);

            task.PropertyChanged += this.OnTaskStatusChanged;
            this.Add(task);
            this.conn.Commit();

            return(task);
        }
Beispiel #6
0
        public HttpTask(TaskConfiguration config, bool upload, string identifier) : base(config, upload)
        {
            this.LocalFilePath = config.LocalFilePath;
            this.Identifier    = identifier;

            this.httpClient = new HttpClient();
            this.cancelSrc  = new CancellationTokenSource();
            this.Identifier = identifier;
            this.Run();
        }
        public override IHttpTask Download(TaskConfiguration config)
        {
            var request = this.CreateRequest(config);
            var native  = this.session.CreateDownloadTask(request);
            var task    = new HttpTask(config, native);

            this.Add(task);
            native.Resume();

            return(task);
        }
        public override IHttpTask Upload(TaskConfiguration config)
        {
            var request = this.CreateRequest(config);
            var native  = this.session.CreateUploadTask(request, NSUrl.FromFilename(config.LocalFilePath));
            var task    = new HttpTask(config, native);

            this.Add(task);
            native.Resume();

            return(task);
        }
Beispiel #9
0
        public override IHttpTask Upload(TaskConfiguration config)
        {
            if (String.IsNullOrWhiteSpace(config.LocalFilePath))
            {
                throw new ArgumentException("You must set the file you wish to upload");
            }

            if (!File.Exists(config.LocalFilePath))
            {
                throw new ArgumentException("File to upload does not exist");
            }

            return(this.CreateTaskAndPersist(config, true));
        }
        public UploadHttpTask(TaskConfiguration config, UploadOperation operation, bool restart) : base(config, true)
        {
            this.operation     = operation;
            this.Identifier    = operation.Guid.ToString();
            this.LocalFilePath = operation.SourceFile.ToString();

            var task = restart ? this.operation.AttachAsync() : this.operation.StartAsync();

            task.AsTask(
                CancellationToken.None,
                new Progress <UploadOperation>(x => this.SetData(
                                                   x.Progress.Status,
                                                   x.Progress.BytesSent,
                                                   x.Progress.TotalBytesToSend,
                                                   x.Progress.HasRestarted
                                                   )
                                               ));
        }
Beispiel #11
0
        IHttpTask RebuildTask(SqlTaskConfiguration sqlTask, IList <SqlTaskConfigurationHeader> headers)
        {
            var config = new TaskConfiguration(sqlTask.Uri, sqlTask.LocalFilePath)
            {
                HttpMethod           = sqlTask.HttpMethod,
                PostData             = sqlTask.PostData,
                UseMeteredConnection = sqlTask.UseMeteredConnection
            };

            foreach (var header in headers)
            {
                config.Headers.Add(header.Key, header.Value);
            }

            var task = this.CreateTask(config, sqlTask.IsUpload, sqlTask.Id.ToString());

            return(task);
        }
Beispiel #12
0
        public DownloadHttpTask(TaskConfiguration config, DownloadOperation operation, bool restart) : base(config, false)
        {
            this.operation     = operation;
            this.Identifier    = this.operation.Guid.ToString();
            this.LocalFilePath = this.operation.ResultFile.Path;

            var task = restart ? this.operation.AttachAsync() : this.operation.StartAsync();

            task.AsTask(
                CancellationToken.None,
                new Progress <DownloadOperation>(x => this.SetData(
                                                     x.Progress.Status,
                                                     x.Progress.BytesReceived,
                                                     x.Progress.TotalBytesToReceive,
                                                     x.Progress.HasRestarted
                                                     )
                                                 ));
        }
        protected virtual NSUrlRequest CreateRequest(TaskConfiguration config)
        {
            var url     = NSUrl.FromString(config.Uri);
            var request = new NSMutableUrlRequest(url)
            {
                HttpMethod           = config.HttpMethod,
                AllowsCellularAccess = config.UseMeteredConnection
            };

            if (!String.IsNullOrWhiteSpace(config.PostData))
            {
                request.Body = NSData.FromString(config.PostData);
            }

            var headerDictionary = NSDictionary.FromObjectsAndKeys(config.Headers.Values.ToArray(), config.Headers.Keys.ToArray());

            request.Headers = headerDictionary;

            return(request);
        }
Beispiel #14
0
        protected virtual NSUrlRequest CreateRequest(TaskConfiguration config)
        {
            var url     = NSUrl.FromString(config.Uri);
            var request = new NSMutableUrlRequest(url)
            {
                HttpMethod           = config.HttpMethod,
                AllowsCellularAccess = config.UseMeteredConnection
            };

            if (!String.IsNullOrWhiteSpace(config.PostData))
            {
                request.Body = NSData.FromString(config.PostData);
            }

            foreach (var header in config.Headers)
            {
                request.Headers.SetValueForKey(
                    new NSString(header.Value),
                    new NSString(header.Key)
                    );
            }
            return(request);
        }
Beispiel #15
0
        IHttpTask CreateTaskAndPersist(TaskConfiguration config, bool upload)
        {
            try
            {
                this.conn.BeginTransaction();

                var sqlTask = new SqlTaskConfiguration
                {
                    HttpMethod           = config.HttpMethod,
                    Uri                  = config.Uri,
                    PostData             = config.PostData,
                    LocalFilePath        = config.LocalFilePath,
                    UseMeteredConnection = config.UseMeteredConnection,
                    IsUpload             = upload
                };
                this.conn.Insert(sqlTask);

                foreach (var header in config.Headers)
                {
                    this.conn.Insert(new SqlTaskConfigurationHeader
                    {
                        SqlTaskConfigurationId = sqlTask.Id,
                        Key   = header.Key,
                        Value = header.Value
                    });
                }
                var task = this.CreateTask(config, upload, sqlTask.Id.ToString());
                this.conn.Commit();

                return(task);
            }
            catch
            {
                this.conn.Rollback();
                throw;
            }
        }
Beispiel #16
0
 protected AbstractUwpHttpTask(TaskConfiguration config, bool isUpload) : base(config, isUpload)
 {
 }
Beispiel #17
0
 public HttpTask(TaskConfiguration config, NSUrlSessionTask task) : base(config, task is NSUrlSessionUploadTask)
 {
     this.task       = task;
     this.Identifier = task.TaskIdentifier.ToString();
 }
 public abstract IHttpTask Download(TaskConfiguration config);
Beispiel #19
0
 protected AbstractHttpTask(TaskConfiguration config, bool upload)
 {
     this.Configuration = config;
     this.IsUpload      = upload;
 }