1
0
mirror of https://github.com/Ylianst/MeshCentralRouter synced 2025-12-18 17:23:24 +00:00

download Folders recursively

This commit is contained in:
Flynn
2022-02-01 09:02:25 +01:00
parent ec8672cae5
commit edc79bebdd

View File

@@ -1,5 +1,5 @@
/*
Copyright 2009-2022 Intel Corporation
Copyright 2009-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -171,8 +171,10 @@ namespace MeshCentralRouter
}
private delegate void updateRemoteFileViewHandler();
public bool bDontupdateRemoteFileView = false; // Flynn
public void updateRemoteFileView()
{
if(bDontupdateRemoteFileView) return; // Flynn
if(this.InvokeRequired) { this.Invoke(new updateRemoteFileViewHandler(updateRemoteFileView)); return; }
// Save the list of selected items
@@ -181,10 +183,13 @@ namespace MeshCentralRouter
rightListView.Items.Clear();
if ((remoteFolder == null) || (remoteFolder == "")) {
if((remoteFolder == null) || (remoteFolder == ""))
{
remoteLabel.Text = Translate.T(Properties.Resources.Remote);
mainToolTip.SetToolTip(remoteLabel, Translate.T(Properties.Resources.Remote));
} else {
}
else
{
if(node.agentid < 5)
{
remoteLabel.Text = string.Format(Translate.T(Properties.Resources.RemotePlus), remoteFolder.Replace("/", "\\"));
@@ -200,10 +205,13 @@ namespace MeshCentralRouter
remoteRefreshButton.Enabled = true;
remoteRootButton.Enabled = !((remoteFolder == null) || (remoteFolder == ""));
remoteUpButton.Enabled = !((remoteFolder == null) || (remoteFolder == ""));
if (node.agentid < 5) {
if(node.agentid < 5)
{
remoteNewFolderButton.Enabled = !((remoteFolder == null) || (remoteFolder == ""));
remoteDeleteButton.Enabled = remoteZipButton.Enabled = (!((remoteFolder == null) || (remoteFolder == ""))) && (rightListView.SelectedItems.Count > 0);
} else {
}
else
{
remoteNewFolderButton.Enabled = true;
remoteDeleteButton.Enabled = remoteZipButton.Enabled = (rightListView.SelectedItems.Count > 0);
}
@@ -223,13 +231,17 @@ namespace MeshCentralRouter
if(fileItem.ContainsKey("t")) { fileIcon = (int)fileItem["t"]; }
if(fileItem.ContainsKey("n")) { fileName = (string)fileItem["n"]; }
if(fileItem.ContainsKey("d")) { fileDate = (string)fileItem["d"]; }
if (fileItem.ContainsKey("s")) {
if(fileItem.ContainsKey("s"))
{
if(fileItem["s"].GetType() == typeof(System.Int32)) { fileSize = (int)fileItem["s"]; }
if(fileItem["s"].GetType() == typeof(System.Int64)) { fileSize = (long)fileItem["s"]; }
}
if (fileIcon == 1) {
if(fileIcon == 1)
{
sortlist.Add(new ListViewItem(fileName, 0)); // Drive
} else if (fileIcon == 2) {
}
else if(fileIcon == 2)
{
sortlist.Add(new ListViewItem(fileName, 1)); // Folder
}
}
@@ -475,14 +487,14 @@ namespace MeshCentralRouter
else if(sub == "cancel")
{
// Unable to download this file
if (transferStatusForm != null) { transferStatusForm.addErrorMessage(String.Format(Translate.T(Properties.Resources.ErrorDownloadingFileX), downloadFileArray[downloadFileArrayPtr].ToString())); }
if(transferStatusForm != null) { try { transferStatusForm.addErrorMessage(String.Format(Translate.T(Properties.Resources.ErrorDownloadingFileX), downloadFileArray[downloadFileArrayPtr].ToString())); } catch { } }
// Send DOWNLOAD command
string cmd = "{\"action\":\"download\",\"sub\":\"stop\",\"id\":" + (downloadFileArrayPtr + 1000) + "}";
byte[] bincmd = UTF8Encoding.UTF8.GetBytes(cmd);
wc.SendBinary(bincmd, 0, bincmd.Length);
if(downloadFileStream != null) { downloadFileStream.Close(); downloadFileStream = null; } // Close the file
try { File.Delete(Path.Combine(downloadLocalPath.FullName, downloadFileArray[downloadFileArrayPtr].ToString())); } catch (Exception) { }
try { File.Delete(Path.Combine(downloadLocalPath.FullName, Path.GetFileName((String)downloadFileArray[downloadFileArrayPtr]).ToString())); } catch(Exception) { }
// Go to next file
if(downloadFileArray.Count > downloadFileArrayPtr + 1)
@@ -508,7 +520,8 @@ namespace MeshCentralRouter
}
}
}
else if (jsonAction.ContainsKey("type") && (jsonAction["type"].GetType() == typeof(string))) {
else if(jsonAction.ContainsKey("type") && (jsonAction["type"].GetType() == typeof(string)))
{
string action = jsonAction["type"].ToString();
switch(action)
{
@@ -546,7 +559,8 @@ namespace MeshCentralRouter
if(state != 3) return;
if (data[offset] == 123) {
if(data[offset] == 123)
{
// Parse the received JSON
Dictionary<string, object> jsonAction = new Dictionary<string, object>();
jsonAction = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(UTF8Encoding.UTF8.GetString(data, offset, length));
@@ -613,7 +627,8 @@ namespace MeshCentralRouter
string remoteHashHex = null;
if(jsonAction.ContainsKey("hash")) { remoteHashHex = (string)jsonAction["hash"]; }
long remoteFileSize = 0;
if (jsonAction.ContainsKey("tag")) {
if(jsonAction.ContainsKey("tag"))
{
if(jsonAction["tag"].GetType() == typeof(int)) { remoteFileSize = (int)jsonAction["tag"]; }
if(jsonAction["tag"].GetType() == typeof(long)) { remoteFileSize = (long)jsonAction["tag"]; }
}
@@ -717,7 +732,8 @@ namespace MeshCentralRouter
}
else
{
if (downloadActive) {
if(downloadActive)
{
if(downloadStop) { downloadCancel(); return; }
downloadGotBinaryData(data, offset, length);
}
@@ -919,7 +935,8 @@ namespace MeshCentralRouter
ListViewItem item = leftListView.GetItemAt(e.X, e.Y);
if(item != null)
{
if (item.Tag.GetType() == typeof(DriveInfo)) {
if(item.Tag.GetType() == typeof(DriveInfo))
{
DirectoryInfo old = localFolder;
localFolder = ((DriveInfo)item.Tag).RootDirectory;
if(updateLocalFileView() == false) { localFolder = old; updateLocalFileView(); }
@@ -948,10 +965,14 @@ namespace MeshCentralRouter
if(item != null)
{
string r = remoteFolder;
if ((item.ImageIndex == 0) || (item.ImageIndex == 1)) {
if ((r == null) || (r == "")) {
if((item.ImageIndex == 0) || (item.ImageIndex == 1))
{
if((r == null) || (r == ""))
{
r = item.Text;
} else {
}
else
{
if(remoteFolder.EndsWith("/")) { r = remoteFolder + item.Text; } else { r = remoteFolder + "/" + item.Text; }
}
requestRemoteFolder(r);
@@ -1039,13 +1060,21 @@ namespace MeshCentralRouter
}
}
bool bRightCWChangedBusy = false;
private void rightListView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
if(bRightCWChangedBusy) return;
bRightCWChangedBusy=true;
try
{
if(rightListView.Columns[0].Width != (rightListView.Width - rightListView.Columns[1].Width - 22))
{
rightListView.Columns[0].Width = rightListView.Width - rightListView.Columns[1].Width - 22;
}
}
catch { }
bRightCWChangedBusy = false;
}
private void leftListView_Resize(object sender, EventArgs e)
{
@@ -1055,13 +1084,21 @@ namespace MeshCentralRouter
}
}
bool bLeftCWChangedBusy = false;
private void leftListView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
if(bLeftCWChangedBusy) return;
bLeftCWChangedBusy=true;
try
{
if(leftListView.Columns[0].Width != (leftListView.Width - leftListView.Columns[1].Width - 22))
{
leftListView.Columns[0].Width = leftListView.Width - leftListView.Columns[1].Width - 22;
}
}
catch { }
bLeftCWChangedBusy = false;
}
private void updateTimer_Tick(object sender, EventArgs e)
{
@@ -1141,7 +1178,7 @@ namespace MeshCentralRouter
bool downloadAllowed = true;
if(localFolder == null) { downloadAllowed = false; }
if(rightListView.SelectedItems.Count == 0) { downloadAllowed = false; }
foreach (ListViewItem l in rightListView.SelectedItems) { if (l.ImageIndex != 2) { downloadAllowed = false; } }
foreach(ListViewItem l in rightListView.SelectedItems) { if(l.ImageIndex != 1 && l.ImageIndex != 2) { downloadAllowed = false; } } // Flynn
if((node.agentid < 5) && ((remoteFolder == null) || (remoteFolder == ""))) { downloadAllowed = false; }
downloadButton.Enabled = downloadAllowed;
}
@@ -1160,8 +1197,10 @@ namespace MeshCentralRouter
// If any files are going to be overwritten
int overWriteCount = 0;
foreach (ListViewItem l in leftListView.SelectedItems) {
if (l.ImageIndex == 2) {
foreach(ListViewItem l in leftListView.SelectedItems)
{
if(l.ImageIndex == 2)
{
string filename = l.Text;
foreach(ListViewItem l2 in rightListView.Items)
@@ -1203,7 +1242,8 @@ namespace MeshCentralRouter
{
string filename2 = l2.Text;
if(node.agentid < 5) { filename = filename.ToLower(); filename2 = filename2.ToLower(); }
if (filename.Equals(filename2)) {
if(filename.Equals(filename2))
{
overwrite = true;
if(skipExistingFiles == false)
{
@@ -1251,7 +1291,8 @@ namespace MeshCentralRouter
localFilePath = (string)uploadFileArray[uploadFileArrayPtr];
localFileName = Path.GetFileName(localFilePath);
}
try { uploadFileStream = File.OpenRead(localFilePath); } catch (Exception)
try { uploadFileStream = File.OpenRead(localFilePath); }
catch(Exception)
{
// Display the error
if(transferStatusForm != null) { transferStatusForm.addErrorMessage(String.Format(Translate.T(Properties.Resources.UnableToOpenFileX), localFileName)); }
@@ -1339,12 +1380,15 @@ namespace MeshCentralRouter
if(dataPriming && (len == 0)) return;
uploadFilePtr += len;
if (len == 0) {
if(len == 0)
{
// Send UPLOADDONE command
string cmd = "{\"action\":\"uploaddone\",\"reqid\":" + (uploadFileArrayPtr + 1000) + "}";
byte[] bincmd = UTF8Encoding.UTF8.GetBytes(cmd);
wc.SendBinary(bincmd, 0, bincmd.Length);
} else {
}
else
{
// Send part of the file
// If starts with 0 or {, add a zero char at the start of the send, this will indicate that it's not a JSON command.
if((buffer[1] == 123) || (buffer[1] == 0)) { wc.SendBinary(buffer, 0, len + 1); } else { wc.SendBinary(buffer, 1, len); }
@@ -1362,14 +1406,21 @@ namespace MeshCentralRouter
// Everything was succesful, close the form
transferStatusForm.Close();
transferStatusForm = null;
} else {
}
else
{
// Error are displayed, keep the form open
transferStatusForm.transferCompleted();
}
remoteFolder = strDownloadRel; // Zurücksetzen
remoteRefresh();
}
private void downloadButton_Click(object sender, EventArgs e)
{
downloadButton_ClickFlynn(sender, e);
return;
// If a transfer is currently active, ignore this.
if(uploadActive || downloadActive || (transferStatusForm != null)) return;
@@ -1449,13 +1500,185 @@ namespace MeshCentralRouter
downloadNextFile();
}
private void downloadButton_ClickFlynn(object sender, EventArgs e)
{
// If a transfer is currently active, ignore this.
if(uploadActive || downloadActive || (transferStatusForm != null)) return;
strDownloadRel = remoteFolder;
// Perform the download
downloadFileArrayPtr = 0;
downloadFileArray = new ArrayList();
downloadFileSizeArray = new ArrayList();
bDontupdateRemoteFileView = true;
foreach(ListViewItem l in rightListView.SelectedItems)
{
if(l.ImageIndex == 1) // Folder
{
RekursivelyCollectDownloadFiles(strDownloadRel+"/"+l.Text);
}
else if(l.ImageIndex == 2) // File
{
if(skipExistingFiles == false)
{
downloadFileArray.Add(strDownloadRel+"/"+l.Text);
downloadFileSizeArray.Add(int.Parse(l.SubItems[1].Text));
}
else
{
bool overwrite = false;
string filename = l.Text;
foreach(ListViewItem l2 in leftListView.Items)
{
if(l2.ImageIndex == 2)
{
string filename2 = l2.Text;
if(node.agentid < 5) { filename = filename.ToLower(); filename2 = filename2.ToLower(); }
if(filename.Equals(filename2)) { overwrite = true; }
}
}
if(overwrite == false)
{
downloadFileArray.Add(strDownloadRel+"/"+l.Text);
downloadFileSizeArray.Add(int.Parse(l.SubItems[1].Text));
}
}
}
}
bDontupdateRemoteFileView = false;
#region SKIP existing?
downloadLocalPath = localFolder;
// If any files are going to be overwritten
int overWriteCount = 0;
for(int i = 0; i < downloadFileArray.Count; i++)
{
string localFilePath;
String strDownloadFileString = (string)downloadFileArray[downloadFileArrayPtr];
if(strDownloadRel.Length > 0)
{
strDownloadFileString = strDownloadFileString.Substring(strDownloadRel.Length);
if(strDownloadFileString.StartsWith("/")) strDownloadFileString = strDownloadFileString.Substring(1);
}
localFilePath = Path.Combine(downloadLocalPath.FullName, strDownloadFileString.Replace("/", "\\"));
if(File.Exists(localFilePath) && new System.IO.FileInfo(localFilePath).Length.Equals(downloadFileSizeArray[i])) { overWriteCount++; }
}
skipExistingFiles = true;
if(overWriteCount > 0)
{
FileConfirmOverwriteForm f = new FileConfirmOverwriteForm();
if(overWriteCount == 1) { f.mainTextLabel = String.Format(Translate.T(Properties.Resources.OverwriteOneFile), overWriteCount); } else { f.mainTextLabel = String.Format(Translate.T(Properties.Resources.OverwriteXfiles), overWriteCount); }
if(f.ShowDialog(this) != DialogResult.OK) return;
skipExistingFiles = f.skipExistingFiles;
}
if(skipExistingFiles)
{
for(int i = 0; i < downloadFileArray.Count; i++)
{
string localFilePath;
String strDownloadFileString = (string)downloadFileArray[downloadFileArrayPtr];
if(strDownloadRel.Length > 0)
{
strDownloadFileString = strDownloadFileString.Substring(strDownloadRel.Length);
if(strDownloadFileString.StartsWith("/")) strDownloadFileString = strDownloadFileString.Substring(1);
}
localFilePath = Path.Combine(downloadLocalPath.FullName, strDownloadFileString.Replace("/", "\\"));
if(File.Exists(localFilePath) && new System.IO.FileInfo(localFilePath).Length.Equals(downloadFileSizeArray[i]))
{
downloadFileArray.RemoveAt(i);
downloadFileSizeArray.RemoveAt(i);
i--;
}
}
}
#endregion
if(downloadFileArray.Count == 0) return;
downloadLocalPath = localFolder;
downloadRemotePath = remoteFolder;
downloadActive = true;
downloadStop = false;
// Show transfer status dialog
transferStatusForm = new FileTransferStatusForm(this);
transferStatusForm.Show(this);
downloadNextFile();
}
private void RekursivelyCollectDownloadFiles(String strFolder)
{
requestRemoteFolder(strFolder);
DateTime to = DateTime.Now.AddSeconds(5);
while(DateTime.Now < to && remoteFolder != strFolder)
Application.DoEvents();
List<Fle> FileList = new List<Fle>();
for(int i = 0; i < remoteFolderList.Count; i++)
{
Dictionary<string, object> fileItem = (Dictionary<string, object>)remoteFolderList[i];
Fle file = new Fle();
file.Size = -1;
file.Path = strFolder.Replace("//", "/");
if(fileItem.ContainsKey("t")) { file.Icon = (int)fileItem["t"]; }
if(fileItem.ContainsKey("n")) { file.Name = (string)fileItem["n"]; }
if(fileItem.ContainsKey("d")) { file.Date = (string)fileItem["d"]; }
if(fileItem.ContainsKey("s"))
{
if(fileItem["s"].GetType() == typeof(System.Int32)) { file.Size = (int)fileItem["s"]; }
if(fileItem["s"].GetType() == typeof(System.Int64)) { file.Size = (long)fileItem["s"]; }
}
FileList.Add(file); // Folder
}
for(int i = 0; i < FileList.Count; i++)
{
Fle file = FileList[i];
if(file.Icon == 2) // Folder
{
RekursivelyCollectDownloadFiles(file.Path+"/"+file.Name);
}
else if(file.Icon == 3) // File
{
if(true) // Abfrage Skip
{
downloadFileArray.Add(file.Path+"/"+file.Name);
downloadFileSizeArray.Add(file.Size);
}
}
}
}
String strDownloadRel = "";
private void downloadNextFile()
{
if((downloadFileArray == null) || (downloadFileArray.Count == 0)) return;
string localFilePath;
localFilePath = Path.Combine(downloadLocalPath.FullName, (string)downloadFileArray[downloadFileArrayPtr]);
try { downloadFileStream = File.OpenWrite(localFilePath); } catch (Exception) {
String strDownloadFileString = (string)downloadFileArray[downloadFileArrayPtr];
if(strDownloadRel.Length > 0)
{
strDownloadFileString = strDownloadFileString.Substring(strDownloadRel.Length);
if(strDownloadFileString.StartsWith("/")) strDownloadFileString = strDownloadFileString.Substring(1);
}
localFilePath = Path.Combine(downloadLocalPath.FullName, strDownloadFileString.Replace("/", "\\"));
if(!Directory.Exists(Path.GetDirectoryName(localFilePath))) Directory.CreateDirectory(Path.GetDirectoryName(localFilePath));
try { downloadFileStream = File.OpenWrite(localFilePath); }
catch(Exception)
{
// Download error, show it in the dialog
FileInfo f = new FileInfo(localFilePath);
if(transferStatusForm != null) { transferStatusForm.addErrorMessage(String.Format(Translate.T(Properties.Resources.UnableToWriteFileX), f.Name)); }
@@ -1481,13 +1704,16 @@ namespace MeshCentralRouter
}
return;
}
downloadFileSize = (int)downloadFileSizeArray[downloadFileArrayPtr];
if(downloadFileSizeArray[downloadFileArrayPtr].GetType() == typeof(System.Int32)) { downloadFileSize = (int)downloadFileSizeArray[downloadFileArrayPtr]; }
if(downloadFileSizeArray[downloadFileArrayPtr].GetType() == typeof(System.Int64)) { downloadFileSize = (long)downloadFileSizeArray[downloadFileArrayPtr]; }
downloadFilePtr = 0;
downloadFileStartTime = DateTime.Now;
string r;
if (downloadRemotePath.EndsWith("/")) { r = downloadRemotePath + downloadFileArray[downloadFileArrayPtr]; } else { r = downloadRemotePath + "/" + downloadFileArray[downloadFileArrayPtr]; }
if(downloadRemotePath.EndsWith("/")) { r = downloadRemotePath + Path.GetFileName((String)downloadFileArray[downloadFileArrayPtr]); } else { r = downloadRemotePath + "/" + Path.GetFileName((String)downloadFileArray[downloadFileArrayPtr]); }
r=(String)downloadFileArray[downloadFileArrayPtr];
// Send DOWNLOAD command
string cmd = "{\"action\":\"download\",\"sub\":\"start\",\"id\":" + (downloadFileArrayPtr + 1000) + ",\"path\":\"" + r + "\"}";
byte[] bincmd = UTF8Encoding.UTF8.GetBytes(cmd);
@@ -1585,11 +1811,13 @@ namespace MeshCentralRouter
{
foreach(string file in filesArray)
{
try {
try
{
string fullpath = Path.Combine(localFolder.FullName, file);
FileAttributes attr = File.GetAttributes(fullpath);
if((attr & FileAttributes.Directory) == FileAttributes.Directory) { Directory.Delete(fullpath, f.recursive); } else { File.Delete(fullpath); }
} catch (Exception) { }
}
catch(Exception) { }
}
updateLocalFileView();
}
@@ -1650,7 +1878,8 @@ namespace MeshCentralRouter
{
string filename2 = l2.Text;
if(node.agentid < 5) { filename = filename.ToLower(); filename2 = filename2.ToLower(); }
if (filename.Equals(filename2)) {
if(filename.Equals(filename2))
{
overwrite = true;
if(skipExistingFiles == false)
{
@@ -1662,7 +1891,8 @@ namespace MeshCentralRouter
}
}
}
if ((skipExistingFiles == false) || (overwrite == false)) {
if((skipExistingFiles == false) || (overwrite == false))
{
uploadFileArray.Add(file);
if(overwriteNotLarger) { uploadFileDuplicateArray.Add(file, remoteLength); }
}
@@ -1852,4 +2082,13 @@ namespace MeshCentralRouter
}
}
}
public class Fle
{
public String Path = "";
public String Name = "";
public String Date = "";
public long Size = 0;
public int Icon = 0;
}
}