Skip to content

Commit

Permalink
fixed windows invalid file name issue. Fix OrchardCMS#6802
Browse files Browse the repository at this point in the history
  • Loading branch information
jchenga committed May 25, 2016
1 parent b0393a0 commit 7abb852
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Orchard.MediaLibrary.Services;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Validation;

namespace Orchard.MediaLibrary.MediaFileName
{
Expand Down Expand Up @@ -61,9 +62,12 @@ protected override DriverResult Editor(MediaPart part, IUpdateModel updater, dyn
try {
_mediaLibraryService.RenameFile(part.FolderPath, priorFileName, model.FileName);
part.FileName = model.FileName;

_notifier.Add(NotifyType.Information, T("File '{0}' was renamed to '{1}'", priorFileName, model.FileName));
}
catch (InvalidWindowsPathException) {
updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file. Invalid Windows file path."));
}
catch (Exception) {
updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file"));
}
Expand Down
19 changes: 18 additions & 1 deletion src/Orchard/Validation/PathValidation.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using NHibernate.Hql;
using Orchard.Environment;

namespace Orchard.Validation {
/// <summary>
/// Provides methods to validate paths.
/// </summary>
public static class PathValidation {

/// <summary>
/// Determines if a path lies within the base path boundaries.
/// If not, an exception is thrown.
Expand All @@ -26,10 +31,22 @@ public static string ValidatePath(string basePath, string mappedPath) {
}

if (!valid) {
throw new ArgumentException("Invalid path");
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT)
throw new InvalidWindowsPathException("Invalid path");
else
throw new ArgumentException("Invalid path");
}

return mappedPath;
}
}

/// <summary>
/// Thrown when an invalid path is encountered on a Windows platform.
/// </summary>
[Serializable]
public class InvalidWindowsPathException : ApplicationException {
public InvalidWindowsPathException(string message) : base(message) {
}
}
}

0 comments on commit 7abb852

Please sign in to comment.