public HttpResponseMessage GetDocumentPicture(string path) { if (path == null) { path = "default-placeholderCrypt.png"; } var filePath = HttpContext.Current.Server.MapPath("~/Images/" + path); if (!File.Exists(filePath)) { path = "default-placeholderCrypt.png"; filePath = HttpContext.Current.Server.MapPath("~/Images/" + path); } var ext = Path.GetExtension(filePath); byte[] contents = null; string eSecretKey = SecretKey.LoadKey(HttpRuntime.AppDomainAppPath + "Images\\SecretKey.txt"); AES_Symm_Algorithm.DecryptFile(filePath, out contents, eSecretKey); MemoryStream ms = new MemoryStream(contents); var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StreamContent(ms); response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/" + ext); return(response); }
public IHttpActionResult EditUser() { var httpRequest = HttpContext.Current.Request; string imageName = null; AppUser appUser; try { var username = User.Identity.Name; var user = _unitOfWork.AppUsers.Find(u => u.Email == username).FirstOrDefault(); if (user == null) { return(BadRequest("Data could not be retrieved, try to relog.")); } appUser = user; } catch { return(BadRequest("Data could not be retrieved, try to relog.")); } var jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } appUser.FullName = httpRequest["FullName"].Trim(); appUser.BirthDate = DateTime.Parse(httpRequest["BirthDate"]); appUser.Email = httpRequest["Email"].Trim(); appUser.ProfileEdited = true; if (appUser.DocumentPicture == null || appUser.DocumentPicture == "") { var postedFile = httpRequest.Files["Image"]; if (postedFile != null) { imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName); appUser.DocumentPicture = imageName; byte[] fileData = null; using (var binaryReader = new BinaryReader(postedFile.InputStream)) { fileData = binaryReader.ReadBytes(postedFile.ContentLength); } string eSecretKey = SecretKey.LoadKey(HttpRuntime.AppDomainAppPath + "Images\\SecretKey.txt"); AES_Symm_Algorithm.EncryptFile(fileData, filePath, eSecretKey); } } jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); try { _unitOfWork.AppUsers.Update(appUser); _unitOfWork.Complete(); } catch { return(BadRequest("Profile could not be edited.")); } return(Ok(appUser)); }