Exemple #1
0
        private void DownloadFile(TestCase testCase)
        {
            List <string> currFolderPath = testCase.GetTestCasePath();

            currFolderPath.Add(DirectoryTools.CreateValidFolderString(testCase.TestCaseName));
            //currFolderPath.Add(_stringHelper.TrimEndNewLine(url.TestCaseName));
            string folderPath          = _saveLocation + GeneratePath(currFolderPath);
            string regexSearch         = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            Regex  r                   = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
            string truncatedFolderPath = folderPath;

            truncatedFolderPath = r.Replace(truncatedFolderPath, "");
            truncatedFolderPath = Regex.Replace(TruncateFolderName(folderPath, 200), @"\t|\n|\r", "");
            truncatedFolderPath = Regex.Replace(truncatedFolderPath, @"/", "");
            //Console.WriteLine(WebUtility.HtmlEncode(truncatedFolderPath));
            //Console.WriteLine(truncatedFolderPath.Length);
            DirectoryTools.createDirectory(truncatedFolderPath);

            if (testCase.Urls.Count == 0)
            {
                _testCasesMissingAttachments.Add(testCase);
            }
            else
            {
                foreach (var currUrl in testCase.Urls)
                {
                    Uri    urlLink = new Uri(currUrl.UrlLink);
                    string urlName = currUrl.UrlName;

                    string uri = "APHP/_apis/wit/attachments" + currUrl.UrlLink.Substring(currUrl.UrlLink.LastIndexOf('/'));

                    HttpMethod         method  = new HttpMethod("GET");
                    HttpRequestMessage request = new HttpRequestMessage(method, uri)
                    {
                    };
                    var response = _client.GetAsync(urlLink).Result;
                    response.EnsureSuccessStatusCode();
                    byte[] data = response.Content.ReadAsByteArrayAsync().Result;
                    //Console.WriteLine("Starting File Write");
                    if (Directory.Exists(truncatedFolderPath))
                    {
                        var    extension = urlName.Substring(urlName.LastIndexOf("."));
                        string truncatedFolderFileName = TruncateFolderName(truncatedFolderPath + urlName, 260, extension);
                        //Console.WriteLine(truncatedFolderFileName);
                        //Console.WriteLine(truncatedFolderFileName.Length);
                        FileStream fileStream = new FileStream(truncatedFolderFileName,
                                                               FileMode.Create, FileAccess.Write, FileShare.None);
                        fileStream.SetLength(0);
                        fileStream.Write(data, 0, data.Length);
                        fileStream.Flush(true);
                        fileStream.Close();
                    }

                    Console.WriteLine("Download of {0} has finished.", urlName);
                    //Console.WriteLine(urlLink + "  " + urlName);
                }
            }
        }