mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-06 05:05:42 +13:00
169 lines
No EOL
5.7 KiB
C#
169 lines
No EOL
5.7 KiB
C#
#region License Information (GPL v3)
|
|
|
|
/*
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
Copyright (c) 2007-2018 ShareX Team
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License
|
|
as published by the Free Software Foundation; either version 2
|
|
of the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace ShareX.HelpersLib
|
|
{
|
|
public class ExternalProgram
|
|
{
|
|
public bool IsActive { get; set; }
|
|
public string Name { get; set; }
|
|
public string Path { get; set; }
|
|
public string Args { get; set; }
|
|
public string OutputExtension { get; set; }
|
|
public string Extensions { get; set; }
|
|
public bool HiddenWindow { get; set; }
|
|
public bool DeleteInputFile { get; set; }
|
|
|
|
public ExternalProgram()
|
|
{
|
|
Args = "%input";
|
|
}
|
|
|
|
public ExternalProgram(string name, string path) : this()
|
|
{
|
|
Name = name;
|
|
Path = path;
|
|
}
|
|
|
|
public ExternalProgram(string name, string path, string args) : this(name, path)
|
|
{
|
|
if (!string.IsNullOrEmpty(args))
|
|
{
|
|
Args += " " + args;
|
|
}
|
|
}
|
|
|
|
public string Run(string inputPath)
|
|
{
|
|
if (!string.IsNullOrEmpty(Path) && File.Exists(Path) && !string.IsNullOrWhiteSpace(inputPath))
|
|
{
|
|
inputPath = inputPath.Trim('"');
|
|
|
|
if (CheckExtension(inputPath, Extensions))
|
|
{
|
|
try
|
|
{
|
|
string outputPath = inputPath;
|
|
|
|
string arguments;
|
|
|
|
if (string.IsNullOrEmpty(Args))
|
|
{
|
|
arguments = '"' + inputPath + '"';
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(OutputExtension))
|
|
{
|
|
outputPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(inputPath), System.IO.Path.GetFileNameWithoutExtension(inputPath));
|
|
|
|
if (!OutputExtension.StartsWith("."))
|
|
{
|
|
outputPath += ".";
|
|
}
|
|
|
|
outputPath += OutputExtension;
|
|
}
|
|
|
|
arguments = CodeMenuEntryActions.Parse(Args, inputPath, outputPath);
|
|
}
|
|
|
|
using (Process process = new Process())
|
|
{
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
{
|
|
FileName = Path,
|
|
Arguments = arguments,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = HiddenWindow
|
|
};
|
|
|
|
process.StartInfo = psi;
|
|
DebugHelper.WriteLine($"CLI: \"{psi.FileName}\" {psi.Arguments}");
|
|
process.Start();
|
|
process.WaitForExit();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(outputPath) && File.Exists(outputPath))
|
|
{
|
|
if (DeleteInputFile && !inputPath.Equals(outputPath, StringComparison.OrdinalIgnoreCase) && File.Exists(inputPath))
|
|
{
|
|
DebugHelper.WriteLine("Deleting input file: " + inputPath);
|
|
File.Delete(inputPath);
|
|
}
|
|
|
|
return outputPath;
|
|
}
|
|
|
|
return inputPath;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugHelper.WriteException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
return inputPath;
|
|
}
|
|
|
|
private bool CheckExtension(string path, string extensions)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(path))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(extensions))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i <= extensions.Length; ++i)
|
|
{
|
|
if (i == extensions.Length || !char.IsLetterOrDigit(extensions[i]))
|
|
{
|
|
if (i > index)
|
|
{
|
|
string extension = "." + extensions.Substring(index, i - index);
|
|
|
|
if (path.EndsWith(extension, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
index = i + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |