/*
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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Everest.Library;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Extjs.Tree;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Ionic.Zip;
using Everest.Library.Web;
namespace Everest.CmsServices.Controllers{
/// <summary>
///
/// </summary>
public class BinaryResourceController : CmsExtController
{
/// <summary>
/// Gets the folder id.
/// </summary>
/// <value>The folder id.</value>
public Guid? FolderUUID
{
get
{
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["folderUUID"]))
{
return new Guid(Request.Form["folderUUID"]);
}
return null;
}
}
#region GetDirectoryNodes
/// <summary>
/// Gets the directory nodes.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult GetDirectoryNodes(string directory, string id, string schema, string application, string dataUrl)
{
string physicalPath = Server.MapPath(directory);
List<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
//get the files in the base folder
if (FolderUUID != null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
//Cms_Folder folder = CachedData.GetFolder(FolderUUID.Value);
IEnumerable<string> baseApps = CachedData.GetBaseApplications(application);
foreach (var app in baseApps)
{
Cms_Folder binaryResourceFolder = CachedData.GetFolder(app, FolderType.BinaryResource);
treeNodes.AddRange(InternalGetDirectoryNodes(CmsGlobal.ToAbsolutePath(binaryResourceFolder.FolderPath), id, schema, application, dataUrl, app));
}
}
else
{
treeNodes.AddRange(InternalGetDirectoryNodes(physicalPath, id, schema, application, dataUrl, application));
}
return Json(treeNodes);
}
/// <summary>
/// Internals the get directory nodes.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="id">The id.</param>
/// <param name="schema">The schema.</param>
/// <param name="application">The application.</param>
/// <param name="dataUrl">The data URL.</param>
/// <param name="folderApplication">The folder application.</param>
/// <returns></returns>
private List<Dictionary<string, object>> InternalGetDirectoryNodes(string directory, string id, string schema, string application, string dataUrl, string folderApplication)
{
List<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
string directoryPath = GetAbsolutePath(directory);
if (!Directory.Exists(directoryPath))
{
return new List<Dictionary<string, object>>();
}
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
foreach (var dir in directoryInfo.GetDirectories())
{
TreeNode treeNode = new TreeNode();
treeNode.Text = dir.Name;
if (folderApplication != application)
{
treeNode.Text += string.Format("({0})", folderApplication);
}
treeNode.ParentNode = id;
treeNode.AddAttribute("schema", schema);
treeNode.AddAttribute("directory", UrlConvertor.AbsolutePathToRelativeUrl(dir.FullName));
//treeNode.AddAttribute("formType", item.FormType);
treeNode.AddAttribute("folderType", FolderType.BinaryResourceFolder.ToString());
treeNode.AddAttribute("application", application);
treeNode.AddAttribute("dataUrl", dataUrl);
treeNode.AddAttribute("folderApplication", folderApplication);
treeNode.AddAttribute("isInherited", folderApplication != application);
treeNodes.Add(treeNode.Attributes);
}
return treeNodes;
}
#endregion
#region FileManager
/// <summary>
/// Gets the absolute path.
/// </summary>
/// <param name="relativePath">The relative path.</param>
/// <returns></returns>
private string GetAbsolutePath(string relativePath)
{
string absolutePath = Path.Combine(CmsGlobal.BaseDirPath, relativePath);
return absolutePath;
}
/// <summary>
/// Uploads the file.
/// </summary>
/// <param name="directory">The directory.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult UploadFile(string directory)
{
JsonResultData resultData = new JsonResultData();
string absolutePath = Server.MapPath(directory);
foreach (var key in Request.Files.AllKeys)
{
string filePath = Path.Combine(absolutePath, Request.Files[key].GetFileName());
Request.Files[key].SaveAs(filePath);
}
ContentResult result = new ContentResult();
result.Content = resultData.ToJSON();
return result;
}
/// <summary>
/// Gets the files.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="application">The application.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult GetFiles(string directory, string application)
{
var physicalDir = Server.MapPath(directory);
List<FolderFile> dirDatas = new List<FolderFile>();
List<FolderFile> folderFiles = new List<FolderFile>();
if (this.Request.Form["noApplication"].ToLower() == "true")
{
folderFiles.AddRange(InternalGetFiles(physicalDir, ""));
dirDatas.AddRange(InternalGetDirectories(physicalDir, ""));
}
else if (FolderUUID != null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
//Cms_Folder folder = CachedData.GetFolder(FolderUUID.Value);
IEnumerable<string> baseApps = CachedData.GetBaseApplications(application);
foreach (var app in baseApps)
{
Cms_Folder binaryResourceFolder = CachedData.GetFolder(app, FolderType.BinaryResource);
folderFiles.AddRange(InternalGetFiles(CmsGlobal.ToAbsolutePath(binaryResourceFolder.FolderPath), app));
dirDatas.AddRange(InternalGetDirectories(CmsGlobal.ToAbsolutePath(binaryResourceFolder.FolderPath), app));
}
}
else
{
//fix aplication parameter by directory parameter
var appName = TemplateFileManager.ParseAppNameFromPath(physicalDir);
folderFiles.AddRange(InternalGetFiles(physicalDir, appName));
dirDatas.AddRange(InternalGetDirectories(physicalDir, appName));
}
IEnumerable<FolderFile> query = OrderByRequest(folderFiles);
dirDatas = OrderByRequest(dirDatas).ToList();
var queryList = query.ToList();
queryList.InsertRange(0, dirDatas);
query = queryList;
string fileName = Request.Form["FileName"];
if (!StringExtensions.IsNullOrEmptyTrim(fileName))
{
query = query.Where(f => f.FileName.ToLower().Contains(fileName.ToLower()));
}
queryList = query.ToList();
queryList.Insert(0, new FolderFile()
{
Application = "...",
FileName = "...",
IsDirectory = true,
FilePath = string.Empty,
FileSize = 0,
Body = string.Empty,
IsUpwards = true
});
query = queryList;
ExtJsonReaderObject jsonReader = new ExtJsonReaderObject(query, query.Count());
return Json(jsonReader);
}
/// <summary>
/// Get Directories
/// </summary>
/// <param name="directory"></param>
/// <param name="application"></param>
/// <returns></returns>
private IEnumerable<FolderFile> InternalGetDirectories(string directory, string application)
{
List<FolderFile> folderFiles = new List<FolderFile>();
DirectoryInfo directoryInfo = new DirectoryInfo(GetAbsolutePath(directory));
if (directoryInfo.Exists)
{
DirectoryInfo[] dirs = directoryInfo.GetDirectories();
foreach (var dir in dirs)
{
folderFiles.Add(new FolderFile()
{
Application = application,
FileName = dir.Name,
FilePath = UrlConvertor.AbsolutePathToRelativeUrl(dir.FullName),
FileSize = 0,
IsDirectory = true
});
}
}
return folderFiles;
}
/// <summary>
/// Gets the files.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="application">The application.</param>
/// <returns></returns>
private IEnumerable<FolderFile> InternalGetFiles(string directory, string application)
{
List<FolderFile> folderFiles = new List<FolderFile>();
DirectoryInfo directoryInfo = new DirectoryInfo(GetAbsolutePath(directory));
if (directoryInfo.Exists)
{
FileInfo[] files = directoryInfo.GetFiles();
foreach (var file in files)
{
folderFiles.Add(new FolderFile()
{
Application = application,
FileName = file.Name,
FilePath = UrlConvertor.AbsolutePathToRelativeUrl(file.FullName),
FileSize = file.Length,
IsDirectory = false
});
}
}
return folderFiles;
}
/// <summary>
/// Gets the style.
/// </summary>
/// <returns></returns>
[ValidateInput(false)]
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult GetBody()
{
string path = Server.MapPath(Request.Form["FilePath"]);
string url = Request.Form["Url"];
return GetFileBody(path, url);
}
/// <summary>
/// Gets the file body.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="url">The URL.</param>
/// <returns></returns>
private ActionResult GetFileBody(string path, string url)
{
JsonResultData resultData = new JsonResultData();
object data = GetFileBodyObject(path, url);
resultData.data = data;
return Json(resultData);
}
private object GetFileBodyObject(string path, string url)
{
string body = string.Empty;
using (FileStream fileStream = new FileStream(GetAbsolutePath(path), FileMode.Open, FileAccess.Read))
{
body = fileStream.ReadString();
}
object data = new
{
FilePath = path,
Url = url,
Body = body
};
return data;
}
/// <summary>
/// Submits the body.
/// </summary>
/// <returns></returns>
[System.Web.Mvc.ValidateInput(false)]
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult SubmitBody(bool closeForm)
{
JsonResultData resultData = new JsonResultData();
string path = Request.Form["oldData.FilePath"];
string url = Request.Form["oldData.Url"];
string body = Request.Form["Body"];
using (FileStream fileStream = new FileStream(GetAbsolutePath(path), FileMode.Create, FileAccess.Write))
{
fileStream.WriteString(body);
}
if (!closeForm && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetFileBodyObject(path, url);
}
return Json(resultData);
}
/// <summary>
/// Deletes the file.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult DeleteFile(string[] FilePath)
{
foreach (var path in FilePath)
{
if (!StringExtensions.IsNullOrEmptyTrim(path))
{
string aPath = Server.MapPath(path);
if (System.IO.File.Exists(aPath))
{
System.IO.File.Delete(aPath);
}
else
{
if (StringExtensions.IsNullOrEmptyTrim(Path.GetExtension(aPath)) || Directory.Exists(aPath))
{
Directory.Delete(aPath, true);
}
}
}
}
return Json(new JsonResultData());
}
/// <summary>
/// Deletes the directory.
/// </summary>
/// <param name="directory">The directory.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult DeleteDirectory(string directory)
{
JsonResultData resultData = new JsonResultData();
Directory.Delete(Server.MapPath(directory), true);
return Json(resultData);
}
/// <summary>
/// Adds the directory.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="parentDir">The parent dir.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult AddDirectory(string parentDir, string directory)
{
JsonResultData resultData = new JsonResultData();
string absoluteDirectory = Path.Combine(Server.MapPath(parentDir), directory);
Directory.CreateDirectory(absoluteDirectory);
return Json(resultData);
}
/// <summary>
/// Unzips the specified file path.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult Unzip(string filePath)
{
JsonResultData resultData = new JsonResultData();
string absoluteFilePath = Server.MapPath(filePath);
using (ZipFile zipFile = ZipFile.Read(absoluteFilePath))
{
zipFile.ExtractAll(Path.GetDirectoryName(absoluteFilePath), ExtractExistingFileAction.OverwriteSilently);
}
return Json(resultData);
}
/// <summary>
/// Copy Files or Directory
/// </summary>
/// <param name="FilePath">The file path</param>
/// <param name="IsDirectory">Is Directory or File</param>
/// <param name="SourceApp">Source Application</param>
/// <param name="TargetApp">Target Appliction</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.BinaryResource)]
public ActionResult Copy(string filePath, Nullable<bool> IsDirectory, string sourceApp, string TargetApp)
{
string sourceAbsoluteFilePath = Server.MapPath(filePath);
string targetAbsoluteFilePath= Server.MapPath(filePath.Replace("template/" + sourceApp+"/","template/"+TargetApp+"/",true));
if (IsDirectory.Value)
{
if (Directory.Exists(sourceAbsoluteFilePath))
{
FileExtensions.CopyDirectory(sourceAbsoluteFilePath, targetAbsoluteFilePath, true);
}
}
else
{
if (System.IO.File.Exists(sourceAbsoluteFilePath))
{
string targetFileDir = Path.GetDirectoryName(targetAbsoluteFilePath);
if (!Directory.Exists(targetFileDir))
Directory.CreateDirectory(targetFileDir);
System.IO.File.Copy(sourceAbsoluteFilePath, targetAbsoluteFilePath, true);
}
}
return Json(new JsonResultData());
}
#endregion
#region Combobox
public ActionResult GetThemes(string application, string baseApplication)
{
if (StringExtensions.IsNullOrEmptyTrim(application))
{
application = baseApplication;
}
var app = CachedData.GetApplication(application);
IEnumerable<string> themes = null;
if (app == null)
{
themes = TemplateFileManager.GetThemes(CmsGlobal.RootApplicationName);
}
else
{
themes = TemplateFileManager.GetThemes(application);
}
var items = themes.ToComboboxItems(s => s, s => s, true);
return Json(new ExtJsonReaderObject(items, items.Count));
}
#endregion
}
}
|