예제 #1
0
        public void TestForWidthLargerThanHeight()
        {
            Image oldImage = (Image)_resourceManager.GetObject("TestJpeg3");

            Assert.IsNotNull(oldImage);
            Assert.AreEqual(5, oldImage.Height);
            Assert.AreEqual(10, oldImage.Width);

            ImageThumbnailCreator creator = new ImageThumbnailCreator();

            Image newSmallImage = creator.CreateThumbnail(oldImage, 2);

            Assert.AreEqual(2, newSmallImage.Height);
            Assert.AreEqual(4, newSmallImage.Width);

            Image newLargeImage = creator.CreateThumbnail(oldImage, 20);

            Assert.AreEqual(20, newLargeImage.Height);
            Assert.AreEqual(40, newLargeImage.Width);
        }
예제 #2
0
        public void TestThumbnailCreation()
        {
            Image oldImage = (Image)_resourceManager.GetObject("TestPhoto");

            Assert.IsNotNull(oldImage);
            Assert.AreEqual(100, oldImage.Height);
            Assert.AreEqual(100, oldImage.Width);

            ImageThumbnailCreator creator = new ImageThumbnailCreator();

            Image newSmallImage = creator.CreateThumbnail(oldImage, 50);

            Assert.AreEqual(50, newSmallImage.Height);
            Assert.AreEqual(50, newSmallImage.Width);

            Image newLargeImage = creator.CreateThumbnail(oldImage, 200);

            Assert.AreEqual(200, newLargeImage.Height);
            Assert.AreEqual(200, newLargeImage.Width);
        }
예제 #3
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 }));
        }
예제 #4
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"));
        }