mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 12:53:33 +08:00
Issue 16229. Validation for empty filenames when uploading media
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using NUnit.Framework;
|
||||
using Orchard.Extensions;
|
||||
|
||||
namespace Orchard.Tests.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public class NullOrEmptyTrimmedStringExtensionsTests
|
||||
{
|
||||
[Test]
|
||||
public void Trimmed_EmptyStringReturnsTrue() {
|
||||
const string testString = "";
|
||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullStringReturnsTrue() {
|
||||
const string testString = null;
|
||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SpacedStringReturnsTrue() {
|
||||
const string testString = " ";
|
||||
Assert.AreEqual(true, testString.IsNullOrEmptyTrimmed());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ActualStringReturnsFalse() {
|
||||
const string testString = "testString";
|
||||
Assert.AreEqual(false, testString.IsNullOrEmptyTrimmed());
|
||||
}
|
||||
}
|
||||
}
|
@@ -148,6 +148,7 @@
|
||||
<Compile Include="EventsTests.cs" />
|
||||
<Compile Include="Extensions\ExtensionFoldersTests.cs" />
|
||||
<Compile Include="Extensions\ExtensionManagerTests.cs" />
|
||||
<Compile Include="Extensions\NullOrEmptyTrimmedStringExtensionsTests.cs" />
|
||||
<Compile Include="Localization\NullLocalizerTests.cs" />
|
||||
<Compile Include="Logging\LoggingModuleTests.cs" />
|
||||
<Compile Include="Mvc\ModelBinders\KeyedListModelBinderTests.cs" />
|
||||
|
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Extensions;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Media.Models;
|
||||
using Orchard.Media.Services;
|
||||
@@ -139,21 +140,32 @@ namespace Orchard.Media.Controllers {
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Add() {
|
||||
public ActionResult Add()
|
||||
{
|
||||
var viewModel = new MediaItemAddViewModel();
|
||||
try {
|
||||
try
|
||||
{
|
||||
UpdateModel(viewModel);
|
||||
if (!Services.Authorizer.Authorize(Permissions.UploadMediaFiles, T("Couldn't upload media file")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
foreach (string fileName in Request.Files) {
|
||||
if(Request.Files[0].FileName.IsNullOrEmptyTrimmed()) {
|
||||
ModelState.AddModelError("File", "Select a file to upload");
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return View(viewModel);
|
||||
|
||||
foreach (string fileName in Request.Files)
|
||||
{
|
||||
HttpPostedFileBase file = Request.Files[fileName];
|
||||
_mediaService.UploadMediaFile(viewModel.MediaPath, file);
|
||||
}
|
||||
|
||||
return RedirectToAction("Edit", new { name = viewModel.FolderName, mediaPath = viewModel.MediaPath });
|
||||
}
|
||||
catch (Exception exception) {
|
||||
catch (Exception exception)
|
||||
{
|
||||
Services.Notifier.Error("Uploading media file failed: " + exception.Message);
|
||||
return View(viewModel);
|
||||
}
|
||||
|
@@ -15,5 +15,11 @@ namespace Orchard.Extensions {
|
||||
|
||||
return cleanTailRegex.Replace(text.Substring(0, characterCount + 1), "") + ellipsis;
|
||||
}
|
||||
|
||||
public static bool IsNullOrEmptyTrimmed(this string text)
|
||||
{
|
||||
if(text == null) return true;
|
||||
return string.IsNullOrEmpty(text.Trim());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user