示例#1
0
        public ActionResult NewAlbum([Bind(Exclude = "AlbumId")] AlbumViewModel viewModel) // , HttpPostedFileBase image = null
        {
            if (ModelState.IsValid)
            {
                DateTime date  = DateTime.Now;
                Album    album = new Album();

                album.Title       = viewModel.Title;
                album.Slug        = SlugHelper.URLFriendly(viewModel.Slug);
                album.Description = viewModel.Description;
                album.Occasion    = viewModel.Occasion;
                album.Published   = viewModel.Published;
                album.CreatedOn   = DateTime.Now;
                album.ModifiedOn  = DateTime.Now;

                unitOfWork.AlbumRepository.InsertAlbum(album);
                unitOfWork.Save();

                // adding id to slug
                string idSlug = album.AlbumId + "-" + album.Slug;
                album.Slug = idSlug;
                unitOfWork.AlbumRepository.UpdateAlbum(album);
                unitOfWork.Save();

                // creating directories for images
                string albumPath = date.Year + "/" + date.ToString("MM") + "/" + idSlug;
                string savePath  = StorageRoot + albumPath;
                Directory.CreateDirectory(savePath + "/Original");
                Directory.CreateDirectory(savePath + "/Thumbs");
                Directory.CreateDirectory(savePath + "/Resized");

                ViewBag.AlbumId = album.AlbumId;
                return(RedirectToAction("Album", new { id = album.AlbumId }));
            }
            else
            {
                return(PartialView("_CreateAlbum", viewModel));
            }
        }
示例#2
0
        public ActionResult Create(PostViewModel postView, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                // upload image or download Bing daily picture
                if (image != null)
                {
                    string imgPath     = DateTime.Now.ToString("yyyyMMddHHmm") + System.IO.Path.GetExtension(image.FileName);
                    string ImgSavePath = HttpContext.Server.MapPath("~/Content/Images/Thoughts/")
                                         + imgPath;
                    try
                    {
                        image.SaveAs(ImgSavePath);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save image.");
                        return(View(postView));
                    }

                    postView.ImagePath = imgPath;
                }
                else
                {
                    using (var webClient = new WebClient())
                    {
                        string alterPath = "";
                        try
                        {
                            var     json     = webClient.DownloadString("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US");
                            JObject jResults = JObject.Parse(json);
                            foreach (var img in jResults["images"])
                            {
                                alterPath = ((string)img["url"]);
                            }

                            string imgPath     = DateTime.Now.ToString("yyyyMMddHHmm") + ".jpg";
                            string ImgSavePath = HttpContext.Server.MapPath("~/Content/Images/Thoughts/") + imgPath;
                            webClient.DownloadFile("https://www.bing.com/" + alterPath, ImgSavePath);
                            postView.ImagePath = imgPath;
                        }
                        catch (Exception)
                        {
                            ModelState.AddModelError(string.Empty, "Can not download Bing image. Try again or upload existing image.");
                            return(View(postView));
                        }
                    }
                } // end of image save if..else statement

                var post = new Post();
                post.Title       = postView.Title;
                post.Description = postView.Description;
                post.PostedOn    = DateTime.Now;
                post.Published   = postView.Published;
                post.UrlSlug     = SlugHelper.URLFriendly(postView.UrlSlug);
                post.ImagePath   = postView.ImagePath;
                post.Tags        = new List <Tag>();

                if (!String.IsNullOrWhiteSpace(postView.Tags))
                {
                    TagsToEntities(postView, post);
                }

                try
                {
                    post = unitOfWork.PostRepository.InsertPost(post);
                    unitOfWork.Save();
                    // Updating slug. Adding {id-} in the beginning
                    post.UrlSlug = post.Id + "-" + post.UrlSlug;
                    unitOfWork.PostRepository.UpdatePost(post);
                    unitOfWork.Save();
                }
                catch (Exception)
                {
                    ModelState.AddModelError(string.Empty, "Unable to save changes.");
                    return(View(postView));
                }

                return(RedirectToAction("Index", new { page = 1 }));
            }

            ViewBag.Title = "Create";
            return(View("CreateEdit", postView));
        }