Merge branch 'feature/Attachments' into develop

# Conflicts:
#	Step/Controllers/WebApi/MaintenanceController.cs
This commit is contained in:
Lucio Maranta
2018-06-19 17:12:44 +02:00
13 changed files with 256 additions and 22 deletions
+3 -3
View File
@@ -72,7 +72,7 @@ namespace Step.Controllers.SignalR
// Call the library functions
libraryError = ncHandler.RefreshAllAlarms();
}
MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh All Alarms.");
// MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh All Alarms.");
}
[SignalRAuthorize(FunctionAccess = ALARM_CMD, Action = ACTIONS.WRITE)]
@@ -87,7 +87,7 @@ namespace Step.Controllers.SignalR
throw new HubException(libraryError.localizationKey);
}
MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh Single Alarm. ID: " + id);
// MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh Single Alarm. ID: " + id);
}
[SignalRAuthorize(FunctionAccess = ALARM_CMD, Action = ACTIONS.WRITE)]
@@ -102,7 +102,7 @@ namespace Step.Controllers.SignalR
throw new HubException(libraryError.localizationKey);
}
MessageServices.Current.Publish(SHOW_MSG_UI, null, "Recovery Alarm. ID: " + id);
// MessageServices.Current.Publish(SHOW_MSG_UI, null, "Recovery Alarm. ID: " + id);
}
[SignalRAuthorize(FunctionAccess = NC_SOFTKEY, Action = ACTIONS.WRITE)]
@@ -1,16 +1,24 @@
using Step.Database.Controllers;
using Newtonsoft.Json;
using Step.Database.Controllers;
using Step.Model.DatabaseModels;
using Step.Model.DTOModels.MaintenanceModels;
using Step.NC;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using static CMS_CORE_Library.DataStructures;
using static Step.Model.Constants;
using static Step.Config.ServerConfig;
using static Step.Model.Constants;
namespace Step.Controllers.WebApi
{
@@ -51,7 +59,7 @@ namespace Step.Controllers.WebApi
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
if (userId == null)
return Unauthorized();
using (MaintenancesController maintenancesController = new MaintenancesController())
{
MaintenanceModel dbMaint = maintenancesController.Create(newMaint, Convert.ToInt32(userId.Value));
@@ -62,7 +70,7 @@ namespace Step.Controllers.WebApi
CmsError cmsError = ncHandler.GetMaintenanceData(out DTOMaintenanceModel maintenance, dbMaint.MaintenanceId, Convert.ToInt32(userId.Value));
if (cmsError.IsError())
return BadRequest(cmsError.localizationKey);
return Ok(maintenance);
}
}
@@ -179,7 +187,6 @@ namespace Step.Controllers.WebApi
if (!ModelState.IsValid)
return BadRequest(ModelState);
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
@@ -206,7 +213,7 @@ namespace Step.Controllers.WebApi
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
@@ -261,6 +268,7 @@ namespace Step.Controllers.WebApi
return Ok();
}
}
[Route("maintenance/{maintenanceId:int}/performe"), HttpPost]
@@ -298,5 +306,133 @@ namespace Step.Controllers.WebApi
}
}
}
[Route("maintenance/{maintenanceId:int}/attachment"), HttpPost]
public async Task<IHttpActionResult> AddAttachment(int maintenanceId)
{
// Check whether the POST operation is MultiPart?
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// Create CustomMultipartFormDataStreamProvider
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
// MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
List<string> files = new List<string>();
// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
var result = await Request.Content.ReadAsMultipartAsync(provider);
MaintenanceFileModel attachment = null;
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Remove foreach
foreach (MultipartFileData file in provider.FileData)
{
var fileName = Path.GetFileName(file.LocalFileName);
files.Add(fileName);
attachment = maintenancesController.AddAttachment(file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty), fileName, maintenanceId);
}
}
// Send OK Response along with saved file names to the client.
return Ok(attachment);
}
[Route("maintenance/{maintenanceId:int}/attachments"), HttpGet]
public IHttpActionResult GetAttachments(int maintenanceId)
{
using (MaintenancesController maintenancesController = new MaintenancesController())
{
return Ok(maintenancesController.FindAttachmentByMaintenance(maintenanceId));
}
}
[Route("attachment/{attachmentId:int}"), HttpGet]
public IHttpActionResult GetAttachment(int attachmentId)
{
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Get single file
MaintenanceFileModel attachment = maintenancesController.FindAttachmentById(attachmentId);
// Check if exist in db or physically
if (attachment == null)
return NotFound();
if (!File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName))
return NotFound();
return new FileResult(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName);
}
}
[Route("attachment/{attachmentId:int}"), HttpDelete]
public IHttpActionResult DeleteAttachment(int attachmentId)
{
using (MaintenancesController maintenancesController = new MaintenancesController())
{
// Get single file
MaintenanceFileModel attachment = maintenancesController.FindAttachmentById(attachmentId);
// Check if exist in db or physically
if (attachment == null)
return NotFound();
maintenancesController.DeleteAttachment(attachment);
if (File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName))
File.Delete(MAINTENANCE_ATTACHMENT_PATH + attachment.LocalFileName);
return Ok();
}
}
}
public class FileResult : IHttpActionResult
{
private readonly string FilePath;
private readonly string ContentType;
private readonly string FileName;
public FileResult(string filePath, string contentType = null, string fileName = null)
{
FilePath = filePath;
ContentType = contentType;
FileName = fileName;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.Run(() =>
{
// Create response
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(File.OpenRead(FilePath)) // Set file
};
// Set header content type
var contentType = ContentType ?? MimeMapping.GetMimeMapping(Path.GetExtension(FilePath));
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = FileName
};
return response;
}, cancellationToken);
}
}
// Override MultipartFormDataStreamProvider to override the GetLocalFileName
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path) : base(path)
{
}
public override string GetLocalFileName(HttpContentHeaders headers)
{
var fileName = headers.ContentDisposition.FileName.Replace("\"", string.Empty);
return Path.GetFileNameWithoutExtension(fileName) + Guid.NewGuid() + Path.GetExtension(fileName);
}
}
}