Beispiel #1
0
        public ActionResult Add(HttpPostedFileBase file, AddImageViewModel model)
        {
            if (file == null)
            {
                ModelState.AddModelError("", GlobalMessages.NoFileErrorMessage);
            }
            else if (ModelState.IsValid)
            {
                var isValidFIle = file.ContentLength > 0 && file.ContentLength <= Constants.ImageMaxSize;

                if (isValidFIle)
                {
                    try
                    {
                        var date     = this.dateProvider.GetDate();
                        var lakeName = this.lakeService.GetLakeName(model.SelectedLakeId);
                        var path     = Server.MapPath(
                            $"{Constants.ImageGalleriesBaseServerFolder}/{lakeName}/");

                        directoryHelper.CreateIfNotExist(path);

                        var url = $"{Constants.ImageGalleriesBaseFolder}/{lakeName}/{file.FileName}";
                        file.SaveAs(Server.MapPath(url));

                        var image = this.imageFactory.CreateImage(url, date, model.ImageInfo);
                        if (User.IsInRole("Moderator"))
                        {
                            image.IsConfirmed = true;
                        }

                        var gallery = this.imageGalleryService.FindById(model.SelectedImageGalleryId);
                        if (gallery == null)
                        {
                            gallery = this.imageGalleryFactory.CreateImageGallery(model.Name, model.SelectedLakeId);
                            this.imageGalleryService.Add(gallery);
                        }

                        gallery.Images.Add(image);

                        this.imageGalleryService.Save();
                    }
                    catch (ArgumentException ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                    }
                }
            }

            this.LoadNames(model);
            model.SelectedImageGalleryId = null;
            model.Name = null;

            return(View(model));
        }