예제 #1
0
        public bool Locate(string path, out string outPath, out string[] checkedPaths)
        {
            if (!path.StartsWith(Path.DirectorySeparatorChar.ToString()))
            {
                path = "{0}{1}"._Format(Path.DirectorySeparatorChar.ToString(), path);
            }
            string        ext          = Path.GetExtension(path).ToLowerInvariant();
            string        foundPath    = string.Empty;
            string        checkNext    = Fs.CleanPath("~" + path);
            Fs            fs           = ContentRoot;
            List <string> pathsChecked = new List <string>
            {
                checkNext
            };

            if (!fs.FileExists(checkNext, out foundPath))
            {
                foundPath = string.Empty;
                SearchRule[] extRules = _searchRules.Where(sr => sr.Ext.ToLowerInvariant().Equals(ext)).ToArray();
                extRules.Each(rule =>
                {
                    rule.SearchDirectories.Each(dir =>
                    {
                        List <string> segments = new List <string>();
                        segments.AddRange(dir.RelativePath.DelimitSplit("/", "\\"));
                        segments.AddRange(path.DelimitSplit("/", "\\"));
                        string subPath = Path.Combine(segments.ToArray());
                        checkNext      = Fs.CleanPath(subPath);
                        pathsChecked.Add(checkNext);

                        if (fs.FileExists(checkNext, out foundPath))
                        {
                            return(false); // stop the each loop
                        }
                        else
                        {
                            foundPath = string.Empty;
                        }

                        return(true); // continue the each loop
                    });

                    if (!string.IsNullOrEmpty(foundPath))
                    {
                        return(false); // stop the each loop
                    }

                    return(true); // continue the each loop
                });
            }

            checkedPaths = pathsChecked.ToArray();
            outPath      = foundPath;
            return(!string.IsNullOrEmpty(foundPath));
        }
예제 #2
0
        protected internal void SetBody(LayoutModel layout, string[] pathSegments)
        {
            Fs appRoot = AppConf.AppRoot;

            if (appRoot.FileExists(pathSegments))
            {
                string html       = appRoot.ReadAllText(pathSegments);
                CQ     dollarSign = CQ.Create(html);
                string body       = dollarSign["body"].Html().Replace("\r", "").Replace("\n", "").Replace("\t", "");
                layout.PageContent = body;

                StringBuilder headLinks = new StringBuilder();
                dollarSign["link", dollarSign["head"]].Each(el =>
                {
                    AddLink(headLinks, el);
                });
                StringBuilder links = new StringBuilder(layout.StyleSheetLinkTags);
                links.Append(headLinks.ToString());
                layout.StyleSheetLinkTags = links.ToString();

                StringBuilder scriptTags = new StringBuilder();
                dollarSign["script", dollarSign["head"]].Each(el =>
                {
                    AddScript(scriptTags, el);
                });
                StringBuilder scripts = new StringBuilder(layout.ScriptTags);
                scripts.Append(scriptTags.ToString());
                layout.ScriptTags = scripts.ToString();
            }
        }
예제 #3
0
        public static ContentLocator Load(Fs rootToCheck)
        {
            ContentLocator locator = null;

            if (rootToCheck.FileExists(FileName))
            {
                FileInfo file = rootToCheck.GetFile(FileName);//new FileInfo(filePath);
                locator = file.FromJsonFile <ContentLocator>();
            }
            else
            {
                locator = new ContentLocator(rootToCheck);
                string[] imageDirs = new string[] { "~/img", "~/image" };
                locator.AddSearchRule(".png", imageDirs);
                locator.AddSearchRule(".gif", imageDirs);
                locator.AddSearchRule(".jpg", imageDirs);

                string[] pageDirs = new string[] { "~/pages" };
                locator.AddSearchRule(".htm", pageDirs);
                locator.AddSearchRule(".html", pageDirs);

                string[] cssDirs = new string[] { "~/css" };
                locator.AddSearchRule(".css", cssDirs);

                string[] jsDirs = new string[] { "~/js", "~/3rdParty", "~/common" };
                locator.AddSearchRule(".js", jsDirs);

                locator.Save();
            }

            locator.ContentRoot = rootToCheck;
            return(locator);
        }
예제 #4
0
        protected internal WebBook GetBookByAppAndPage(string appName = "localhost", string pageName = "home")
        {
            AppConf appConf = BamConf[appName];
            WebBook result  = new WebBook();

            if (appConf != null)
            {
                string booksDir = "books";
                string bookFile = "{0}.json"._Format(pageName);
                Fs     appRoot  = appConf.AppRoot;
                if (appRoot.FileExists(booksDir, bookFile))
                {
                    result = appRoot.ReadAllText(booksDir, bookFile).FromJson <WebBook>();
                }
            }
            return(result);
        }
예제 #5
0
        public bool TryRespond(IHttpContext context, out string[] checkedPaths)
        {
            checkedPaths = new string[] { };
            IRequest  request  = context.Request;
            IResponse response = context.Response;
            string    path     = request.Url.AbsolutePath;

            string ext = Path.GetExtension(path);

            path = RemoveBamAppsPrefix(path);

            string[] split   = path.DelimitSplit("/");
            byte[]   content = new byte[] { };
            bool     result  = false;

            string locatedPath;

            if (path.Equals("/upload", StringComparison.InvariantCultureIgnoreCase))
            {
                HandleUpload(context, HttpPostedFile.FromRequest(request));
                string query = request.Url.Query.Length > 0 ? request.Url.Query : string.Empty;
                if (query.StartsWith("?"))
                {
                    query = query.TruncateFront(1);
                }
                content = RenderLayout(response, path, query);
                result  = true;
            }
            else if (string.IsNullOrEmpty(ext) && !ShouldIgnore(path) ||
                     (AppRoot.FileExists("~/pages{0}.html"._Format(path), out locatedPath)))
            {
                content = RenderLayout(response, path);
                result  = true;
            }
            else if (AppContentLocator.Locate(path, out locatedPath, out checkedPaths))
            {
                result = true;
                string foundExt = Path.GetExtension(locatedPath);
                if (FileCachesByExtension.ContainsKey(foundExt))
                {
                    FileCache cache = FileCachesByExtension[ext];
                    if (ShouldZip(request))
                    {
                        SetGzipContentEncodingHeader(response);
                        content = cache.GetZippedContent(locatedPath);
                    }
                    else
                    {
                        content = cache.GetContent(locatedPath);
                    }
                }
                else
                {
                    content = File.ReadAllBytes(locatedPath);
                }
            }

            if (result)
            {
                SetContentType(response, path);
                SetContentDisposition(response, path);
                SendResponse(response, content);
                OnResponded(context);
            }
            else
            {
                OnNotResponded(context);
            }
            return(result);
        }