/* POST * API Endpoint: api/image/ * Description: Uploads image to the server * Request Requirements: * 1. User JWT in header field * 2. Image file attachment * 3. Metadata(optional) * * Server response and status code: * 201 - image upload was successful server should return a link to the image and its metadata * 400 - malformed request due to unsupported file extension or etc * 401 - the JWT attached to the header is invalid or expired (should redirect to login) * 409 - image already exists on the server */ public IActionResult UploadImage([FromForm] IFormFile image) { // check if image is passed in and also if it's valid image type if (image == null) { return(BadRequest("no image passed in")); } else if (!_repo.IsImageFileType(image)) { return(BadRequest("invalid file extension")); } // check if image exists Image img = GetImageModel(image); if (ImageExists(img.IId)) { Image dbImgRecord = (Image)_context.Image.Where(i => i.IId == img.IId).First(); if (dbImgRecord.Trashed) { dbImgRecord.Trashed = false; _context.Update(dbImgRecord); _context.SaveChanges(); return(Created(dbImgRecord.IId, img)); } else { return(Conflict("image already exists")); } } // add image meta data into database try { _context.Add(img); _context.SaveChanges(); } catch (Exception e) { Debug.Write("SQL exception" + e.Message); return(BadRequest("malform request")); } // store image onto disk string uri = _repo.StoreImageToDisk(image); img.IId = uri; // change database iId type return(Created(uri, img)); }
public Object SubmitImage([FromBody] Submission submission) { Log dbLog = GenerateLog(submission.Images.Count.ToString() + " images submitted"); List <Image> images = new List <Image>(); try { _context.Add(dbLog); _context.SaveChanges(); } catch (Exception e) { Console.Write(e); return(BadRequest("malform request log")); } foreach (string imageid in submission.Images) { try { Image image = (Image)_context.Image.Where(i => i.IId == imageid).First(); image.Submitted = true; ProjectLink dbProjectLink = GenerateProjectLink(image, submission.Project); LogLink dbLogLink = GenerateLogLink(image, dbLog); _context.Add(dbLogLink); _context.Add(dbProjectLink); _context.SaveChanges(); } catch (Exception e) { Console.Write(e); return(BadRequest("malform request project")); } } try { var username = HttpContext.User.FindFirstValue("name"); var logLink = "https://aeimagehub.azurewebsites.net/logview?src=%22" + dbLog.LId + "%22"; sendEmail(User.FindFirst(ClaimTypes.Email)?.Value, username, logLink); } catch (Exception e) { Console.Write(e.Message); } return(Ok()); }
public void PostUser() { var role = "User"; if (isAdmin(HttpContext.User.FindAll("groups"))) { role = "Admin"; } User user = new User() { UId = HttpContext.User.FindFirstValue("http://schemas.microsoft.com/identity/claims/objectidentifier"), UserName = HttpContext.User.FindFirstValue("name"), Role = role, Active = true, }; _context.User.Add(user); _context.SaveChanges(); }
public Object PostProject([FromBody] JObject payload) { try { Project project = new Project() { ProjectName = (string)payload["ProjectName"], CreatedDate = (DateTime)payload["CreatedDate"], Description = (string)payload["Description"], Active = (bool)payload["Active"] }; _context.Project.Add(project); _context.SaveChanges(); return("success"); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void PostTag([FromBody] JObject payload) { Tag tag = new Tag() { TagName = (string)payload["TagName"], Description = (string)payload["Description"], Active = (bool)payload["Active"], }; _context.Tag.Add(tag); _context.SaveChanges(); }
public Object PostLog([FromBody] JObject payload) { try { Log log = new Log() { LId = (string)payload["LId"], UId = (string)payload["UId"], CreatedDate = (DateTime)payload["CreatedDate"], LogFile = (string)payload["LogFile"] }; _context.Log.Add(log); _context.SaveChanges(); return("success"); } catch (Exception e) { return(e); } }
public Object PutMetadata(string metaname, [FromBody] JObject payload) { try { Metadata metadata = (Metadata)_context.Metadata.Where(m => m.MetaName == metaname).First(); if (payload["Active"].Type != JTokenType.Null) { metadata.Active = (bool)payload["Active"]; } ; if (payload["Mandatory"].Type != JTokenType.Null) { metadata.Mandatory = (bool)payload["Mandatory"]; } ; _context.SaveChanges(); return("success"); } catch (Exception e) { return(e); } }