[Fixes #7404] Users own media folder name provider (#7454)

Fixes #7404
This commit is contained in:
LorenzoFrediani-Laser 2016-12-01 22:14:52 +01:00 committed by Sébastien Ros
parent 4b2e1ca19d
commit a7f51c13d4
4 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1,20 @@
using System;
using Orchard.ContentManagement;
using Orchard.MediaLibrary.Providers;
using Orchard.Security;
namespace Orchard.MediaLibrary.Implementation {
public class DefaultMediaUsername : IMediaFolderProvider {
public virtual string GetFolderName(IUser content) {
string folder = "";
foreach (char c in content.UserName) {
if (char.IsLetterOrDigit(c)) {
folder += c;
}
else
folder += "_" + String.Format("{0:X}", Convert.ToInt32(c));
}
return folder;
}
}
}

View File

@ -128,6 +128,7 @@
<Compile Include="Extensions\MediaMetaDataExtensions.cs" />
<Compile Include="Factories\VectorImageFactory.cs" />
<Compile Include="Handlers\MediaLibrarySettingsPartHandler.cs" />
<Compile Include="Implementation\DefaultMediaUsername.cs" />
<Compile Include="Models\MediaLibrarySettingsPart.cs" />
<Compile Include="Models\VectorImagePart.cs" />
<Compile Include="Models\IMediaFolder.cs" />
@ -200,6 +201,7 @@
<Compile Include="ResourceManifest.cs" />
<Compile Include="Security\MediaAuthorizationEventHandler.cs" />
<Compile Include="Services\IMediaLibraryService.cs" />
<Compile Include="Providers\IMediaFolderProvider.cs" />
<Compile Include="Services\MediaLibraryService.cs" />
<Compile Include="Services\Shapes.cs" />
<Compile Include="Services\XmlRpcHandler.cs" />

View File

@ -0,0 +1,7 @@
using Orchard.Security;
namespace Orchard.MediaLibrary.Providers {
public interface IMediaFolderProvider : IDependency {
string GetFolderName(IUser content);
}
}

View File

@ -12,6 +12,7 @@ using Orchard.MediaLibrary.Factories;
using Orchard.MediaLibrary.Models;
using Orchard.Core.Title.Models;
using Orchard.Validation;
using Orchard.MediaLibrary.Providers;
namespace Orchard.MediaLibrary.Services {
public class MediaLibraryService : IMediaLibraryService {
@ -19,18 +20,20 @@ namespace Orchard.MediaLibrary.Services {
private readonly IMimeTypeProvider _mimeTypeProvider;
private readonly IStorageProvider _storageProvider;
private readonly IEnumerable<IMediaFactorySelector> _mediaFactorySelectors;
private readonly IMediaFolderProvider _mediaFolderProvider;
private static char[] HttpUnallowed = new char[] { '<', '>', '*', '%', '&', ':', '\\', '?', '#' };
public MediaLibraryService(
IOrchardServices orchardServices,
IMimeTypeProvider mimeTypeProvider,
IStorageProvider storageProvider,
IEnumerable<IMediaFactorySelector> mediaFactorySelectors) {
IEnumerable<IMediaFactorySelector> mediaFactorySelectors,
IMediaFolderProvider mediaFolderProvider) {
_orchardServices = orchardServices;
_mimeTypeProvider = mimeTypeProvider;
_storageProvider = storageProvider;
_mediaFactorySelectors = mediaFactorySelectors;
_mediaFolderProvider = mediaFolderProvider;
T = NullLocalizer.Instance;
}
@ -229,8 +232,7 @@ namespace Orchard.MediaLibrary.Services {
if (_orchardServices.Authorizer.Authorize(Permissions.ManageOwnMedia)) {
var currentUser = _orchardServices.WorkContext.CurrentUser;
var userPath = _storageProvider.Combine("Users", currentUser.UserName);
var userPath = _storageProvider.Combine("Users", _mediaFolderProvider.GetFolderName(currentUser));
return new MediaFolder() {
Name = currentUser.UserName,
MediaPath = userPath