You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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