限制上传的大小和 umbraco 媒体纠察队的文件类型
Limiting the size of upload and the file type for umbraco media picket
像这样通过 dialogService 尝试 select 媒体
dialogService.mediaPicker({
startNodeId: undefined,
multiPicker: false,
cropSize: undefined,
showDetails: true,
callback: function (data) {
}
是否有一些方法可以自定义此媒体选择器配置并仅允许某些媒体类型并指定大小
我认为您最好的选择是为 MediaService 的 Saving 事件创建一个事件处理程序,并在保存文件之前检查您的条件。
更多信息在这里:https://our.umbraco.com/Documentation/Getting-Started/Code/Subscribing-To-Events/
public class RestrictMediaTypeAndSize : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += MediaService_Saving;
}
private void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
{
foreach (var media in e.SavedEntities)
{
//you can check for other types of content
if (media.ContentType.Alias == Image.ModelTypeAlias)
{
var fileExtension = GetFileExtension(media);
if (!HasAllowedExtension(fileExtension))
{
//we cancel the saving process
e.Cancel = true;
//we send a message to the user.
var errorMessage = new EventMessage("Error", $"Files of type {fileExtension} are not allowed.");
e.Messages.Add(errorMessage);
//we found an error so we exit the foreach loop to finish the execution.
break;
}
if (!HasValidDimensions(media))
{
//we cancel the saving process
e.Cancel = true;
//we send a message to the user.
var errorMessage = new EventMessage("Error", "Files with a size of (whatever your restrictions) are not allowed.");
e.Messages.Add(errorMessage);
//we found an error so we exit the foreach loop to finish the execution.
break;
}
}
}
}
private bool HasAllowedExtension(string fileExtension)
{
string[] allowedExtensions = new string[] { ".jpg", ".png" };
return allowedExtensions.Contains(fileExtension);
}
private bool HasValidDimensions(IMedia media)
{
//take the image dimensions for the properties populated when you upload the file.
var height = (int)media.Properties["umbracoHeight"].Value;
var width = (int)media.Properties["umbracoWidth"].Value;
//check for whatever conditions you want.
return height < 1000 && width < 2000;
}
private string GetFileExtension(IMedia media)
{
//The umbracoFile Propery is a Json object and we need the src property from it which is the file path.
var filePath = JObject.Parse(media.Properties["umbracoFile"].Value.ToString()).SelectToken("$.src").Value<string>();
return System.IO.Path.GetExtension(filePath);
}
}
像这样通过 dialogService 尝试 select 媒体
dialogService.mediaPicker({
startNodeId: undefined,
multiPicker: false,
cropSize: undefined,
showDetails: true,
callback: function (data) {
}
是否有一些方法可以自定义此媒体选择器配置并仅允许某些媒体类型并指定大小
我认为您最好的选择是为 MediaService 的 Saving 事件创建一个事件处理程序,并在保存文件之前检查您的条件。
更多信息在这里:https://our.umbraco.com/Documentation/Getting-Started/Code/Subscribing-To-Events/
public class RestrictMediaTypeAndSize : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += MediaService_Saving;
}
private void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
{
foreach (var media in e.SavedEntities)
{
//you can check for other types of content
if (media.ContentType.Alias == Image.ModelTypeAlias)
{
var fileExtension = GetFileExtension(media);
if (!HasAllowedExtension(fileExtension))
{
//we cancel the saving process
e.Cancel = true;
//we send a message to the user.
var errorMessage = new EventMessage("Error", $"Files of type {fileExtension} are not allowed.");
e.Messages.Add(errorMessage);
//we found an error so we exit the foreach loop to finish the execution.
break;
}
if (!HasValidDimensions(media))
{
//we cancel the saving process
e.Cancel = true;
//we send a message to the user.
var errorMessage = new EventMessage("Error", "Files with a size of (whatever your restrictions) are not allowed.");
e.Messages.Add(errorMessage);
//we found an error so we exit the foreach loop to finish the execution.
break;
}
}
}
}
private bool HasAllowedExtension(string fileExtension)
{
string[] allowedExtensions = new string[] { ".jpg", ".png" };
return allowedExtensions.Contains(fileExtension);
}
private bool HasValidDimensions(IMedia media)
{
//take the image dimensions for the properties populated when you upload the file.
var height = (int)media.Properties["umbracoHeight"].Value;
var width = (int)media.Properties["umbracoWidth"].Value;
//check for whatever conditions you want.
return height < 1000 && width < 2000;
}
private string GetFileExtension(IMedia media)
{
//The umbracoFile Propery is a Json object and we need the src property from it which is the file path.
var filePath = JObject.Parse(media.Properties["umbracoFile"].Value.ToString()).SelectToken("$.src").Value<string>();
return System.IO.Path.GetExtension(filePath);
}
}