Exemple #1
0
        public virtual ActionResult New(NewPhotoModel model)
        {
            model.Categories = _context.GetCategoriesList(model.CategoryId);

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

            var photo = _context.Photos.Create();

            photo.Title         = model.Title;
            photo.Slug          = model.Slug;
            photo.Description   = model.Description;
            photo.CreationDate  = DateTime.Now;
            photo.UserId        = HttpContext.FindUser().UserId;
            photo.CategoryId    = model.CategoryId;
            photo.PhotoMimeType = model.PhotoFile.ContentType;
            photo.IsApproved    = HttpContext.FindUser().IsAdmin;

            using (var stream = new BinaryReader(model.PhotoFile.InputStream))
                photo.PhotoFile = stream.ReadBytes(model.PhotoFile.ContentLength);

            _context.Add(photo);
            _context.SaveChanges();

            return(RedirectToAction(T4Routes.Photo.Detail(photo.Category.Slug, photo.Slug)));
        }
Exemple #2
0
        public virtual ActionResult New()
        {
            var model = new NewPhotoModel {
                Categories = _context.GetCategoriesList()
            };

            return(View(Views.New, model));
        }
Exemple #3
0
        public static Image PrepareImageFromRemoteOrLocal(NewPhotoModel photo)
        {
            HttpPostedFileBase input = photo.FileInput;
            string remoteFilename = photo.PhotoURL;

            if (input != null)
                return PrepareImage(input);
            else
                if (string.IsNullOrEmpty(remoteFilename))
                    throw new FileUploadException("Can't upload your photo from provided URL. Please check your URL and try again later.");
                else
                    return PrepareImage(remoteFilename);
        }
        public ActionResult AddPhoto(NewPhotoModel photo)
        {
            UserModel user = new UserRepository().GetByUsernameWithAlbums(HttpContext.User.Identity.Name, withFollowers: true);
            ViewBag.Albums = user.Albums;

            if (photo.Source == "remote")
                photo.FileInput = null;
            else
                photo.PhotoURL = null;
            ITransaction transaction = null;
            ISession session = null;
            try
            {
                using (Image img = FileHelper.PrepareImageFromRemoteOrLocal(photo))
                {
                    if (img == null)
                        throw new FileUploadException("Can't upload your photo. Please try again later.");

                    if (ModelState.IsValid)
                    {
                        AlbumModel selectedAlbum = null;
                        foreach (AlbumModel album in user.Albums)
                        {
                            if (album.Id == photo.AlbumId)
                            {
                                selectedAlbum = album;
                                break;
                            }
                        }

                        session = SessionProvider.SessionFactory.OpenSession();
                        transaction = session.BeginTransaction();

                        string photoName = "photo_" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                        string path = FileHelper.getPhotoPathWithoutExtension(selectedAlbum, photoName) + ".jpg";
                        if (string.IsNullOrEmpty(path))
                            throw new Exception("Can't save image");

                        path = FileHelper.SavePhoto(img, selectedAlbum, photoName);
                        if (string.IsNullOrEmpty(path))
                            throw new Exception("Returned path is empty");

                        PhotoRepository repo = new PhotoRepository();
                        PhotoModel newPhoto = new PhotoModel()
                        {
                            Path = path,
                            Date = DateTime.Parse(photo.Date),
                            Description = photo.Description,
                            Album = selectedAlbum
                        };

                        double? locLatitude = img.GPSLatitude();
                        double? locLongitude = img.GPSLongitude();
                        if (locLatitude.HasValue && locLongitude.HasValue)
                        {
                            newPhoto.LocationLatitude = locLatitude.Value;
                            newPhoto.LocationLongitude = locLongitude.Value;
                        }

                        repo.Create(newPhoto);
                        System.Diagnostics.Debug.WriteLine("Created db entry " + newPhoto.Id);

                        transaction.Commit();
                        Helpers.NotifyAlbumObservers(newPhoto.Album);
                        return RedirectToAction("Show", new { id = photo.AlbumId });

                    }
                }
            }
            catch (FileUploadException ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                    transaction.Dispose();
                }
                if (session != null)
                    session.Dispose();
                ModelState.AddModelError("FileInput", ex.Message);
            }
            catch (Exception)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                    transaction.Dispose();
                }
                if (session != null)
                    session.Dispose();
                ModelState.AddModelError("FileInput", "Can't upload your photo. Please try again later.");
            }
            return View(photo);
        }