private async Task PerformBulkUpload(UploadProgressForm uploadForm, string destinationPath, List <string> compressedFiles, bool isGameData) { System.Timers.Timer dotsTimer = new System.Timers.Timer(1000); dotsTimer = new System.Timers.Timer(1000); dotsTimer.AutoReset = true; dotsTimer.Elapsed += (s, e) => { int dotsCount = uploadForm.uploadLabel.Text.Count(c => c == '.'); uploadForm.Invoke((MethodInvoker) delegate { uploadForm.uploadLabel.Text = ((string)lang[isGameData ? "uploading_data" : "uploading_saves"]) + new string('.', (dotsCount + 1) % 4); }); }; dotsTimer.Enabled = true; int threadCount = 0; foreach (var file in compressedFiles) { Task.Run(async() => { await UploadItem(file, destinationPath + (isGameData ? "data/" : "saves/") + Path.GetFileName(file), uploadForm); threadCount--; }); threadCount++; while (threadCount >= Settings.Default.MaxUploadThreads) { await Task.Delay(100); } } while (uploadedCompressedFiles < compressedFilesCount) { await Task.Delay(100); } dotsTimer.Stop(); }
private async Task <Dictionary <string, List <string> > > CompressFiles(UploadProgressForm uploadForm, string folderPath, string gameName, bool isGameData) { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Temp/")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Temp/"); } string compressedFilePath = AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameName + (isGameData ? "_Data.tar.gz" : "_Saves.tar.gz"); System.Timers.Timer dotsTimer = new System.Timers.Timer(1000); dotsTimer.AutoReset = true; dotsTimer.Elapsed += (s, e) => { int dotsCount = uploadForm.uploadLabel.Text.Count(c => c == '.'); uploadForm.Invoke((MethodInvoker) delegate { uploadForm.uploadLabel.Text = ((string)lang[isGameData ? "compressing_data" : "compressing_saves"]) + new string('.', (dotsCount + 1) % 4); }); }; dotsTimer.Enabled = true; List <string> compressedFiles = await Task.Run(async() => { return(await CompressionHelper.CompressAndSplit(compressedFilePath, folderPath)); }); compressedFilesCount = compressedFiles.Count; uploadedCompressedFiles = 0; dotsTimer.Stop(); Dictionary <string, List <string> > result = new Dictionary <string, List <string> >(); result.Add(compressedFilePath, compressedFiles); return(result); }
static async Task <byte[]> ReadAllParts(Game gameData, bool isGameData, UploadProgressForm uploadForm) { OnedriveHelper onedriveHelper = OnedriveHelper.Instance; int bytesRead = 0; byte[] buffer = new byte[isGameData ? gameData.DataSize : gameData.SaveSize]; int partsNumber = isGameData ? gameData.DataParts : gameData.SavesParts; try { for (int i = 0; i < partsNumber; i++) { byte[] data = await onedriveHelper.ReadItem("GameLinker/" + gameData.GameName + (isGameData ? "/data/" : "/saves/") + gameData.GameName + (isGameData ? "_Data" : "_Saves") + ".tar.gz.part_" + i); data.CopyTo(buffer, bytesRead); bytesRead += data.Length; uploadForm.Invoke((MethodInvoker) delegate { int percentage = (int)(((float)bytesRead / (float)buffer.Length) * 100); uploadForm.uploadProgressBar.Value = percentage; uploadForm.uploadValueLabel.Text = percentage + "%"; }); GC.Collect(); } } catch (Exception ex) { } return(buffer); }
public async static Task JoinAndDecompress(Game gameData, UploadProgressForm uploadForm, string dataPath = null, string savesPath = null) { uploadForm.uploadLabel.Text = (string)lang["updating_library"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.Show(); if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Temp/")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Temp/"); } if (dataPath != "") { uploadForm.uploadLabel.Text = (string)lang["downloading_data_fragments"]; uploadForm.uploadValueLabel.Text = "0%"; uploadForm.uploadProgressBar.Value = 0; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Continuous; byte[] dataBuffer = await ReadAllParts(gameData, true, uploadForm); uploadForm.uploadLabel.Text = (string)lang["decompressing_data"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.uploadProgressBar.Value = 100; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Marquee; bool dataFileCreated = await SaveData(dataBuffer, AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameData.GameName + "_Data.tar.gz"); Decompress(AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameData.GameName + "_Data.tar.gz", dataPath, true, uploadForm); } if (savesPath != "") { uploadForm.uploadLabel.Text = (string)lang["downloading_saves_fragments"]; uploadForm.uploadValueLabel.Text = "0%"; uploadForm.uploadProgressBar.Value = 0; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Continuous; byte[] savesDataBuffer = await ReadAllParts(gameData, false, uploadForm); uploadForm.uploadLabel.Text = (string)lang["decompressing_saves"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.uploadProgressBar.Value = 100; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Marquee; bool savesFileCreated = await SaveData(savesDataBuffer, AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameData.GameName + "_Saves.tar.gz"); Decompress(AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameData.GameName + "_Saves.tar.gz", savesPath, false, uploadForm); } uploadForm.uploadLabel.Text = (string)lang["restore_successful"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.uploadProgressBar.Value = 100; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Continuous; uploadForm.acceptButton.Enabled = true; }
private async void addGameButton_Click(object sender, EventArgs e) { string savesPath = savesPathTextBox.Text, dataPath = dataPathTextBox.Text, gameName = gameNameTextbox.Text; try { Game item = new Game(savesPath, dataPath, gameName); UploadProgressForm uploadForm = new UploadProgressForm(callback); uploadForm.Show(Owner); Hide(); if (dataPath != "") { int[] filesData = await onedriveManager.UploadFolder(dataPath, "GameLinker/" + gameName + "/", uploadForm, gameName); item.DataParts = filesData[1]; item.DataSize = filesData[0]; } if (savesPath != "") { int[] savesData = await onedriveManager.UploadFolder(dataPath, "GameLinker/" + gameName + "/", uploadForm, gameName); item.SavesParts = savesData[1]; item.SaveSize = savesData[0]; } uploadForm.uploadLabel.Text = (string)lang["updating_library"]; uploadForm.uploadValueLabel.Text = ""; LibraryHelper.Library.AddGame(item); LibraryHelper.SaveLibrary(); uploadForm.uploadLabel.Text = (string)lang["uploading_updated_library"]; uploadForm.uploadValueLabel.Text = "0%"; onedriveManager.compressedFilesCount = 1; onedriveManager.uploadedCompressedFiles = 0; await onedriveManager.UploadItem(AppDomain.CurrentDomain.BaseDirectory + "Library.bin", "GameLinker/" + "Library.bin", uploadForm); uploadForm.uploadLabel.Text = (string)lang["upload_successful"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.acceptButton.Enabled = true; Close(); } catch (ArgumentNullException err) { MessageBox.Show(this, err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static async Task LoadLibrary() { if (Library == null) { UploadProgressForm uploadForm = new UploadProgressForm(); uploadForm.uploadLabel.Text = (string)lang["downloading_library"]; uploadForm.Text = (string)lang["download_progress"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.uploadProgressBar.Value = 100; try { Library = (GamesLibrary)Deserialize(AppDomain.CurrentDomain.BaseDirectory + "Library.bin"); } catch (FileNotFoundException) { DialogResult response = MessageBox.Show((string)lang["library_not_found"], (string)lang["warning"], MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (response == DialogResult.Yes) { uploadForm.Show(); byte[] libraryData = await OnedriveHelper.Instance.ReadItem("GameLinker/Library.bin"); uploadForm.Hide(); if (libraryData == null) { Library = new GamesLibrary(); MessageBox.Show((string)lang["library_being_created"], (string)lang["warning"], MessageBoxButtons.OK, MessageBoxIcon.Warning); SaveLibrary(); } else { Library = (GamesLibrary)DeserializeBytes(libraryData); SaveLibrary(); MessageBox.Show((string)lang["library_restored"], (string)lang["success"], MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { Library = new GamesLibrary(); MessageBox.Show((string)lang["library_being_created"], (string)lang["warning"], MessageBoxButtons.OK, MessageBoxIcon.Warning); SaveLibrary(); } } catch (InvalidCastException) { DialogResult response = MessageBox.Show((string)lang["library_corrupted"], (string)lang["warning"], MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (response == DialogResult.Yes) { uploadForm.Show(); byte[] libraryData = await OnedriveHelper.Instance.ReadItem("GameLinker/Library.bin"); uploadForm.Hide(); if (libraryData == null) { Library = new GamesLibrary(); MessageBox.Show((string)lang["library_being_created"], (string)lang["warning"], MessageBoxButtons.OK, MessageBoxIcon.Warning); SaveLibrary(); } else { Library = (GamesLibrary)DeserializeBytes(libraryData); SaveLibrary(); MessageBox.Show((string)lang["library_restored"], (string)lang["success"], MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { Library = new GamesLibrary(); MessageBox.Show((string)lang["library_being_created"], (string)lang["warning"], MessageBoxButtons.OK, MessageBoxIcon.Warning); SaveLibrary(); } } } }
public async Task <int[]> UploadFolder(string folderPath, string destinationPath, UploadProgressForm uploadForm, string gameName, bool isGameData = true) { Item folder = await CreateFolder(destinationPath); var folderName = Path.GetFileName(folderPath); uploadForm.uploadLabel.Text = (string)lang[isGameData ? "compressing_data" : "compressing_saves"]; uploadForm.uploadValueLabel.Text = ""; uploadForm.uploadProgressBar.Value = 100; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Marquee; Dictionary <string, List <string> > compressedFilesData = await CompressFiles(uploadForm, folderPath, gameName, isGameData); System.GC.Collect(); uploadForm.uploadLabel.Text = (string)lang[isGameData ? "uploading_data" : "uploading_saves"]; uploadForm.uploadValueLabel.Text = "0%"; uploadForm.uploadProgressBar.Value = 0; uploadForm.uploadProgressBar.Style = ProgressBarStyle.Continuous; await PerformBulkUpload(uploadForm, destinationPath, compressedFilesData.ElementAt(0).Value, isGameData); int fileSize = (int)new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "/Temp/" + gameName + (isGameData ? "_Data.tar.gz" : "_Saves.tar.gz")).Length; CleanTempFiles(compressedFilesData.ElementAt(0).Key, compressedFilesData.ElementAt(0).Value); return(new int[2] { fileSize, compressedFilesData.ElementAt(0).Value.Count }); }
public async Task <Item> UploadItem(string itemPath, string destinationPath, UploadProgressForm uploadForm) { if (authenticator == null) { InitAuthenticator(); } Item itemResult = null; try { using (var fileStream = FileToStreamHelper.GetFileStream(itemPath)) { var myMaxChunkSize = 16 * 320 * 1024; // 5MiB var session = await client.Drive.Root.ItemWithPath(destinationPath).CreateSession().Request().PostAsync(); var provider = new ChunkedUploadProvider(session, client, fileStream, (int)myMaxChunkSize); if (uploadForm != null && !uploadForm.Visible) { uploadForm.Show(); } //upload the chunks // Setup the chunk request necessities var chunkRequests = provider.GetUploadChunkRequests(); var readBuffer = new byte[(int)myMaxChunkSize]; var trackedExceptions = new List <Exception>(); UploadChunkResult result; int chunkCount = 0, totalChunks = chunkRequests.Count(); for (var chunk = 0; chunk < chunkRequests.Count(); chunk++) { try { result = await provider.GetChunkRequestResponseAsync(chunkRequests.ElementAt(chunk), readBuffer, trackedExceptions); chunkCount++; if (result.UploadSucceeded) { uploadedCompressedFiles++; uploadForm.Invoke((MethodInvoker) delegate { uploadForm.uploadProgressBar.Value = (int)(((float)uploadedCompressedFiles / compressedFilesCount) * 100); uploadForm.uploadValueLabel.Text = (int)(((float)uploadedCompressedFiles / compressedFilesCount) * 100) + "%"; }); itemResult = result.ItemResponse; } } catch (Exception ex) { } } } } catch (Exception ex) { } // Check that upload succeeded if (itemResult == null) { itemResult = await UploadItem(itemPath, destinationPath, uploadForm); } System.GC.Collect(); return(itemResult); }
public static void UploadEmail(SharePointListViewControl listviewControl, ISPCFolder dragedFolder, DragEventArgs e, List <EUEmailUploadFile> emailUploadFiles, bool isListItemAndAttachmentMode) { try { EUFieldInformations fieldInformations = null; EUFieldCollection fields = null; UploadProgressForm uploadProgressForm = new UploadProgressForm(); if (EUSettingsManager.GetInstance().Settings == null) { MessageBox.Show("You need to configure settings first."); SettingsForm settingsControl = new SettingsForm(); settingsControl.ShowDialog(); return; } if (dragedFolder as EUFolder != null) { EUFolder dragedSPFolder = dragedFolder as EUFolder; List <EUContentType> contentTypes = SharePointManager.GetContentTypes(dragedSPFolder.SiteSetting, dragedSPFolder.WebUrl, dragedSPFolder.ListName); fields = SharePointManager.GetFields(dragedSPFolder.SiteSetting, dragedSPFolder.WebUrl, dragedSPFolder.ListName); ListItemEditForm listItemEditForm = new ListItemEditForm(); listItemEditForm.InitializeForm(dragedSPFolder, null); listItemEditForm.ShowDialog(); if (listItemEditForm.DialogResult != DialogResult.OK) { return; } foreach (EUEmailUploadFile emailUploadFile in emailUploadFiles) { emailUploadFile.FieldInformations = listItemEditForm.FieldInformations; } } string sourceFolder = EUSettingsManager.GetInstance().CreateATempFolder(); if (EUSettingsManager.GetInstance().Settings.UploadAutomatically == true || dragedFolder as FSFolder != null || dragedFolder as GFolder != null) { if (listviewControl != null) { for (int i = 0; i < emailUploadFiles.Count; i++) { EUEmailUploadFile emailUploadFile = emailUploadFiles[i]; listviewControl.LibraryContentDataGridView.Rows.Insert(i, 1); listviewControl.LibraryContentDataGridView.Rows[i].Tag = emailUploadFile.UniqueID.ToString(); if (dragedFolder as EUFolder != null) { listviewControl.LibraryContentDataGridView.Rows[i].Cells["ExtensionImageColumn"].Value = Sobiens.Office.SharePointOutlookConnector.Properties.Resources.ajax_loader; } string title = emailUploadFile.FilePath.Split('\\')[emailUploadFile.FilePath.Split('\\').Length - 1]; listviewControl.LibraryContentDataGridView.Rows[i].Cells["TitleColumn"].Value = title; } } // JOEL JEFFERY 20110712 Hook up the UploadFailed event // JON SILVER JULY 2011 Hook up the UploadSucceeded event if (!addedEventHandler) { OutlookConnector.GetConnector(dragedFolder.SiteSetting).UploadFailed += new EventHandler(EUEmailManager_UploadFailed); OutlookConnector.GetConnector(dragedFolder.SiteSetting).UploadSucceeded += new EventHandler(EUEmailManager_UploadSucceeded); addedEventHandler = true; } OutlookConnector.GetConnector(dragedFolder.SiteSetting).UploadFiles(dragedFolder, emailUploadFiles, fields, fieldInformations, listviewControl); } else { uploadProgressForm.Initialize(dragedFolder, sourceFolder, emailUploadFiles, isListItemAndAttachmentMode, fieldInformations); uploadProgressForm.ShowDialog(); } } catch (System.Exception ex) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogManager.LogAndShowException(methodName, ex); throw ex; } }
static async void Decompress(string gzArchiveName, string destFolder, bool isGameData, UploadProgressForm uploadForm) { Stream inStream = File.OpenRead(gzArchiveName); Stream gzipStream = new GZipInputStream(inStream); TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream); await Task.Run(() => { tarArchive.ExtractContents(destFolder); }); tarArchive.Close(); gzipStream.Close(); inStream.Close(); File.Delete(gzArchiveName); }