Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public class LogsController : ApiController
  2. {
  3.  
  4. public HttpResponseMessage PostLog(List<LogDto> logs)
  5. {
  6. if (logs != null && logs.Any())
  7. {
  8. var goodLogs = new List<Log>();
  9. var badLogs = new List<LogBad>();
  10.  
  11. foreach (var logDto in logs)
  12. {
  13. if (logDto.IsValid())
  14. {
  15. goodLogs.Add(logDto.ToLog());
  16. }
  17. else
  18. {
  19. badLogs.Add(logDto.ToLogBad());
  20. }
  21. }
  22.  
  23. if (goodLogs.Any())
  24. {
  25. _logsRepo.Save(goodLogs);
  26. }
  27.  
  28. if(badLogs.Any())
  29. {
  30. _logsBadRepo.Save(badLogs);
  31. }
  32.  
  33.  
  34. }
  35. return new HttpResponseMessage(HttpStatusCode.OK);
  36. }
  37. }
  38.  
  39. public class gZipHandler : DelegatingHandler
  40. {
  41.  
  42. protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
  43. {
  44. string encodingType = request.Headers.AcceptEncoding.First().Value;
  45.  
  46. request.Content = new DeCompressedContent(request.Content, encodingType);
  47.  
  48. return base.SendAsync(request, cancellationToken);
  49. }
  50. }
  51.  
  52. public class DeCompressedContent : HttpContent
  53. {
  54. private HttpContent originalContent;
  55. private string encodingType;
  56.  
  57. public DeCompressedContent(HttpContent content, string encodType)
  58. {
  59. originalContent = content;
  60. encodingType = encodType;
  61. }
  62.  
  63. protected override bool TryComputeLength(out long length)
  64. {
  65. length = -1;
  66.  
  67. return false;
  68. }
  69.  
  70.  
  71. protected override Task<Stream> CreateContentReadStreamAsync()
  72. {
  73. return base.CreateContentReadStreamAsync();
  74. }
  75.  
  76. protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
  77. {
  78. Stream compressedStream = null;
  79.  
  80. if (encodingType == "gzip")
  81. {
  82. compressedStream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true);
  83. }
  84.  
  85. return originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =>
  86. {
  87. if (compressedStream != null)
  88. {
  89. compressedStream.Dispose();
  90. }
  91. });
  92. }
  93.  
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement