private void Rollback(CustomMultipartFormDataStreamProvider provider)
 {
     foreach (var file in provider.FileData)
     {
         try
         {
             File.Delete(file.LocalFileName);
         }
         catch (Exception) { }
     }
 }
        public async Task <HttpResponseMessage> Upload(int issueId)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "The request content must be in multipart format."));
            }

            // Fetch issue from db
            var issue = _issueRepository.Get(issueId, User.Identity.Name);

            if (issue == null)
            {
                throw new IssueNotFoundException();
            }

            var location = _locationRepository.Get(issue.LocationId);

            if (issue == null)
            {
                throw new LocationNotFoundException();
            }

            // TODO: Validate user access to this issue
            // ?

            // Include airport name in path (upper case)
            // C:\IssueManager\ImagesStorage\LJU\2\2-guid.jpg"
            String imageStorageLocation = @"C:\IssueManager\ImagesStorage\";

            imageStorageLocation += location.Name.ToUpper();
            imageStorageLocation += @"\";
            imageStorageLocation += issueId.ToString();

            // Makesure path exists
            if (System.IO.Directory.Exists(imageStorageLocation) == false)
            {
                System.IO.Directory.CreateDirectory(imageStorageLocation);
            }

            //string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new CustomMultipartFormDataStreamProvider(_imageRepository, imageStorageLocation, issueId);

            HttpResponseMessage response;

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);


                var vm = _imageRepository.Get(provider.ImageID);

                ApiResponse result = new ApiResponse
                {
                    Result      = ApiResult.Success,
                    JsonContent = JsonConvert.SerializeObject(vm, Formatting.None)
                };

                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (IOException ex)
            {
                switch (provider.Error)
                {
                case CustomMultipartFormDataStreamProvider.ErrorType.InvalidImageType:
                    response = Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "Invalid image type specified. Must be jpg or png.");
                    break;

                default:
                    response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                    break;
                }
            }
            catch (Exception e)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            Rollback(provider);

            return(response);
        }