Exemple #1
0
        public ResourcesModule() : base("/resources")
        {
            Get("/{name}", _ =>
            {
                string name = _.name;
                if (name.Length < 1)
                {
                    return(new NotFoundResponse());
                }

                string mime = "application/octet-stream";
                if (name.EndsWith(".png"))
                {
                    mime = "image/png";
                }
                else if (name.EndsWith(".jpg"))
                {
                    mime = "image/jpeg";
                }

                var fontstream = ResourceHelper.LoadResource(name);
                if (fontstream == null)
                {
                    return(new NotFoundResponse());
                }

                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(fontstream); }, mime);

                return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
            });
        }
Exemple #2
0
        public FontsModule() : base("/fonts")
        {
            Get("/{name}", _ =>
            {
                string name = _.name;
                if (name.Length < 1)
                {
                    return(new NotFoundResponse());
                }

                string mime = "application/octet-stream";
                if (name.EndsWith(".eot"))
                {
                    mime = "application/vnd.ms-fontobject";
                }
                else if (name.EndsWith(".svg"))
                {
                    mime = "image/svg+xml";
                }
                else if (name.EndsWith(".ttf"))
                {
                    mime = "application/octet-stream";
                }
                else if (name.EndsWith(".woff"))
                {
                    mime = "application/font-woff";
                }
                else if (name.EndsWith(".woff2"))
                {
                    mime = "application/font-woff2";
                }

                var fontstream = ResourceHelper.LoadResource("fonts." + name);
                if (fontstream == null)
                {
                    return(new NotFoundResponse());
                }

                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(fontstream); }, mime);

                return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
            });
        }
Exemple #3
0
        public HomeModule()
        {
            var config = TinyfxCore.Configuration;

            if (!config.IsSitePublic)
            {
                this.RequiresAuthentication();
            }

            _tinyfxPageRender = new Cores.TinyfxPageRender(config);

            Get("/", _ =>
            {
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(1, 0), "text/html"));
            });

            Get("/page/{page}", _ =>
            {
                int page = 0;
                try
                {
                    page = _.page;
                }
                catch
                {
                    page = 1;
                }
                if (page < 1)
                {
                    page = 1;
                }
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(page, 0), "text/html"));
            });

            Get("/post/{post}", _ =>
            {
                long post = 0;
                try
                {
                    post = _.post;
                }
                catch
                {
                    post = 0;
                }
                return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(0, post), "text/html"));
            });

            Get("/files/{filename}", _ =>
            {
                string filename = _.filename;
                if (filename == null || filename.Length < 1)
                {
                    return(new NotFoundResponse());
                }
                else
                {
                    string[] seqs = filename.Split(new char[] { '_' });
                    if (seqs.Length != 3)
                    {
                        return(new NotFoundResponse());
                    }
                    else
                    {
                        string realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]);
                        if (!String.IsNullOrEmpty(TinyfxCore.Configuration.DataDirectory))
                        {
                            realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]);
                        }
                        if (System.IO.File.Exists(realfile))
                        {
                            string mime = "application/octet-stream";

                            string ext = System.IO.Path.GetExtension(filename);
                            if (!string.IsNullOrEmpty(ext))
                            {
                                if (TinyfxCore.Mime.ContainsKey(ext))
                                {
                                    mime = TinyfxCore.Mime[ext];
                                }
                            }

                            var fs = System.IO.File.OpenRead(realfile);

                            if (TinyfxCore.Configuration.Encryption)
                            {
                                var ms    = new System.IO.MemoryStream();
                                Faes faes = new Faes();
                                faes.Decrypt(fs, ms);
                                ms.Seek(0, System.IO.SeekOrigin.Begin);

                                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(ms); }, mime);
                                if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif")
                                {
                                    return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
                                }
                                else
                                {
                                    return(streamResponse);
                                }
                            }
                            else
                            {
                                Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(fs); }, mime);
                                if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif")
                                {
                                    return(streamResponse.WithHeader("Cache-Control", "max-age=315360000"));
                                }
                                else
                                {
                                    return(streamResponse);
                                }
                            }
                        }
                        else
                        {
                            return(new NotFoundResponse());
                        }
                    }
                }
            });
        }
        // Deauthorises the user and redirects.

        public WebsiteModule()
        {
            Get("/", _ => {
                if (Context.Request.Cookies.ContainsKey("auth"))
                {
                    return new Nancy.Responses.RedirectResponse("/a");
                }
                var template = TemplateCacher.ReadTemplate("./templates/index.html");
                return new Nancy.Response
                {
                    StatusCode = Nancy.HttpStatusCode.OK,
                    ContentType = "text/html",
                    Contents = stream => (new StreamWriter(stream) { AutoFlush = true }).Write(template.Render(new
                    {
                        Name = XSSPrevention.XSSParse(Program.config_handler.config.server_name)
                    }))
                };
            });
            // Renders the login page (or redirects if there is a session cookie).

            Get("/a/{path_info*}", async args => {
                var user_tuple = CheckAuthCookie(Context);
                if (user_tuple.Item1 == null)
                {
                    return await DeauthAndRedirect("../", Context);
                }
                string uuid = user_tuple.Item1;
                User user = user_tuple.Item2;

                var template = TemplateCacher.ReadTemplate("./templates/dashboard.html");
                return new Nancy.Response
                {
                    StatusCode = Nancy.HttpStatusCode.OK,
                    ContentType = "text/html",
                    Contents = stream => (new StreamWriter(stream) { AutoFlush = true }).Write(template.Render(new
                    {
                        Name = XSSPrevention.XSSParse(Program.config_handler.config.server_name),
                        Username = XSSPrevention.XSSParse(user.username)
                    }))
                };
            });
            // The authenticated route for showing files/folders.

            Get("/static/{static_path*}", args => {
                string StaticArgs = args.static_path;
                string[] ArgsSplit = StaticArgs.Split('/');
                string Name = ArgsSplit[ArgsSplit.Length - 1];
                var response = new Nancy.Responses.StreamResponse(() => new FileStream("./static/" + StaticArgs, FileMode.Open), MimeTypes.GetMimeType(Name));
                return response.AsAttachment(Name);
            });
            // Gets any static objects.

            Get("/f/{path*}", async args =>
            {
                var user_tuple = CheckAuthCookie(Context);
                if (user_tuple.Item1 == null)
                {
                    return await DeauthAndRedirect("../", Context);
                }
                string uuid = user_tuple.Item1;
                User user = user_tuple.Item2;

                string data = args.path;

                var file_info = Utils.GetFile(uuid, user, data);
                if (file_info == null)
                {
                    return "Either the file was not found or you do not have permission to read it.";
                }

                var file = new FileStream(file_info.path, FileMode.Open);

                var response = new Nancy.Responses.StreamResponse(() => file, MimeTypes.GetMimeType(file_info.name));
                return response.AsAttachment(file_info.name);
            });
            // Handles file downloading.

            Get("/logout", async _ => {
                var user_tuple = CheckAuthCookie(Context);
                if (user_tuple.Item1 == null)
                {
                    return await DeauthAndRedirect("../", Context);
                }

                var cookie = Context.Request.Cookies["auth"];
                Program.config_handler.config.access_tokens.Remove(cookie);
                Program.config_handler.SaveConfig();

                return await DeauthAndRedirect("../", Context);
            });
            // Logs out the user.
        }