using System; using System.Collections.Generic; using System.Data; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.EventLog; using CMS.GlobalHelper; using CMS.IO; using CMS.SettingsProvider; namespace CMS.SharePoint { /// /// Output file class containing data from SharePoint. /// public class CMSOutputSharePointFile : IDataContainer { #region "Variables" private DateTime mInstantiated = DateTime.Now; private int mWidth = 0; private int mHeight = 0; private int mMaxSideSize = 0; private byte[] mOutputData = null; private string mMimeType = null; private bool mDataLoaded = false; private DateTime mValidFrom = DateTime.MinValue; private DateTime mValidTo = DateTime.MaxValue; private string mSharePointFilePath = null; private string mSharePointServer = null; private string mFileExtension = null; private bool mResized = false; #endregion #region "Properties" /// /// Output file data. /// public byte[] OutputData { get { return mOutputData; } set { mOutputData = value; mDataLoaded = true; } } /// /// Last modified date is not available for CMSOutputSharePointFile, use instantiation DateTime. /// public DateTime LastModified { get { return mInstantiated; } } /// /// Requested output width. /// public int Width { get { return mWidth; } set { mWidth = value; } } /// /// Requested output Height. /// public int Height { get { return mHeight; } set { mHeight = value; } } /// /// Requested output MaxSideSize. /// public int MaxSideSize { get { return mMaxSideSize; } set { mMaxSideSize = value; } } /// /// Returns true if the data is loaded to the object. /// public bool DataLoaded { get { return mDataLoaded; } } public string FileExtension { get { if ((mFileExtension == null) && (SharePointFilePath) != null) { mFileExtension = Path.GetExtension(SharePointFilePath); } return mFileExtension; } } /// /// Mime type. /// public string MimeType { get { if ((mMimeType == null) && (SharePointFilePath != null)) { mMimeType = MimeTypeHelper.GetMimetype(FileExtension); } return mMimeType; } set { mMimeType = value; } } /// /// Time to which the file is valid. /// public DateTime ValidTo { get { return mValidTo; } set { mValidTo = value; } } /// /// Time from which the file is valid. /// public DateTime ValidFrom { get { return mValidFrom; } set { mValidFrom = value; } } /// /// Returns true if the file is valid. /// public bool IsValid { get { return (DateTime.Now >= ValidFrom) && (DateTime.Now <= ValidTo); } } /// /// If true, the file is resized version of the file. /// public bool Resized { get { return mResized; } set { mResized = value; } } /// /// Path to file on SharePoint server. /// public string SharePointFilePath { get { return mSharePointFilePath; } set { mSharePointFilePath = value; } } /// /// Gets or sets the address of SharePoint server. /// public string SharePointServer { get { return mSharePointServer; } set { mSharePointServer = value; } } #endregion #region "Constructors" /// /// Constructor. /// public CMSOutputSharePointFile() { } /// /// Constructor. /// /// Address of the SharePoint server /// Path to file on the server /// Output file data public CMSOutputSharePointFile(string server, string filePath, byte[] data) { SharePointServer = server; mSharePointFilePath = filePath; mOutputData = data; mDataLoaded = (data != null); } #endregion #region "Methods" /// /// Ensures that the object contains the output data. /// /// Default data which should be loaded if data required /// Returns true if new data has been loaded public bool EnsureData(byte[] defaultData) { if (!mDataLoaded) { if (defaultData != null) { // Use default data OutputData = defaultData; } else { // Load the file data if (SharePointFilePath != null) { LoadData(); } else { OutputData = null; } } mDataLoaded = true; return true; } return false; } /// /// Loads the data to the object. /// /// New data /// Attachment manager public void LoadData() { if (SharePointFilePath == null) { throw new Exception("[CMSOutputSharePointFile.LoadData]: Cannot load data to the file object, the SharePoint information missing."); } // Get data mOutputData = GetSPDocument(); if (mOutputData != null) { // If is image if (ImageHelper.IsMimeImage(MimeType)) { // Resize image if parameters set if ((MaxSideSize > 0) || (Width > 0) || (Height > 0)) { ImageHelper imgHelper = new ImageHelper(mOutputData); // Original dimensions int originalWidth = imgHelper.ImageWidth; int originalHeight = imgHelper.ImageHeight; // Resize int[] dim = imgHelper.EnsureImageDimensions(Width, Height, MaxSideSize); if ((dim[0] != originalWidth) || (dim[1] != originalHeight)) { // Get altered data mOutputData = imgHelper.GetResizedImageData(dim[0], dim[1], ImageHelper.DefaultQuality); Resized = true; } } } } mDataLoaded = true; } /// /// Retrieves file(document or image) content of specified document from SharePoint server /// trough Copy web service /// /// Byte array private byte[] GetSPDocument() { // Get parameters from URL string serverUrl = SharePointServer; if ((serverUrl == null) || (SharePointFilePath == null)) { return null; } // Prepare valid server address if (!serverUrl.StartsWithCSafe("http://") && !serverUrl.StartsWithCSafe("https://")) { serverUrl = "http://" + serverUrl; } serverUrl = serverUrl.TrimEnd('/'); // Complete URL to path string fileUrl = serverUrl + "/" + SharePointFilePath; // Download file from SharePoint byte[] fileContents = null; WebClient wc = new WebClient(); try { // Try download file wc.Credentials = SharePointFunctions.GetSharePointCredetials(); fileContents = wc.DownloadData(fileUrl); } catch (Exception ex) { // Log exception to Event log EventLogProvider ep = new EventLogProvider(); ep.LogEvent("GetSharePointFile", "GetItem", ex); } return fileContents; } #endregion #region "IDataContainer Members" /// /// Gets or sets the value of the column. /// /// Column name public object this[string columnName] { get { return GetValue(columnName); } set { SetValue(columnName, value); } } /// /// Column names. /// public List ColumnNames { get { return TypeHelper.NewList( "dataloaded", "fileextension", "height", "isvalid", "lastmodified", "maxsidesize", "mimetype", "outputdata", "resized", "sharepointfilepath", "sharepointserver", "validfrom", "validto", "width" ); } } /// /// Returns value of column. /// /// Column name /// Returns the value /// Returns true if the operation was successful (the value was present) public bool TryGetValue(string columnName, out object value) { switch (columnName.ToLowerCSafe()) { case "dataloaded": value = DataLoaded; return true; case "fileextension": value = FileExtension; return true; case "height": value = Height; return true; case "isvalid": value = IsValid; return true; case "lastmodified": value = LastModified; return true; case "maxsidesize": value = MaxSideSize; return true; case "mimetype": value = MimeType; return true; case "outputdata": value = OutputData; return true; case "resized": value = Resized; return true; case "sharepointfilepath": value = SharePointFilePath; return true; case "sharepointserver": value = SharePointServer; return true; case "validfrom": value = ValidFrom; return true; case "validto": value = ValidTo; return true; case "width": value = Width; return true; } value = null; return false; } /// /// Returns value of column. /// /// Column name public object GetValue(string columnName) { object value = null; TryGetValue(columnName, out value); return value; } /// /// Sets value of column. /// /// Column name /// Column value public bool SetValue(string columnName, object value) { throw new NotImplementedException(); } /// /// Returns true if the object contains specified column. /// /// Column name public bool ContainsColumn(string columnName) { throw new NotImplementedException(); } #endregion } }