示例#1
0
        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());
        }
示例#2
0
        /* 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));
        }