Ejemplo n.º 1
0
        public ActionResult Upload(AlbumUploadViewModel model)
        {
            if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0)
            {
                ModelState.AddModelError("Photo", "Provide a photo");
            }

            if (!ModelState.IsValid)
            {
                model.Albums = this.repository.GetAlbumsByOwner(User.Identity.Name.ToLowerInvariant());
                return View(model);
            }

            var photo = Request.Files[0];

            // generate a unique name
            string id = SlugHelper.GetSlug(model.Title);
            string owner = User.Identity.Name.ToLowerInvariant();
            string albumName = model.Album;

            try
            {
                this.repository.Add(
                                    new Photo()
                                    {
                                        AlbumId = albumName,
                                        Description = model.Description,
                                        Owner = owner,
                                        PhotoId = id,
                                        Title = model.Title,
                                        ThumbnailUrl = "/Content/images/processing.png",
                                        Url = "/Content/images/processing.png",
                                        RawTags = string.IsNullOrEmpty(model.Tags) ? "uncategorized" : model.Tags
                                    },
                                    photo.InputStream,
                                    photo.ContentType,
                                    photo.FileName);
            }
            catch (PhotoNameAlreadyInUseException)
            {
                ModelState.AddModelError("Title", "A picture with the same name already exists on this album");
                model.Albums = this.repository.GetAlbumsByOwner(User.Identity.Name.ToLowerInvariant());
                return View(model);
            }

            return RedirectToAction("Index", "Photo", new { owner = owner, album = model.Album, photoId = id });
        }
Ejemplo n.º 2
0
        public ActionResult Upload()
        {
            AlbumUploadViewModel albumViewModel = new AlbumUploadViewModel
            {
                Title = "Untitled"
            };

            albumViewModel.Albums = this.repository.GetAlbumsByOwner(User.Identity.Name.ToLowerInvariant());

            if (albumViewModel.Albums.Count() > 0)
            {
                return View(albumViewModel);
            }

            return RedirectToAction("Create");
        }