private async Task OpenFolderAsync(TreeViewItem itemToOpen) { itemToOpen.Items.Clear(); string[] folders = await FtpUtils.FtpListFilesFoldersAsync(itemToOpen.Tag as string, Credential); foreach (string s in folders) { if (string.IsNullOrWhiteSpace(s)) { continue; } if (s.Equals(".") || s.Equals("..")) { continue; } if (!Path.HasExtension(s)) { TreeViewItem item = new TreeViewItem() { Header = s, Tag = (itemToOpen.Tag as string) + s + "/" }; item.MouseDoubleClick += Item_MouseDoubleClick; itemToOpen.Items.Add(item); } } }
public override async Task RunTask() { NetworkCredential networkCredential = new NetworkCredential(AutomationSettings.BigmodsUsername, AutomationSettings.BigmodsPassword); string serverPath = string.Format("{0}{1}", PrivateStuff.BigmodsFTPUsersRoot, WoTOnlineFolderVersion); Logging.Info("Checking if {0} already exists on the server in folder {1}", ZipFileName, WoTOnlineFolderVersion); string[] listOfFilesOnServer = await FtpUtils.FtpListFilesFoldersAsync(serverPath, networkCredential); int duplicateIncriment = 1; string nameWithNoExtension = Path.GetFileNameWithoutExtension(ZipFileName); string extension = Path.GetExtension(ZipFileName); string newFilename = ZipFileName; while (listOfFilesOnServer.Contains(newFilename)) { Logging.Info("Filename already exists on server, giving it a unique name"); newFilename = string.Format("{0}_{1}{2}", nameWithNoExtension, duplicateIncriment++.ToString(), extension); Logging.Info("Propose {0}", newFilename); } ZipFileName = newFilename; using (WebClient = new WebClient { Credentials = networkCredential }) { string uploadUrl = string.Format("{0}/{1}", serverPath, ZipFileName); Logging.Info(Logfiles.AutomationRunner, "Uploading package"); Logging.Debug(Logfiles.AutomationRunner, "Upload zip url = {0}, file = {1}", uploadUrl, FilePath); //https://stackoverflow.com/questions/2953403/c-sharp-passing-method-as-the-argument-in-a-method if (DatabaseAutomationRunner != null) { WebClient.UploadProgressChanged += DatabaseAutomationRunner.UploadProgressChanged; WebClient.UploadFileCompleted += DatabaseAutomationRunner.UploadFileCompleted; } try { await WebClient.UploadFileTaskAsync(uploadUrl, FilePath); DatabasePackage.UpdatePackageName(ZipFileName); FtpUtils.TriggerMirrorSyncAsync(); TransferSuccess = true; } catch (OperationCanceledException) { } catch (Exception ex) { Logging.Exception(ex.ToString()); } finally { if (DatabaseAutomationRunner != null) { WebClient.UploadProgressChanged -= DatabaseAutomationRunner.UploadProgressChanged; WebClient.UploadFileCompleted -= DatabaseAutomationRunner.UploadFileCompleted; } } } }
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(); }