/*
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 Everest.Library.ExtensionMethod;
using Everest.Library.Net;
using System.Net;
using System.Collections.Specialized;
namespace Everest.CmsServices.Services{
public class StartPageService
{
static string StartPageUrl
{
get
{
string startPageUrl = System.Configuration.ConfigurationManager.AppSettings["StartPageUrl"];
if (string.IsNullOrEmpty(startPageUrl))
{
startPageUrl = "http://www.kooboo.com/start";
}
return startPageUrl;
}
}
string DefaultStartPageFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template/StartPages/StartPage.html");
public string GetCachedStartPage(string culture)
{
string startPageHtml = "";
var fileName = GetStartPageFileName(culture);
if (!File.Exists(fileName))
{
fileName = DefaultStartPageFile;
}
if (File.Exists(fileName))
{
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
startPageHtml = fileStream.ReadString();
}
}
return startPageHtml;
}
public string GetLastestStartPage(string culture)
{
HttpStatusCode statusCode;
HtmlRequest htmlRequest = new HtmlRequest();
var parameter = new NameValueCollection();
parameter.Add("culture", culture);
parameter.Add("version", CmsGlobal.KoobooVersion);
try
{
var html = htmlRequest.Request(StartPageUrl, parameter, "GET", out statusCode);
if (statusCode == HttpStatusCode.OK)
{
SaveToFile(html, culture);
return html;
}
}
catch (Exception e)
{
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
return "";
}
private void SaveToFile(string html, string culture)
{
var fileName = GetStartPageFileName(culture);
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read))
{
fileStream.WriteString(html);
}
}
private string GetStartPageFileName(string culture)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template/StartPages/StartPage_" + culture + ".html");
}
}
}
|