ProcessImageController.cs 3.81 KB
using AIAHighlight.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace AIAHighlight.Controllers
{
    public class ProcessImageController : ApiController
    {
        // GET: api/ProcessImage
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/ProcessImage/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/ProcessImage
        [HttpPost]
        public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
        {
            var httpRequest = HttpContext.Current.Request;
            Stream requestStream = await request.Content.ReadAsStreamAsync();
            HttpResponseMessage result = null;

            IEnumerable<string> layerNumberHeaderValue = request.Headers.GetValues("layerNumber");
            var layerNumber = layerNumberHeaderValue.FirstOrDefault();

            IEnumerable<string> bodyViewIndexHeaderValue = request.Headers.GetValues("bodyViewIndex");
            var bodyViewIndex = bodyViewIndexHeaderValue.FirstOrDefault();

            IEnumerable<string> bodyRegionHeaderValue = request.Headers.GetValues("bodyRegion");
            var bodyRegionId = bodyRegionHeaderValue.FirstOrDefault();

            if (requestStream != null)
            {

                string fileName = "BodyView" + bodyViewIndex + "_layer" + layerNumber + "_bodyRegionId" + bodyRegionId;
               // string filePath = "D:\\ADAM\\100-PROJECTS\\001-AIAHighlight.FileSave\\AIAHighlight.Serevr\\AIAHighlight\\data1\\75\\" + fileName + ".json";
                string filePath = "D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data1\\75\\" + fileName + ".json";


                try
                {
                    using (FileStream fileStream = System.IO.File.Create(filePath, (int)requestStream.Length))
                    {
                        // Fill the bytes[] array with the stream data
                        byte[] bytesInStream = new byte[requestStream.Length];
                     //   string response1 = (JsonConvert.SerializeObject(bytesInStream));
                        requestStream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                        // Use FileStream object to write to the specified file
                        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                        result = Request.CreateResponse(HttpStatusCode.Created, "TEMP.txt");
                    }
                    return result;
                }
                catch (HttpException ex)
                {
                    return result = Request.CreateResponse(HttpStatusCode.BadGateway, "Http Exception Come" + ex.Message);
                }
                catch (Exception ex)
                {
                    return result = Request.CreateResponse(HttpStatusCode.BadGateway, "Http Exception Come" + ex.Message);
                }
            }

            return result; ;
        }
        //public HttpResponseMessage Post([FromBody]JObject image)
        //{
        //    dynamic response = new ProcessImageHelper().writeFile(image);
        //    return new HttpResponseMessage { StatusCode = HttpStatusCode.Created, Content = new StringContent(JsonConvert.SerializeObject(response)) };
        //}

        // PUT: api/ProcessImage/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE: api/ProcessImage/5
        //public void Delete(int id)
        //{
        //}
    }
}