示例#1
0
        /// <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 };
        }
示例#2
0
        /// <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
            });
        }