Files
DearFTP/DearFTP/Connection/Commands/DeleteCommand.cs
Eveldee 94431aebb8 Clean code
Remove unused usings
Remove blank lines
Fix style issues
2019-07-20 16:16:23 +02:00

68 lines
2.0 KiB
C#

using DearFTP.Utils;
using System;
using System.IO;
using System.Linq;
namespace DearFTP.Connection.Commands
{
class DeleteCommand : ICommand
{
public string[] Aliases { get; } = new string[]
{
"DELE",
"RMD",
"XRMD"
};
public void Execute(Session session, FtpStream stream, string alias, string argument)
{
(NavigablePath navigablePath, string path, bool isDirectory) = session.NavigablePath.GetSystemFilePath(argument);
if (navigablePath == null)
{
stream.Send(ResponseCode.FileUnavailable, "File does not exist.");
return;
}
if (navigablePath.CurrentDirectory == "/")
{
stream.Send(ResponseCode.FileUnavailable, "Operation not allowed: can't remove a share.");
return;
}
if (!session.WritablesShares.Contains(navigablePath.CurrentShare))
{
stream.Send(ResponseCode.FileUnavailable, "You don't have write access to this file.");
return;
}
if (isDirectory)
{
if (alias.ToUpper() == "RMD")
{
Directory.Delete(path, true);
stream.Send(ResponseCode.FileActionOK, "Directory successfully deleted.");
}
else
{
stream.Send(ResponseCode.ArgumentsError, "Path is a directory and not a file.");
}
}
else
{
if (alias.ToUpper() == "DELE")
{
File.Delete(path);
stream.Send(ResponseCode.FileActionOK, "File successfully deleted");
}
else
{
stream.Send(ResponseCode.ArgumentsError, "Path is a file and not a directory.");
}
}
}
}
}