123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web.Http;
- using iiie.Logs.DataAccess;
- using iiie.Logs.DBO;
-
- namespace iiie.WebApiUtils.BusinessManager
- {
- /// <summary>
- /// Handle business manager results
- /// </summary>
- public class BMRHandler : ApiController
- {
- /// <summary>
- /// Converts ResultStatus codes to HttpStatusCodes codes
- /// </summary>
- /// <param name="status">The status code</param>
- /// <returns>The http code</returns>
- public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
- {
- if (status == ResultStatus.LoginError)
- return HttpStatusCode.Unauthorized;
- if (status == ResultStatus.PermissionError)
- return HttpStatusCode.Forbidden;
- if (status == ResultStatus.InputError)
- return HttpStatusCode.BadRequest;
- if (status == ResultStatus.NotFound)
- return HttpStatusCode.NotFound;
- return HttpStatusCode.InternalServerError;
- }
-
- /// <summary>
- /// Returns a string corresponding to the error
- /// </summary>
- /// <param name="result">The result of the operation</param>
- /// <returns>The error string</returns>
- public static string OpResultToString<T>(OpResult<T> result)
- {
- if (result.PublicDetails != null)
- return result.PublicDetails;
-
- if (result.Status == ResultStatus.LoginError)
- return "Bad username/password";
- if (result.Status == ResultStatus.PermissionError)
- return "You don\'t have the permission to do this action";
- if (result.Status == ResultStatus.InputError)
- return "Invalid data was provided";
- if (result.Status == ResultStatus.NotFound)
- return "Ressource not found";
-
- return "Internal error";
- }
-
- /// <summary>
- /// Handle business manager results
- /// </summary>
- /// <param name="result">The result to handle</param>
- /// <returns>The data to return to the user</returns>
- [NonAction]
- public T Handle<T>(OpResult<T> result)
- {
- if (result.Status == ResultStatus.Success)
- return result.Data;
- result.Log();
- var msg = Request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
- throw new HttpResponseException(msg);
- }
-
- /// <summary>
- /// Save the uploaded file to the temp dir
- /// </summary>
- /// <returns>The file path</returns>
- public async Task<OpResult<string>> SaveFileToTemp()
- {
- if (!Request.Content.IsMimeMultipartContent())
- {
- return OpResult<string>.Error(ResultStatus.InputError, "Bad content type", "");
- }
- IEnumerable<HttpContent> parts = null;
- try
- {
- Task.Factory.StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,
- CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait();
- }
- catch (Exception e)
- {
- return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to read uploaded file");
- }
- HttpContent file = parts.FirstOrDefault();
- if (file == null)
- {
- return OpResult<string>.Error(ResultStatus.InputError, "No uploaded file", "");
- }
- try
- {
- var path = Path.GetTempFileName();
- using (var memoryStream = new MemoryStream(await file.ReadAsByteArrayAsync()))
- {
- using (var streamWriter = new FileStream(path, FileMode.OpenOrCreate))
- {
- memoryStream.WriteTo(streamWriter);
- memoryStream.Close();
- streamWriter.Close();
- }
- }
- return OpResult<string>.Ok(path);
- }
- catch (Exception e)
- {
- return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to write uploaded file");
- }
- }
- }
- }
|