選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BMRHandler.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. [NonAction]
  74. public async Task<OpResult<string>> SaveFileToTemp()
  75. {
  76. IEnumerable<HttpContent> parts = null;
  77. try
  78. {
  79. Task.Factory.StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,
  80. CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait();
  81. }
  82. catch (Exception e)
  83. {
  84. return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to read post parts (maximum size exceeded?)");
  85. }
  86. HttpContent file = parts.FirstOrDefault();
  87. if (file == null)
  88. {
  89. return OpResult<string>.Error(ResultStatus.InputError, "No uploaded file", "");
  90. }
  91. try
  92. {
  93. var path = Path.GetTempFileName();
  94. using (var memoryStream = new MemoryStream(await file.ReadAsByteArrayAsync()))
  95. {
  96. using (var streamWriter = new FileStream(path, FileMode.OpenOrCreate))
  97. {
  98. memoryStream.WriteTo(streamWriter);
  99. memoryStream.Close();
  100. streamWriter.Close();
  101. }
  102. }
  103. return OpResult<string>.Ok(path);
  104. }
  105. catch (Exception e)
  106. {
  107. return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to write uploaded file");
  108. }
  109. }
  110. }
  111. }