예제 #1
0
        public async Task StartUpload(string tag, string url, string method, HttpContent content)
        {
            FileUploadRequest request = new FileUploadRequest()
            {
                Identifier       = tag,
                ContentType      = content.Headers.ContentType.ToString(),
                FilePath         = System.IO.Path.GetTempFileName(),
                Method           = method,
                Url              = url,
                ResponseFilePath = System.IO.Path.GetTempFileName()
            };

            List <Header> headerlist = new List <Header>();

            foreach (var header in content.Headers)
            {
                if (string.Equals(header.Key, "content-type", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                headerlist.Add(new Header(header.Key, string.Join(" ", header.Value)));
            }

            using (var fs = System.IO.File.OpenWrite(request.FilePath)) {
                await content.CopyToAsync(fs);
            }


            await QueueRequest(request);
        }
예제 #2
0
        protected override void StartUploadInternal(FileUploadRequest r)
        {
            var session = GetSession(r.Identifier);

            var request = new NSMutableUrlRequest(NSUrl.FromString(r.Url));

            request.HttpMethod = r.Method;


            NSMutableDictionary headers = new NSMutableDictionary();

            headers.SetValueForKey(new NSString(r.ContentType), (NSString)"Content-Type");

            if (r.Headers != null)
            {
                foreach (var h in r.Headers)
                {
                    headers.SetValueForKey(new NSString(h.Key), new NSString(h.Value));
                }
            }
            request.Headers = headers;


            var uploadTask = session.CreateUploadTask(request, NSUrl.FromFilename(r.FilePath));

            uploadTask.Resume();
        }
예제 #3
0
        internal Task ReportStatus(FileUploadRequest r)
        {
            return(Task.Run(async() =>
            {
                try
                {
                    //lock (r)
                    //{
                    //    if (r.IsNotifying)
                    //        return;
                    //    r.IsNotifying = true;
                    //}

                    if (r.ResponseCode == 200)
                    {
                        if (await Receiver.CompletedAsync(r))
                        {
                            r.Processed = true;
                        }
                    }
                    else
                    {
                        if (await Receiver.FailedAsync(r))
                        {
                            r.Processed = true;
                        }
                    }

                    if (r.Processed)
                    {
                        // delete file...
                        Storage.Remove(r);
                        if (System.IO.File.Exists(r.FilePath))
                        {
                            System.IO.File.Delete(r.FilePath);
                        }
                        if (r.ResponseFilePath != null && System.IO.File.Exists(r.ResponseFilePath))
                        {
                            System.IO.File.Delete(r.ResponseFilePath);
                        }
                        SaveState();
                    }
                }
                catch (Exception ex)
                {
                    ReportFatalError(ex);
                }
                //finally
                //{
                //    lock (r)
                //    {
                //        r.IsNotifying = false;
                //    }
                //}
            }));
        }
예제 #4
0
        protected virtual Task QueueRequest(FileUploadRequest request)
        {
            Storage.Add(request);

            StartUploadInternal(request);

            SaveState();

            return(Task.CompletedTask);
        }
 public void Add(FileUploadRequest request)
 {
     lock (this)
     {
         if (requests.Any(x => x.Identifier == request.Identifier))
         {
             throw new InvalidOperationException();
         }
         requests.Add(request);
         Save(false);
     }
 }
 public bool Remove(FileUploadRequest request)
 {
     lock (this)
     {
         int index = requests.FindIndex(x => x.Identifier == request.Identifier);
         if (index != -1)
         {
             requests.RemoveAt(index);
             Save(false);
             return(true);
         }
     }
     return(false);
 }
예제 #7
0
 protected abstract void OnCancel(FileUploadRequest r);
예제 #8
0
 internal void ReportProgress(FileUploadRequest r)
 {
     Receiver.OnProgress(r);
     SaveState();
 }
예제 #9
0
 protected abstract void StartUploadInternal(FileUploadRequest request);
예제 #10
0
 protected override void OnCancel(FileUploadRequest request)
 {
     BackgroundUploadService.CancellationTokenSource?.Cancel(false);
 }
예제 #11
0
        protected override void StartUploadInternal(FileUploadRequest request)
        {
            Intent intent = new Intent(Context, typeof(BackgroundUploadService));

            Context.StartService(intent);
        }
 protected override void StartUploadInternal(FileUploadRequest request)
 {
     throw new NotImplementedException();
 }
 protected override void OnCancel(FileUploadRequest r)
 {
     throw new NotImplementedException();
 }
예제 #14
0
 protected override void OnCancel(FileUploadRequest r)
 {
     GetSession(r.Identifier).InvalidateAndCancel();
 }
예제 #15
0
 public FileUploadRequestArgs(FileUploadRequest request)
 {
     this.Request = request;
 }