예제 #1
0
        public ActionResult AddImage(AlbumAddItemViewModel model, HttpPostedFileBase image)
        {
            if (!Request.IsAuthenticated)
            {
                throw new AuthenticationException();
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string    fileName;
            AlbumItem albumItem;

            if (image != null)
            {
                if (UploadImageValidationService.ValidateImageAndGetNewName(image, out fileName))
                {
                    albumItem = AlbumService.AddImage(UserContext.Current, model.AlbumId, model.Title, model.Description, fileName);

                    var path = Path.Combine(Server.MapPath("~/MediaContent/Albums/Images/"), fileName);
                    image.SaveAs(path);
                }
                else
                {
                    throw new BusinessLogicException("Неверный формат файла");
                }
            }
            else if (!string.IsNullOrWhiteSpace(model.Url))
            {
                byte[] content;
                var    hwReq  = (HttpWebRequest)WebRequest.Create(model.Url);
                var    wResp  = hwReq.GetResponse();
                var    stream = wResp.GetResponseStream();

                if (stream == null)
                {
                    throw new BusinessLogicException("Произошла ошибка при открытии ссылки");
                }

                using (var br = new BinaryReader(stream))
                {
                    content = br.ReadBytes(5242880); // Ограничение по размеру в 5Мб
                    br.Close();
                }
                wResp.Close();

                if (UploadImageValidationService.ValidateImageTypeAndGetNewName(wResp, out fileName))
                {
                    albumItem = AlbumService.AddImage(UserContext.Current, model.AlbumId, model.Title, model.Description, fileName);

                    var path = Path.Combine(Server.MapPath("~/MediaContent/Albums/Images/"), fileName);
                    var fs   = new FileStream(path, FileMode.Create);
                    var w    = new BinaryWriter(fs);

                    try
                    {
                        w.Write(content);
                    }
                    finally
                    {
                        fs.Close();
                        w.Close();
                    }
                }
                else
                {
                    throw new ValidationException("Указана некорректная ссылка");
                }
            }
            else
            {
                throw new ValidationException("Изображение не выбрано");
            }

            if (Request.UrlReferrer != null && Request.UrlReferrer.Segments[2] == "foreditor/")
            {
                return(RedirectToAction("itemforeditor", "album", new { id = albumItem.Id, groupId = model.GroupId }));
            }

            return(RedirectToAction("item", new { id = albumItem.Id }));
        }