/// <summary> /// post a note /// </summary> /// <param name="id">ID of note</param> public void OnPostNote(int id) { try { Note note = _note.GetNoteByID(id).Result; string blobText = _blob.GetText(note).Result; Note = note; Text = blobText; byte[] blobImage = _blob.GetImage(note).Result; ImageDisplayExtensions.DisplayImage(blobImage); Users = _userManager.Users.ToList(); List <Note> Notes = _note.GetAllNotes().Result; foreach (Note tempNote in Notes) { var user = _userManager.FindByIdAsync(tempNote.UserID).Result; AVMs.Add(new AdminViewModel() { Note = tempNote, UserName = user.UserName }); } } catch (Exception) { TempData["Error"] = "Something went wrong with the Post Note"; } }
/// <summary> /// User views one of their notes based on its ID /// </summary> /// <param name="id">ID of note the user wants to view</param> public void OnPostNote(int id) { var user = _userManager.GetUserAsync(User).Result; UserName = user.UserName; FirstName = user.FirstName; LastName = user.LastName; Bio = user.Bio; Email = user.Email; Notes = _notes.GetNotesByUserID(user.Id).Result; try { Note note = _notes.GetNoteByID(id).Result; if (!Notes.Contains(note)) { throw new Exception("You are not the owner of that note"); } string blobText = _blob.GetText(note).Result; Ncvm.Note = note; Ncvm.Text = blobText; Ncvm.Title = note.Title; byte[] blobImage = _blob.GetImage(note).Result; ImageDisplayExtensions.DisplayImage(blobImage); } catch (Exception) { TempData["Error"] = "Something went wrong with accessing that note"; } }
/// <summary> /// ReadHandWrittenText - Takes in the uploaded image and, if able, sends it to the API to be converted. /// </summary> /// <param name="formFile">form from the front end</param> /// <returns></returns> public async Task ReadHandwrittenText(IFormFile formFile) { try { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", Configuration["CognitiveServices:subscriptionKey"]); string uri = $"{Configuration["CognitiveServices:uriBase"]}?mode=Handwritten"; HttpResponseMessage response; string operationLocation; GetImageAsByteArray(formFile); using (ByteArrayContent content = new ByteArrayContent(Ncvm.ByteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); } if (response.IsSuccessStatusCode) { operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault(); } else { string errorString = await response.Content.ReadAsStringAsync(); TempData["Error"] = "Improper image format."; return; } string contentString; int i = 0; do { System.Threading.Thread.Sleep(1000); response = await client.GetAsync(operationLocation); contentString = await response.Content.ReadAsStringAsync(); ++i; }while (i < 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1); if (i == 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1) { TempData["Error"] = "Request timed-out. Try again later."; return; } ResponseContent = JToken.Parse(contentString).ToString(); RootObject ImageText = JsonParse(contentString); List <Line> Lines = FilteredJson(ImageText); Ncvm.Text = TextString(Lines); ImageDisplayExtensions.DisplayImage(Ncvm.ByteData); } catch (Exception) { TempData["Error"] = "Hmm.. Something went wrong."; } }