Part of the fix for U4-1689
Adds an alternative to the naming of the media subfolders instead of using the PropertyId.
This commit is contained in:
60
src/Umbraco.Core/Media/MediaSubFolders.cs
Normal file
60
src/Umbraco.Core/Media/MediaSubFolders.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Umbraco.Core.IO;
|
||||
|
||||
namespace Umbraco.Core.Media
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal singleton to handle the numbering of subfolders within the Media-folder.
|
||||
/// When this class is initiated it will look for numbered subfolders and select the highest number,
|
||||
/// which will be the start point for the naming of the next subfolders. If no subfolders exists
|
||||
/// then the starting point will be 1000, ie. /media/1000/koala.jpg
|
||||
/// </summary>
|
||||
internal class MediaSubFolders
|
||||
{
|
||||
#region Singleton
|
||||
|
||||
private long _numberedFolder = 1000;//Default starting point
|
||||
private static readonly ReaderWriterLockSlim ClearLock = new ReaderWriterLockSlim();
|
||||
private static readonly Lazy<MediaSubFolders> Lazy = new Lazy<MediaSubFolders>(() => new MediaSubFolders());
|
||||
|
||||
public static MediaSubFolders Current { get { return Lazy.Value; } }
|
||||
|
||||
private MediaSubFolders()
|
||||
{
|
||||
var folders = new List<long>();
|
||||
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
||||
var directories = fs.GetDirectories("/");
|
||||
if (directories.Any())
|
||||
{
|
||||
foreach (var directory in directories)
|
||||
{
|
||||
long dirNum;
|
||||
if (long.TryParse(directory, out dirNum))
|
||||
{
|
||||
folders.Add(dirNum);
|
||||
}
|
||||
}
|
||||
long last = folders.OrderBy(x => x).Last();
|
||||
_numberedFolder = last;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns an increment of the numbered media subfolders.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="System.Int64"/> value</returns>
|
||||
public long Increment()
|
||||
{
|
||||
using (new ReadLock(ClearLock))
|
||||
{
|
||||
_numberedFolder++;
|
||||
return _numberedFolder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Media;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
@@ -109,9 +110,10 @@ namespace Umbraco.Core.Models
|
||||
return;
|
||||
|
||||
bool supportsResizing = false;
|
||||
var numberedFolder = MediaSubFolders.Current.Increment();
|
||||
string fileName = UmbracoSettings.UploadAllowDirectories
|
||||
? Path.Combine(property.Id.ToString(), name)
|
||||
: property.Id + "-" + name;
|
||||
? Path.Combine(numberedFolder.ToString(CultureInfo.InvariantCulture), name)
|
||||
: numberedFolder + "-" + name;
|
||||
string extension = Path.GetExtension(name);
|
||||
|
||||
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
||||
|
||||
@@ -139,6 +139,7 @@
|
||||
<Compile Include="Events\SendToPublishEventArgs.cs" />
|
||||
<Compile Include="IApplicationEventHandler.cs" />
|
||||
<Compile Include="MacroPropertyTypeResolver.cs" />
|
||||
<Compile Include="Media\MediaSubFolders.cs" />
|
||||
<Compile Include="Models\ContentBase.cs" />
|
||||
<Compile Include="Models\ContentExtensions.cs" />
|
||||
<Compile Include="Enum.cs" />
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Xml;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Media;
|
||||
using umbraco.cms.businesslogic.Files;
|
||||
using umbraco.cms.businesslogic.property;
|
||||
|
||||
@@ -55,9 +57,10 @@ namespace umbraco.cms.businesslogic.datatype
|
||||
// handle upload
|
||||
if (name != String.Empty)
|
||||
{
|
||||
var numberedFolder = MediaSubFolders.Current.Increment();
|
||||
string fileName = UmbracoSettings.UploadAllowDirectories
|
||||
? Path.Combine(PropertyId.ToString(), name)
|
||||
: PropertyId + "-" + name;
|
||||
? Path.Combine(numberedFolder.ToString(CultureInfo.InvariantCulture), name)
|
||||
: numberedFolder + "-" + name;
|
||||
|
||||
//fileName = Path.Combine(SystemDirectories.Media, fileName);
|
||||
UmbracoFile um = UmbracoFile.Save(fileStream, fileName);
|
||||
|
||||
Reference in New Issue
Block a user