Example #1
0
        public async Task <IActionResult> Report(int postId)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            try
            {
                var post = _db.Post.Include(p => p.File)
                           .Include(p => p.Admin)
                           .First(p => p.PostId == postId) ?? throw new Exception();

                ViewBag.Board = _collection.Boards.First(brd => brd.BoardId == post.BoardId);

                ViewBag.Thread = _db.Thread.First(t => t.ThreadId == post.ThreadId);

                post.Comment = PostFormatter.GetFormattedPostText(post);

                ViewBag.PostFiles = new KeyValuePair <Post, List <File> >(post, post.File.ToList());

                return(View());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(NotFound());
            }
        }
Example #2
0
        public async Task <IActionResult> Thread(int id)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            ViewBag.Id = 0;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            if (User.Identity.IsAuthenticated)
            {
                ViewBag.CurrentAdmin = _db.Admin.First(a => a.Email == User.Identity.Name);
            }

            try
            {
                using (_db)
                {
                    try
                    {
                        var thread = _db.Thread.Where(t => t.ThreadId == id)
                                     .Include(t => t.Board)
                                     .ToList()[0];

                        var posts = _db.Post.Where(p => p.ThreadId == id)
                                    .Include(p => p.File)
                                    .Include(p => p.Admin)
                                    .ToList();

                        var postsFiles = new Dictionary <Post, List <File> >();

                        ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(null, null);

                        foreach (var post in posts)
                        {
                            post.Comment = PostFormatter.GetFormattedPostText(post);
                            if (post.IsPinned)
                            {
                                ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(post, post.File.ToList());
                                continue;
                            }

                            postsFiles.Add(post, post.File.ToList());
                        }

                        ViewBag.Board      = thread.Board;
                        ViewBag.Thread     = thread;
                        ViewBag.UserIsOp   = IpCheck.UserIsOp(thread, anon.IpHash);
                        ViewBag.PostsFiles = postsFiles;
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        return(NotFound());
                    }
                    catch (Exception e)
                    {
                        await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                          LoggingInformationKind.Error);
                    }
                }
            }
            catch (InvalidOperationException)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);
            }

            return(View());
        }
Example #3
0
        public async Task <IActionResult> AddThread(Post post, List <IFormFile> files)
        {
            try
            {
                var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                             .GetString();

                var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

                if (anon.IsBanned)
                {
                    return(RedirectToAction("YouAreBanned", "Ban"));
                }

                if (ModelState.IsValid)
                {
                    var board = _db.Board.First(b => b.BoardId == post.BoardId);

                    if (_db.Thread.Count(t => t.BoardId == post.BoardId) >= 20)
                    {
                        return(RedirectToAction("Board", new { prefix = board.Prefix }));
                    }


                    post.AnonIpHash = ipHash;
                    if (!string.IsNullOrEmpty(post.AnonName))
                    {
                        post.AnonName = PostFormatter.GetHtmlTrimmedString(post.AnonName);
                    }

                    post.Comment       = PostFormatter.GetHtmlTrimmedString(post.Comment);
                    post.IsWrittenByOp = true;

                    if (User.Identity.IsAuthenticated)
                    {
                        var admin = _db.Admin.First(a => a.Email == User.Identity.Name);
                        post.AnonName = admin.Login;
                        post.Admin    = admin;
                    }

                    DbAccess.AddThreadToBoard(_db, ref post);

                    if (files.Count > 0)
                    {
                        var fileDirectory = Path.Combine(_env.WebRootPath, "postImages");

                        var thumbNailDirectory = Path.Combine(_env.WebRootPath, "thumbnails");

                        foreach (var file in files)
                        {
                            if (file.Length > 0)
                            {
                                switch (Path.GetExtension(file.FileName))
                                {
                                case ".jpeg":
                                case ".jpg":
                                case ".png":
                                {
                                    var imageThumbnailCreator = new ImageThumbnailCreator(file, fileDirectory, thumbNailDirectory);
                                    imageThumbnailCreator.CreateThumbnail();

                                    DbAccess.AddFilesToPost(_db, post, imageThumbnailCreator.FileInfo);

                                    break;
                                }

                                case ".gif":
                                {
                                    var gifThumbnailCreator = new GifThumbnailCreator(file, fileDirectory, thumbNailDirectory);
                                    gifThumbnailCreator.CreateThumbnail();

                                    DbAccess.AddFilesToPost(_db, post, gifThumbnailCreator.FileInfo);

                                    break;
                                }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("FileLengthNotValid", "Файл пустой.");
                            }
                        }
                    }

                    await LogIntoFile(_logDirectory, string.Concat("Added new thread: ", post.ThreadId),
                                      LoggingInformationKind.Info);

                    return(RedirectToAction("Thread", "Thread", new { id = post.ThreadId }));
                }
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                Console.WriteLine(e);
                return(StatusCode(500));
            }

            return(RedirectToAction("Board"));
        }
Example #4
0
        public async Task <IActionResult> AddPost(Post post, List <IFormFile> files, bool sage, bool isWrittenByOp = false)
        {
            try
            {
                var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                             .GetString();

                var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

                if (anon.IsBanned)
                {
                    return(RedirectToAction("YouAreBanned", "Ban"));
                }

                post.Comment           = PostFormatter.GetHtmlTrimmedString(post.Comment);
                post.AnonIpHash        = ipHash;
                post.TimeInUnixSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();

                if (User.Identity.IsAuthenticated)
                {
                    var admin = _db.Admin.First(a => a.Email == User.Identity.Name);
                    post.AnonName = admin.Login;
                    post.Admin    = admin;
                }

                if (ModelState.IsValid)
                {
                    DbAccess.AddPostToThread(_db, post, sage, isWrittenByOp);

                    if (files.Count > 0)
                    {
                        var fileDirectory = Path.Combine(_env.WebRootPath, "postImages");

                        var thumbNailDirectory = Path.Combine(_env.WebRootPath, "thumbnails");

                        foreach (var file in files)
                        {
                            if (file.Length > 0)
                            {
                                switch (Path.GetExtension(file.FileName))
                                {
                                case ".jpeg":
                                case ".jpg":
                                case ".png":
                                {
                                    var imageThumbnailCreator =
                                        new ImageThumbnailCreator(file, fileDirectory, thumbNailDirectory);

                                    imageThumbnailCreator.CreateThumbnail();

                                    DbAccess.AddFilesToPost(_db, post, imageThumbnailCreator.FileInfo);

                                    break;
                                }

                                case ".gif":
                                {
                                    var gifThumbnailCreator =
                                        new GifThumbnailCreator(file, fileDirectory, thumbNailDirectory);

                                    gifThumbnailCreator.CreateThumbnail();

                                    DbAccess.AddFilesToPost(_db, post, gifThumbnailCreator.FileInfo);

                                    break;
                                }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("FileLengthNotValid", "Файл пустой.");
                            }
                        }
                    }

                    await LogIntoFile(_logDirectory,
                                      string.Concat("Added new post: ", post.PostId, "at thread: ", post.ThreadId),
                                      LoggingInformationKind.Info);
                }
                else
                {
                    return(StatusCode(500));
                }
            }
            catch (InvalidOperationException)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);
            }

            return(RedirectToAction("Thread", new { id = post.ThreadId }));
        }
Example #5
0
        public async Task <IActionResult> Board(string prefix, int page = 1)
        {
            var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes())
                         .GetString();

            var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash));

            ViewBag.UserIpHash = anon.IpHash;

            ViewBag.UserIsBanned = false;

            if (anon.IsBanned)
            {
                ViewBag.UserIsBanned = anon.IsBanned;
                var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash);
                ViewBag.BanReason = ban.Reason;
                ViewBag.BanEnd    = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime();
            }

            ViewBag.Id = 0;

            ViewBag.Page = page;

            try
            {
                ViewBag.Board = _collection.Boards.First(brd => brd.Prefix == prefix);

                var allThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >();

                var boards = _db.Board.Where(b => b.Prefix == prefix).Include(b => b.Thread).ToList();

                Board board;
                if (boards.Count > 0)
                {
                    board             = boards[0];
                    ViewBag.FileLimit = board.FileLimit;
                }
                else
                {
                    return(NotFound());
                }

                ViewBag.ThreadCount = board.Thread.Count;

                foreach (var thrd in board.Thread.OrderByDescending(t => t.BumpInUnixTime))
                {
                    var thread = _db.Thread.Include(t => t.Post).First(t => t.ThreadId == thrd.ThreadId);

                    if (thread != null)
                    {
                        var postFiles = new List <KeyValuePair <Post, List <File> > >();

                        if (thread.Post.Count >= 4)
                        {
                            var posts = new List <Post> {
                                thread.Post.ToArray()[0]
                            };

                            posts.AddRange(thread.Post.ToList().OrderByDescending(p => p.TimeInUnixSeconds).Take(3));

                            foreach (var post in posts)
                            {
                                var p = _db.Post
                                        .Include(pp => pp.File)
                                        .Include(pp => pp.Admin)
                                        .First(pp => pp.PostId == post.PostId);

                                postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList()));
                            }

                            allThreads.Add(
                                new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles));
                        }
                        else if (thread.Post.Count > 0 && thread.Post.Count <= 3)
                        {
                            var p = _db.Post
                                    .Include(pp => pp.File)
                                    .Include(pp => pp.Admin)
                                    .First(pp => pp.ThreadId == thread.ThreadId);
                            postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList()));

                            allThreads.Add(
                                new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles));
                        }
                    }
                }

                var pageInfo = new PageInfo(page, Constants.BOARD_PAGE_SIZE, allThreads.Count);

                var pageThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >();

                for (var i = (page - 1) * pageInfo.PageSize; i < page * pageInfo.PageSize; i++)
                {
                    try
                    {
                        pageThreads.Add(allThreads[i]);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        break;
                    }
                    catch (Exception e)
                    {
                        await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                          LoggingInformationKind.Error);

                        return(StatusCode(500));
                    }
                }

                foreach (var pt in pageThreads)
                {
                    foreach (var pf in pt.Value)
                    {
                        pf.Key.Comment = PostFormatter.GetFormattedPostText(pf.Key);
                    }
                }

                ViewBag.PageInfo = pageInfo;

                ViewBag.BoardViewModel = pageThreads;
            }
            catch (InvalidOperationException e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(NotFound());
            }
            catch (Exception e)
            {
                await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace),
                                  LoggingInformationKind.Error);

                return(StatusCode(500));
            }

            return(View());
        }