using FileFlows.Plugin; using FileFlows.Plugin.Attributes; namespace FileFlows.BasicNodes.File; /// /// A flow element that deletes a single item (file or directory) /// public class Delete : Node { /// /// Gets the number of inputs /// public override int Inputs => 1; /// /// Gets the number of outputs /// public override int Outputs => 1; /// /// Gets the type of flow element /// public override FlowElementType Type => FlowElementType.Process; /// /// Gets the icon for the flow element /// public override string Icon => "far fa-trash-alt"; /// /// Gets the help URL for the flow element /// public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/delete"; /// /// Gets or sets the FileName/path to delete /// [TextVariable(1)] public string FileName { get; set; } /// /// Tests if a path is a directory /// /// the path to test /// true if a directory, otherwise false private bool IsDirectory(string path) { if (string.IsNullOrWhiteSpace(path)) return false; try { return Directory.Exists(path); } catch (Exception) { return false; } } /// /// Executes the flow element /// /// the node parameters /// the next output to call public override int Execute(NodeParameters args) { string path = args.ReplaceVariables(this.FileName ?? string.Empty, stripMissing: true); if (string.IsNullOrEmpty(path)) path = args.WorkingFile; bool originalFile = path == args.FileName; if (originalFile && args.IsRemote) { args.Logger?.ILog("Deleting original file remotely: " + args.LibraryFileName); var result = args.DeleteRemote(args.LibraryFileName, false, null); return 1; } if (IsDirectory(path)) { try { args.Logger?.ILog("Deleting directory: " + path); Directory.Delete(path, true); args.Logger?.ILog("Deleted directory: " + path); return 1; } catch (Exception ex) { args.Logger?.ELog("Failed to delete directory: " + ex.Message); return -1; } } else { try { args.Logger?.ILog("Deleting file: " + path); System.IO.File.Delete(path); args.Logger?.ILog("Deleted file: " + path); return 1; } catch (Exception ex) { args.Logger?.ELog($"Failed to delete file: '{path}' => {ex.Message}"); return -1; } } } }