/*
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.Web;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Ionic.Zip;
namespace Everest.CmsServices.Services{
public class TemplateFileManager
{
public const string TemplateDirName = "Template";
public const string ThemeDirName = "Theme";
public const string JavascriptDirName = "Javascript";
const string ThumbnailDirName = "Thumbnail";
#region Theme
/// <summary>
/// Gets the theme stylesheets.
/// </summary>
/// <param name="theme">The theme.</param>
/// <returns></returns>
public static IEnumerable<string> GetThemeStylesheets(string theme)
{
string themePath = GetThemePath(theme);
if (!Directory.Exists(themePath))
{
return new string[0];
}
return Directory.GetFiles(themePath, "*.css", SearchOption.AllDirectories).OrderBy(f => f);
}
/// <summary>
/// Gets the theme path.
/// </summary>
/// <param name="theme">The theme.</param>
/// <returns></returns>
public static string GetThemePath(string theme)
{
UniqueName uniqueName = new UniqueName(theme);
string themePath = Path.Combine(GetThemeBasePath(uniqueName.ApplicationName), uniqueName.ItemName);
return themePath;
}
/// <summary>
/// Gets the binary resource base path.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <returns></returns>
private static string GetThemeBasePath(string appName)
{
string templatePath = GetAbsoluteTemplateFolderPath(appName, FolderType.BinaryResource);
return Path.Combine(templatePath, ThemeDirName);
}
/// <summary>
/// Gets the all themes it and its bases.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <returns></returns>
public static IEnumerable<string> GetThemes(string appName)
{
IEnumerable<string> baseApps = CachedData.GetBaseApplications(appName);
List<string> themes = new List<string>();
foreach (var app in baseApps)
{
var themeBasePath = GetThemeBasePath(app);
if (Directory.Exists(themeBasePath))
{
var themeDirs = (new DirectoryInfo(themeBasePath)).GetDirectories();
foreach (var themeDir in themeDirs)
{
if ((themeDir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
themes.Add((new UniqueName(app, themeDir.Name)).ToString());
}
}
}
}
themes.Sort();
return themes;
}
#endregion
#region Template
/// <summary>
/// Deletes the template directory.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
public static void DeleteTemplateDirectory(string applicationName)
{
try
{
string templatePath = GetAbsoluteTemplatePath(applicationName);
if (Directory.Exists(templatePath))
{
Directory.Delete(templatePath, true);
}
}
catch
{
}
}
/// <summary>
/// Gets the absolute template path.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <returns></returns>
public static string GetAbsoluteTemplatePath(string applicationName)
{
return CmsGlobal.ToAbsolutePath(GetRelativeTemplatePath(applicationName));
}
/// <summary>
/// Gets the template relative path.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <returns></returns>
public static string GetRelativeTemplatePath(string applicationName)
{
return Path.Combine(TemplateFileManager.TemplateDirName, applicationName);
}
#endregion
#region PageTemplate
/// <summary>
/// Gets the name of the content template file.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static string GetAbsolutePageTemplateFilePath(string appName, string name)
{
string filePath = Path.Combine(GetAbsoluteTemplateFolderPath(appName, FolderType.LayoutTemplate), name + ".aspx");
return filePath;
}
/// <summary>
/// Gets the name of the page template thumbnail file.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <param name="templateName">Name of the template.</param>
/// <param name="fileExtension">The file extension.</param>
/// <returns></returns>
public static string GetPageTemplateThumbnailPath(string appName, string templateName)
{
string thumbnailDir = Path.Combine(GetAbsoluteTemplateFolderPath(appName, FolderType.LayoutTemplate), ThumbnailDirName);
thumbnailDir = Path.Combine(thumbnailDir, templateName);
return thumbnailDir;
}
/// <summary>
/// Saves the page template.
/// </summary>
/// <param name="app">The app.</param>
/// <param name="name">The name.</param>
/// <param name="body">The body.</param>
/// <returns>the file name</returns>
public static string SavePageTemplate(string app, string name, string body)
{
string fileName = GetAbsolutePageTemplateFilePath(app, name);
FileExtensions.SaveFileBody(fileName, body);
fileName = fileName.Replace(CmsGlobal.BaseDirPath, "");
return fileName;
}
#endregion
#region ContentTemplate
/// <summary>
/// Gets the name of the content template file.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static string GetAbsoluteContentTemplateFilePath(string appName, string name)
{
string filePath = Path.Combine(GetAbsoluteTemplateFolderPath(appName, FolderType.ContentTemplate), name + ".ascx");
return filePath;
}
/// <summary>
/// Saves the content template body.
/// </summary>
/// <param name="app">The app.</param>
/// <param name="name">The name.</param>
/// <param name="body">The body.</param>
/// <returns>The file name</returns>
public static string SaveContentTemplate(string app, string name, string body)
{
string filePath = GetAbsoluteContentTemplateFilePath(app, name);
FileExtensions.SaveFileBody(filePath, body);
filePath = filePath.Replace(CmsGlobal.BaseDirPath, "");
return filePath;
}
#endregion
#region BinaryResource
public static string GetAbsoluteTemplateFolderPath(string appName, FolderType folderType)
{
string templatePath = CmsGlobal.ToAbsolutePath(GetRelativeTemplateFolderPath(appName, folderType));
return templatePath;
}
/// <summary>
/// Gets the relative folder path.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <param name="folderType">Type of the folder.</param>
/// <returns></returns>
public static string GetRelativeTemplateFolderPath(string appName, FolderType folderType)
{
string dirName = folderType.ToString();
string appBaseDir = GetRelativeTemplatePath(appName);
return Path.Combine(appBaseDir, dirName);
}
/// <summary>
/// Parses the app name from path.
/// </summary>
/// <param name="absolutePath">The absolute path.</param>
/// <returns></returns>
public static string ParseAppNameFromPath(string absolutePath)
{
string relativePath = CmsGlobal.ToRelativePath(absolutePath);
string[] pathes = relativePath.Split('\\');
if (pathes.Length < 2)
{
return "";
}
return pathes[1];
}
#endregion
/// <summary>
/// Uploads the posted file to the specified app name.
/// </summary>
/// <param name="appName">Name of the app.</param>
/// <param name="postedFile"></param>
/// <returns></returns>
public static bool Upload(string appName, HttpPostedFileBase postedFile)
{
string fileExtension = Path.GetExtension(postedFile.FileName).ToLower();
string resourcesDir = GetAbsoluteTemplateFolderPath(appName, FolderType.BinaryResource);
FileExtensions.EnsureDirtectoryExists(resourcesDir);
switch (fileExtension)
{
case ".zip":
using (ZipFile zipFile = ZipFile.Read(postedFile.InputStream))
{
zipFile.ExtractAll(resourcesDir, ExtractExistingFileAction.OverwriteSilently);
}
return true;
//case ".css":
// postedFile.SaveAs(Path.Combine(resourcesDir, postedFile.FileName));
// break;
case ".jpg":
case ".png":
case ".gif":
default:
//string imagesDir = Path.Combine(resourcesDir, FolderType.Images.ToString());
//EnsureDirtectoryExists(imagesDir);
postedFile.SaveAs(Path.Combine(resourcesDir, postedFile.FileName));
break;
}
return false;
}
#region Javascripts
public static IEnumerable<string> GetJavascriptFiles(string appName)
{
string javascriptPath = Path.Combine(GetAbsoluteTemplateFolderPath(appName, FolderType.BinaryResource), JavascriptDirName);
if (!Directory.Exists(javascriptPath))
{
return new string[0];
}
return Directory.GetFiles(javascriptPath, "*.js", SearchOption.AllDirectories).OrderBy(f => f);
}
#endregion
}
}
|