using System;
using System.IO;
using System.Web.UI;
using System.Web;

namespace ExWeb
{
    public partial class Mod_Upload : Mod
    {
        public static string Help_Description =
            "Upload einer Datei";
        public static string[][] Help_Attributes = new string[][]
        {
            // Attribut, Wert, DefaultWert, Optional, Hilfe
            new string[] {"PathToFiles","path", @"..\..\..\db\upload\","opt","Upload-Verzeichnis","PathToFiles=\"/upload/\""},
            new string[] {"PasswordU","string","123456","opt","Passwort","Passwort=\"DeinPasswort\""},
            new string[] {"UploadLength","int","4MB","opt","Erlaubte Dateilänge","UploadLength=\"10000000\""},
        };
        string[] strExtensionsAllowed = { ".jpg", ".gif", ".png", ".bmp", ".pdf", ".zip", ".doc", ".htm", ".txt" };

        protected string _PathToFiles = @"..\..\..\db\upload\";
        public string PathToFiles
        {
            set
            {
                if (value!="") _PathToFiles = value;
            }
        }
        protected string _PasswordU = "123456";
        public string PasswordU
        {
            set
            {
                if (value != "") _PasswordU = value;
            }
        }
        protected int _UploadLength = 4096 * 1024;
        public string UploadLength
        {
            set
            {
                int.TryParse(value, out _UploadLength);
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            SetPanelLayout(this.Controls);
        }
        protected new void Page_Load(object sender, EventArgs e)
        {
            Page.LoadComplete += new EventHandler(Page_LoadComplete);

            string[] temp = GetConfigStringList("Ext_Upload", true);
            if (temp != null) strExtensionsAllowed = temp;
  
            _PathToFiles = Request.ServerVariables["APPL_PHYSICAL_PATH"] + _PathToFiles;
            if (!Directory.Exists(_PathToFiles))
                Directory.CreateDirectory(_PathToFiles);
            if (_UploadLength >= 4096 * 1024) _UploadLength = 4096 * 1024;
            Label_Description.Text = "Maximale Größe: " + _UploadLength / 1024 + " kB";
            Label_Description.Text += "<br/>Erlaubt: ";
            for (int i = 0; i < strExtensionsAllowed.Length; i++)
            {
                Label_Description.Text += strExtensionsAllowed[i] + " ";
            }
        }
        void Submit_Upload_ServerClick(object sender, System.EventArgs e)
        {
            if (_PasswordU != Input_Password.Value)
            {
                Label_Upload.Text = "Falsches Passwort";
                return;
            }
            if ((File_Upload.PostedFile != null) && (File_Upload.PostedFile.ContentLength > 0))
            {
                if (File_Upload.PostedFile.ContentLength > _UploadLength)
                {
                    Label_Upload.Text = "Datei zu gro߸<br/>";
                    return;
                }
                string fn = System.IO.Path.GetFileName(File_Upload.PostedFile.FileName);
                string strExtension = Path.GetExtension(fn).ToLower();
                bool AllowedFileType = false;
                for (int i = 0; i < strExtensionsAllowed.Length; i++)
                {
                    if (strExtension == strExtensionsAllowed[i])
                    {
                        AllowedFileType = true;
                        break;
                    }
                }
                if (!AllowedFileType)
                {
                    Label_Upload.Text = "Dateityp " + strExtension + " nicht erlaubt<br/>";
                    return;
                }
                try
                {
                    File_Upload.PostedFile.SaveAs(_PathToFiles + @"\" + fn);
                    Label_Upload.Text = "Datei <b>" + File_Upload.PostedFile.FileName + "</b> erfolgreich am Server gespeichert.";
                }
                catch (Exception ex)
                {
                    Label_Upload.Text = "Fehler: " + ex.Message;
                }
            }
            else
            {
                Label_Upload.Text = "Bitte eine Datei zum Upload auswählen.";
            }
        }
    }
}