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.IO;
using System.Text.RegularExpressions;
using Spaces;
public partial class System_EditDoc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.Name == string.Empty)
        {
           // SpacesContext.RedirectToMessage(MessageType.LoginBanned);
            Response.Write("您没有登录系统! ");
            Response.End();
        }
        string opt=Request.Form["OptType"];
        if (opt==null||opt==string.Empty)
            BindDoc();

        switch (opt)
        { 
            case "save":
                SaveDoc();
                break;
            case "saveas":
                SaveAs();
                break;
            case "encrypt":
                Encrypt();
                break;
        
        }
       
    }
    void Encrypt()
    {
        string type = Request.Form["m"];
        string key = Request.Form["DesKey"];
        UserFile uf = Tools.GetCurrentFile();
        string path = Tools.GetFilePath(Account, uf);
        string strcode = Request.Form["content"];
      
        System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(936);
        try
        {
            if (type == "1")
            {
                strcode = Des.Encrypt(strcode, key);
            }
            else
            {
                try
                {
                    strcode = Des.Decrypt(strcode, key);
                }
                catch { }
            }
            using (StreamWriter sw = new StreamWriter(path, false, resEncoding))
            {
                sw.Write(strcode);
                sw.Close();
            }
        }
        catch (IOException) { }
        this.content.Value = strcode;
    }
    void SaveAs()
    {
        if (Account != User.Identity.Name)
        {

            Response.Write("参数错误! ");
            Response.End();
        }
        UserFile uf = Tools.GetCurrentFile();
        string filepath = Tools.GetFileDirectory(Account, uf);
        string strcode = Request.Form["content"];
        string newfileName = Request.Form["FileAsName"];
     
        if (newfileName == null || newfileName.Trim() == string.Empty ||!Utils.CheckFileName(newfileName))
        {
            Response.Redirect(Globals.GetSiteUrls().UserDisk(Account, DirectoryID));
        }
   
        System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(936);
        try
        {
            FileInfo f = new FileInfo(filepath+"\\"+uf.SourceName);//
            using (StreamWriter sw = new StreamWriter(f.CreateText().BaseStream, resEncoding))
            {
                sw.Write(strcode);
                sw.Close();
            }

        }
        catch (IOException) { }
        uf.Name = newfileName;
        Files.UpdateFile(uf);
        Response.Redirect(Globals.GetSiteUrls().UserDisk(Account, DirectoryID));
    }
    void SaveDoc()
    {
        if (Account != User.Identity.Name)
        {

            Response.Write("参数错误! ");
            Response.End();
        }
        UserFile uf = Tools.GetCurrentFile();
        string filepath = Tools.GetFilePath(Account, uf);
        string strcode = Request.Form["content"];
        System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(936);
        try
        {
            using (StreamWriter sw = new StreamWriter(filepath, false, resEncoding))
            {
                sw.Write(strcode);
                sw.Close();
            }
        }
        catch (IOException) { }
        Response.Redirect(Globals.GetSiteUrls().UserDisk(Account, DirectoryID));
    }
    private void BindDoc()
    {
      
       // string filepath = Tools.GetFilePath();
        //this.filepath.Value = FileName;
        if (Account != User.Identity.Name)
        {
     
            Response.Write("参数错误! ");
            Response.End();
        }
        UserFile uf = Tools.GetCurrentFile();
        string filePath = Tools.GetFilePath(Account, uf);
        FileInfo file = new FileInfo(filePath);
        this.filepath.Value = uf.Name;
        if (!file.Exists)
        {
           // this.Response.Write(filePath);
            Response.Redirect(Globals.GetSiteUrls().UserDisk(Account, DirectoryID));
            return;
        }
     
        System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(936);
        StreamReader sr = new StreamReader(file.OpenText().BaseStream, resEncoding);
        content.Value = sr.ReadToEnd();
        sr.Close();
        sr.Dispose();
    }

    string account = string.Empty;
    string currentDirectory = string.Empty;
    /// <summary>
    /// 用户帐号
    /// </summary>
    public string Account
    {
        get
        {
            if (account == string.Empty)
                account = SpacesContext.Current.Account;
            return account;

        }
    }
    /// <summary>
    /// 当前目录
    /// </summary>
    public string CurrentDirectory
    {
        get
        {

            if (currentDirectory == string.Empty)
            {
                currentDirectory = SpacesContext.Current.CurrentDirectory;
                currentDirectory = Regex.Replace(currentDirectory, "[\\,]+", ",", RegexOptions.IgnoreCase);
            }
            return currentDirectory;
        }
    }

    string fileName = string.Empty;
    public string FileName
    {
        get
        {
            if (fileName == string.Empty)
                fileName = SpacesContext.Current.FileName;
            return fileName;
        }
    }
    public int DirectoryID
    {
        get { return SpacesContext.Current.DirectoryID; }
    }
}
