public static CommentImageDTO CommentImagePostViewModel_To_CommentImageDTO(CommentImagePostViewModel commentImagePostViewModel) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <CommentImagePostViewModel, CommentImageDTO>()).CreateMapper(); CommentImageDTO commentImageDTO = mapper.Map <CommentImagePostViewModel, CommentImageDTO>(commentImagePostViewModel); return(commentImageDTO); }
public string postImageComment(CommentImagePostViewModel postImageComment) { var file = this.Request.Files[0]; if (file.ContentType == "image/jpeg" || file.ContentType == "image/png") { if (file.ContentLength <= Int32.Parse(ConfigurationManager.AppSettings["MaxFileSize"])) { CommentImageDTO commentImageDTO = MapperModule.CommentImagePostViewModel_To_CommentImageDTO(postImageComment); commentImageDTO.userID = this.User.Identity.GetUserId(); commentImageDTO.contentLength = file.ContentLength; commentImageDTO.contentType = file.ContentType; commentImageDTO.fileName = file.FileName; byte[] data = new byte[file.ContentLength]; file.InputStream.Read(data, 0, file.ContentLength); commentImageDTO.data = data; try { commentService.PostImageComment(commentImageDTO); } catch (DbEntityValidationException ex) { logger.Error(ex, "Comment not saved to the Database validation of entities fails"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (DbUpdateException ex) { logger.Error(ex, "Comment not saved to the Database"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (DataException ex) { logger.Error(ex, "Comment not saved to the Database"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (SystemException ex) { logger.Error(ex, "Exeption occured when user post new comment"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } string imageBase64Data = Convert.ToBase64String(data); string imageDataURL = string.Format("data:{0};base64,{1}", file.ContentType, imageBase64Data); return(JsonConvert.SerializeObject(new { id = 10, content = "", file_url = imageDataURL, file_mime_type = file.ContentType })); } else { return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = "File is too big, you can attach file whith size less than 5000 bytes" })); } } else { return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = "You can attach only photo in jpeg and png" })); } }