/*
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;
}
}
}
|