Example #1
0
        //----------------------------------//

        /// <summary>
        /// Start a web site and server with the specified path as the root directory.
        /// </summary>
        public HttpSite(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                Log.Warning("No path specified for site. A path should be set.");
            }

            // persist and clean the path
            Path = Fs.Combine(path);

            // get the configuration for the web site
            ManagerResources.LoadConfig(Fs.Combine(Path, "site.config"), new Act <Configuration>(OnConfiguration));

            // create the authentication dictionary
            Authentication = new Dictionary <string, HttpRequirement>();
            // create the redirects collection
            Redirects = new Dictionary <string, HttpRedirect>();

            // create the cache
            _cache = new Cache <string, WebResource>(Global.Megabyte * 100, r => r.Size);

            _onAccessDenied    = new ActionPop <HttpRequest>();
            _onInvalidResource = new ActionPop <HttpRequest>();

            _defaultSendOptions = new HttpSendOptions {
                ContentType = "text/html"
            };
        }
Example #2
0
        //----------------------------------//

        /// <summary>
        /// Initialize a web resource.
        /// </summary>
        public WebResource(HttpSite site, string path, string mime = null)
        {
            _lock = new LockShared();

            // persist the local path
            Path = path;

            // does the path indicate a web resource?
            if (Fs.IsWebPath(Path))
            {
                // persist the path
                FullPath = Path;
            }
            else
            {
                // persist the path
                FullPath = Fs.Combine(site.Path, Path);
            }

            _compression = System.Net.DecompressionMethods.None;

            // get the web resource extension if set
            MimeType = mime ?? Mime.GetType(Fs.GetExtension(FullPath));

            // the resource must be loaded to begin with
            _reset = true;
        }
Example #3
0
        /// <summary>
        /// Remove metadata from the media found in the input stream.
        /// </summary>
        public MetaRemoval(MediaCoordinator coordinator, Stream input, Stream output, IAction <MetaRemoval> onComplete, bool removeTempFile = true)
        {
            _coordinator    = coordinator;
            _removeTempFile = removeTempFile;

            // create a name for the media
            Path            = Fs.Combine(_coordinator.MediaPath, Guid.NewGuid().ToString());
            Output          = output;
            OnComplete      = onComplete;
            OnComplete.ArgA = this;

            try {
                // open a stream to the file
                FileStream file = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.Read);
                // copy the stream into a temporary file
                WriteTemporaryFile(file, input);
            } catch (IOException ex) {
                // if the file is still being used
                if (ex.HResult == -2147024864)
                {
                    // try twice more
                    ManagerUpdate.Iterant.AddSingle(TryWriteTemporaryFile, input, 0);
                    return;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Add or set a page. The cache time is a number of milliseconds that the result
        /// of the 'onBuild' action can be cached for.
        /// </summary>
        public void Set(string key, string path, IAction <Element> onBuild, int cacheTime = -1, bool buildNow = true)
        {
            path = Fs.Combine(Path, path);
            _lock.Take();
            var link = _elements[key] = new ElementLink(path, onBuild, cacheTime);

            _paths[path] = link;
            _lock.Release();
            if (buildNow)
            {
                link.Build();
            }
        }
Example #5
0
        /// <summary>
        /// Add or set an element.
        /// </summary>
        public void Set(string key, string path, bool buildNow = true)
        {
            path = Fs.Combine(Path, path);
            _lock.Take();
            var link = _elements[key] = new ElementLink(path);

            _paths[path] = link;
            _lock.Release();
            if (buildNow)
            {
                link.Build();
            }
        }
Example #6
0
        /// <summary>
        /// Remove metadata from the media found in the input stream.
        /// </summary>
        public MetaRetrieval(MediaCoordinator coordinator, Stream input, IAction <MetaRetrieval> onComplete, bool removeTempFile = true)
        {
            _coordinator    = coordinator;
            _removeTempFile = removeTempFile;

            // create a name for the media
            Path            = Fs.Combine(_coordinator.MediaPath, Guid.NewGuid().ToString());
            OnComplete      = onComplete;
            OnComplete.ArgA = this;

            // open a stream to the file
            FileStream file = new FileStream(Path, FileMode.Create);

            // add a task to copy the stream into a temporary file
            // this then triggers the metadata retrieval
            WriteTemporaryFile(file, input);
        }
Example #7
0
        /// <summary>
        /// Resolve the resource at the specified path.
        /// </summary>
        public virtual WebResource GetResource(string path)
        {
            // does the path indicate a directory?
            if (path.Length == 0 || path[path.Length - 1] == Chars.ForwardSlash)
            {
                // yes, get the default html page of that directory
                path = Fs.Combine(path, "default.html");
            }

            // attempt to get the web resource from the cache
            WebResource resource = _cache.Get(path);

            // was the resource found in the cache?
            if (resource == null)
            {
                // no, create a new resource
                resource = new WebResource(this, path);
                // add the resource to the cache
                _cache.Add(path, resource);
            }

            return(resource);
        }