public ActionResult Upload(photo photo) { if (ModelState.IsValid) { //only members can upload so check if user logged in else redirect to login page var loggedUser = GetLoggedUser(); if (loggedUser == null) { return RedirectToAction("LogOn", "Account"); } //get the uploaded file HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase; //check if the length of file > 0 i.e. its not a empty file if (file.ContentLength > 0) { var fileName = Path.GetFileNameWithoutExtension(file.FileName); //just filename var fileExt = Path.GetExtension(file.FileName); // file extension var destinationDirectory = Path.Combine(Server.MapPath("~/Images/Uploads/"), loggedUser.id.ToString()); //where the image is going to be uploaded var newFileTemp = loggedUser.id.ToString() + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); // filename format var newFileName = newFileTemp + "_L" + fileExt; //append the size L(large);M(Medium);S(Small) to the filename var path = Path.Combine(destinationDirectory, newFileName); try { //start transaction using (TransactionScope scope = new TransactionScope()) { //if the directory does not exist create it if (!Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } // now save the file to the path file.SaveAs(path); //get the exif data of the image //thanks to http://www.goheer.com for this simple utility exif_data exifData = GetImageExifData(path); //create medium thumbnail var thumbMedium = newFileTemp + "_M" + fileExt; CreateThumbnail(path, ImageSize.Medium, Path.Combine(destinationDirectory, thumbMedium)); //create small thumbnail var thumbSmall = newFileTemp + "_S" + fileExt; CreateThumbnail(path, ImageSize.Small, Path.Combine(destinationDirectory, thumbSmall)); //save this photo to the data /* *INSERT INTO `photoshare`.`photos`(`name`,`description`,`uploaded_date`,`user_id`,`photo_url_o`,`photo_url_m`,`photo_url_s`,`photo_category`,`tags`,`views_count`,`likes_count`) VALUES(?,?,?,?,?,?,?,?,?,?,?); */ photo.photo_url_o = Url.Content("~/Images/Uploads/" + loggedUser.id.ToString() + "/" + newFileName); photo.photo_url_m = Url.Content("~/Images/Uploads/" + loggedUser.id.ToString() + "/" + thumbMedium); photo.photo_url_s = Url.Content("~/Images/Uploads/" + loggedUser.id.ToString() + "/" + thumbSmall); photo.user_id = loggedUser.id; photo.views_count = 0; photo.likes_count = 0; photo.popularity = 0; photo.comments_count = 0; photo.favs_count = 0; db.photos.Add(photo); db.SaveChanges(); //save to exif_data /** * INSERT INTO `photoshare`.`exif_data`(`photo_id`,`camera`,`aperture`,`shutter_speed`,`iso`,`date_taken`,`focal_length`)VALUES(?,?,?,?,?,?,?); */ exifData.photo = photo; db.exif_data.Add(exifData); db.SaveChanges(); //everything went well complete the transaction scope.Complete(); } return RedirectToAction("Index", "User"); } catch { //something bad has heppened. delete the original file if it was saved FileInfo _file = new FileInfo(path); if (_file.Exists) { _file.Delete(); } } } else { ModelState.AddModelError("", "Please select a valid file"); } } ViewBag.photo_category = new SelectList(db.categories, "category_id", "category_name", photo.photo_category); return View(photo); }
public ActionResult GenerateFakePhotos() { List<user> users; using (var db2 = new photoshareEntities()) { users = db2.users.ToList(); } foreach (user u in users) { using (TransactionScope scope = new TransactionScope()) { for (int i = 0; i < 10; i++) { int cat_id = new Random().Next(1, 21); int randDays = new Random().Next(1, 28); photo p = new photo { uploaded_date = DateTime.Now.AddMonths(-1).AddDays(randDays), description = RandomString(200), name = RandomString(10), photo_category = cat_id, tags = RandomString(5) + "," + RandomString(5) + "," + RandomString(5), user_id = u.id, views_count = 0, photo_url_o = string.Format("/Images/Uploads/{0}/{1}.jpg", u.id.ToString(), (i + 1).ToString() + "_L"), photo_url_m = string.Format("/Images/Uploads/{0}/{1}.jpg", u.id.ToString(), (i + 1).ToString() + "_M"), photo_url_s = string.Format("/Images/Uploads/{0}/{1}.jpg", u.id.ToString(), (i + 1).ToString() + "_S") }; db.photos.Add(p); } db.SaveChanges(); scope.Complete(); } } return RedirectToAction("Index", "User"); }
public ActionResult Edit(photo photo) { try { if (ModelState.IsValid) { db.Entry(photo).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Details", "Photo", new { id = photo.id }); } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } ViewBag.photo_category = new SelectList(db.categories, "category_id", "category_name", photo.photo_category); ViewBag.exif_data = new SelectList(db.exif_data, "id", "manufacturer", photo.exif_data); ViewBag.user_id = new SelectList(db.users, "id", "first_name", photo.user_id); return View(photo); }