public void Init() { _repository = new DocumentDbRepository(new TestEnvironmentDefinition()); _userRegistrationReferenceProviderMock = new UserRegistrationReferenceProviderMock(); var photoValidation = new PhotoValidation(_repository); _photoController = new PhotoController( _repository, new TelemetryClient(), photoValidation, _userRegistrationReferenceProviderMock); }
public ActionResult UpdatePhoto(int uid, FormCollection frm) { var u = _repository._UserRepositories.GetUserByUID(new Users { UID = uid }); if (u != null) { if (u.UserName != HttpContext.User.Identity.Name) { return(null); } else { try { int pid = Convert.ToInt32(frm["pid"]); string name = Request["photoname"]; string description = Request["photodescription"]; int albumid = Convert.ToInt32(Request["album"]); var photo = _repository._PhotoRepositories.Getphoto(pid); photo.PhotoName = name; photo.Description = description; photo.AlbumID = albumid; PhotoValidation photoValidation = new PhotoValidation(); ValidationResult validationResult = photoValidation.Validate(photo); string Msg = ""; if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { Msg += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } return(Json(new { Result = "Error", Message = Msg })); } _repository._PhotoRepositories.UpdatePhoto(photo); return(Json(new { Result = "OK" })); } catch (Exception ex) { return(Json(new { Result = "Error", Message = ex.Message })); } } } else { return(null); } }
public ActionResult AddPhoto(int uid) { var u = _repository._UserRepositories.GetUserByUID(new Users { UID = uid }); if (u != null) { if (u.UserName != HttpContext.User.Identity.Name) { return(null); } else { try { string name = Request["photoname"]; string description = Request["photodescription"]; int albumid = Convert.ToInt32(Request["album"]); HttpPostedFileBase file = Request.Files["path"]; if (file != null) { if (file.InputStream == null || file.InputStream.Length > 2048000) { return(Json(new { Result = "ERROR", Message = "上传单张图片最大为2M" })); } else { string FileType = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1); if (FileType == "gif" || FileType == "GIF" || FileType == "jpg" || FileType == "JPG" || FileType == "png" || FileType == "PNG") { Photos photo = new Photos { PhotoName = name, Description = description, Path = " ", Thumbnail = " ", Thumbnailw = " ", CreateTime = DateTime.Now, AlbumID = albumid, UID = uid }; PhotoValidation photoValidation = new PhotoValidation(); ValidationResult validationResult = photoValidation.Validate(photo); string Msg = ""; if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { Msg += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } return(Json(new { Result = "Error", Message = Msg })); } var a = _repository._PhotoRepositories.AddPhoto(photo); string savePath = string.Format("/Content/Upload/{0}/{1}/", HttpContext.User.Identity.Name, a.AlbumID); string saveUrl = "http://" + Request.Url.Authority + savePath; string dirPath = Server.MapPath(savePath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string fileName = file.FileName; string fileExt = Path.GetExtension(fileName).ToLower(); string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; string newThumbnailFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_thumbnail" + fileExt; string newThumbnailwFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_thumbnailw" + fileExt; string filePath = dirPath + newFileName; file.SaveAs(filePath); string fileUrl = saveUrl + newFileName; a.Path = fileUrl; Common.ImageHelp.LocalImage2Thumbs(filePath, dirPath + newThumbnailFileName, 300, 300, "CUT"); a.Thumbnail = saveUrl + newThumbnailFileName; int w = 0; int h = 0; Common.ImageHelp.LocalImage2Thumbs2(filePath, dirPath + newThumbnailwFileName, 200, 300, "W", ref w, ref h); a.Thumbnailw_width = w; a.Thumbnailw_height = h; a.Thumbnailw = saveUrl + newThumbnailwFileName; _repository._PhotoRepositories.UpdatePhoto(a); return(Json(new { Result = "OK" })); } else { return(Json(new { Result = "ERROR", Message = "上传图片格式错误" })); } } } else { return(Json(new { Result = "ERROR", Message = "请选择上传图片" })); } } catch (Exception ex) { return(Json(new { Result = "Error", Message = ex.Message })); } } } else { return(null); } }