private void InitializeDownload() { string fileName = GetStringParameter("f", ""); if (System.IO.File.Exists(PTMagicBasePath + fileName)) { if (!System.IO.Directory.Exists(PTMagicMonitorBasePath + "wwwroot" + System.IO.Path.DirectorySeparatorChar + "assets" + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar)) { System.IO.Directory.CreateDirectory(PTMagicMonitorBasePath + "wwwroot" + System.IO.Path.DirectorySeparatorChar + "assets" + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar); } string sourcefilePath = PTMagicBasePath + fileName; string destinationFilePath = PTMagicMonitorBasePath + "wwwroot" + System.IO.Path.DirectorySeparatorChar + "assets" + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar + fileName + ".zip"; ZIPHelper.CreateZipFile(new ArrayList() { sourcefilePath }, destinationFilePath); Response.Redirect(PTMagicConfiguration.GeneralSettings.Monitor.RootUrl + "assets/tmp/" + fileName + ".zip"); } }
private async Task UploadFilesAsync(IEnumerable <string> files, bool fromCommandline = false) { try { resetProgressBar(); progress_bar.IsIndeterminate = true; var ftp = new FTPHelper(App.Settings.Host, App.Settings.User, App.Settings.Password, App.Settings.ServerPath, App.Settings.Port, App.Settings.PassiveMode); // create file and directory list var fileList = files.OrderBy(x => x).ToList(); var dirList = fileList.Where(x => Utils.IsDirectory(x)).ToList(); // remove directories from filelist fileList.RemoveAll(x => dirList.Contains(x)); var uploadList = new List <UploadFile>(fileList.Select(x => new UploadFile(x))); if (dirList.Count > 0) { var zippedFiles = await ZIPHelper.ZipDirectoriesAsync(dirList, new Progress <ZIPHelper.ZIPProgress>((x => { progress_bar.IsIndeterminate = false; progress_bar.Maximum = x.BytesTotal; progress_bar.Value = x.BytesDone; progress_label.Content = "ZIP: " + x.ToString(); }))); uploadList.AddRange(zippedFiles); } progress_bar.IsIndeterminate = true; uploadList = uploadList.OrderBy(x => x.FileInfo.Name).ToList(); if (App.Settings.CheckOverwrite) { var existingFiles = (await ftp.GetExistingFiles(uploadList)).ToList(); if (existingFiles.Any()) { var result = MessageBox.Show(this, "The following files will be overwritten:\r\n" + string.Join("\r\n", existingFiles.Select(x => x.FileInfo.Name)), "Confirm overwrite", MessageBoxButton.YesNo, MessageBoxImage.Information); if (result != MessageBoxResult.Yes) { // remove the files from our list if we don't want to overwrite them uploadList.RemoveAll(x => existingFiles.Contains(x)); } } } var uploadedFiles = await ftp.UploadFilesAsync(uploadList, new Progress <Helper.FTPHelper.FTPProgress>((x => { progress_bar.IsIndeterminate = false; progress_bar.Maximum = x.BytesTotal; progress_bar.Value = x.BytesDone; progress_label.Content = "Upload: " + x.ToString(); }))); if (App.Settings.CopyClipboard) { var urls = uploadedFiles.Select(x => Utils.FormatHTTPUrl(App.Settings.UrlPath, App.Settings.Host, App.Settings.ServerPath, x.UploadName)); if (App.Settings.Minify) { progress_label.Content = "Minifying links..."; urls = await URLHelper.ShortenUrlsAsync(urls); } // always add newline at the end Clipboard.SetText(string.Join("\r\n", urls) + "\r\n"); } // clean up temp files foreach (var uf in uploadedFiles.Where(x => x.IsTemp)) { System.IO.File.Delete(uf.FilePath); } resetProgressBar(); } catch (Exception ex) { MessageBox.Show(this, "I screwed up.\r\n" + ex.Message + "\r\n" + ex.StackTrace, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } if (fromCommandline && !App.Settings.KeepWindowOpen) { Application.Current.Shutdown(); } }