PageRenderTime 51ms CodeModel.GetById 33ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/XmlProvider/DataStore.cs

#
C# | 141 lines | 68 code | 15 blank | 58 comment | 4 complexity | b74b529bb97e9a93af38f3ed038faf89 MD5 | raw file
  1// --------------------------------------------------------------------------------------------------------------------
  2// <summary>
  3//   A storage provider for BlogEngine that uses XML files.
  4//   <remarks>
  5//   To build another provider, you can just copy and modify
  6//   this one. Then add it to the web.config's BlogEngine section.
  7//   </remarks>
  8// </summary>
  9// --------------------------------------------------------------------------------------------------------------------
 10
 11using System.Linq;
 12
 13namespace BlogEngine.Core.Providers
 14{
 15    using System.IO;
 16    using System.Web.Hosting;
 17    using System.Xml.Serialization;
 18
 19    using BlogEngine.Core.DataStore;
 20
 21    /// <summary>
 22    /// A storage provider for BlogEngine that uses XML files.
 23    ///     <remarks>
 24    /// To build another provider, you can just copy and modify
 25    ///         this one. Then add it to the web.config's BlogEngine section.
 26    ///     </remarks>
 27    /// </summary>
 28    public partial class XmlBlogProvider : BlogProvider
 29    {
 30        #region Public Methods
 31
 32        /// <summary>
 33        /// Loads settings from generic data store
 34        /// </summary>
 35        /// <param name="extensionType">
 36        /// Extension Type
 37        /// </param>
 38        /// <param name="extensionId">
 39        /// Extension ID
 40        /// </param>
 41        /// <returns>
 42        /// Stream Settings
 43        /// </returns>
 44        public override object LoadFromDataStore(ExtensionType extensionType, string extensionId)
 45        {
 46            var fileName = string.Format("{0}{1}.xml", StorageLocation(extensionType), extensionId);
 47            Stream str = null;
 48            if (!Directory.Exists(StorageLocation(extensionType)))
 49            {
 50                Directory.CreateDirectory(StorageLocation(extensionType));
 51            }
 52
 53            if (File.Exists(fileName))
 54            {
 55                var reader = new StreamReader(fileName);
 56                str = reader.BaseStream;
 57            }
 58
 59            return str;
 60        }
 61
 62        /// <summary>
 63        /// Removes settings from data store
 64        /// </summary>
 65        /// <param name="extensionType">
 66        /// Extension Type
 67        /// </param>
 68        /// <param name="extensionId">
 69        /// Extension Id
 70        /// </param>
 71        public override void RemoveFromDataStore(ExtensionType extensionType, string extensionId)
 72        {
 73            var fileName = string.Format("{0}{1}.xml", StorageLocation(extensionType), extensionId);
 74            File.Delete(fileName);
 75        }
 76
 77        /// <summary>
 78        /// Save settings to generic data store
 79        /// </summary>
 80        /// <param name="extensionType">
 81        /// Type of extension
 82        /// </param>
 83        /// <param name="extensionId">
 84        /// Extension ID
 85        /// </param>
 86        /// <param name="settings">
 87        /// Stream Settings
 88        /// </param>
 89        public override void SaveToDataStore(ExtensionType extensionType, string extensionId, object settings)
 90        {
 91            var fileName = string.Format("{0}{1}.xml", StorageLocation(extensionType), extensionId);
 92            if (!Directory.Exists(StorageLocation(extensionType)))
 93            {
 94                Directory.CreateDirectory(StorageLocation(extensionType));
 95            }
 96
 97            using (TextWriter writer = new StreamWriter(fileName))
 98            {
 99                var x = new XmlSerializer(settings.GetType());
100                x.Serialize(writer, settings);
101            }
102        }
103
104        #endregion
105
106        #region Methods
107
108        /// <summary>
109        /// Data Store Location
110        /// </summary>
111        /// <param name="extensionType">
112        /// Type of extension
113        /// </param>
114        /// <returns>
115        /// Path to storage directory
116        /// </returns>
117        private static string StorageLocation(ExtensionType extensionType)
118        {
119            switch (extensionType)
120            {
121                case ExtensionType.Extension:
122                    Blog blog = Blog.Blogs.FirstOrDefault(b => b.IsPrimary);
123                    return
124                        HostingEnvironment.MapPath(
125                            Path.Combine(blog.StorageLocation, @"datastore\extensions\"));
126                case ExtensionType.Widget:
127                    return
128                        HostingEnvironment.MapPath(
129                            Path.Combine(Blog.CurrentInstance.StorageLocation, @"datastore\widgets\"));
130                case ExtensionType.Theme:
131                    return
132                        HostingEnvironment.MapPath(
133                            Path.Combine(Blog.CurrentInstance.StorageLocation, @"datastore\themes\"));
134            }
135
136            return string.Empty;
137        }
138
139        #endregion
140    }
141}