public async Task <ProfileUpdateSuccessModel> Put() { ProfileUpdateSuccessModel response = new ProfileUpdateSuccessModel(); var username = GetUserNameFromRequest(Request); var user = Db.C_records.SingleOrDefault(r => r.username == username); if (user == null) { response.SetError(404, "User Not Found"); return(response); } var readTask = await Request.Content.ReadAsMultipartAsync(); foreach (var content in readTask.Contents) { string fieldName = (content.Headers.ContentDisposition.Name ?? "").Trim('"'); if ("ProfileImage".Equals(fieldName)) { var currentFileData = await content.ReadAsByteArrayAsync(); var fileName = (content.Headers.ContentDisposition.FileName ?? "").Trim('"'); var fileFormat = fileName.Split('.')[1]; MemoryStream ms = new MemoryStream(currentFileData); Image returnImage = Image.FromStream(ms); if (returnImage.Size.Height != 100 || returnImage.Size.Width != 75) { response.SetError(400, "Image dimensions must be 75x100"); return(response); } fileName = $"{user.id}.{fileFormat}"; try { var fileToSave = new FileInfo(Path.Combine(DataDirectory, fileName)); File.WriteAllBytes(fileToSave.FullName, currentFileData); } catch (Exception e) { response.SetError(500, $"{"Error when trying to write the bytes"}-{e.InnerException}-{e.Message}"); } user.ProfileImageName = fileName; await Db.SaveChangesAsync(); } else { response.SetError(404, "Photo not found"); } } return(response); }
public async Task <ProfileUpdateSuccessModel> Put(ProfileRequestModel profile) { var username = GetUserNameFromRequest(Request); var user = Db.C_records.SingleOrDefault(r => r.username == username); ProfileUpdateSuccessModel response = new ProfileUpdateSuccessModel(); if (profile.Biography.Length > 1024) { response.SetError(400, "Biography must be below 1024 chars in length."); return(response); } if (user != null) { user.ProfileBiography = profile.Biography; } await Db.SaveChangesAsync(); return(response); }