Updating MediaLibrary not to log an error when a file can't be uploaded due to invalid characters

This commit is contained in:
Benedek Farkas 2024-04-12 13:03:45 +02:00
parent 08ae5ddccf
commit e43e3d9b55

View File

@ -3,15 +3,14 @@ using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.FileSystems.Media;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.MediaLibrary.Models;
using Orchard.MediaLibrary.Services;
using Orchard.MediaLibrary.ViewModels;
using Orchard.Themes;
using Orchard.UI.Admin;
using Orchard.MediaLibrary.Models;
using Orchard.Localization;
using System.Linq;
using Orchard.FileSystems.Media;
using Orchard.Logging;
namespace Orchard.MediaLibrary.Controllers {
[Admin, Themed(false)]
@ -107,10 +106,16 @@ namespace Orchard.MediaLibrary.Controllers {
url = mediaPart.FileName,
});
}
catch (Exception ex) {
Logger.Error(ex, "Unexpected exception when uploading a media.");
catch (InvalidNameCharacterException) {
statuses.Add(new {
error = T(ex.Message).Text,
error = T("The file name contains invalid character(s)").Text,
progress = 1.0,
});
}
catch (Exception ex) {
Logger.Error(ex, T("Unexpected exception when uploading a media.").Text);
statuses.Add(new {
error = ex.Message,
progress = 1.0,
});
}
@ -130,7 +135,7 @@ namespace Orchard.MediaLibrary.Controllers {
return HttpNotFound();
// Check permission
if (!(_mediaLibraryService.CheckMediaFolderPermission(Permissions.EditMediaContent, replaceMedia.FolderPath) && _mediaLibraryService.CheckMediaFolderPermission(Permissions.ImportMediaContent, replaceMedia.FolderPath))
if (!(_mediaLibraryService.CheckMediaFolderPermission(Permissions.EditMediaContent, replaceMedia.FolderPath) && _mediaLibraryService.CheckMediaFolderPermission(Permissions.ImportMediaContent, replaceMedia.FolderPath))
&& !_mediaLibraryService.CanManageMediaFolder(replaceMedia.FolderPath)) {
return new HttpUnauthorizedResult();
}
@ -138,7 +143,7 @@ namespace Orchard.MediaLibrary.Controllers {
var statuses = new List<object>();
var settings = Services.WorkContext.CurrentSite.As<MediaLibrarySettingsPart>();
// Loop through each file in the request
for (int i = 0; i < HttpContext.Request.Files.Count; i++) {
// Pointer to file
@ -146,7 +151,8 @@ namespace Orchard.MediaLibrary.Controllers {
var filename = Path.GetFileName(file.FileName);
// if the file has been pasted, provide a default name
if (file.ContentType.Equals("image/png", StringComparison.InvariantCultureIgnoreCase) && !filename.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) {
if (file.ContentType.Equals("image/png", StringComparison.InvariantCultureIgnoreCase)
&& !filename.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) {
filename = "clipboard.png";
}
@ -184,7 +190,7 @@ namespace Orchard.MediaLibrary.Controllers {
});
}
catch (Exception ex) {
Logger.Error(ex, "Unexpected exception when uploading a media.");
Logger.Error(ex, T("Unexpected exception when uploading a media.").Text);
statuses.Add(new {
error = T(ex.Message).Text,