Created
March 13, 2015 04:32
-
-
Save scjunkie/1fa57e036765f7d10514 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the interface | |
using System.IO; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Resources.Media; | |
namespace Sitecore.Sandbox.Resources.Media | |
{ | |
public interface ICustomIDMediaCreator | |
{ | |
Item AttachStreamToMediaItem(Stream stream, string itemPath, ID id, string filePath, MediaCreatorOptions options); | |
Item CreateFromStream(Stream stream, string filePath, ID id, MediaCreatorOptions options); | |
Item CreateFromStream(Stream stream, string filePath, ID id, bool setStreamIfEmpty, MediaCreatorOptions options); | |
} | |
} | |
// the class | |
using System.IO; | |
using Sitecore; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Managers; | |
using Sitecore.Diagnostics; | |
using Sitecore.IO; | |
using Sitecore.Resources.Media; | |
using Sitecore.SecurityModel; | |
namespace Sitecore.Sandbox.Resources.Media | |
{ | |
public class CustomIDMediaCreator : MediaCreator, ICustomIDMediaCreator | |
{ | |
protected string DefaultItemName { get; private set; } | |
public Item AttachStreamToMediaItem(Stream stream, string itemPath, ID id, string filePath, MediaCreatorOptions options) | |
{ | |
Assert.ArgumentNotNull(stream, "stream"); | |
Assert.ArgumentNotNull(itemPath, "itemPath"); | |
Assert.ArgumentCondition(id.IsNull, "id", "id must be set!"); | |
Assert.ArgumentNotNullOrEmpty(filePath, "filePath"); | |
Assert.ArgumentNotNull(options, "options"); | |
string itemName = GetItemName(itemPath); | |
string extension = FileUtil.GetExtension(filePath); | |
string destination = GetItemParentPath(itemPath); | |
Database database = GetDatabase(options); | |
Item folder = GetParentFolder(database, itemPath); | |
ID templateID = GetMediaTemplateID(extension, options.Versioned, database);; | |
Item mediaItem = ItemManager.CreateItem(itemName, folder, templateID, id, SecurityCheck.Disable); | |
MediaCreatorOptions clonedOptions = CloneMediaCreatorOptions(options); | |
clonedOptions.KeepExisting = true; | |
Sitecore.Resources.Media.Media media = MediaManager.GetMedia(CreateItem(mediaItem.Paths.FullPath, FileUtil.GetFileName(filePath), clonedOptions)); | |
media.SetStream(stream, extension); | |
return (Item)media.MediaData.MediaItem; | |
} | |
public Item CreateFromStream(Stream stream, string filePath, ID id, MediaCreatorOptions options) | |
{ | |
Assert.ArgumentNotNull(stream, "stream"); | |
Assert.ArgumentNotNull(filePath, "filePath"); | |
Assert.ArgumentCondition(id.IsNull, "id", "id must be set!"); | |
Assert.ArgumentNotNull(options, "options"); | |
return CreateFromStream(stream, filePath, id, true, options); | |
} | |
public Item CreateFromStream(Stream stream, string filePath, ID id, bool setStreamIfEmpty, MediaCreatorOptions options) | |
{ | |
Assert.ArgumentNotNull(stream, "stream"); | |
Assert.ArgumentNotNullOrEmpty(filePath, "filePath"); | |
Assert.ArgumentCondition(id.IsNull, "id", "id must be set!"); | |
Assert.ArgumentNotNull(options, "options"); | |
string itemPath = GetItemPath(filePath, options); | |
return AttachStreamToMediaItem(stream, itemPath, filePath, options); | |
} | |
protected virtual ID GetMediaTemplateID(string extension, bool isVersioned, Database database) | |
{ | |
Assert.ArgumentNotNull(extension, "extension"); | |
Assert.ArgumentNotNull(database, "database"); | |
string template = MediaManager.Config.GetTemplate(extension, isVersioned); | |
if(string.IsNullOrWhiteSpace(template)) | |
{ | |
return ID.Null; | |
} | |
TemplateItem templateItem = database.Templates[template]; | |
if (templateItem == null) | |
{ | |
return ID.Null; | |
} | |
return templateItem.ID; | |
} | |
protected virtual Database GetDatabase(MediaCreatorOptions options) | |
{ | |
if(options == null || options.Database == null) | |
{ | |
return Context.ContentDatabase ?? Context.Database; | |
} | |
return options.Database; | |
} | |
protected virtual Item GetParentFolder(Database database, string itemPath) | |
{ | |
string parentPath = GetItemParentPath(itemPath); | |
Item folder = database.GetItem(parentPath); | |
if(folder != null) | |
{ | |
return folder; | |
} | |
return database.CreateItemPath(parentPath, database.Templates[TemplateIDs.MediaFolder]); | |
} | |
protected virtual string GetItemParentPath(string itemPath) | |
{ | |
Assert.ArgumentNotNull(itemPath, "itemPath"); | |
return itemPath.Substring(0, itemPath.LastIndexOf("/")); | |
} | |
protected virtual string GetItemName(string itemPath) | |
{ | |
Assert.ArgumentNotNull(itemPath, "itemPath"); | |
string name = StringUtil.GetLastPart(itemPath, '/', string.Empty); | |
if (!string.IsNullOrEmpty(name)) | |
{ | |
return name; | |
} | |
Assert.IsNotNullOrEmpty(DefaultItemName, "DefaultItemName must be set in configuration!"); | |
return DefaultItemName; | |
} | |
private MediaCreatorOptions CloneMediaCreatorOptions(MediaCreatorOptions options) | |
{ | |
return new MediaCreatorOptions | |
{ | |
AlternateText = options.AlternateText, | |
Database = options.Database, | |
Destination = options.Destination, | |
FileBased = options.FileBased, | |
IncludeExtensionInItemName = options.IncludeExtensionInItemName, | |
KeepExisting = options.KeepExisting, | |
Language = options.Language, | |
OutputFilePath = options.OutputFilePath, | |
Versioned = options.Versioned | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment