public override async Task RunTask() { using (WebClient = new WebClient { Credentials = new NetworkCredential(AutomationSettings.BigmodsUsername, AutomationSettings.BigmodsPassword) }) { string serverPath = string.Format("{0}{1}/{2}", PrivateStuff.BigmodsFTPUsersRoot, WoTOnlineFolderVersion, DatabasePackage.ZipFile); Logging.Info(Logfiles.AutomationRunner, "Downloading package"); Logging.Debug(Logfiles.AutomationRunner, "Download zip url = {0}, file = {1}", serverPath, FilePath); //https://stackoverflow.com/questions/2953403/c-sharp-passing-method-as-the-argument-in-a-method if (DatabaseAutomationRunner != null) { WebClient.DownloadProgressChanged += DatabaseAutomationRunner.DownloadProgressChanged; WebClient.DownloadFileCompleted += DatabaseAutomationRunner.DownloadFileCompleted; } try { if (DatabaseAutomationRunner != null) { if (DatabaseAutomationRunner.DownloadProgressChanged != null) { long filesize = await FtpUtils.FtpGetFilesizeAsync(serverPath, WebClient.Credentials); DatabaseAutomationRunner.ProgressChanged.Invoke(null, new ProgressChangedEventArgs(69, filesize)); } } await WebClient.DownloadFileTaskAsync(serverPath, FilePath); TransferSuccess = true; } catch (OperationCanceledException) { } catch (Exception ex) { Logging.Exception(ex.ToString()); } finally { if (DatabaseAutomationRunner != null) { WebClient.DownloadProgressChanged -= DatabaseAutomationRunner.DownloadProgressChanged; WebClient.DownloadFileCompleted -= DatabaseAutomationRunner.DownloadFileCompleted; } } } }
private async void RelhaxWindow_Loaded(object sender, RoutedEventArgs e) { if (EditorSettings == null) { throw new NullReferenceException(); } //init timer timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Background, Timer_Elapsed, this.Dispatcher) { IsEnabled = false }; //setup UI based on transfer type OpenFodlerButton.Visibility = (TransferMode == EditorTransferMode.DownloadZip) ? Visibility.Visible : Visibility.Hidden; OpenFileButton.Visibility = (TransferMode == EditorTransferMode.DownloadZip) ? Visibility.Visible : Visibility.Hidden; //set log based on file upload type switch (TransferMode) { case EditorTransferMode.UploadZip: ProgressBody.Text = string.Format("Uploading {0} to FTP folder {1}", Path.GetFileName(ZipFilePathDisk), WoTModpackOnlineFolderVersion); break; case EditorTransferMode.UploadMedia: ProgressBody.Text = string.Format("Uploading {0} to FTP folder Medias/...", ZipFileName); break; case EditorTransferMode.DownloadZip: ProgressBody.Text = string.Format("Downloading {0} from FTP folder {1}", Path.GetFileName(ZipFilePathDisk), WoTModpackOnlineFolderVersion); break; } //set initial text(s) ProgressHeader.Text = string.Format("{0} 0 kb of 0 kb", TransferMode.ToString()); CompleteFTPPath = string.Format("{0}{1}", ZipFilePathOnline, ZipFileName); using (client = new WebClient() { Credentials = Credential }) { switch (TransferMode) { case EditorTransferMode.UploadZip: case EditorTransferMode.UploadMedia: //before uploading, make sure it doesn't exist first (zipfile or media) ProgressHeader.Text = "Checking if file exists on server..."; Logging.Editor("Checking if {0} already exists on the server in folder {1}", LogLevel.Info, ZipFileName, WoTModpackOnlineFolderVersion); string[] listOfFilesOnServer = await FtpUtils.FtpListFilesFoldersAsync(ZipFilePathOnline, Credential); if (listOfFilesOnServer.Contains(ZipFileName) && MessageBox.Show("File already exists, overwrite?", "", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { Logging.Editor("DOES exist and user said don't overwrite, aborting"); ProgressHeader.Text = "Canceled"; return; } //attach upload event handlers client.UploadProgressChanged += Client_UploadProgressChanged; client.UploadFileCompleted += Client_DownloadUploadFileCompleted; //run the FTP upload Logging.Editor("Starting FTP upload of {0} from folder {1}", LogLevel.Info, ZipFileName, WoTModpackOnlineFolderVersion); try { await client.UploadFileTaskAsync(CompleteFTPPath, ZipFilePathDisk); Logging.Editor("FTP upload complete of {0}", LogLevel.Info, ZipFileName); //run upload event handler OnEditorUploadDownloadClosed?.Invoke(this, new EditorTransferEventArgs() { Package = PackageToUpdate, UploadedFilename = ZipFileName, UploadedFilepathOnline = ZipFilePathOnline, TransferMode = this.TransferMode }); DeleteFileButton.IsEnabled = true; if (TransferMode == EditorTransferMode.UploadZip && EditorSettings.DeleteUploadLocallyUponCompletion) { DeleteFileButton_Click(null, null); } } catch (Exception ex) { Logging.Editor("FTP UPLOAD Failed"); Logging.Editor(ex.ToString()); } finally { CancelButton.IsEnabled = false; } break; case EditorTransferMode.DownloadZip: //attach download event handlers client.DownloadProgressChanged += Client_DownloadProgressChanged; client.DownloadFileCompleted += Client_DownloadUploadFileCompleted; //run the FTP download Logging.Editor("Starting FTP download of {0} from folder {1}", LogLevel.Info, ZipFileName, WoTModpackOnlineFolderVersion); try { FTPDownloadFilesize = await FtpUtils.FtpGetFilesizeAsync(CompleteFTPPath, Credential); await client.DownloadFileTaskAsync(CompleteFTPPath, ZipFilePathDisk); Logging.Editor("FTP download complete of {0}", LogLevel.Info, ZipFileName); DeleteFileButton.IsEnabled = true; } catch (Exception ex) { Logging.Editor("FTP download failed of {0}", LogLevel.Info, ZipFileName); Logging.Editor(ex.ToString()); } finally { CancelButton.IsEnabled = false; OpenFileButton.IsEnabled = true; OpenFodlerButton.IsEnabled = true; } break; } } StartTimerForClose(); }