Created
July 12, 2022 06:25
-
-
Save DheeraSite/73a50b770e7ced778fc0a4a19dc57227 to your computer and use it in GitHub Desktop.
Convert File system based Media to Database storage in Sitecore
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
public bool ConvertMediaToDatabase(string itemPath) | |
{ | |
using (new EnforceVersionPresenceDisabler()) | |
{ | |
Sitecore.Data.Items.Item item = database.GetItem(itemPath); | |
Sitecore.Data.Items.MediaItem mediaItem = item; | |
Sitecore.Data.Items.Item duplicateItem = item.CopyTo(item.Parent, item.Name + "copy"); | |
var altText = mediaItem.Alt; | |
var mediaItemFullPath = mediaItem.InnerItem.Paths.Path; | |
var mediaCreatorOptions = new MediaCreatorOptions | |
{ | |
FileBased = false, | |
Destination = mediaItemFullPath, | |
OverwriteExisting = true, | |
Versioned = true, | |
AlternateText = altText | |
}; | |
var filename = mediaItem.Name + "." + mediaItem.Extension; | |
using (new SecurityDisabler()) | |
{ | |
if (AttachStreamToMediaItem(duplicateItem, mediaItemFullPath, mediaCreatorOptions)) | |
{ | |
duplicateItem.Delete(); | |
return true; | |
} | |
else | |
return false; | |
} | |
} | |
} | |
public bool AttachStreamToMediaItem(Item duplicateItem, string itemPath, MediaCreatorOptions options) | |
{ | |
Assert.ArgumentNotNull(options, "options"); | |
Assert.ArgumentNotNull(itemPath, "itemPath"); | |
FileStream filestream; | |
try | |
{ | |
using (new EnforceVersionPresenceDisabler()) | |
{ | |
Item item = CreateItem(itemPath, duplicateItem.TemplateID, options); | |
Language[] itemLanguages = GetItemMediaLanguages(options, duplicateItem); | |
if (itemLanguages != null && itemLanguages.Any()) | |
{ | |
foreach (Language language in itemLanguages) | |
{ | |
Sitecore.Data.Items.MediaItem mediaItem = database.GetItem(duplicateItem.ID, language); | |
Sitecore.Data.Items.MediaItem langitem = database.GetItem(item.ID, language); | |
Media media = MediaManager.GetMedia(langitem); | |
if (mediaItem != null && !string.IsNullOrEmpty(mediaItem.FilePath) && langitem != null) | |
{ | |
var path = ModifyFilePath(mediaItem.FilePath, langitem.ID.ToString()); | |
var filePath = Sitecore.IO.FileUtil.MapPath(path); | |
string extension = FileUtil.GetExtension(filePath); | |
using (new EditContext(langitem, SecurityCheck.Disable)) | |
{ | |
langitem.Extension = Sitecore.StringUtil.GetString(mediaItem.Extension, extension); | |
langitem.Alt = StringUtil.GetString(mediaItem.Alt, options.AlternateText); | |
langitem.InnerItem["Width"] = mediaItem.InnerItem["Width"] != null ? mediaItem.InnerItem["Width"] : ""; | |
langitem.InnerItem["Height"] = mediaItem.InnerItem["Height"] != null ? mediaItem.InnerItem["Height"] : ""; | |
langitem.InnerItem.Statistics.UpdateRevision(); | |
} | |
using (filestream = new FileStream(filePath, FileMode.Open)) | |
{ | |
media.SetStream(filestream, extension); | |
} | |
} | |
} | |
return true; | |
} | |
} | |
} | |
catch (DirectoryNotFoundException ex) | |
{ | |
Log.Error("Directory Not Found", ex.Message); | |
} | |
catch (FileNotFoundException ex) | |
{ | |
Log.Error("File Not Found", ex.Message); | |
} | |
catch (Exception ex) | |
{ | |
Log.Error("Error while attaching stream to media", ex.Message); | |
} | |
return false; | |
} | |
protected Item CreateItem(string itemPath, Sitecore.Data.ID templateId, MediaCreatorOptions options) | |
{ | |
Assert.ArgumentNotNullOrEmpty(itemPath, "itemPath"); | |
Assert.ArgumentNotNull(options, "options"); | |
Item item2; | |
using (new EnforceVersionPresenceDisabler()) | |
{ | |
using (new SecurityDisabler()) | |
{ | |
Item item = (options.OverwriteExisting ? database.GetItem(itemPath) : null); | |
string itemName = GetItemName(itemPath); | |
item2 = item; | |
item2.Versions.RemoveAll(true); | |
item2 = item2.Database.GetItem(item2.ID, item2.Language, Sitecore.Data.Version.Latest); | |
Assert.IsNotNull(item2, "item"); | |
item2.Editing.BeginEdit(); | |
foreach (Field field in item2.Fields) | |
{ | |
field.Reset(); | |
} | |
item2.Editing.EndEdit(); | |
item2.Editing.BeginEdit(); | |
item2.Name = itemName; | |
item2.TemplateID = templateId; | |
item2.Editing.EndEdit(); | |
} | |
} | |
Assert.IsNotNull(item2, typeof(Item), "Could not create media item: '{0}'.", itemPath); | |
item2.Reload(); | |
return item2; | |
} | |
private string GetItemName(string itemPath) | |
{ | |
using (new EnforceVersionPresenceDisabler()) | |
{ | |
Assert.ArgumentNotNull(itemPath, "itemPath"); | |
string lastPart = Sitecore.StringUtil.GetLastPart(itemPath, '/', string.Empty); | |
if (string.IsNullOrEmpty(lastPart)) | |
{ | |
if (!Settings.Media.IncludeExtensionsInItemNames) | |
{ | |
return "unnamed"; | |
} | |
throw new InvalidOperationException("Invalid item path for media item: " + itemPath); | |
} | |
return lastPart; | |
} | |
} | |
protected virtual Language[] GetItemMediaLanguages(MediaCreatorOptions options, Item item) | |
{ | |
Assert.ArgumentNotNull(options, "options"); | |
Assert.ArgumentNotNull(item, "item"); | |
Assert.Required(item.Database, "item.Database"); | |
using (new EnforceVersionPresenceDisabler()) | |
{ | |
if (!options.Versioned) | |
{ | |
return item.Database.Languages; | |
} | |
else | |
{ | |
var itemLanguages = ItemManager.GetContentLanguages(item) | |
.Select(lang => new { Language = lang, HasLanguage = ItemManager.GetVersions(item, lang).Count > 0 }) | |
.Where(langInfo => langInfo.HasLanguage) | |
.Select(langInfo => langInfo.Language).ToArray(); | |
return itemLanguages; | |
} | |
} | |
} | |
private string ModifyFilePath(string filePath, string itemId) | |
{ | |
string newid = filePath.Split('{', '}')[1]; | |
return filePath.Replace(newid, itemId.Split('{', '}')[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment