コード例 #1
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 }));
        }
コード例 #2
0
ファイル: BoardController.cs プロジェクト: MixaKonan/Menhera
        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"));
        }