static void Main(string[] args)
        {
            // Ignore self-signed SSL certs
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            var user       = "******";
            var password   = "******";
            var url        = "https://api.agile.lldns.net";
            var localPath  = @"c:\the\path\to\be\uploaded.txt";
            var remotePath = "/uploaded-file.txt";

            var client = new ApiClient(user, password, url);

            // There's no need to login, this is done internally
            var result = client.MakeFile(localPath, remotePath);

            Console.WriteLine("Got result: Size={0}, Checksum={1}, Path={2}", result.Size, result.Checksum, result.Path);

            var uploader = new SmartUpload(client);
            var mpId     = uploader.MakeFile(localPath, remotePath, 100);
        }
Esempio n. 2
0
        public void Test_Make_Multipart_file()
        {
            var size = 100;
            var sb   = new StringBuilder();

            for (int i = 0; i < size; i++)
            {
                sb.Append(i.ToString());
                sb.Append("-");
            }
            var localPath  = this.Fixture.CreateFile(sb.ToString());
            var remotePath = string.Format("/multipartfile-{0}.txt", System.Guid.NewGuid().ToString());
            var chunkSize  = 50;

            // Create the multipart file
            var smart = new SmartUpload(this.Client);

            smart.OnProgress += Client_OnProgress;

            var mpId = smart.MakeFile(localPath, remotePath, chunkSize);

            // Calculate the hexlified version of the checksum
            var bytes         = Encoding.UTF8.GetBytes(sb.ToString());
            var hasher        = SHA256.Create();
            var hash          = hasher.ComputeHash(bytes);
            var strHexBuilder = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                strHexBuilder.AppendFormat("{0:x2}", hash[i]);
            }
            var strHash = strHexBuilder.ToString();

            // Wait for the consumers to put together the multipart file
            while (true)
            {
                var status = this.Client.GetMultipartStatus(mpId);
                if (status.State == 6)
                {
                    break;
                }
                Thread.Sleep(200);
            }

            // Request the file
            var        url     = string.Format("http://global.mt.lldns.net/{0}{1}", this.Client.GetAgilePath(), remotePath);
            WebRequest request = null;

            var tries = 10;

            for (int i = 0; i < tries; i++)
            {
                try
                {
                    request = HttpWebRequest.Create(url);
                }
                catch (WebException)
                {
                    Thread.Sleep(1000);
                }
            }
            using (var response = request.GetResponse())
            {
                var fileChecksum  = response.Headers.Get("X-Agile-Checksum");
                var contentLength = Convert.ToInt32(response.Headers.Get("Content-Length"));
                using (var respStream = response.GetResponseStream())
                {
                    var buffer    = new byte[contentLength];
                    var read      = respStream.Read(buffer, 0, contentLength);
                    var bufferStr = Encoding.UTF8.GetString(buffer);

                    // Confirm that the contents of the file match what we sent
                    Assert.AreEqual(sb.ToString(), bufferStr);
                    // Confirm the locally calculated checksum matches the checksum returned when GET'ing the file
                    Assert.AreEqual(strHash, fileChecksum);
                }
            }
        }