private bool TryDownloadFileShard( BackblazeB2AuthorizationSession authorizationSession, string fileShardID, string filePathDestination, FileResult remoteFile ) { this.Verbose($"Starting download of: {fileShardID}"); using (DownloadFileAction fileShardDownload = new DownloadFileAction(authorizationSession, filePathDestination, remoteFile.FileID)) { BackblazeB2ActionResult <BackblazeB2DownloadFileResult> downloadResult = fileShardDownload.Execute(); if (downloadResult.HasErrors) { this.Critical($"Exception occurred during downloading a file shard: {downloadResult}."); return(false); } } return(true); }
private static IAction GetAction(ActionModel action) { IAction actionInstance = null; switch (action.Action) { case ActionType.WriteFile: actionInstance = new WriteFileAction(); break; case ActionType.IF: actionInstance = new IFAction(); break; case ActionType.HttpRequest: actionInstance = new HttpRequestAction(); break; case ActionType.Shutdown: actionInstance = new ShutdownAction(); break; case ActionType.StartProcess: actionInstance = new StartProcessAction(); break; case ActionType.OpenURL: actionInstance = new OpenURLAction(); break; case ActionType.Snipping: actionInstance = new SnippingAction(); break; case ActionType.DeleteFile: actionInstance = new DeleteFileAction(); break; case ActionType.SoundPlay: actionInstance = new SoundPlayAction(); break; case ActionType.GetIPAddress: actionInstance = new GetIPAddressAction(); break; case ActionType.Keyboard: actionInstance = new KeyboardAction(); break; case ActionType.SystemNotification: actionInstance = new SystemNotificationAction(); break; case ActionType.DownloadFile: actionInstance = new DownloadFileAction(); break; case ActionType.Dialog: actionInstance = new DialogAction(); break; case ActionType.Delay: actionInstance = new DelayAction(); break; case ActionType.Loops: actionInstance = new LoopsAction(); break; case ActionType.KillProcess: actionInstance = new KillProcessAction(); break; case ActionType.SetDeviceVolume: actionInstance = new SetDeviceVolumeAction(); break; case ActionType.Regex: actionInstance = new RegexAction(); break; case ActionType.ReadFile: actionInstance = new ReadFileAction(); break; case ActionType.JsonDeserialize: actionInstance = new JsonDeserializeAction(); break; } if (actionInstance != null) { return(actionInstance); } return(null); }
private async void btnDownload_Click() { var file = SelectedItem as FileInfo; if (file == null) { return; } if (DownloadFileAction != null) { DownloadFileAction.Invoke(JSRuntime, file.FullName); return; } System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource(); loading.Show(TextDownload, file.Name, false, cts.Cancel); var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); DateTime lastDisplayTime = DateTime.MinValue; byte[] buffer = new byte[24 * 1024]; var fileSize = file.Length; long readTotalCount = 0; try { using (var fs = file.OpenRead()) { while (!cts.IsCancellationRequested) { var ret = fs.Read(buffer, 0, buffer.Length); if (ret <= 0) { break; } readTotalCount += ret; if ((DateTime.Now - lastDisplayTime).TotalSeconds > 0.5 && stopwatch.ElapsedMilliseconds > 0) { StringBuilder sb = new StringBuilder(); var speed = Convert.ToDouble(readTotalCount / stopwatch.ElapsedMilliseconds); sb.Append(TextTransferSpeed + ": " + storageUSC.GetString(Convert.ToDecimal(speed * 1000), 1, true) + "B/s"); var remainingTime = TimeSpan.FromMilliseconds((fileSize - readTotalCount) / speed); sb.Append("," + TextRemainingTime + ": " + remainingTime.ToString(@"hh\:mm\:ss")); loading.UpdateProgress(Convert.ToInt32(readTotalCount * 100 / fileSize), sb.ToString()); await InvokeAsync(StateHasChanged); lastDisplayTime = DateTime.Now; } await BlazorDownloadFileService.AddBuffer(new ArraySegment <byte>(buffer, 0, ret), cts.Token); } } if (cts.IsCancellationRequested) { alert.Show(TextUpload, TextCanceled); return; } var result = await BlazorDownloadFileService.DownloadBinaryBuffers(file.Name, cts.Token); if (!result.Succeeded) { alert.Show(TextDownload, TextFailed + Environment.NewLine + result.ErrorName + Environment.NewLine + result.ErrorMessage); } } catch (TaskCanceledException) { alert.Show(TextUpload, TextCanceled); } catch (Exception ex) { alert.Show(TextUpload, TextFailed + Environment.NewLine + ExceptionUtils.GetExceptionMessage(ex)); } finally { await BlazorDownloadFileService.ClearBuffers(); stopwatch.Stop(); loading.Close(); } }
/// <summary> /// Initializes this file database manifest /// </summary> private static FileDatabaseManifest GetOrCreateFileDatabaseManifest( BackblazeB2AuthorizationSession authorizationSession, Config config ) { if (SharedFileDatabaseManifest == null) { lock (SharedFileDatabaseManifestLock) { if (SharedFileDatabaseManifest == null) { // Get just the file names on the server ListFilesAction listFilesAction = ListFilesAction.CreateListFileActionForFileNames( authorizationSession, config.BucketID, true ); BackblazeB2ActionResult <BackblazeB2ListFilesResult> listFilesActionResult = listFilesAction.Execute(); if (listFilesActionResult.HasErrors) { throw new FailedToGetListOfFilesOnB2Exception { BackblazeErrorDetails = listFilesActionResult.Errors, }; } FileResult fileDatabaseManifestFileResult = listFilesActionResult.Result.Files .Where(f => f.FileName.Equals(RemoteFileDatabaseManifestName, StringComparison.Ordinal)) .SingleOrDefault(); if (fileDatabaseManifestFileResult == null) { SharedFileDatabaseManifest = new FileDatabaseManifest { DataFormatVersionNumber = CurrentDatabaseDataFormatVersion, Files = new Database.File[0] }; } else { using (MemoryStream outputStream = new MemoryStream()) using (DownloadFileAction manifestFileDownloadAction = new DownloadFileAction( authorizationSession, outputStream, fileDatabaseManifestFileResult.FileID )) { BackblazeB2ActionResult <BackblazeB2DownloadFileResult> manifestResultOption = manifestFileDownloadAction.Execute(); if (manifestResultOption.HasResult) { // Now, read string from manifest outputStream.Flush(); SharedFileDatabaseManifest = DeserializeManifest( outputStream.ToArray(), config ); } else { SharedFileDatabaseManifest = new FileDatabaseManifest { DataFormatVersionNumber = CurrentDatabaseDataFormatVersion, Files = new Database.File[0] }; } } } } } } return(SharedFileDatabaseManifest); }