public void Upload(string url, string fileName, List <KeyValuePair <string, string> > headers) { WebClient webClient = new WebClient(); // add event handlers for completed and progress changed webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressChanged); webClient.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompleted); webClient.Headers.Add("user-agent", Config.settings.about.product + " " + Config.settings.about.version); foreach (KeyValuePair <string, string> header in headers) { webClient.Headers.Add(header.Key, header.Value); } Uri uri = new Uri(url); string sync = MySerialize.ToJSON(Sync.syncSettings); MyLog.Add("Upload to uri:" + uri.ToString() + " sync:" + sync + " file:" + fileName); int progress = 0; subFormProgressSyncUp.Value(progress); subFormProgressSyncUp.Text("Uploading.."); webClient.UploadFileAsync(uri, fileName); }
public bool SyncUpVideoSource(FileInfo sourceFileInfo) { // clean up old uploads IEnumerable <string> files = MyFile.EnumerateFiles(@"sync", "*.gz"); foreach (string file in files) { MyFile.DeleteFile(file); } // compress file, video source string compressedFile = @"sync\" + MyFile.SafeFileName(sourceFileInfo.Name); if (!MyFile.Compress(sourceFileInfo.FullName, compressedFile)) { return(false); } // rename file so has 'rand' key/iv Random random = new Random(); int rand = random.Next(0, MyEncrypt.sharedKeys.Length - 1); string uploadFile = compressedFile.Replace("." + Config.settings.exportExt, "-" + rand + "." + Config.settings.exportExt); File.Move(compressedFile + ".gz", uploadFile + ".gz"); string key = MyEncrypt.sharedKeys[rand]; string iv = MyEncrypt.GenerateIV(); string delimiter = "#"; // now encrypt compressed file contents string fileContents = MyFile.ReadAllBinaryToString(uploadFile + ".gz"); string contentsHeader = "{"; contentsHeader += "\"apiKey\":\"" + this.apiKey + "\", "; contentsHeader += "\"iv\":\"" + iv + "\", "; contentsHeader += "\"sync\":" + MySerialize.ToJSON(Sync.syncSettings); contentsHeader += "}"; contentsHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentsHeader)); fileContents = MyEncrypt.EncryptRJ256(key, iv, fileContents); if (fileContents == null) { return(false); } contentsHeader = MyEncrypt.EncryptRJ256(key, iv, contentsHeader); string contentsToEncode = contentsHeader + delimiter + fileContents; // write base64 encoded file File.WriteAllText(uploadFile + ".enc", contentsToEncode); // log it FileInfo encodedFileInfo = MyFile.FileInfo(uploadFile + ".enc"); if (encodedFileInfo == null) { return(false); } MyLog.Add(String.Format("Encrypted {0} to {1}", encodedFileInfo.Name, MyFile.FormatSize(encodedFileInfo.Length))); // test decrypt fileContents = MyFile.ReadAllText(uploadFile + ".enc"); string[] fileParts = fileContents.Split(new string[] { delimiter }, StringSplitOptions.None); fileContents = MyEncrypt.DecryptRJ256(key, iv, fileParts[1]); MyFile.DeleteFile(uploadFile + ".gz"); MyFile.WriteAllBinaryFromString(uploadFile + ".gz", fileContents); // post encoded file to website string url = apiURL; List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> > { }; headers.Add(new KeyValuePair <string, string>("api-key", apiKey)); headers.Add(new KeyValuePair <string, string>("access-token", iv)); Upload(url, uploadFile + ".enc", headers); return(true); }