⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadmodule.cs

📁 PowUpload的C#源文件,用来做大文件上传的项目
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace ElementIT.PowUpload
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Web;

    internal class UploadModule : IHttpModule
    {
        internal static SortedList colProcessProgress = null;
        internal static SortedList DebugTrace = null;
        private static DateTime LastTimeOfClassesClear;
        private int UploadModuleID = -1;
        private static bool WriteDebug;

        private UploadModule()
        {
            this.UploadModuleID = new Random().Next(0x7fffffff);
            this.TestFrameworkVersion();
            if (colProcessProgress == null)
            {
                colProcessProgress = new SortedList();
            }
            WriteDebug = (bool) Settings.CommonSettings["writeDebugLogs"];
            if (WriteDebug && (DebugTrace == null))
            {
                DebugTrace = new SortedList();
            }
            AddDebugInfo("UploadModule.Constructor() end. UploadModuleID=" + this.UploadModuleID.ToString());
        }

        internal static void AddDebugInfo(string newData)
        {
            if (WriteDebug)
            {
                SortedList list;
                object obj2;
                if (!DebugTrace.ContainsKey("common"))
                {
                    DebugTrace.Add("common", "");
                }
                object obj3 = list[obj2];
                (list = DebugTrace)[obj2 = "common"] = string.Concat(new object[] { obj3, newData, " DATE: ", DateTime.Now.ToLongTimeString(), "\r\n" });
            }
        }

        internal static void AddDebugInfo(string newData, string id)
        {
            if (WriteDebug)
            {
                if (id != null)
                {
                    if (!DebugTrace.ContainsKey(id))
                    {
                        DebugTrace.Add(id, "");
                    }
                    if (newData != null)
                    {
                        SortedList list;
                        object obj2;
                        object obj3 = list[obj2];
                        (list = DebugTrace)[obj2 = id] = string.Concat(new object[] { obj3, newData, " DATE: ", DateTime.Now.ToLongTimeString(), "\r\n" });
                    }
                }
                else
                {
                    AddDebugInfo(newData);
                }
            }
        }

        private void ClearOldClasses()
        {
            try
            {
                if (DateTime.Now.Subtract(LastTimeOfClassesClear).TotalMinutes > 30)
                {
                    LastTimeOfClassesClear = DateTime.Now;
                    if (colProcessProgress != null)
                    {
                        for (int i = colProcessProgress.Count - 1; i >= 0; i--)
                        {
                            ProcessProgress byIndex = (ProcessProgress) colProcessProgress.GetByIndex(i);
                            string key = (string) colProcessProgress.GetKey(i);
                            if (byIndex == null)
                            {
                                AddDebugInfo("UploadModule.ClearOldClasses(). Remove ProcessProgress with id=" + key + " becouse it is null");
                                colProcessProgress.RemoveAt(i);
                            }
                            else if ((byIndex._EndTime != DateTime.MinValue) && (DateTime.Now.Subtract(byIndex._EndTime).TotalMinutes >= 60))
                            {
                                AddDebugInfo("UploadModule.ClearOldClasses(). Dispose ProcessProgress with id=" + key + " becouse total minutes after EndTime>constant");
                                byIndex.Dispose();
                            }
                            else if (((byIndex._EndTime == DateTime.MinValue) && (byIndex._StartTime != DateTime.MinValue)) && (DateTime.Now.Subtract(byIndex._StartTime).TotalMinutes >= 1440))
                            {
                                AddDebugInfo("UploadModule.ClearOldClasses(). Dispose ProcessProgress with id=" + key + " becouse total minutes after StartTime>constant");
                                byIndex.Dispose();
                            }
                        }
                    }
                    if (SingleRequestSettings._personalSettings != null)
                    {
                        for (int j = SingleRequestSettings._personalSettings.Count - 1; j >= 0; j--)
                        {
                            SettingsCollection settingss = (SettingsCollection) SingleRequestSettings._personalSettings.GetByIndex(j);
                            string str2 = (string) SingleRequestSettings._personalSettings.GetKey(j);
                            if (((settingss != null) && (settingss._timeCreated != DateTime.MinValue)) && ((DateTime.Now.Subtract(settingss._timeCreated).TotalMinutes >= 1440) && (colProcessProgress[str2] == null)))
                            {
                                AddDebugInfo("UploadModule.ClearOldClasses(). Dispose SingleRequestSettings._personalSettings with id=" + str2);
                                SingleRequestSettings._personalSettings[str2] = null;
                                SingleRequestSettings._personalSettings.RemoveAt(j);
                            }
                        }
                    }
                    if (DebugTrace != null)
                    {
                        for (int k = DebugTrace.Count - 1; k >= 0; k--)
                        {
                            string strA = (string) DebugTrace.GetKey(k);
                            if ((string.Compare(strA, "common") != 0) && (colProcessProgress[strA] == null))
                            {
                                AddDebugInfo("UploadModule.ClearOldClasses(). Dispose DebugTrace with id=" + strA);
                                DebugTrace[strA] = null;
                                DebugTrace.RemoveAt(k);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void CloseConnection(HttpApplication app)
        {
            try
            {
                this.GetWorkerRequest(app).CloseConnection();
            }
            catch
            {
            }
        }

        public void Dispose()
        {
        }

        private void fileUpload_BeginRequest(object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication) source;
            if (this.IsValidMethod(application))
            {
                long contentLength = this.GetContentLength(application.Context.Request);
                if ((contentLength > 0) && this.IsValidPost(application))
                {
                    AddDebugInfo("UploadModule.BeginRequest: START NEW REQUEST. UploadModuleID=" + this.UploadModuleID.ToString() + "colProcessProgress count=" + colProcessProgress.Count.ToString() + " DebugTrace count=" + ((DebugTrace == null) ? "0" : DebugTrace.Count.ToString()));
                    string uploadID = this.GetUploadID(application.Context);
                    try
                    {
                        if (this.IsUploadPage(application, uploadID))
                        {
                            AddDebugInfo("UploadModule.BeginRequest. Request looks correct. Method: " + application.Context.Request.ServerVariables["REQUEST_METHOD"] + ". Content-Type:" + application.Context.Request.ServerVariables["CONTENT_TYPE"] + " Content length: " + contentLength.ToString() + " page: " + application.Request.Path + " id=" + uploadID);
                            if (colProcessProgress[uploadID] != null)
                            {
                                AddDebugInfo("UploadModule.BeginRequest. colProcessProgress[id] already exist. Dispose it");
                                ((ProcessProgress) colProcessProgress[uploadID]).Dispose();
                            }
                            this.ClearOldClasses();
                            if (this.IsValidPost(application))
                            {
                                this.PostRequest(application, uploadID, contentLength);
                            }
                            AddDebugInfo("UploadModule.BeginRequest end", uploadID);
                        }
                    }
                    catch (Exception exception)
                    {
                        Exception exception2;
                        if (exception.InnerException == null)
                        {
                            exception2 = new Exception("UploadModule exception catch: " + exception.Message, exception);
                        }
                        else
                        {
                            exception2 = exception;
                        }
                        this.WriteLog(exception2.ToString(), uploadID);
                        this.SetLastError(exception2, uploadID);
                        AddDebugInfo("UploadModule.BeginRequest: Error occured:" + exception2.ToString(), uploadID);
                        if ((bool) Settings.GetValue(uploadID, "throwErrorsImmediately"))
                        {
                            throw exception2;
                        }
                    }
                    finally
                    {
                        this.WriteDebugTrace(uploadID);
                    }
                }
            }
        }

        private void fileUpload_EndRequest(object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication) source;
            string key = null;
            try
            {
                key = this.GetUploadID(application.Context);
            }
            catch
            {
            }
            try
            {
                if ((((key != null) && (colProcessProgress != null)) && (colProcessProgress.ContainsKey(key) && this.IsValidPost(application))) && (this.IsUploadPage(application, key) && (((ProcessProgress) colProcessProgress[key])._Files != null)))
                {
                    AddDebugInfo("UploadModule.fileUpload_EndRequest(): Dispose _Files id=" + key);
                    ((ProcessProgress) colProcessProgress[key])._Files.Dispose();
                }
            }
            catch
            {
            }
        }

        private void fileUpload_Error(object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication) source;
            string uploadID = this.GetUploadID(application.Context);
            this.WriteLog(application.Context.Error.ToString(), uploadID);
            this.SetLastError(application.Context.Error, uploadID);
        }

        ~UploadModule()
        {
            AddDebugInfo("UploadModule.Finalize(): UploadModuleID=" + this.UploadModuleID.ToString() + " Stack trace for current level: " + Environment.StackTrace);
            AddDebugInfo("UploadModule.Finalize(): Dispose and clear colProcessProgress");
            if (colProcessProgress != null)
            {
                for (int i = colProcessProgress.Count - 1; i >= 0; i--)
                {
                    ProcessProgress byIndex = (ProcessProgress) colProcessProgress.GetByIndex(i);
                    if ((byIndex != null) && (byIndex._UploadModuleID == this.UploadModuleID))
                    {
                        byIndex.Dispose();
                    }
                }
            }
        }

        private long GetContentLength(HttpRequest request)
        {
            long contentLength;
            try
            {
                contentLength = long.Parse(request.Headers[HttpWorkerRequest.GetKnownRequestHeaderName(11)]);
            }
            catch
            {
                try
                {
                    contentLength = long.Parse(request.Headers["XContent-Length"]);
                }
                catch
                {
                    contentLength = request.ContentLength;
                }
            }
            return contentLength;
        }

        private IUploadHandle GetUploadHandle(string id)
        {
            try
            {
                object obj2 = Settings.GetValue(id, "uploadHandleObject");
                if (obj2.GetType().GetInterface("IUploadHandle", true) == null)
                {
                    return null;
                }
                return (IUploadHandle) obj2;
            }
            catch
            {
                return null;
            }
        }

        private string GetUploadID(HttpContext context)
        {
            string str = (string) Settings.GetValue(null, "uploadIDQueryField");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -