public Uploader StartUpload(string apiKey, string apiSecret, string apiBase, DS4Session session) { Uploader uploader; if(!_uploaders.TryGetValue(session, out uploader)) { uploader = new Uploader(apiKey, apiSecret, apiBase, session); uploader.Completing += (sender, e) => { if (e.Success) { //session.Metadata.BodyhubPersonId = e.BodyhubPersonId; //_store.Save(); } }; _uploaders[session] = uploader; } if (!File.Exists(session.CompressedScanFile)) { // Something went wrong probably during Compression and file is deleted throw new InvalidOperationException("Compressed Scan file is missing!"); } session.Uploader = uploader; if(uploader.Waiting) { throw new InvalidOperationException("Uploader is already enqueued for upload"); } else if (uploader.InProgress) { throw new InvalidOperationException("Upload is already in progress"); } else if (uploader.ReTrying) { AutoReTryer autoReTryer = uploader.ReTryer; if (autoReTryer == null) { throw new InvalidOperationException("Uploader in ReTrying state should have a ReTryer registered"); } autoReTryer.StartNow(); } else { EnqueueUploadWork(uploader); } return uploader; }
/// <summary> /// Processing upload result and re-upload using a linear back-off function /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Workqueue_UploadWorkCompleted(object sender, WorkCompletedEventArgs <Uploader, UploadResult> e) { Uploader uploader = e.WorkItem; UploadResult result = e.Result; if (result.Exception != null && !result.Canceled) { //Re-upload AutoReTryer autoReTryer = uploader.ReTryer; if (autoReTryer == null) { autoReTryer = new AutoReTryer( () => EnqueueUploadWork(uploader), (seq) => seq + 1, REUPLOAD_INTERVAL_IN_SECONDS); uploader.ReTryer = autoReTryer; } autoReTryer.StartOrContinueCountdown(); } }
private void EnqueueUploadWork(Uploader uploader) { _workQueue.EnqueueWork(uploader, (_uploader) => _uploader.PerformUpload()); uploader.Waiting = true; }
/// <summary> /// Uploads the specified file to the cloud. /// </summary> /// <param name="file">The full path to the desired .zip file.</param> private async Task <UploadResult> Upload(DS4Session session, CancellationToken ct) { string file = session.CompressedScanFile; string contentType = "application/x/7z-compressed"; WebClient proxy; string result = string.Empty; // Step1: Get s3 signed URL proxy = new WebClient(); // Gather data string fileName = Path.GetFileName(file); Dictionary <string, string> postDict = new Dictionary <string, string> { { "filename", fileName }, { "file_type", contentType }, }; String postData = JSONHelper.Instance.Serialize(postDict); // Prepare request proxy.Headers["Content-Type"] = "application/json"; proxy.Headers["Authorization"] = _authHeader; // Perform request try { result = await proxy.UploadStringTaskAsync(this.BuildUri("s3/sign"), "POST", postData); } catch (WebException ex) { return(new UploadResult { Success = false, Exception = ex }); } ct.ThrowIfCancellationRequested(); // Step 2: Upload to s3 signed PUT URL _s3Proxy = proxy = new WebClient(); proxy.UploadProgressChanged += new UploadProgressChangedEventHandler(this.Progress); proxy.UploadDataCompleted += (s, e) => { if (ct.IsCancellationRequested) { _canceled = true; } }; // Gather data Dictionary <string, string> response = JSONHelper.Instance.CreateDictionary(result); string url = response["signedUrl"]; string key = response["key"]; byte[] binaryData = File.ReadAllBytes(file); // Prepare headers proxy.Headers["Content-Type"] = contentType; // Prepare request Uri uri = new Uri(url, UriKind.Absolute); // Perform request try { byte[] uploadResponse = await proxy.UploadDataTaskAsync(uri, "PUT", binaryData); } catch (WebException ex) { Console.WriteLine(Uploader.GetErrorMsgFromWebException(ex)); return(new UploadResult { Success = false, Exception = ex, Canceled = _canceled }); } ct.ThrowIfCancellationRequested(); // Step 3: PostUpload and get returned BodyId proxy = new WebClient(); //Assemble payload List <Dictionary <string, string> > artifacts = new List <Dictionary <string, string> >(); artifacts.Add(new Dictionary <string, string> { { "artifactsType", "DS4Measurements" } }); artifacts.Add(new Dictionary <string, string> { { "artifactsType", "DS4Alignment" } }); DS4BodyRequest bodyRequest = new DS4BodyRequest("ds4_scan", key, artifacts); postData = JSONHelper.Instance.Serialize(bodyRequest); // Prepare request proxy.Headers["Content-Type"] = "application/json"; proxy.Headers["Authorization"] = _authHeader; // Perform request try { result = await proxy.UploadStringTaskAsync(this.BuildUri("bodies/from_scan"), "POST", postData); } catch (WebException ex) { Console.WriteLine(Uploader.GetErrorMsgFromWebException(ex)); return(new UploadResult { Success = false, Exception = ex }); } DS4PostUploadResponse ds4PostUploadResponse; using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(result))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DS4PostUploadResponse)); ds4PostUploadResponse = (DS4PostUploadResponse)serializer.ReadObject(ms); ms.Close(); } string bodyId = ds4PostUploadResponse.BodyId; return(new UploadResult { Success = true, BodyId = bodyId, Artifacts = ds4PostUploadResponse.Artifacts }); }
private void EnqueueUploadWork(Uploader uploader) { _workQueue.EnqueueWork(uploader, (_uploader) => _uploader.PerformUpload()); uploader.Waiting = true; }
private void _upload(DS4Session session, string apiBase, string apiKey, string apiSecret) { Uploader uploader = this.StartUpload(apiKey, apiSecret, apiBase, session); uploader.Completed += UploadFinished; }