mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding setting to set allowed media types
This commit is contained in:
+33
-4
@@ -8,19 +8,29 @@ 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;
|
||||
|
||||
namespace Orchard.MediaLibrary.Controllers {
|
||||
[Admin, Themed(false)]
|
||||
public class ClientStorageController : Controller {
|
||||
private readonly IMediaLibraryService _mediaLibraryService;
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public ClientStorageController(IMediaLibraryService mediaManagerService, IContentManager contentManager) {
|
||||
public ClientStorageController(IMediaLibraryService mediaManagerService, IOrchardServices orchardServices) {
|
||||
_mediaLibraryService = mediaManagerService;
|
||||
_contentManager = contentManager;
|
||||
Services = orchardServices;
|
||||
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IOrchardServices Services { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult Index(string folderPath, string type) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Cannot manage media"))) {
|
||||
return new HttpUnauthorizedResult();
|
||||
}
|
||||
|
||||
var viewModel = new ImportMediaViewModel {
|
||||
FolderPath = folderPath,
|
||||
@@ -32,7 +42,15 @@ namespace Orchard.MediaLibrary.Controllers {
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Upload(string folderPath, string type) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent, T("Cannot manage media"))) {
|
||||
return new HttpUnauthorizedResult();
|
||||
}
|
||||
|
||||
var statuses = new List<object>();
|
||||
var settings = Services.WorkContext.CurrentSite.As<MediaLibrarySettingsPart>();
|
||||
var allowedExtensions = (settings.UploadAllowedFileTypeWhitelist ?? "")
|
||||
.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Where(x => x.StartsWith("."));
|
||||
|
||||
// Loop through each file in the request
|
||||
for (int i = 0; i < HttpContext.Request.Files.Count; i++) {
|
||||
@@ -45,8 +63,19 @@ namespace Orchard.MediaLibrary.Controllers {
|
||||
filename = "clipboard.png";
|
||||
}
|
||||
|
||||
// skip file if the allowed extensions is defined and doesn't match
|
||||
if(allowedExtensions.Any()) {
|
||||
if(!allowedExtensions.Any(e => filename.EndsWith(e, StringComparison.OrdinalIgnoreCase))) {
|
||||
statuses.Add(new {
|
||||
error = T("This file type is not allowed: {0}", Path.GetExtension(filename)).Text,
|
||||
progress = 1.0,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
var mediaPart = _mediaLibraryService.ImportMedia(file.InputStream, folderPath, filename, type);
|
||||
_contentManager.Create(mediaPart);
|
||||
Services.ContentManager.Create(mediaPart);
|
||||
|
||||
statuses.Add(new {
|
||||
id = mediaPart.Id,
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Localization;
|
||||
using Orchard.MediaLibrary.Models;
|
||||
|
||||
namespace Orchard.MediaLibrary.Handlers {
|
||||
public class MediaLibrarySettingsPartHandler : ContentHandler {
|
||||
public MediaLibrarySettingsPartHandler() {
|
||||
T = NullLocalizer.Instance;
|
||||
Filters.Add(new ActivatingFilter<MediaLibrarySettingsPart>("Site"));
|
||||
Filters.Add(new TemplateFilterForPart<MediaLibrarySettingsPart>("MediaLibrarySettings", "Parts/MediaLibrary.MediaLibrarySettings", "media"));
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
||||
if (context.ContentItem.ContentType != "Site")
|
||||
return;
|
||||
base.GetItemMetadata(context);
|
||||
context.Metadata.EditorGroupInfo.Add(new GroupInfo(T("Media")));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.MediaLibrary.Models {
|
||||
public class MediaLibrarySettingsPart : ContentPart {
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of file extensions that can be uploaded
|
||||
/// </summary>
|
||||
public string UploadAllowedFileTypeWhitelist {
|
||||
get { return this.Retrieve(x => x.UploadAllowedFileTypeWhitelist); }
|
||||
set { this.Store(x => x.UploadAllowedFileTypeWhitelist, value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,8 @@
|
||||
<Content Include="Styles\Web.config" />
|
||||
<Compile Include="Drivers\VectorImagePartDriver.cs" />
|
||||
<Compile Include="Factories\VectorImageFactory.cs" />
|
||||
<Compile Include="Handlers\MediaLibrarySettingsPartHandler.cs" />
|
||||
<Compile Include="Models\MediaLibrarySettingsPart.cs" />
|
||||
<Compile Include="Models\VectorImagePart.cs" />
|
||||
<Compile Include="Models\IMediaFolder.cs" />
|
||||
<Compile Include="MediaFileName\MediaFileNameDriver.cs" />
|
||||
@@ -381,6 +383,9 @@
|
||||
<ItemGroup>
|
||||
<Content Include="Views\Media-VectorImage.Thumbnail.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Views\EditorTemplates\Parts\MediaLibrary.MediaLibrarySettings.cshtml" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
|
||||
function transferViewModel(filename, size) {
|
||||
var self = this;
|
||||
|
||||
|
||||
self.filename = filename;
|
||||
self.size = size;
|
||||
self.label = ko.observable();
|
||||
@@ -60,7 +60,7 @@
|
||||
|
||||
function clientStorageViewModel() {
|
||||
var self = this;
|
||||
|
||||
|
||||
self.transfers = ko.observableArray([]);
|
||||
|
||||
self.upload = function(file) {
|
||||
@@ -69,7 +69,7 @@
|
||||
return transfer;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
var viewModel = new clientStorageViewModel();
|
||||
ko.applyBindings(viewModel);
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
$('#fileupload > input').click(function(event) {
|
||||
event.stopPropagation();
|
||||
});
|
||||
|
||||
|
||||
// Add drag-n-drop HTML5 support
|
||||
$('#fileupload').fileupload({
|
||||
url: '@Url.Action("Upload", "ClientStorage")',
|
||||
@@ -103,7 +103,7 @@
|
||||
// See the basic file upload widget for more information:
|
||||
add: function (e, data) {
|
||||
var self = $(this);
|
||||
|
||||
|
||||
if (!data.files.length) {
|
||||
return;
|
||||
}
|
||||
@@ -113,8 +113,8 @@
|
||||
var file = data.files[i];
|
||||
data.context = viewModel.upload(file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
data.submit();
|
||||
},
|
||||
// Callback for upload progress events:
|
||||
@@ -134,9 +134,15 @@
|
||||
},
|
||||
done: function(e, data) {
|
||||
var result = data.result[0];
|
||||
|
||||
if (result.error) {
|
||||
data.context.label(result.error);
|
||||
return;
|
||||
}
|
||||
|
||||
data.context.label('@HttpUtility.JavaScriptStringEncode(T("Loading thumbnail...").Text)');
|
||||
|
||||
|
||||
|
||||
|
||||
var url = '@HttpUtility.JavaScriptStringEncode(Url.Action("MediaItem", "Admin"))/' + result.id + '?displayType=Thumbnail';
|
||||
|
||||
$.ajax({
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
@model Orchard.MediaLibrary.Models.MediaLibrarySettingsPart
|
||||
|
||||
<fieldset>
|
||||
<legend>@T("Media Library")</legend>
|
||||
<div>
|
||||
<label for="@Html.IdFor(m => m.UploadAllowedFileTypeWhitelist)">@T("Accepted file extensions")</label>
|
||||
@Html.TextBoxFor(m => m.UploadAllowedFileTypeWhitelist, new { @class = "text large"})
|
||||
<span class="hint">@T("A comma separated list of file extensions, e.g., \".jpg, .avi, .txt\". Leave empty to accept any file types.")</span>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user