mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
BUG-1750 check filename pattern for invalid characters
This commit is contained in:
parent
2e29728a19
commit
f393f3a9d4
3 changed files with 78 additions and 13 deletions
1
Greenshot/Forms/SettingsForm.Designer.cs
generated
1
Greenshot/Forms/SettingsForm.Designer.cs
generated
|
@ -234,6 +234,7 @@ namespace Greenshot {
|
|||
this.textbox_screenshotname.PropertyName = "OutputFileFilenamePattern";
|
||||
this.textbox_screenshotname.Size = new System.Drawing.Size(233, 20);
|
||||
this.textbox_screenshotname.TabIndex = 3;
|
||||
this.textbox_screenshotname.TextChanged += new System.EventHandler(this.FilenamePatternChanged);
|
||||
//
|
||||
// label_language
|
||||
//
|
||||
|
|
|
@ -226,23 +226,55 @@ namespace Greenshot {
|
|||
|
||||
// Check the settings and somehow visibly mark when something is incorrect
|
||||
private bool CheckSettings() {
|
||||
return CheckFilenamePattern() && CheckStorageLocationPath();
|
||||
}
|
||||
|
||||
private bool CheckFilenamePattern() {
|
||||
bool settingsOk = true;
|
||||
if(!Directory.Exists(FilenameHelper.FillVariables(textbox_storagelocation.Text, false))) {
|
||||
textbox_storagelocation.BackColor = Color.Red;
|
||||
settingsOk = false;
|
||||
} else {
|
||||
// "Added" feature #3547158
|
||||
if (Environment.OSVersion.Version.Major >= 6) {
|
||||
textbox_storagelocation.BackColor = SystemColors.Window;
|
||||
} else {
|
||||
textbox_storagelocation.BackColor = SystemColors.Control;
|
||||
}
|
||||
string filename = FilenameHelper.GetFilenameFromPattern(textbox_screenshotname.Text, coreConfiguration.OutputFileFormat, null);
|
||||
// we allow dynamically created subfolders, need to check for them, too
|
||||
string[] pathParts = filename.Split(Path.DirectorySeparatorChar);
|
||||
|
||||
string filenamePart = pathParts[pathParts.Length-1];
|
||||
settingsOk = FilenameHelper.IsFilenameValid(filenamePart);
|
||||
|
||||
for (int i = 0; (settingsOk && i<pathParts.Length-1); i++) {
|
||||
settingsOk = FilenameHelper.IsDirectoryNameValid(pathParts[i]);
|
||||
}
|
||||
|
||||
DisplayTextBoxValidity(textbox_screenshotname, settingsOk);
|
||||
|
||||
return settingsOk;
|
||||
}
|
||||
|
||||
private bool CheckStorageLocationPath() {
|
||||
bool settingsOk = true;
|
||||
if(!Directory.Exists(FilenameHelper.FillVariables(textbox_storagelocation.Text, false))) {
|
||||
settingsOk = false;
|
||||
}
|
||||
DisplayTextBoxValidity(textbox_storagelocation, settingsOk);
|
||||
return settingsOk;
|
||||
}
|
||||
|
||||
private void DisplayTextBoxValidity(GreenshotTextBox textbox, bool valid) {
|
||||
if (valid) {
|
||||
// "Added" feature #3547158
|
||||
if (Environment.OSVersion.Version.Major >= 6) {
|
||||
textbox.BackColor = SystemColors.Window;
|
||||
} else {
|
||||
textbox.BackColor = SystemColors.Control;
|
||||
}
|
||||
} else {
|
||||
textbox.BackColor = Color.Red;
|
||||
}
|
||||
}
|
||||
|
||||
private void FilenamePatternChanged(object sender, EventArgs e) {
|
||||
CheckFilenamePattern();
|
||||
}
|
||||
|
||||
private void StorageLocationChanged(object sender, EventArgs e) {
|
||||
CheckSettings();
|
||||
CheckStorageLocationPath();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -428,7 +428,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// Fill the pattern wit the supplied details
|
||||
/// </summary>
|
||||
/// <param name="pattern">Pattern</param>
|
||||
/// <param name="captureDetails">CaptureDetails</param>
|
||||
/// <param name="captureDetails">CaptureDetails, can be null</param>
|
||||
/// <param name="filenameSafeMode">Should the result be made "filename" safe?</param>
|
||||
/// <returns>Filled pattern</returns>
|
||||
public static string FillPattern(string pattern, ICaptureDetails captureDetails, bool filenameSafeMode) {
|
||||
|
@ -461,10 +461,42 @@ namespace GreenshotPlugin.Core {
|
|||
);
|
||||
} catch (Exception e) {
|
||||
// adding additional data for bug tracking
|
||||
e.Data.Add("title", captureDetails.Title);
|
||||
if (captureDetails != null) {
|
||||
e.Data.Add("title", captureDetails.Title);
|
||||
}
|
||||
e.Data.Add("pattern", pattern);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a directory name is valid in the current file system
|
||||
/// </summary>
|
||||
/// <param name="directoryName">directory name (not path!)</param>
|
||||
/// <returns>true if directory name is valid</returns>
|
||||
public static bool IsDirectoryNameValid(string directoryName) {
|
||||
var forbiddenChars = Path.GetInvalidPathChars();
|
||||
foreach (var forbiddenChar in forbiddenChars) {
|
||||
if (directoryName == null || directoryName.Contains(forbiddenChar.ToString())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a filename is valid in the current file system
|
||||
/// </summary>
|
||||
/// <param name="filename">name of the file</param>
|
||||
/// <returns>true if filename is valid</returns>
|
||||
public static bool IsFilenameValid(string filename) {
|
||||
var forbiddenChars = Path.GetInvalidFileNameChars();
|
||||
foreach (var forbiddenChar in forbiddenChars) {
|
||||
if (filename == null || filename.Contains(forbiddenChar.ToString())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue