Example #1
0
        protected override async Task <HttpTransfer> CreateDownload(HttpTransferRequest request)
        {
            var task = new BackgroundDownloader
            {
                Method     = request.HttpMethod.Method,
                CostPolicy = request.UseMeteredConnection
                    ? BackgroundTransferCostPolicy.Default
                    : BackgroundTransferCostPolicy.UnrestrictedOnly
            };

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

            if (!request.LocalFile.Exists)
            {
                request.LocalFile.Create();
            }

            var winFile = await StorageFile.GetFileFromPathAsync(request.LocalFile.FullName).AsTask();

            var operation = task.CreateDownload(new Uri(request.Uri), winFile);

            operation.StartAsync();

            return(operation.FromNative());
        }
Example #2
0
        public async Task <IHttpTransfer> Create(HttpTransferRequest request)
        {
            var task = new BackgroundDownloader
            {
                Method     = request.HttpMethod,
                CostPolicy = request.UseMeteredConnection
                    ? BackgroundTransferCostPolicy.Default
                    : BackgroundTransferCostPolicy.UnrestrictedOnly
            };

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

            //var filePath = config.LocalFilePath ?? Path.Combine(ApplicationData.Current.LocalFolder.Path, Path.GetRandomFileName());
            var winFile = await StorageFile.GetFileFromPathAsync(request.LocalFilePath.FullName).AsTask();

            var op = task.CreateDownload(new Uri(request.Uri), winFile);

            //var operation = task.CreateDownload(new Uri(config.Uri), file);
            //var httpTask = new DownloadHttpTask(config, operation, false);
            //this.Add(httpTask);
            return(null);
        }
Example #3
0
        public async Task <IHttpTransfer> Create(HttpTransferRequest request)
        {
            var native = new Native.Request(request.LocalFilePath.ToNativeUri());

            //native.SetAllowedNetworkTypes(DownloadNetwork.Wifi)
            //native.SetAllowedOverRoaming()
            //native.SetNotificationVisibility(DownloadVisibility.Visible);
            //native.SetRequiresDeviceIdle
            //native.SetRequiresCharging
            //native.SetTitle("")
            //native.SetVisibleInDownloadsUi(true);
            native.SetAllowedOverMetered(request.UseMeteredConnection);

            foreach (var header in request.Headers)
            {
                native.AddRequestHeader(header.Key, header.Value);
            }

            var id = this.GetManager().Enqueue(native);

            //await this.repository.Set(id.ToString(), new HttpTransferStore
            //{

            //});
            return(null);
        }
Example #4
0
        public UploadHttpTransfer(HttpTransferRequest request) : base(request, true)
        {
            this.httpClient = new HttpClient();
            this.cancelSrc  = new CancellationTokenSource();
            //this.Identifier = identifier;

            //this.GetManager().Remove(id);
        }
Example #5
0
        public Task <IHttpTransfer> Create(HttpTransferRequest request)
        {
            var task = this.session.CreateUploadTask(
                NSUrlRequest.FromUrl(NSUrl.FromFilename(request.Uri)),
                NSUrl.FromFilename(request.LocalFilePath.FullName)
                );

            return(null);
        }
Example #6
0
        protected override async Task <HttpTransfer> CreateUpload(HttpTransferRequest request)
        {
            if (request.HttpMethod != System.Net.Http.HttpMethod.Post && request.HttpMethod != System.Net.Http.HttpMethod.Put)
            {
                throw new ArgumentException($"Invalid Upload HTTP Verb {request.HttpMethod} - only PUT or POST are valid");
            }

            var boundary = Guid.NewGuid().ToString("N");

            var native = request.ToNative();

            native["Content-Type"] = $"multipart/form-data; boundary={boundary}";

            var tempPath = platform.GetUploadTempFilePath(request);

            this.logger.LogInformation("Writing temp form data body to " + tempPath);

            using (var fs = new FileStream(tempPath, FileMode.Create))
            {
                if (!request.PostData.IsEmpty())
                {
                    fs.Write("--" + boundary);
                    fs.Write($"Content-Type: text/plain; charset=utf-8");
                    fs.Write("Content-Disposition: form-data;");
                    fs.WriteLine();
                    fs.Write(request.PostData !);
                    fs.WriteLine();
                }
            }
            using (var fs = new FileStream(tempPath, FileMode.Append))
            {
                using (var uploadFile = request.LocalFile.OpenRead())
                {
                    fs.Write("--" + boundary);
                    fs.Write($"Content-Type: application/octet-stream");
                    fs.Write($"Content-Disposition: form-data; name=\"blob\"; filename=\"{request.LocalFile.Name}\"");
                    fs.WriteLine();
                    await uploadFile.CopyToAsync(fs);

                    fs.WriteLine();
                    fs.Write($"--{boundary}--");
                }
            }

            this.logger.LogInformation("Form body written");
            var tempFileUrl = NSUrl.CreateFileUrl(tempPath, null);

            var task   = this.Session.CreateUploadTask(native, tempFileUrl);
            var taskId = TaskIdentifier.Create(request.LocalFile);

            task.TaskDescription = taskId.ToString();
            var transfer = task.FromNative();

            task.Resume();

            return(transfer);
        }
        public virtual async Task <HttpTransfer> Enqueue(HttpTransferRequest request)
        {
            var task = request.IsUpload
                ? this.CreateUpload(request)
                : this.CreateDownload(request);

            var transfer = await task.ConfigureAwait(false);

            return(transfer);
        }
Example #8
0
        protected override Task <HttpTransfer> CreateUpload(HttpTransferRequest request)
        {
            var task   = this.Session.CreateUploadTask(request.ToNative());
            var taskId = TaskIdentifier.Create(request.LocalFile);

            task.TaskDescription = taskId.ToString();
            var transfer = task.FromNative();

            task.Resume();

            return(Task.FromResult(transfer));
        }
Example #9
0
        protected override async Task <HttpTransfer> CreateDownload(HttpTransferRequest request)
        {
            if (request.HttpMethod != HttpMethod.Get)
            {
                throw new ArgumentException("Only GETs are supported for downloads on Android");
            }

            var access = await this.Services.Android.RequestAccess(Manifest.Permission.WriteExternalStorage);

            if (access != AccessState.Available)
            {
                throw new ArgumentException("Invalid access to external storage - " + access);
            }

            access = await this.Services.Android.RequestAccess(Manifest.Permission.ReadExternalStorage);

            if (access != AccessState.Available)
            {
                throw new ArgumentException("Invalid access to external storage - " + access);
            }

            var dlPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
            var path   = Path.Combine(dlPath, request.LocalFile.Name);

            var native = new Native
                         .Request(Android.Net.Uri.Parse(request.Uri))
                         .SetDescription(request.LocalFile.FullName)
                         .SetDestinationUri(ToNativeUri(path)) // WRITE_EXTERNAL_STORAGE
                         .SetAllowedOverMetered(request.UseMeteredConnection);

            foreach (var header in request.Headers)
            {
                native.AddRequestHeader(header.Key, header.Value);
            }

            var id = this.Services.Android.GetManager().Enqueue(native);

            return(new HttpTransfer(
                       id.ToString(),
                       request.Uri,
                       request.LocalFile.FullName,
                       false,
                       request.UseMeteredConnection,
                       null,
                       0,
                       0,
                       HttpTransferState.Pending
                       ));
        }
Example #10
0
        protected override Task <HttpTransfer> CreateDownload(HttpTransferRequest request)
        {
            var task   = this.Session.CreateDownloadTask(request.ToNative());
            var taskId = TaskIdentifier.Create(request.LocalFile);

            task.TaskDescription = taskId.ToString();
            //task.Response.SuggestedFilename
            //task.Response.ExpectedContentLength

            var transfer = task.FromNative();

            task.Resume();

            return(Task.FromResult(transfer));
        }
Example #11
0
        public Task <IHttpTransfer> Create(HttpTransferRequest request)
        {
            //public override IHttpTransfer Upload(HttpTransferConfiguration 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 UploadHttpTransfer(config, operation, false);
            //          this.Add(httpTask);

            //          return httpTask;
            //      }
            //BackgroundUploader
            //               .GetCurrentUploadsAsync()
            //               .AsTask()
            //               .ContinueWith(result =>
            //               {
            //                   foreach (var task in result.Result)
            //                   {
            //                       var config = new HttpTransferConfiguration(task.RequestedUri.ToString(), task.SourceFile.Path)
            //                       {
            //                           HttpMethod = task.Method,
            //                           UseMeteredConnection = task.CostPolicy != BackgroundTransferCostPolicy.UnrestrictedOnly
            //                       };
            //                       this.Add(new UploadHttpTransfer(config, task, true));
            //                   }
            //               });
            throw new NotImplementedException();
        }
Example #12
0
        public static NSMutableUrlRequest ToNative(this HttpTransferRequest request)
        {
            var url    = NSUrl.FromString(request.Uri);
            var native = new NSMutableUrlRequest(url)
            {
                HttpMethod           = request.HttpMethod.Method,
                AllowsCellularAccess = request.UseMeteredConnection,
            };

            if (request.Headers.Any())
            {
                native.Headers = NSDictionary.FromObjectsAndKeys(
                    request.Headers.Values.ToArray(),
                    request.Headers.Keys.ToArray()
                    );
            }

            return(native);
        }
Example #13
0
        async Task <HttpTransfer> Create(HttpTransferRequest request)
        {
            var id = Guid.NewGuid().ToString();

            await this.Services
            .Repository
            .Set(id, new HttpTransferStore
            {
                Id                   = id,
                Uri                  = request.Uri,
                IsUpload             = request.IsUpload,
                PostData             = request.PostData,
                LocalFile            = request.LocalFile.FullName,
                UseMeteredConnection = request.UseMeteredConnection,
                HttpMethod           = request.HttpMethod.ToString(),
                Headers              = request.Headers
            })
            .ConfigureAwait(false);

            await this.jobManager.Register(new JobInfo(typeof(TransferJob), id)
            {
                RequiredInternetAccess = InternetAccess.Any,
                Repeat = true
            });

            var transfer = new HttpTransfer(
                id,
                request.Uri,
                request.LocalFile.FullName,
                request.IsUpload,
                request.UseMeteredConnection,
                null,
                request.IsUpload ? request.LocalFile.Length : 0L,
                0,
                HttpTransferState.Pending
                );

            // fire and forget
            this.jobManager.Run(id);
            return(transfer);
        }
Example #14
0
 protected AbstractHttpTransfer(HttpTransferRequest request, bool upload)
 {
     this.Request  = request;
     this.IsUpload = upload;
 }
Example #15
0
 public static string GetUploadTempFilePath(this IPlatform platform, HttpTransferRequest request)
 => GetUploadTempFilePath(platform, request.LocalFile.Name);
Example #16
0
 protected override Task <HttpTransfer> CreateUpload(HttpTransferRequest request)
 => this.Create(request);
Example #17
0
 public HttpTransfer(HttpTransferRequest request, DownloadOperation operation) : base(request, false)
 {
 }
 protected virtual Task <HttpTransfer> CreateDownload(HttpTransferRequest request)
 {
     throw new NotImplementedException();
 }
Example #19
0
 public Task <IHttpTransfer> Create(HttpTransferRequest request)
 {
     throw new NotImplementedException();
 }
 public DownloadHttpTransfer(HttpTransferRequest request) : base(request, false)
 {
 }
Example #21
0
 public HttpTransfer(HttpTransferRequest request, UploadOperation operation) : base(request, true)
 {
 }