Esempio n. 1
0
        }         // func CreateScript

        public static void WriteContent(this IDEContext context, Func <Stream> createSource, string cacheId, string contentType)
        {
            if (cacheId == null)
            {
                throw new ArgumentNullException("cacheId");
            }
            if (contentType == null)
            {
                throw new ArgumentNullException("contentType");
            }

            var http = context.Server as IDEHttpServer;
            var o    = http?.GetWebCache(cacheId);

            // create the item
            if (o == null)
            {
                using (var src = createSource())
                {
                    var isLua     = contentType == MimeTypes.Text.Lua;
                    var isHtml    = contentType == MimeTypes.Text.Html;
                    var cacheItem = isLua || isHtml || (src.CanSeek && src.Length < CacheSize);
                    if (cacheItem)
                    {
                        var isText = contentType.StartsWith("text/");
                        if (isLua)
                        {
                            o = CreateScript(context, cacheId, () => Procs.OpenStreamReader(src, Encoding.Default));
                        }
                        else if (isHtml)
                        {
                            bool isPlainText;
                            var  content = "otext('text/html');" + ParseHtml(Procs.OpenStreamReader(src, Encoding.Default), src.CanSeek ? src.Length : 1024, out isPlainText);
                            o = isPlainText ? content : CreateScript(context, cacheId, () => new StringReader(content));
                        }
                        else if (isText)
                        {
                            using (var tr = Procs.OpenStreamReader(src, Encoding.Default))
                                o = tr.ReadToEnd();
                        }
                        else
                        {
                            o = src.ReadInArray();
                        }

                        // write the cache item
                        http?.UpdateWebCache(cacheId, o);
                    }
                    else                     // write data without cache
                    {
                        WriteStream(context, src, contentType);
                        return;
                    }
                }
            }

            // write the item to the output
            if (o == null)
            {
                throw new ArgumentNullException("output", "No valid output.");
            }
            else if (o is ILuaScript)
            {
                var       c = (ILuaScript)o;
                LuaResult r;
                using (var g = new LuaHttpTable(context, contentType))
                    r = c.Run(g, true);

                if (!context.IsOutputStarted && r.Count > 0)
                {
                    WriteObject(context, r[0], r.GetValueOrDefault(1, MimeTypes.Text.Html));
                }
            }
            else if (o is byte[])
            {
                WriteBytes(context, (byte[])o, contentType);
            }
            else if (o is string)
            {
                WriteText(context, (string)o, contentType, context.Server.Encoding);
            }
            else
            {
                throw new ArgumentException($"Invalid cache item. Type '{o.GetType()}' is not supported.");
            }
        }         // func GetContent