Ejemplo n.º 1
0
        public ActionResult Index(int?id)
        {
            ViewBag.ShowImageOnly = !id.HasValue;

            if (!ViewBag.ShowImageOnly)
            {
                using (var ent = new LocationImagesEntities())
                {
                    var location = ent.Locations.FirstOrDefault(l => l.LocationId == id);
                    ViewBag.Location = location ?? throw new HttpException(404, "Location not found using ID: " + id);
                    ViewBag.Photos   = location.Photos.Where(p => p.PhotoTakenDate.Value.Year == DateTime.Now.Year).OrderByDescending(p => p.RecordCreatedDateTime).ToList();
                }
            }

            return(View());
        }
Ejemplo n.º 2
0
        public ActionResult Photo(int photoId)
        {
            PhotoImage foundPhoto;
            Photo      photo;

            using (var ent = new LocationImagesEntities())
            {
                foundPhoto = ent.PhotoImages.FirstOrDefault(p => p.PhotoId == photoId);
                photo      = foundPhoto.Photo;
            }

            if (foundPhoto == null)
            {
                throw new HttpException(404, "Photo not found using ID: " + photoId);
            }
            else
            {
                Response.AppendHeader("Content-Disposition", "inline; filename=" + photo.FileName);
                return(File(foundPhoto.Image, MimeMapping.GetMimeMapping(photo.FileName)));
            }
        }
Ejemplo n.º 3
0
        public ActionResult Upload()
        {
            // First, get all the location "states"
            using (var ent = new LocationImagesEntities())
            {
                var states = ent.Locations.Select(loc => loc.State).Distinct();

                var stateList = new List <SelectListItem>();


                var textInfo = new CultureInfo("en-US", false).TextInfo;

                foreach (var locationState in states)
                {
                    if (_stateLookup.ContainsKey(locationState))
                    {
                        stateList.Add(new SelectListItem
                        {
                            Text  = _stateLookup[locationState],
                            Value = locationState
                        });
                    }
                    else
                    {
                        stateList.Add(new SelectListItem
                        {
                            Text  = textInfo.ToTitleCase(locationState.ToLower()),
                            Value = locationState
                        });
                    }
                }

                ViewBag.StateList = stateList.OrderBy(sl => sl.Text);
            }

            if (Request.Files.Count > 0 && Request.Files[0] != null)
            {
                if (IsValid())
                {
                    var file = Request.Files[0];
                    using (var ent = new LocationImagesEntities())
                    {
                        var city  = Request.Form["photoTakenCity"].Trim();
                        var state = Request.Form["photoTakenState"].Trim();

                        var foundLocation = ent.Locations.FirstOrDefault(l => l.City == city && l.State == state);

                        if (foundLocation == null)
                        {
                            string         url     = String.Format("http://nominatim.openstreetmap.org/search?city={0}&{1}={2}&format=json", Server.UrlEncode(city), _stateLookup.ContainsValue(state.ToUpper()) ? "state" : "country", Server.UrlEncode(state));
                            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                            request.Referer   = "http://www.microsoft.com";
                            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                            {
                                if (response.StatusCode == HttpStatusCode.OK)
                                {
                                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                                    {
                                        List <GeoCode> gcs = JsonConvert.DeserializeObject <List <GeoCode> >(reader.ReadToEnd());

                                        if (gcs.Count > 0)
                                        {
                                            foundLocation = ent.Locations.Add(new Location()
                                            {
                                                City = city.ToUpper(), Latitude = Double.Parse(gcs.First().lat), Longitude = Double.Parse(gcs.First().lon), State = state.ToUpper()
                                            });
                                            ent.SaveChanges();
                                        }
                                        else
                                        {
                                            throw new HttpException(404, "City not found");
                                        }
                                    }
                                }
                                else
                                {
                                    throw new HttpException(400, response.StatusDescription);
                                }
                            }
                        }

                        var newPhoto = new Photo
                        {
                            LocationId            = foundLocation.LocationId,
                            FileName              = file.FileName.Split('\\').Last(),
                            Description           = string.IsNullOrWhiteSpace(Request.Form["description"]) ? null : Request.Form["description"].Trim(),
                            ImageProvidedBy       = string.IsNullOrWhiteSpace(Request.Form["photoSubmittedBy"]) ? null : Request.Form["photoSubmittedBy"],
                            PhotoTakenDate        = string.IsNullOrWhiteSpace(Request.Form["photoTakenDate"]) ? null : (DateTime?)DateTime.Parse(Request.Form["photoTakenDate"]),
                            RecordCreatedDateTime = DateTime.Now
                        };

                        newPhoto.PhotoImage = new PhotoImage
                        {
                            Image = ReadFully(file.InputStream)
                        };

                        ent.Photos.Add(newPhoto);

                        ent.SaveChanges();
                    }

                    ViewBag.Message = "<span class=\"complete\">Photo successfully uploaded!</span><br /><br />";
                }
            }

            return(View());
        }