History

TVA.ServiceProcess - Added new Trasfer command to ServiceHelper for uploading and downloading files from remote console. - Added new Files command to ServiceHelper for copying and moving files on the server.

Pinal Patel committed 10 years ago

Commit c8be7121 parent 4e21d082 files · +244 −15Browse files

Showing the changes to Source/Libraries/GSF.ServiceProcess/ServiceHelper.cs (1 of 2 files). Show all

Unified · Split

Source/Libraries/GSF.ServiceProcess/ServiceHelper.cs+185−12
@@ -89,12 +89,25 @@ · 88 unmodified lines
8989 //
9090 //******************************************************************************************************
9191
92+using GSF.Annotations;
93+using GSF.Collections;
94+using GSF.Communication;
95+using GSF.Configuration;
96+using GSF.Diagnostics;
97+using GSF.ErrorManagement;
98+using GSF.IO;
99+using GSF.Reflection;
100+using GSF.Scheduling;
101+using GSF.Security;
102+using GSF.Security.Cryptography;
103+using GSF.Units;
92104 using System;
93105 using System.Collections.Generic;
94106 using System.ComponentModel;
95107 using System.Configuration;
96108 using System.Diagnostics;
97109 using System.Drawing;
110+using System.IO;
98111 using System.Linq;
99112 using System.Reflection;
100113 using System.Security;
@@ -102,18 +115,6 @@ using System.Security.Principal; · 1 unmodified line
102115 using System.ServiceProcess;
103116 using System.Text;
104117 using System.Threading;
105−using GSF.Annotations;
106−using GSF.Collections;
107−using GSF.Communication;
108−using GSF.Configuration;
109−using GSF.Diagnostics;
110−using GSF.ErrorManagement;
111−using GSF.IO;
112−using GSF.Reflection;
113−using GSF.Scheduling;
114−using GSF.Security;
115−using GSF.Security.Cryptography;
116−using GSF.Units;
117118
118119 namespace GSF.ServiceProcess
119120 {
@@ -1223,6 +1224,8 @@ namespace GSF.ServiceProcess · 1103 unmodified lines
12231224 m_clientRequestHandlers.Add(new ClientRequestHandler("Version", "Displays current service version", ShowVersion, new[] { "ver" }));
12241225 m_clientRequestHandlers.Add(new ClientRequestHandler("Time", "Displays current system time", ShowTime));
12251226 m_clientRequestHandlers.Add(new ClientRequestHandler("User", "Displays current user information", ShowUser, new[] { "whoami" }));
1227+ m_clientRequestHandlers.Add(new ClientRequestHandler("Files", "Manages files on the server", ManageFiles));
1228+ m_clientRequestHandlers.Add(new ClientRequestHandler("Transfer", "Transfers files to and from the server", TransferFile));
12261229
12271230 // Enable telnet support if requested.
12281231 if (m_supportTelnetSessions)
@@ -3915,6 +3918,176 @@ namespace GSF.ServiceProcess · 2686 unmodified lines
39153918 }
39163919 }
39173920
3921+ private void ManageFiles(ClientRequestInfo requestInfo)
3922+ {
3923+ if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 2)
3924+ {
3925+ StringBuilder helpMessage = new StringBuilder();
3926+
3927+ helpMessage.Append("Manages files on the server.");
3928+ helpMessage.AppendLine();
3929+ helpMessage.AppendLine();
3930+ helpMessage.Append(" Usage:");
3931+ helpMessage.AppendLine();
3932+ helpMessage.Append(" Files \"Source Path\" \"Target Path\" -options");
3933+ helpMessage.AppendLine();
3934+ helpMessage.AppendLine();
3935+ helpMessage.Append(" Options:");
3936+ helpMessage.AppendLine();
3937+ helpMessage.Append(" -?".PadRight(20));
3938+ helpMessage.Append("Displays this help message");
3939+ helpMessage.AppendLine();
3940+ helpMessage.Append(" -copy".PadRight(20));
3941+ helpMessage.Append("Copies files from one location to another");
3942+ helpMessage.AppendLine();
3943+ helpMessage.Append(" -move".PadRight(20));
3944+ helpMessage.Append("Moves files from one location to another");
3945+ helpMessage.AppendLine();
3946+ helpMessage.Append(" -overwrite".PadRight(20));
3947+ helpMessage.Append("Overwrites files if they exist in the target location");
3948+ helpMessage.AppendLine();
3949+ helpMessage.AppendLine();
3950+
3951+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
3952+ }
3953+ else
3954+ {
3955+ string source = FilePath.GetAbsolutePath(requestInfo.Request.Arguments["orderedarg1"]);
3956+ string target = FilePath.GetDirectoryName(FilePath.GetAbsolutePath(requestInfo.Request.Arguments["orderedarg2"]));
3957+ string directory = FilePath.GetDirectoryName(source);
3958+ string filePattern = FilePath.GetFileName(source);
3959+ bool copy = requestInfo.Request.Arguments.Exists("copy");
3960+ bool move = requestInfo.Request.Arguments.Exists("move");
3961+ bool overwrite = requestInfo.Request.Arguments.Exists("overwrite");
3962+
3963+ string targetFile;
3964+ foreach (string sourceFile in Directory.GetFiles(directory, filePattern))
3965+ {
3966+ targetFile = Path.Combine(target, FilePath.GetFileName(sourceFile));
3967+ if (!File.Exists(targetFile) || overwrite)
3968+ {
3969+ // File can be copied or moved.
3970+ if (copy)
3971+ {
3972+ // Copy the file.
3973+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Copying file '{0}'...\r\n\r\n", target);
3974+ File.Copy(sourceFile, targetFile, true);
3975+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' copied successfully.\r\n\r\n", target);
3976+ }
3977+ else if (move)
3978+ {
3979+ // Move the file.
3980+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Moving file '{0}'...\r\n\r\n", target);
3981+ File.Delete(targetFile);
3982+ File.Move(sourceFile, targetFile);
3983+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' moved successfully.\r\n\r\n", target);
3984+ }
3985+ else
3986+ {
3987+ // Invalid command option specified.
3988+ UpdateStatus(UpdateType.Alarm, "Command option is not valid.\r\n\r\n");
3989+ }
3990+ }
3991+ else
3992+ {
3993+ // File already exists.
3994+ UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", targetFile);
3995+ }
3996+ }
3997+ }
3998+ }
3999+
4000+ private void TransferFile(ClientRequestInfo requestInfo)
4001+ {
4002+ if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 2)
4003+ {
4004+ StringBuilder helpMessage = new StringBuilder();
4005+
4006+ helpMessage.Append("Transfers files to and from the server.");
4007+ helpMessage.AppendLine();
4008+ helpMessage.AppendLine();
4009+ helpMessage.Append(" Usage:");
4010+ helpMessage.AppendLine();
4011+ helpMessage.Append(" Transfer \"Source File Path\" \"Target File Path\" -options");
4012+ helpMessage.AppendLine();
4013+ helpMessage.AppendLine();
4014+ helpMessage.Append(" Options:");
4015+ helpMessage.AppendLine();
4016+ helpMessage.Append(" -?".PadRight(20));
4017+ helpMessage.Append("Displays this help message");
4018+ helpMessage.AppendLine();
4019+ helpMessage.Append(" -upload".PadRight(20));
4020+ helpMessage.Append("Uploads file from local machine to server");
4021+ helpMessage.AppendLine();
4022+ helpMessage.Append(" -download".PadRight(20));
4023+ helpMessage.Append("Downloads file from server to local machine");
4024+ helpMessage.AppendLine();
4025+ helpMessage.Append(" -overwrite".PadRight(20));
4026+ helpMessage.Append("Overwrites file if one exists in the target location");
4027+ helpMessage.AppendLine();
4028+ helpMessage.AppendLine();
4029+
4030+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
4031+ }
4032+ else
4033+ {
4034+ string source = requestInfo.Request.Arguments["orderedarg1"];
4035+ string target = requestInfo.Request.Arguments["orderedarg2"];
4036+ bool upload = requestInfo.Request.Arguments.Exists("upload");
4037+ bool download = requestInfo.Request.Arguments.Exists("download");
4038+ bool overwrite = requestInfo.Request.Arguments.Exists("overwrite");
4039+
4040+ if (upload)
4041+ {
4042+ // Request is for uploading a file.
4043+ if (requestInfo.Request.Attachments.Count == 1)
4044+ {
4045+ // File content is present.
4046+ target = FilePath.GetAbsolutePath(target);
4047+ if (!File.Exists(target) || overwrite)
4048+ {
4049+ // Save the received file.
4050+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Saving file '{0}'...\r\n\r\n", target);
4051+ File.WriteAllBytes(target, (byte[])requestInfo.Request.Attachments[0]);
4052+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' saved successfully.\r\n\r\n", target);
4053+ }
4054+ else
4055+ {
4056+ // File exists and cannot be overwritten.
4057+ UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", target);
4058+ }
4059+ }
4060+ else
4061+ {
4062+ // File content is not present.
4063+ UpdateStatus(UpdateType.Alarm, "Content for file '{0}' is missing.\r\n\r\n", target);
4064+ }
4065+ }
4066+ else if (download)
4067+ {
4068+ // Request is for uploading a file.
4069+ source = FilePath.GetAbsolutePath(source);
4070+ if (File.Exists(source))
4071+ {
4072+ // Send file to client.
4073+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Sending file '{0}'...\r\n\r\n", source);
4074+ SendActionableResponse(requestInfo, true, File.ReadAllBytes(source));
4075+ UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' sent successfully.\r\n\r\n", source);
4076+ }
4077+ else
4078+ {
4079+ // File does not exist.
4080+ UpdateStatus(UpdateType.Alarm, "File '{0}' does not exist.\r\n\r\n", source);
4081+ }
4082+ }
4083+ else
4084+ {
4085+ // Invalid command option specified.
4086+ UpdateStatus(UpdateType.Alarm, "Command option is not valid.\r\n\r\n");
4087+ }
4088+ }
4089+ }
4090+
39184091 private void RemoteTelnetSession(ClientRequestInfo requestinfo)
39194092 {
39204093 if ((object)m_remoteCommandProcess == null && requestinfo.Request.Arguments.ContainsHelpRequest)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t