Пример #1
0
        public ActionResult PhotoChange()
        {
            int id        = int.Parse(Request.QueryString["x"]);
            var page      = Request.QueryString["page"];
            var photolist = db.dbphoto.ToList();
            var x         = photolist.Find(r => r.photoID == id);

            x.photoVisible = !x.photoVisible;
            if (TryUpdateModel(x))
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewBag.ErrorMessage = e;
                    return(RedirectToAction("DatabaseError", "Error"));
                }
            }
            return(RedirectToAction("PhotoManage", new { page = page }));
        }
Пример #2
0
 public void AddPhoto(Photo photo)
 {
     context.Photos.Add(photo);
     context.SaveChanges();
 }
Пример #3
0
        public ActionResult Compare(string win)
        {
            try
            {
                Random rnd = new Random();
                int    ph1, ph2;
                var    photoslist = db.dbphoto.ToList();

                /*odczytywanie wyników*/
                win = Request.Form["image"];
                var lost = "";

                if (win != Request.Form["ph1"])
                {
                    lost = Request.Form["ph1"];
                }
                else
                {
                    lost = Request.Form["ph2"];
                }

                /*Update zdjęć*/
                var photoUpdate = photoslist.Find(x => x.photoID == int.Parse(win));
                photoUpdate.photoScore = photoUpdate.photoScore + 1;
                photoUpdate.photoTotal = photoUpdate.photoTotal + 1;

                if (TryUpdateModel(photoUpdate))
                {
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception x)
                    {
                        ViewBag.ErrorMessage = x;
                        return(RedirectToAction("DatabaseError", "Error"));
                    }
                }
                photoUpdate            = photoslist.Find(x => x.photoID == int.Parse(lost));
                photoUpdate.photoTotal = photoUpdate.photoTotal + 1;
                if (TryUpdateModel(photoUpdate))
                {
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception x)
                    {
                        ViewBag.ErrorMessage = x;
                        return(RedirectToAction("DatabaseError", "Error"));
                    }
                }

                /*losowanie nowych zdjęć*/
                ph1 = photoslist.FindIndex(x => x.photoID == int.Parse(win));
                while (true)
                {
                    ph2 = rnd.Next(0, photoslist.Count() - 1);
                    if (ph2 != ph1 && photoslist[ph2].photoVisible)
                    {
                        break;
                    }
                }
                List <photos> CompareList = new List <photos>();
                CompareList.Add(photoslist[ph1]);
                CompareList.Add(photoslist[ph2]);
                ViewBag.ResultCMessage = "Wybrałeś zdjęcie numer " + win;
                return(View(CompareList));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e;
                return(RedirectToAction("CompareError", "Error"));
            }
        }
Пример #4
0
        public Stream GetPhotoResized(string albumId, string photoId, string dimension)
        {
            int    albumid, photoid;
            double size;

            if (int.TryParse(albumId, out albumid) && int.TryParse(photoId, out photoid) && double.TryParse(dimension, out size))
            {
                using (var db = new PhotosContext())
                {
                    var photos = (from a in db.Albums//.Include(p => p.Photos)
                                  where a.Id == albumid
                                  select a.Photos).FirstOrDefault();
                    if (photos != null)
                    {
                        var photo = (from p in photos
                                     where p.Id == photoid && p.OriginalPhotoId == null && p.Dimension == null
                                     select p).FirstOrDefault();

                        if (photo != null)
                        {
                            if (photo.ResizedPhotos == null)
                            {
                                photo.ResizedPhotos = new List <Photo>();
                            }

                            using (new NetworkConnection())
                            {
                                var resizedPhoto = (from p in db.Photos
                                                    where p.OriginalPhotoId == photoid && p.Dimension.HasValue && p.Dimension.Value == size
                                                    select p).FirstOrDefault();

                                string fullPath = string.Empty;
                                if (resizedPhoto != null)
                                {
                                    fullPath = System.IO.Path.Combine(resizedPhoto.Path, resizedPhoto.Name);
                                    if (!File.Exists(fullPath))
                                    {
                                        var newPhotoPath = System.IO.Path.Combine(photo.Path, string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)size).ToString(), System.IO.Path.GetExtension(photo.Name)));

                                        ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), newPhotoPath, size, 50);
                                        photo.ResizedPhotos.Add(new Photo {
                                            Name = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)size).ToString(), System.IO.Path.GetExtension(photo.Name)), Path = photo.Path, Dimension = size
                                        });
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    var newPhotoFileName = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)size).ToString(), System.IO.Path.GetExtension(photo.Name));
                                    fullPath = System.IO.Path.Combine(photo.Path, newPhotoFileName);
                                    ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), fullPath, size, 50);
                                    photo.ResizedPhotos.Add(new Photo {
                                        Name = newPhotoFileName, Path = photo.Path, Dimension = size
                                    });
                                    db.SaveChanges();
                                }

                                FileStream fs = File.OpenRead(fullPath);
                                WebOperationContext.Current.OutgoingResponse.ContentType = GetContentTypeFromFileName(photo.Name);
                                return(fs);
                            }
                        }
                    }
                }
            }
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
            return(null);
        }
Пример #5
0
        public HttpResponseMessage Get(int id, int photoId, int dimension = 0)
        {
            using (var db = new PhotosContext())
            {
                var response = Request.CreateResponse();
                if (dimension == 0)
                {
                    var photo = (from a in db.Albums//.Include(p => p.Photos)
                                 where a.Id == id
                                 select a.Photos.FirstOrDefault(p => p.Id == photoId && p.OriginalPhotoId == null)).FirstOrDefault();
                    if (photo != null)
                    {
                        using (new NetworkConnection())
                        {
                            var fullPath       = System.IO.Path.Combine(photo.Path, photo.Name);
                            var mediaStreaming = new MediaStreaming(fullPath);
                            response.Content = new PushStreamContent(mediaStreaming.WriteToStream, new MediaTypeHeaderValue(MediaContentHelper.GetContentTypeFromFileName(photo.FullPath)));
                        }
                    }
                }
                else
                {
                    var photos = (from a in db.Albums//.Include(p => p.Photos)
                                  where a.Id == id
                                  select a.Photos).FirstOrDefault();
                    if (photos != null)
                    {
                        var photo = (from p in photos
                                     where p.Id == photoId && p.OriginalPhotoId == null && p.Dimension == null
                                     select p).FirstOrDefault();

                        if (photo != null)
                        {
                            if (photo.ResizedPhotos == null)
                            {
                                photo.ResizedPhotos = new List <Photo>();
                            }

                            using (new NetworkConnection())
                            {
                                var resizedPhoto = (from p in db.Photos
                                                    where p.OriginalPhotoId == photoId && p.Dimension.HasValue && p.Dimension.Value == dimension
                                                    select p).FirstOrDefault();

                                string fullPath = string.Empty;
                                if (resizedPhoto != null)
                                {
                                    fullPath = System.IO.Path.Combine(resizedPhoto.Path, resizedPhoto.Name);
                                    if (!File.Exists(fullPath))
                                    {
                                        var newPhotoPath = System.IO.Path.Combine(photo.Path, string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)dimension).ToString(), System.IO.Path.GetExtension(photo.Name)));

                                        ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), newPhotoPath, dimension, 50);
                                        photo.ResizedPhotos.Add(new Photo {
                                            Name = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), dimension.ToString(), System.IO.Path.GetExtension(photo.Name)), Path = photo.Path, Dimension = dimension
                                        });
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    var newPhotoFileName = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), dimension.ToString(), System.IO.Path.GetExtension(photo.Name));
                                    fullPath = System.IO.Path.Combine(photo.Path, newPhotoFileName);
                                    ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), fullPath, dimension, 50);
                                    photo.ResizedPhotos.Add(new Photo {
                                        Name = newPhotoFileName, Path = photo.Path, Dimension = dimension
                                    });
                                    db.SaveChanges();
                                }
                                var mediaStreaming = new MediaStreaming(fullPath);
                                response.Content = new PushStreamContent(mediaStreaming.WriteToStream, new MediaTypeHeaderValue(MediaContentHelper.GetContentTypeFromFileName(photo.FullPath)));
                            }
                        }
                    }
                }

                return(response);
            }
        }