public static void AddSocialMediaChannelTheme(string theme) { string themesPath = HttpContext.Current.Server.MapPath(SOCIAL_MEDIA_CHANNELS_FOLDER); string newThemePath = Path.Combine(themesPath, theme); DirectoryInfo newThemeDir = new DirectoryInfo(newThemePath); MediaService mediaService = (MediaService)ApplicationContext.Current.Services.MediaService; var rootFolderId = mediaService.GetMediaOfMediaType(1031).Where(x => x.Name == SOCIAL_MEDIA_THEME_FOLDER_NAME).FirstOrDefault().Id; //Get the media type id ContentTypeService cts = (ContentTypeService)ApplicationContext.Current.Services.ContentTypeService; int mediaTypeId = cts.GetMediaType(SOCIAL_MEDIA_THEME_ALIAS).Id; FileInfo infoFile = newThemeDir.GetFiles("info.txt")[0]; string themeDir = newThemeDir.Name; string thumbnailPath = Path.Combine(newThemeDir.Parent.FullName, themeDir + ".jpg"); string themeNick, themeName, themeUrl, themeCreatedBy, themeCreatedDate; using (StreamReader reader = new StreamReader(infoFile.FullName)) { themeNick = reader.ReadLine(); themeName = reader.ReadLine(); themeCreatedBy = reader.ReadLine(); themeUrl = reader.ReadLine(); themeCreatedDate = reader.ReadLine(); } bool newTheme = mediaService.GetMediaOfMediaType(mediaTypeId).Where(x => x.GetValue("themeName").ToString() == themeName).Count() == 0; if (newTheme)//only add if it's new { var themePck = mediaService.CreateMedia(themeNick, rootFolderId, SOCIAL_MEDIA_THEME_ALIAS); var thumbnail = new FileStream(thumbnailPath, FileMode.Open); themePck.SetValue("umbracoFile", Path.GetFileName(thumbnailPath), thumbnail); themePck.SetValue("themeName", themeName); themePck.SetValue("themeUrl", themeUrl); themePck.SetValue("createdBy", themeCreatedBy); themePck.SetValue("createdDate", themeCreatedDate); mediaService.Save(themePck); int currThemeId = mediaService.GetMediaOfMediaType(mediaTypeId).Where(x => x.GetValue("themeName").ToString() == themeName).FirstOrDefault().Id; foreach (FileInfo image in newThemeDir.EnumerateFiles()) { if (!image.Extension.Equals(".txt") && !image.Extension.Equals(".xml")) { var media = mediaService.CreateMedia(image.Name, currThemeId, "Image"); var channel = new FileStream(image.FullName, FileMode.Open); media.SetValue("umbracoFile", Path.GetFileName(image.FullName), channel); mediaService.Save(media); } } } }
public static void RemoveSocialMediaChannelMediaType() { ContentTypeService contentTypeService = (ContentTypeService)ApplicationContext.Current.Services.ContentTypeService; contentTypeService.Delete(contentTypeService.GetMediaType(SOCIAL_MEDIA_THEME_ALIAS)); }