예제 #1
0
        public void RequestFileUpload(string filePath, CryptoChoice answer, bool replaceOnConflict = false)
        {
            string fileName = Path.GetFileName(filePath);

            byte[] fileBytes = File.ReadAllBytes(filePath);

            SHA1Hasher sha1 = new SHA1Hasher();

            sha1.ComputeHash(fileBytes);
            string hashValue = sha1.HashedString;

            byte[] encryptedBytes;
            try
            {
                encryptedBytes = cryptionController.EncryptFile(fileName, fileBytes, answer, replaceOnConflict);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Couldn't encrypt the file: " + e.Message);
            }

            HttpWebResponse resp = null;

            try
            {
                string FileManagerServiceUrl = "http://localhost:56082/MyCloudStore/CloudStoreService.svc";
                var    serviceUrl            = string.Format($"{FileManagerServiceUrl}/UploadFile/{fileName}/{hashValue}");
                var    request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method      = "POST";
                request.ContentType = "text/plain";

                System.IO.Stream reqStream = request.GetRequestStream();
                reqStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                reqStream.Close();

                resp = (HttpWebResponse)request.GetResponse();
                System.Windows.Forms.MessageBox.Show(string.Format("Client: Receive Response HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription));
            }
            catch (Exception e)
            {
                throw new Exception("Error uploading the file to the service: " + e.GetFullMessage());
            }
            finally
            {
                if (resp != null && resp.StatusCode != HttpStatusCode.OK)
                {
                    cryptionController.RemoveFileRecord(fileName);
                }

                resp.Dispose();
            }
        }