AssemblyFileManager.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Services » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » Services » AssemblyFileManager.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

using Everest.CmsServices.Models;
using Everest.CmsServices.Extension;
using Everest.CmsServices.Extension.PagePlugin;

using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Services{
    public class PagePluginType
    {
        public PagePluginType(Type type)
        {
            ModuleName = type.Module.Name;
            TypeName = type.FullName;
        }
        public string TypeName { get; set; }
        public string ModuleName { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class AssemblyFileManager
    {
        #region DirectoryName
        public const string AssemblyDirName = "Assemblies";
        #endregion
        /// <summary>
        /// Gets the absolute template path.
        /// </summary>
        /// <returns></returns>
        public static string GetAbsoluteAssemblyPath()
        {
            return Path.Combine(CmsGlobal.BaseDirPath, AssemblyDirName);
        }
        #region PluginResolver
        private static IPluginResolver<IControllerPlugin> pagePluginResolver = null;
        /// <summary>
        /// Gets the page plugin resolver.
        /// </summary>
        /// <value>The page plugin resolver.</value>
        public static IPluginResolver<IControllerPlugin> PagePluginResolver
        {
            get
            {
                if (pagePluginResolver == null)
                {
                    pagePluginResolver = new StandardPluginResolver<IControllerPlugin>(GetAbsoluteAssemblyPath());
                }
                return pagePluginResolver;
            }
        }
        #endregion

        /// <summary>
        /// Gets the relative application assembly path.
        /// </summary>
        /// <param name="appName">Name of the app.</param>
        /// <returns></returns>
        public static string GetAbsoluteApplicationAssemblyPath(string appName)
        {
            string appBaseDir = Path.Combine(GetAbsoluteAssemblyPath(), appName);
            return appBaseDir;
        }

        /// <summary>
        /// Uploads the specified app name.
        /// </summary>
        /// <param name="appName">Name of the app.</param>
        /// <param name="postedFile">The posted file.</param>
        /// <returns></returns>
        public static bool Upload(string appName, System.Web.HttpPostedFileBase postedFile)
        {
            string moduleDir = GetAbsoluteApplicationAssemblyPath(appName);
            FileExtensions.EnsureDirtectoryExists(moduleDir);
            postedFile.SaveAs(Path.Combine(moduleDir, Path.GetFileName(postedFile.FileName)));
            return true;
        }

        /// <summary>
        /// Gets the module types.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public static IEnumerable<PagePluginType> GetPagePluginTypes(string filePath)
        {
            Assembly assembly = Assembly.LoadFile(Path.Combine(CmsGlobal.BaseDirPath, filePath));
            Type[] types = assembly.GetTypes();
            Type moduleInterface = typeof(IControllerPlugin);
            List<PagePluginType> moduleTypes = new List<PagePluginType>();
            foreach (var type in types)
            {
                if (moduleInterface.IsAssignableFrom(type))
                {
                    moduleTypes.Add(new PagePluginType(type));
                }
            }
            return moduleTypes;
        }

        /// <summary>
        /// Gets the plugin types.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IEnumerable<PagePluginType> GetPluginTypes(string application)
        {
            IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
            Cms_Folder moduleFolder = dataContext.QueryFolders(application, FolderType.PagePlugin).First();
            var files = GetAssemblyFiles(application);
            var plugins = PagePluginResolver.ResolveAll();
            List<PagePluginType> moduleTypes = new List<PagePluginType>();
            foreach (var plugin in plugins)
            {
                if (files.Exists(f => f.FilePath.Equals(plugin.Value, StringComparison.OrdinalIgnoreCase)))
                {
                    moduleTypes.Add(new PagePluginType(plugin.Key));
                }
            }
            return moduleTypes;
        }


        /// <summary>
        /// Gets the assembly files .
        /// </summary>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static List<FolderFile> GetAssemblyFiles(string application)
        {
            var apps = CachedData.GetBaseApplications(application);

            Dictionary<FileInfo, string> files = new Dictionary<FileInfo, string>();
            foreach (var app in apps)
            {
                IEnumerable<FileInfo> dirFiles = GetFiles(app);
                if (dirFiles != null)
                {
                    foreach (var f in dirFiles)
                    {
                        files.Add(f, app);
                    }
                }
            }
            List<FolderFile> folderFiles = new List<FolderFile>();
            foreach (var item in files)
            {
                folderFiles.Add(new FolderFile()
                {
                    FileName = item.Key.Name,
                    FileSize = (int)item.Key.Length,
                    FilePath = CmsGlobal.ToRelativePath(item.Key.FullName),
                    Application = item.Value
                });
            }
            return folderFiles;
        }

        /// <summary>
        /// Gets the files.
        /// </summary>
        /// <param name="app">The app.</param>
        /// <returns></returns>
        private static IEnumerable<FileInfo> GetFiles(string app)
        {
            string dir = GetAbsoluteApplicationAssemblyPath(app);

            if (Directory.Exists(dir))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                return dirInfo.GetFiles();
            }
            return null;
        }

    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.