Taylohtio/CondoUpdate/CondoUpdate.ResponsiveLayou.../UpdaterImpl.cs

175 lines
5.9 KiB
C#

using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ResponsiveLayout.SendDevelopmentIdeaPmc
{
public class UpdaterImpl:ICondoUpdater
{
private const string PUBLISHING_IMAGES_URL = "/publishingimages/";
private const string PUBLISHING_IMAGES_FOLDER = "etusivukuvat";
public event EventHandler<LogEventArgs> OnNotify;
public void Update(object args)
{
string url = args as string;
if (string.IsNullOrEmpty(url))
{
this.warn("Url is empty");
return;
}
try
{
this.updateImpl(url);
}
catch (Exception x)
{
this.error("Error occured during updating of Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace);
}
}
private void updateImpl(string url)
{
this.sendDevelopmentIdea(url);
}
private void sendDevelopmentIdea(string url)
{
try
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (!web.Exists)
{
this.warn("Web site '{0}' doesn't exist. It will be ignored", url);
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
var isCustom = WebPropertyHelper.Get(web, Constants.PropertyBag.KEY_CUSTOM_BACKGROUND_USE_CUSTOM) as string;
if (isCustom != null)
{
return;
}
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.DefaultViewUrl.ToLower().Contains(PUBLISHING_IMAGES_URL));
if (list == null)
{
return;
}
var folder = list.RootFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => string.Compare(f.Name, PUBLISHING_IMAGES_FOLDER, true) == 0);
if (folder == null || !folder.Exists)
{
return;
}
string imageUrl = string.Empty;
foreach (SPFile file in folder.Files)
{
using (var fs = file.OpenBinaryStream())
{
using (var image = Image.FromStream(fs))
{
if (image.Width >= 1500)
{
//imageUrl = SPUrlUtility.CombineUrl(web.Url, file.Url);
imageUrl = file.ServerRelativeUrl;
break;
}
}
}
}
if (string.IsNullOrEmpty(imageUrl))
{
return;
}
WebPropertyHelper.Set(web, Constants.PropertyBag.KEY_CUSTOM_BACKGROUND_USE_CUSTOM, "true");
WebPropertyHelper.Set(web, Constants.PropertyBag.KEY_CUSTOM_BACKGROUND_URL, imageUrl);
}
}
}
catch (Exception ex)
{
}
}
private void warn(string msg, params object[] args)
{
this.notify(LogLevel.Warn, msg, args);
}
private void error(string msg, params object[] args)
{
this.notify(LogLevel.Error, msg, args);
}
private void notify(LogLevel level, string msg, params object[] args)
{
this.notify(level, string.Format(msg, args));
}
private void notify(LogLevel level, string msg)
{
if (this.OnNotify != null)
{
this.OnNotify(this, new LogEventArgs { LogLevel = level, Message = msg });
}
}
}
public static class WebPropertyHelper
{
public static void Set(SPWeb web, string propertyName, string propertyValue)
{
web.AllProperties[propertyName] = propertyValue;
web.Update();
}
public static object Get(SPWeb web, string propertyName)
{
if (web.AllProperties.ContainsKey(propertyName))
{
return web.AllProperties[propertyName];
}
return null;
}
// Remove doesn't work on Properties - only on AllProperties
// public static void Remove(SPWeb site, string propertyName)
// {
// if (!site.Properties.ContainsKey(propertyName))
// {
// return;
// }
// site.Properties.Remove(propertyName);
// }
}
public static class Constants
{
public static class PropertyBag
{
public const string KEY_CONDO_POST_CONFIGURED = "PostConfigured";
public const string KEY_CONDO_SHORT_NAME = "CondoShortName";
public const string KEY_CUSTOM_BACKGROUND_USE_CUSTOM = "CondoAutomation_CustomBackground_UseCustom";
public const string KEY_CUSTOM_BACKGROUND_URL = "CondoAutomation_CustomBackground_Url";
public const string CONDO_VERSION = "CondoVersion";
}
}
}