Exemple #1
0
        private bool GetByCache(string name, bool forced, out IWebFileRecord record)
        {
            foreach (var mask in caches)
            {
                if (name.StartsWith(mask.Key))
                {
                    var resolvedName = name.Substring(mask.Key.Length);
                    var file         = mask.Value.Resolve(resolvedName, forced);
                    if (null == file)
                    {
                        record = _cache[name] = null;
                    }
                    else
                    {
                        record = _cache[name] = new FileSystemWebFileRecord {
                            Name           = name,
                            FileSystemName = file,
                            FullName       = file
                        };
                    }

                    return(true);
                }
            }
            record = null;
            return(false);
        }
        /// <summary>
        /// Осуществляет поиск описателя файла по условию
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IWebFileRecord Find(string search)
        {
            var nsearch = ("/" + search).NormalizePath();

            if (this.Prefix != "/")
            {
                if (!nsearch.StartsWith(Prefix + "/"))
                {
                    return(null);
                }
                nsearch = nsearch.Substring(Prefix.Length, nsearch.Length - Prefix.Length);
            }
            if (!Cache.ContainsKey(search))
            {
                IWebFileRecord result = null;
                foreach (var p in Providers)
                {
                    if (!p.IsMatch(nsearch))
                    {
                        continue;
                    }
                    result = p.Find(nsearch);
                    if (null != result)
                    {
                        break;
                    }
                }
                if (null == result)
                {
                    foreach (var p in Providers)
                    {
                        if (!p.IsMatch(nsearch))
                        {
                            continue;
                        }
                        result = p.Find(nsearch, WebFileSerachMode.IgnorePath);
                        if (null != result)
                        {
                            break;
                        }
                    }
                }
                Cache[search] = result;
            }
            return(Cache[search]);
        }
Exemple #3
0
        private bool GetByMask(string name, out IWebFileRecord record)
        {
            foreach (var mask in masks)
            {
                if (name.StartsWith(mask.Key))
                {
                    var resolvedName = name.Substring(mask.Key.Length);
                    var file         = Path.Combine(mask.Value.Path, resolvedName);

                    record = _cache[name] =
                        File.Exists(file)
                                ? new FileSystemWebFileRecord {
                        Name           = name,
                        FileSystemName = file,
                        FullName       = file,
                        Role           = mask.Value.Role
                    }
                                : null;
                    return(true);
                }
            }
            record = null;
            return(false);
        }
        private static void Finish200(IHostServer server, WebContext context, IWebFileRecord staticdescriptor)
        {
            var filetime = context.SetLastModified(staticdescriptor.Version);

            if (filetime <= context.GetIfModifiedSince())
            {
                context.Finish("", status: 304);
            }
            else
            {
                context.SetHeader("Qorpent-Disposition", staticdescriptor.FullName);
                if (server.Config.Cached.Contains(Path.GetFileName(staticdescriptor.FullName)))
                {
                    context.SetHeader("Cache-Control", "public, max-age=86400");
                }
                else if (server.Config.ForceNoCache)
                {
                    context.SetHeader("Cache-Control", "no-cache, must-revalidate");
                }
                else
                {
                    context.SetHeader("Cache-Control", "public");
                }

                RangeDescriptor range = null;
                if (0 < staticdescriptor.Length && (staticdescriptor.MimeType.StartsWith("image/") || staticdescriptor.MimeType.StartsWith("video/")))
                {
                    context.Response.ConentLength = staticdescriptor.Length;
                    if (staticdescriptor.Length > 4096)
                    {
                        context.Response.SetHeader("Accept-Ranges", "bytes");
                    }
                    var rangeHeader = context.Request.GetHeader("Range");
                    if (!string.IsNullOrWhiteSpace(rangeHeader))
                    {
                        var rangeparts = rangeHeader.Substring(6).SmartSplit(false, true, '-');
                        range = new RangeDescriptor {
                            Total = staticdescriptor.Length
                        };
                        range.Finish = range.Total - 1;
                        range.Start  = rangeparts[0].ToInt();
                        if (rangeparts.Count > 1)
                        {
                            range.Finish = rangeparts[1].ToInt();
                        }
                    }
                }

                try {
                    if (staticdescriptor.IsFixedContent)
                    {
                        if (null != staticdescriptor.FixedData)
                        {
                            context.Finish(staticdescriptor.FixedData, staticdescriptor.MimeType, range: range);
                        }
                        else
                        {
                            context.Finish(staticdescriptor.FixedContent, staticdescriptor.MimeType, range: range);
                        }
                    }
                    else
                    {
                        using (var s = staticdescriptor.Open()) {
                            context.Finish(s, staticdescriptor.MimeType, range: range);
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.GetType().Name);
                    context.Finish(e.ToString(), status: 500);
                }
                finally {
                    context.Response.Close();
                }
            }
        }