/*
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.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Everest.CmsServices.Models;
using Ionic.Zip;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Extension.Module{
public static class ModuleManager
{
public const string ModuleDirName = "Module";
/// <summary>
/// Uploads the module.
/// </summary>
/// <param name="moduleName">Name of the module.</param>
/// <param name="zipStream">The zip stream.</param>
/// <returns></returns>
public static string UploadModule(string moduleName, Stream zipStream, bool upgrade)
{
var modules = GetModules(moduleName);
if (modules.Where(m => m.ModuleName.Equals(moduleName, StringComparison.InvariantCultureIgnoreCase)).Count() != 0 && upgrade == false)
{
return string.Format(Resources.ModuleAlreadyExists, moduleName);
}
string moduleDirectory = ModuleManager.GetModuleDirectory(moduleName);
using (ZipFile zipFile = ZipFile.Read(zipStream))
{
//determine if the module is valid.
var moduleConfigEntry = zipFile[ModuleInfo.Const_ModuleConfigFile];
var routesConfigEntry = zipFile[ModuleInfo.Const_ModuleRouteTableFile];
if (moduleConfigEntry == null || routesConfigEntry == null)
{
return string.Format(Resources.ModuleInvalid, moduleName);
}
zipFile.ExtractAll(moduleDirectory, ExtractExistingFileAction.OverwriteSilently);
}
//delete some inefficacy files
DirectoryInfo directoryInfo = new DirectoryInfo(moduleDirectory);
FileInfo[] webConfigFile = directoryInfo.GetFiles("web.config", SearchOption.TopDirectoryOnly);
foreach (var item in webConfigFile)
{
File.Delete(item.FullName);
}
return string.Empty;
}
/// <summary>
/// Gets the module.
/// </summary>
/// <param name="moduleApplication">The module application.</param>
/// <param name="moduleName">Name of the module.</param>
/// <param name="runtimeApplication">The runtime application.</param>
/// <returns></returns>
public static ModuleInfo GetModule(string moduleName, string runtimeApplication)
{
return ModuleInfo.GetModule(moduleName, runtimeApplication, true);
}
/// <summary>
/// Gets the modules by application.
/// </summary>
/// <param name="runtimeApplication">The runtime application.</param>
/// <returns></returns>
public static IEnumerable<ModuleInfo> GetModules(string runtimeApplication)
{
IList<ModuleInfo> modules = new List<ModuleInfo>();
var directoryInfo = new DirectoryInfo(GetModuleBaseDirectory());
if (directoryInfo.Exists)
{
var moduleDirectories = directoryInfo.GetDirectories().Where(di => (di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden);
foreach (var dir in moduleDirectories)
{
var moduleInfo = ModuleInfo.GetModule(dir.Name, runtimeApplication, true);
if (moduleInfo.Verify())
{
var applications = moduleInfo.GetAllowedApplications();
if (runtimeApplication == CmsGlobal.RootApplicationName || applications.Contains(CmsGlobal.RootApplicationName, StringComparer.InvariantCultureIgnoreCase))
{
modules.Add(moduleInfo);
}
else
{
if (applications.Contains(runtimeApplication, StringComparer.InvariantCultureIgnoreCase))
{
modules.Add(moduleInfo);
}
}
}
}
}
return modules;
}
/// <summary>
/// Deletes the module.
/// </summary>
/// <param name="moduleName">Name of the module.</param>
public static void DeleteModule(string moduleName)
{
var dataContext = EverestCmsEntities.GetDataContext();
ModuleInfo.DeleteModuleInApplications(dataContext, moduleName);
ModuleInfo.DeletePermissions(dataContext, string.Empty, moduleName);
var directoryInfo = new DirectoryInfo(GetModuleDirectory(moduleName));
DeleteDirectory(directoryInfo);
}
private static void DeleteDirectory(DirectoryInfo directory)
{
if (directory.Exists)
{
// contain files
var files = directory.GetFiles();
foreach (var file in files)
{
if (file.Exists)
{
file.IsReadOnly = false;
file.Delete();
}
}
// contain directories
var dirs = directory.GetDirectories();
foreach (var dir in dirs)
{
DeleteDirectory(dir);
}
// self
directory.Delete(true);
}
}
/// <summary>
/// Installs the specified application.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="moduleName">Name of the module.</param>
public static string Install(string moduleName, string userName)
{
ModuleInstaller installer = new ModuleInstaller();
return installer.Install(ModuleInfo.GetModule(moduleName, CmsGlobal.RootApplicationName, true), userName);
}
/// <summary>
/// Unstalls the specified application.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="moduleName">Name of the module.</param>
public static string Uninstall(string moduleName)
{
ModuleUninstall uninstall = new ModuleUninstall();
return uninstall.Uninstall(ModuleInfo.GetModule(moduleName, CmsGlobal.RootApplicationName, true));
}
/// <summary>
/// Gets the module base directory.
/// </summary>
/// <param name="moduleName">Name of the module.</param>
/// <returns></returns>
public static string GetModuleDirectory(string moduleName)
{
return Path.Combine(GetModuleBaseDirectory(), moduleName);
}
/// <summary>
/// Gets the application modules base directory.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
public static string GetModuleBaseDirectory()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ModuleDirName);
}
}
}
|