private void TransferFile(ClientRequestInfo requestInfo)
{
if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 2)
{
StringBuilder helpMessage = new StringBuilder();
helpMessage.Append("Transfers files to and from the server.");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Usage:");
helpMessage.AppendLine();
helpMessage.Append(" Transfer \"Source File Path\" \"Target File Path\" -options");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Options:");
helpMessage.AppendLine();
helpMessage.Append(" -?".PadRight(20));
helpMessage.Append("Displays this help message");
helpMessage.AppendLine();
helpMessage.Append(" -upload".PadRight(20));
helpMessage.Append("Uploads file from local machine to server");
helpMessage.AppendLine();
helpMessage.Append(" -download".PadRight(20));
helpMessage.Append("Downloads file from server to local machine");
helpMessage.AppendLine();
helpMessage.Append(" -overwrite".PadRight(20));
helpMessage.Append("Overwrites file if one exists in the target location");
helpMessage.AppendLine();
helpMessage.AppendLine();
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
}
else
{
string source = requestInfo.Request.Arguments["orderedarg1"];
string target = requestInfo.Request.Arguments["orderedarg2"];
bool upload = requestInfo.Request.Arguments.Exists("upload");
bool download = requestInfo.Request.Arguments.Exists("download");
bool overwrite = requestInfo.Request.Arguments.Exists("overwrite");
if (upload)
{
// Request is for uploading a file.
if (requestInfo.Request.Attachments.Count == 1)
{
// File content is present.
target = FilePath.GetAbsolutePath(target);
if (!File.Exists(target) || overwrite)
{
// Save the received file.
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Saving file '{0}'...\r\n\r\n", target);
File.WriteAllBytes(target, (byte[])requestInfo.Request.Attachments[0]);
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' saved successfully.\r\n\r\n", target);
}
else
{
// File exists and cannot be overwritten.
UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", target);
}
}
else
{
// File content is not present.
UpdateStatus(UpdateType.Alarm, "Content for file '{0}' is missing.\r\n\r\n", target);
}
}
else if (download)
{
// Request is for uploading a file.
source = FilePath.GetAbsolutePath(source);
if (File.Exists(source))
{
// Send file to client.
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Sending file '{0}'...\r\n\r\n", source);
SendActionableResponse(requestInfo, true, File.ReadAllBytes(source));
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' sent successfully.\r\n\r\n", source);
}
else
{
// File does not exist.
UpdateStatus(UpdateType.Alarm, "File '{0}' does not exist.\r\n\r\n", source);
}
}
else
{
// Invalid command option specified.
UpdateStatus(UpdateType.Alarm, "Command option is not valid.\r\n\r\n");
}
}
}