Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BMRHandler.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Web.Http;
  10. using iiie.Logs.DataAccess;
  11. using iiie.Logs.DBO;
  12. using iiie.WebApiUtils.DBO;
  13. namespace iiie.WebApiUtils.BusinessManager
  14. {
  15. /// <summary>
  16. /// Handle business manager results
  17. /// </summary>
  18. public class BMRHandler : ApiController
  19. {
  20. /// <summary>
  21. /// Converts ResultStatus codes to HttpStatusCodes codes
  22. /// </summary>
  23. /// <param name="status">The status code</param>
  24. /// <returns>The http code</returns>
  25. public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
  26. {
  27. if (status == ResultStatus.LoginError)
  28. return HttpStatusCode.Unauthorized;
  29. if (status == ResultStatus.PermissionError)
  30. return HttpStatusCode.Forbidden;
  31. if (status == ResultStatus.InputError)
  32. return HttpStatusCode.BadRequest;
  33. if (status == ResultStatus.NotFound)
  34. return HttpStatusCode.NotFound;
  35. return HttpStatusCode.InternalServerError;
  36. }
  37. /// <summary>
  38. /// Returns a string corresponding to the error
  39. /// </summary>
  40. /// <param name="result">The result of the operation</param>
  41. /// <returns>The error string</returns>
  42. public static string OpResultToString<T>(OpResult<T> result)
  43. {
  44. if (result.PublicDetails != null)
  45. return result.PublicDetails;
  46. if (result.Status == ResultStatus.LoginError)
  47. return "Bad username/password";
  48. if (result.Status == ResultStatus.PermissionError)
  49. return "You don\'t have the permission to do this action";
  50. if (result.Status == ResultStatus.InputError)
  51. return "Invalid data was provided";
  52. if (result.Status == ResultStatus.NotFound)
  53. return "Ressource not found";
  54. return "Internal error";
  55. }
  56. /// <summary>
  57. /// Handle business manager results
  58. /// </summary>
  59. /// <param name="result">The result to handle</param>
  60. /// <returns>The data to return to the user</returns>
  61. [NonAction]
  62. public T Handle<T>(OpResult<T> result)
  63. {
  64. if (result.Status == ResultStatus.Success)
  65. return result.Data;
  66. result.Log();
  67. var msg = Request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
  68. throw new HttpResponseException(msg);
  69. }
  70. /// <summary>
  71. /// Handle business manager results
  72. /// </summary>
  73. /// <param name="result">The result to handle</param>
  74. /// <returns>The data to return to the user</returns>
  75. [NonAction]
  76. public DboGetSingle<T> HandleSingle<T>(OpResult<T> result)
  77. {
  78. return new DboGetSingle<T>
  79. {
  80. Value = Handle(result)
  81. };
  82. }
  83. /// <summary>
  84. /// Save the uploaded file to the temp dir
  85. /// </summary>
  86. /// <returns>The file path</returns>
  87. [NonAction]
  88. public async Task<OpResult<string>> SaveFileToTemp()
  89. {
  90. IEnumerable<HttpContent> parts = null;
  91. try
  92. {
  93. Task.Factory.StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,
  94. CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait();
  95. }
  96. catch (Exception e)
  97. {
  98. return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to read post parts (maximum size exceeded?)");
  99. }
  100. HttpContent file = parts.FirstOrDefault();
  101. if (file == null)
  102. {
  103. return OpResult<string>.Error(ResultStatus.InputError, "No uploaded file", "");
  104. }
  105. try
  106. {
  107. var path = Path.GetTempFileName();
  108. using (var memoryStream = new MemoryStream(await file.ReadAsByteArrayAsync()))
  109. {
  110. using (var streamWriter = new FileStream(path, FileMode.OpenOrCreate))
  111. {
  112. memoryStream.WriteTo(streamWriter);
  113. memoryStream.Close();
  114. streamWriter.Close();
  115. }
  116. }
  117. return OpResult<string>.Ok(path);
  118. }
  119. catch (Exception e)
  120. {
  121. return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to write uploaded file");
  122. }
  123. }
  124. }
  125. }