Example #1
0
        protected override void OnExecute(BackgroundWorker backgroundWorker)
        {
            // Prepare upload
            var client = new HttpClient();

            client.MultipartFormat = true;
            client.AddPostString("token", Token);
            using (var stream = File.OpenRead(SharedData.Instance.ArchiveFileName))
            {
                client.AddPostFile("logFile", Path.GetFileName(SharedData.Instance.ArchiveFileName), stream);
            }

            // Send file
            client.Request(new Uri(Url), backgroundWorker);

            // Check result
            if (client.LastHttpStatusCode != 200)
            {
                throw new Exception(Tx.T("msg.error.http response status", "code", client.LastHttpStatusCode.ToString(), "msg", client.LastResponseData));
            }
            var match = Regex.Match(client.LastResponseData, @"^OK (\S+) ([0-9]+) ([0-9a-f]+)");

            if (!match.Success)
            {
                throw new Exception(Tx.T("msg.error.http response pattern"));
            }
            if (match.Groups[1].Value != Token)
            {
                throw new Exception(Tx.T("msg.error.http response token"));
            }
            var fileInfo = new FileInfo(SharedData.Instance.ArchiveFileName);

            if (long.Parse(match.Groups[2].Value) != fileInfo.Length)
            {
                throw new Exception(Tx.T("msg.error.http response file size", "actual", match.Groups[2].Value, "expected", fileInfo.Length.ToString()));
            }
            string sha1 = GetFileSha1(SharedData.Instance.ArchiveFileName);

            if (!match.Groups[3].Value.Equals(sha1, StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception(Tx.T("msg.error.http response file hash"));
            }
        }