using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Globalization;
using Subtext.Framework;
using Subtext.Framework.Configuration;
using Subtext.Framework.Web.HttpModules;

namespace Subtext.Web.Admin.UserControls
{
    public partial class attachment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Uploadbtn_Click(object sender, EventArgs e)
        {
            string uploadFileType = attachmentUpload.PostedFile.FileName.Substring(attachmentUpload.PostedFile.FileName.LastIndexOf(".") + 1);
            UploadFile upload = new UploadFile();
            upload.Path = FileDirectory();
            upload.Sizes = Config.Settings.UploadFileSize;
            upload.FileType = Config.Settings.UploadFileType;
            upload.PostedFile = attachmentUpload.PostedFile;
            string uploadFileUrl = upload.Upload();
            string hostUrl = HostUrl();
            addUploadItem(uploadFileType, hostUrl + uploadFileUrl);
            Response.Write("<div style='padding:6px'><a href='attachment.aspx'>文件上传成功,请返回继续上传.</a></div>");
        }

        #region 插入文件到FCKeditor
        /// <summary>
        /// 根据文件类型和地址，插入到FCKeditor
        /// </summary>
        /// <param name="type"></param>
        /// <param name="path"></param>
        private void addUploadItem(string type, string path)
        {
            switch (type.ToLower())
            {
                case "gif":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + " rel=lightbox><img src=" + path + " border=0 /></a>');</script>");
                    break;
                case "jpg":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + " rel=lightbox><img src=" + path + " border=0 /></a>');</script>");
                    break;
                case "png":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + " rel=lightbox><img src=" + path + " border=0 /></a>');</script>");
                    break;
                case "bmp":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + " rel=lightbox><img src=" + path + " border=0 /></a>');</script>");
                    break;
                case "jpeg":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + " rel=lightbox><img src=" + path + " border=0 /></a>');</script>");
                    break;
                case "mp3":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 id=MediaPlayer width=450 height=70><param name=howStatusBar value=-1><param name=AutoStart value=False><param name=Filename value=" + path + "></object>');</script>");
                    break;
                case "wma":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95  id=MediaPlayer width=450 height=70><param name=howStatusBar value=-1><param name=AutoStart value=False><param name=Filename value=" + path + "></object>');</script>");
                    break;
                case "rm":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=300><param name=SRC value=" + path + " /><param name=CONTROLS VALUE=ImageWindow /><param name=CONSOLE value=one /><param name=AUTOSTART value=true /><embed src=" + path + " nojava=true controls=ImageWindow console=one width=400 height=300></object><br/><object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=32 /><param name=CONTROLS value=StatusBar /><param name=AUTOSTART value=true /><param name=CONSOLE value=one /><embed src=" + path + " nojava=true controls=StatusBar console=one width=400 height=24 /></object><br/><object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=32 /><param name=CONTROLS value=ControlPanel /><param name=AUTOSTART value=true /><param name=CONSOLE value=one /><embed src=" + path + " nojava=true controls=ControlPanel console=one width=400 height=24 autostart=true loop=false /></object>');</script>");
                    break;
                case "rmvb":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=300><param name=SRC value=" + path + " /><param name=CONTROLS VALUE=ImageWindow /><param name=CONSOLE value=one /><param name=AUTOSTART value=true /><embed src=" + path + " nojava=true controls=ImageWindow console=one width=400 height=300></object><br/><object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=32 /><param name=CONTROLS value=StatusBar /><param name=AUTOSTART value=true /><param name=CONSOLE value=one /><embed src=" + path + " nojava=true controls=StatusBar console=one width=400 height=24 /></object><br/><object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA width=400 height=32 /><param name=CONTROLS value=ControlPanel /><param name=AUTOSTART value=true /><param name=CONSOLE value=one /><embed src=" + path + " nojava=true controls=ControlPanel console=one width=400 height=24 autostart=true loop=false /></object>');</script>");
                    break;
                case "ra":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA id=RAOCX width=450 height=60><param name=_ExtentX value=6694><param name=_ExtentY value=1588><param name=AUTOSTART value=true><param name=SHUFFLE value=0><param name=PREFETCH value=0><param name=NOLABELS value=0><param name=SRC value=" + path + "><param name=CONTROLS value=StatusBar,ControlPanel><param name=LOOP value=0><param name=NUMLOOP value=0><param name=CENTER value=0><param name=MAINTAINASPECT value=0><param name=BACKGROUNDCOLOR value=#000000><embed src=" + path + " width=450 autostart=true height=60></embed></object>');</script>");
                    break;
                case "asf":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95 codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 type=application/x-oleobject standby=Loading... width=400 height=300><param name=FileName VALUE=" + path + " /><param name=ShowStatusBar value=-1 /><param name=AutoStart value=true /><embed type=application/x-mplayer2 pluginspage=http://www.microsoft.com/Windows/MediaPlayer/ src=" + path + " autostart=true width=400 height=300 /></object>');</script>");
                    break;
                case "avi":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95 codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 type=application/x-oleobject standby=Loading... width=400 height=300><param name=FileName VALUE=" + path + " /><param name=ShowStatusBar value=-1 /><param name=AutoStart value=true /><embed type=application/x-mplayer2 pluginspage=http://www.microsoft.com/Windows/MediaPlayer/ src=" + path + " autostart=true width=400 height=300 /></object>');</script>");
                    break;
                case "wmv":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object classid=clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95 codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 type=application/x-oleobject standby=Loading... width=400 height=300><param name=FileName VALUE=" + path + " /><param name=ShowStatusBar value=-1 /><param name=AutoStart value=true /><embed type=application/x-mplayer2 pluginspage=http://www.microsoft.com/Windows/MediaPlayer/ src=" + path + " autostart=true width=400 height=300 /></object>');</script>");
                    break;
                case "swf":
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=400 height=300><param name=movie value=" + path + " /><param name=quality value=high /><param name=AllowScriptAccess value=never /><embed src=" + path + " quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=400 height=300 /></object>');</script>");
                    break;
                default:
                    Response.Write(@"<script>parent.FCKeditorAPI.GetInstance('ctl00_pageContent_Editor_Edit_richTextEditor_richTextEditor').InsertHtml('<a href=" + path + ">点击下载此文件</a>');</script>");
                    break;
            }
        }
        #endregion

        /// <summary>
        /// 上传文件目录
        /// </summary>
        /// <returns></returns>
        static protected string FileDirectory() 
        {
            BlogRequest blogRequest = (BlogRequest)HttpContext.Current.Items["Subtext__CurrentRequest"];
            string webApp = HttpContext.Current.Request.ApplicationPath;
            if (webApp.Length <= 1)
            {
                webApp = "";
            }
            string subfolder = blogRequest.Subfolder;
            string virtualPath = string.Format(CultureInfo.InvariantCulture, "images/{0}{1}/month_{2}", Regex.Replace(blogRequest.Host + webApp, @"\:|\.", "_"), subfolder, DateTime.Now.ToString("yyMM"));
            return virtualPath;
        }

        /// <summary>
        /// 网站地址+虚拟目录
        /// </summary>
        /// <returns></returns>
        static protected string HostUrl()
        {
            BlogConfigurationSettings settings = Config.Settings;
            BlogRequest blogRequest = (BlogRequest)HttpContext.Current.Items["Subtext__CurrentRequest"];
            string webApp = HttpContext.Current.Request.ApplicationPath;
            if (webApp.Length <= 1)
            {
                webApp = "";
            }
            string formattedHost = GetFormattedHost(blogRequest.Host, settings.UseWWW) + webApp;
            return formattedHost;
        }
        
        /// <summary>
        /// 网站地址
        /// </summary>
        /// <param name="host"></param>
        /// <param name="useWWW"></param>
        /// <returns></returns>
        static protected string GetFormattedHost(string host, bool useWWW)
        {
            if (useWWW)
            {
                return HttpContext.Current.Request.Url.Scheme + "://www." + host;
            }
            else
            {
                return HttpContext.Current.Request.Url.Scheme + "://" + host;
            }
        }
    }
}