/*
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.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Json;
using Everest.Library.Net;
using Everest.CmsServices.Extension.Module;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
namespace Everest.CmsServices.Controllers{
[PermissionFilter(Permission = FolderType.KoobooStore)]
public class StoreController : CmsExtController
{
#region DownloadManager
internal class StoreDownloadManager : DownloadManager<KoobooStore>
{
public override string GetSavedFilePath(KoobooStore downloadObject)
{
var koobooStore = (KoobooStore)downloadObject;
var fileName = Path.GetFileName(koobooStore.DownloadLink);
if (koobooStore.Type == "Modules")
{
return Path.Combine(Everest.CmsServices.Extension.Module.ModuleManager.GetModuleBaseDirectory(), fileName);
}
else
{
//var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SitePackage");
return Path.Combine((new SitePackageManager()).BasePath, fileName);
}
}
protected override void DownloadComplete(KoobooStore downloadObject)
{
var koobooStore = (KoobooStore)downloadObject;
if (koobooStore.Type == "Modules")
{
var savePath = GetSavedFilePath(koobooStore);
string moduleName = Everest.Library.ExtensionMethod.StringExtensions.ReplaceToValidUrl(Path.GetFileNameWithoutExtension(savePath));
using (FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
ModuleManager.UploadModule(moduleName, fs, true);
}
if (System.IO.File.Exists(savePath))
{
System.IO.File.Delete(savePath);
}
}
}
protected override void SetStatus(KoobooStore downloadObject)
{
var koobooStore = (KoobooStore)downloadObject;
if (koobooStore.Type == "Modules")
{
var fileName = Path.GetFileNameWithoutExtension(koobooStore.DownloadLink);
var moduleDir = Path.Combine(Everest.CmsServices.Extension.Module.ModuleManager.GetModuleBaseDirectory(), fileName);
if (Directory.Exists(moduleDir))
{
downloadObject.Status = DownloadStatus.Downloaded;
}
else
{
downloadObject.Status = DownloadStatus.NotDownload;
}
}
else
{
string updateFile = GetSavedFilePath(downloadObject);
if (System.IO.File.Exists(updateFile))
{
downloadObject.Status = DownloadStatus.Downloaded;
}
}
}
}
private StoreDownloadManager downloadManager = new StoreDownloadManager();
#endregion
static string KoobooStoreUrl
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["KoobooStoreUrl"];
}
}
private static void IncreaseDownloadCount(Guid uuid)
{
HtmlRequest htmlRequest = new HtmlRequest();
System.Net.HttpStatusCode statusCode;
NameValueCollection value = new NameValueCollection();
value["templateid"] = uuid.ToString();
htmlRequest.Request(KoobooStoreUrl + "/IncreaseDownloadCount", value, "POST", out statusCode);
}
public ActionResult GetList(string ResourceName)
{
HtmlRequest htmlRequest = new HtmlRequest();
System.Net.HttpStatusCode statusCode;
NameValueCollection requestValues = new NameValueCollection(Request.Form);
requestValues["Version"] = CmsGlobal.KoobooVersion;
var jsonString = htmlRequest.Request(KoobooStoreUrl + "/GetStores", requestValues, "POST", out statusCode);
var data = JSONHelper.DeserializeJSON<ExtJsonReaderObject<KoobooStore>>(jsonString);
downloadManager.SetDownloadStatus(data.Rows);
return this.Json(data);
}
public ActionResult Download(KoobooStore storeObject)
{
var started = downloadManager.Download(storeObject);
if (started)
{
IncreaseDownloadCount(storeObject.UUID);
}
return Json(started);
}
public ActionResult AbortDownload(KoobooStore storeObject)
{
return Json(downloadManager.Cancel(storeObject));
}
}
}
|