Пример #1
0
        public IHttpActionResult AddMenu()
        {
            if (!HttpContext.Current.Request.Files.AllKeys.Any())
            {
                throw new ValidationException(ErrorCodes.EmptyCategoryImage);
            }
            var httpPostedFile = HttpContext.Current.Request.Files[0];

            var menuModel = new JavaScriptSerializer().Deserialize <MenuModel>(HttpContext.Current.Request.Form.Get(0));

            if (httpPostedFile == null)
            {
                throw new ValidationException(ErrorCodes.EmptyCategoryImage);
            }

            if (httpPostedFile.ContentLength > 2 * 1024 * 1000)
            {
                throw new ValidationException(ErrorCodes.ImageExceedSize);
            }


            if (Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpg" &&
                Path.GetExtension(httpPostedFile.FileName).ToLower() != ".png" &&
                Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpeg")
            {
                throw new ValidationException(ErrorCodes.InvalidImageType);
            }

            var menuDto = Mapper.Map <MenuDTO>(menuModel);

            //restaurantDto.Image = (MemoryStream) restaurant.Image.InputStream;
            menuDto.Image = new MemoryStream();
            httpPostedFile.InputStream.CopyTo(menuDto.Image);

            _menuFacade.AddMenu(menuDto, UserId, HostingEnvironment.MapPath("~/Images/"));
            return(Ok());
        }