示例#1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="man"></param>
        /// <param name="cmd"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override bool OnStream(ManagerEngine man, string cmd, NameValueCollection input)
        {
            HttpContext context = HttpContext.Current;
            string path, contentType;
            ManagerConfig config;
            IFile file;

            switch (cmd) {
                case "streamFile":
                    // Get input
                    path = man.DecryptPath(input["path"]);

                    // Get and stream file
                    file = man.GetFile(path);
                    config = file.Config;

                    if (file is LocalFile) {
                        string url = PathUtils.RemoveTrailingSlash(config["preview.urlprefix"]) + man.ConvertPathToURI(file.AbsolutePath) + config["preview.urlsuffix"];

                        // Pass through rnd
                        if (input["rnd"] != null)
                            context.Response.Redirect(url + (url.IndexOf('?') == -1 ? ("?rnd=" + input["rnd"]) : ("&rnd" + input["rnd"])), true);

                        context.Response.Redirect(url, true);
                    } else {
                        // Verify that we can stream the file
                        if (!man.VerifyFile(file, "stream"))
                            throw new ManagerException("Requested resource could not be found. Or access was denied.");

                        // Set content type
                        contentType = man.MimeTypes[PathUtils.GetExtension(path).ToLower()];
                        if (contentType != null)
                            context.Response.ContentType = contentType;

                        IOUtils.StreamFromTo(file.Open(FileMode.Open), context.Response.OutputStream, 1024);
                    }

                    break;

                case "download":
                    // Get input
                    path = man.DecryptPath(input["path"]);

                    // Get and stream file
                    file = man.GetFile(path);
                    config = file.Config;

                    // Verify that we can stream the file
                    if (!man.VerifyFile(file, "download"))
                        throw new ManagerException("Requested resource could not be found. Or access was denied.");

                    // Set content type
                    //contentType = man.MimeTypes[PathUtils.GetExtension(path).ToLower()];
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("Content-Disposition:", "attachment; filename=\"" + file.Name + "\"");
                    //IOUtils.StreamFromTo(file.Open(FileMode.Open), context.Response.OutputStream, 4096);

                    byte[] buff = new byte[4096];
                    int len;
                    Stream inStream = file.Open(FileMode.Open), outStream = context.Response.OutputStream;

                    try {
                        while ((len = inStream.Read(buff, 0, buff.Length)) > 0) {
                            outStream.Write(buff, 0, len);
                            outStream.Flush();
                            context.Response.Flush();
                        }
                    } finally {
                        if (inStream != null)
                            inStream.Close();

                        if (outStream != null)
                            outStream.Close();
                    }

                    break;
            }

            // Devkit commands
            switch (cmd) {
                case "viewServerInfo":
                    if (!man.Config.GetBool("general.debug", false))
                        throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");

                    context.Response.ContentType = "text/html";
                    context.Trace.IsEnabled = true;

                    context.Response.Write("<pre># Config from Web.config\r\n\r\n");

                    foreach (string key in man.Config.Keys)
                        context.Response.Write(key + "=" + man.Config[key] + "\r\n");

                    context.Response.Write("</pre>");

                    break;

                case "downloadServerInfo":
                    if (!man.Config.GetBool("general.debug", false))
                        throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");

                    context.Response.AppendHeader("Content-Disposition", "attachment; filename=trace.htm");
                    context.Response.ContentType = "text/html";
                    context.Trace.IsEnabled = true;

                    context.Response.Write("<pre># Config from Web.config\r\n\r\n");

                    foreach (string key in man.Config.Keys)
                        context.Response.Write(key + "=" + man.Config[key] + "\r\n");

                    context.Response.Write("</pre>");

                    break;

                case "viewLog":
                    if (!man.Config.GetBool("general.debug", false))
                        throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");

                    if (input["level"] == "debug") {
                        if (File.Exists(man.MapPath("logs/debug.log")))
                            context.Response.WriteFile(man.MapPath("logs/debug.log"));
                    } else {
                        if (File.Exists(man.MapPath("logs/error.log")))
                            context.Response.WriteFile(man.MapPath("logs/error.log"));
                    }

                    break;

                case "clearLog":
                    if (!man.Config.GetBool("general.debug", false))
                        throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");

                    if (input["level"] == "debug")
                        File.Delete(man.MapPath("logs/debug.log"));
                    else
                        File.Delete(man.MapPath("logs/error.log"));

                    context.Response.Write("Log cleared.");

                    break;
            }

            return true;
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="man"></param>
        /// <param name="cmd"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override bool OnStream(ManagerEngine man, string cmd, NameValueCollection input)
        {
            HttpContext   context = HttpContext.Current;
            string        path, contentType;
            ManagerConfig config;
            IFile         file;

            switch (cmd)
            {
            case "streamFile":
                // Get input
                path = man.DecryptPath(input["path"]);

                // Get and stream file
                file   = man.GetFile(path);
                config = file.Config;

                if (file is LocalFile)
                {
                    string url = PathUtils.RemoveTrailingSlash(config["preview.urlprefix"]) + man.ConvertPathToURI(file.AbsolutePath) + config["preview.urlsuffix"];

                    // Pass through rnd
                    if (input["rnd"] != null)
                    {
                        context.Response.Redirect(url + (url.IndexOf('?') == -1 ? ("?rnd=" + input["rnd"]) : ("&rnd" + input["rnd"])), true);
                    }

                    context.Response.Redirect(url, true);
                }
                else
                {
                    // Verify that we can stream the file
                    if (!man.VerifyFile(file, "stream"))
                    {
                        throw new ManagerException("Requested resource could not be found. Or access was denied.");
                    }

                    // Set content type
                    contentType = man.MimeTypes[PathUtils.GetExtension(path).ToLower()];
                    if (contentType != null)
                    {
                        context.Response.ContentType = contentType;
                    }

                    IOUtils.StreamFromTo(file.Open(FileMode.Open), context.Response.OutputStream, 1024);
                }

                break;

            case "download":
                // Get input
                path = man.DecryptPath(input["path"]);

                // Get and stream file
                file   = man.GetFile(path);
                config = file.Config;

                // Verify that we can stream the file
                if (!man.VerifyFile(file, "download"))
                {
                    throw new ManagerException("Requested resource could not be found. Or access was denied.");
                }

                // Set content type
                //contentType = man.MimeTypes[PathUtils.GetExtension(path).ToLower()];
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("Content-Disposition:", "attachment; filename=\"" + file.Name + "\"");
                //IOUtils.StreamFromTo(file.Open(FileMode.Open), context.Response.OutputStream, 4096);

                byte[] buff = new byte[4096];
                int    len;
                Stream inStream = file.Open(FileMode.Open), outStream = context.Response.OutputStream;

                try {
                    while ((len = inStream.Read(buff, 0, buff.Length)) > 0)
                    {
                        outStream.Write(buff, 0, len);
                        outStream.Flush();
                        context.Response.Flush();
                    }
                } finally {
                    if (inStream != null)
                    {
                        inStream.Close();
                    }

                    if (outStream != null)
                    {
                        outStream.Close();
                    }
                }

                break;
            }

            // Devkit commands
            switch (cmd)
            {
            case "viewServerInfo":
                if (!man.Config.GetBool("general.debug", false))
                {
                    throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");
                }

                context.Response.ContentType = "text/html";
                context.Trace.IsEnabled      = true;

                context.Response.Write("<pre># Config from Web.config\r\n\r\n");

                foreach (string key in man.Config.Keys)
                {
                    context.Response.Write(key + "=" + man.Config[key] + "\r\n");
                }

                context.Response.Write("</pre>");

                break;

            case "downloadServerInfo":
                if (!man.Config.GetBool("general.debug", false))
                {
                    throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");
                }

                context.Response.AppendHeader("Content-Disposition", "attachment; filename=trace.htm");
                context.Response.ContentType = "text/html";
                context.Trace.IsEnabled      = true;

                context.Response.Write("<pre># Config from Web.config\r\n\r\n");

                foreach (string key in man.Config.Keys)
                {
                    context.Response.Write(key + "=" + man.Config[key] + "\r\n");
                }

                context.Response.Write("</pre>");

                break;

            case "viewLog":
                if (!man.Config.GetBool("general.debug", false))
                {
                    throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");
                }

                if (input["level"] == "debug")
                {
                    if (File.Exists(man.MapPath("logs/debug.log")))
                    {
                        context.Response.WriteFile(man.MapPath("logs/debug.log"));
                    }
                }
                else
                {
                    if (File.Exists(man.MapPath("logs/error.log")))
                    {
                        context.Response.WriteFile(man.MapPath("logs/error.log"));
                    }
                }

                break;

            case "clearLog":
                if (!man.Config.GetBool("general.debug", false))
                {
                    throw new ManagerException("You have to enable debugging in config by setting general.debug to true.");
                }

                if (input["level"] == "debug")
                {
                    File.Delete(man.MapPath("logs/debug.log"));
                }
                else
                {
                    File.Delete(man.MapPath("logs/error.log"));
                }

                context.Response.Write("Log cleared.");

                break;
            }

            return(true);
        }