-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathCallFilesController.cs
More file actions
291 lines (232 loc) · 9.55 KB
/
Copy pathCallFilesController.cs
File metadata and controls
291 lines (232 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Resgrid.Framework;
using Resgrid.Model.Services;
using Resgrid.Providers.Claims;
using System.Threading.Tasks;
using Resgrid.Web.Services.Helpers;
using Resgrid.Web.Services.Models.v4.CallPriorities;
using System.Linq;
using Resgrid.Model;
using Resgrid.Model.Helpers;
using Resgrid.Web.Services.Models.v4.CallFiles;
using System;
using System.IO;
using System.Net.Mime;
using System.Threading;
using System.Web;
using System.Text;
namespace Resgrid.Web.Services.Controllers.v4
{
/// <summary>
/// Call Priorities, for example Low, Medium, High. Call Priorities can be system provided ones or custom for a department
/// </summary>
[Route("api/v{VersionId:apiVersion}/[controller]")]
[ApiVersion("4.0")]
[ApiExplorerSettings(GroupName = "v4")]
[Authorize(AuthenticationSchemes = "BasicAuthentication,SystemApiKey")]
public class CallFilesController : V4AuthenticatedApiControllerbaseSystemAuth
{
#region Members and Constructors
private readonly ICallsService _callsService;
private readonly IDepartmentsService _departmentsService;
public CallFilesController(ICallsService callsService, IDepartmentsService departmentsService)
{
_callsService = callsService;
_departmentsService = departmentsService;
}
#endregion Members and Constructors
/// <summary>
/// Get the files for a call in the Resgrid System
/// </summary>
/// <param name="callId">CallId to get the files for</param>
/// <param name="includeData">Include the data in the result</param>
/// <param name="type">Type of file to get (Any = 0, Audio = 1, Images = 2, Files = 3, Videos = 4)</param>
/// <returns></returns>
[HttpGet("GetFilesForCall")]
[ProducesResponseType(StatusCodes.Status200OK)]
[Authorize(Policy = ResgridResources.Call_View)]
public async Task<ActionResult<CallFilesResult>> GetFilesForCall(int callId, bool includeData, int type)
{
var result = new CallFilesResult();
var call = await _callsService.GetCallByIdAsync(callId);
if (call == null)
{
ResponseHelper.PopulateV4ResponseNotFound(result);
return Ok(result);
}
if (call.DepartmentId != DepartmentId)
return Unauthorized();
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
call = await _callsService.PopulateCallData(call, false, true, false, false, false, false, false, false, false);
if (call.Attachments != null && call.Attachments.Any())
{
foreach (var attachment in call.Attachments)
{
if (type == 0)
result.Data.Add(ConvertCallFileData(attachment, department, includeData));
else if (type == attachment.CallAttachmentType)
result.Data.Add(ConvertCallFileData(attachment, department, includeData));
}
result.PageSize = result.Data.Count;
result.Status = ResponseHelper.Success;
}
else
{
result.PageSize = 0;
result.Status = ResponseHelper.NotFound;
}
ResponseHelper.PopulateV4ResponseData(result);
return Ok(result);
}
/// <summary>
/// Get a users avatar from the Resgrid system based on their ID
/// </summary>
/// <param name="query">ID of the file</param>
/// <returns></returns>
[HttpGet("GetFile")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetFile(string query)
{
if (String.IsNullOrWhiteSpace(query))
return NotFound();
var decodedQuery = Encoding.UTF8.GetString(Convert.FromBase64String(query));
var decryptedQuery = SymmetricEncryption.Decrypt(decodedQuery, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase);
var items = decryptedQuery.Split(char.Parse("|"));
if (String.IsNullOrWhiteSpace(items[0]) || items[0] == "0" || String.IsNullOrWhiteSpace(items[1]))
return NotFound();
int departmentId = int.Parse(items[0].Trim());
string id = items[1].Trim();
var attachment = await _callsService.GetCallAttachmentAsync(int.Parse(id));
if (attachment == null)
return NotFound();
var call = await _callsService.GetCallByIdAsync(attachment.CallId);
if (call.DepartmentId != departmentId)
return Unauthorized();
if (String.IsNullOrWhiteSpace(attachment.FileName) || attachment.Data == null || attachment.Data.Length == 0)
return NotFound();
var extension = Path.GetExtension(attachment.FileName).ToLowerInvariant();
var contentType = FileHelper.GetContentTypeByExtension(extension);
if (String.IsNullOrWhiteSpace(contentType))
contentType = MediaTypeNames.Image.Png;
return File(attachment.Data, contentType);
}
/// <summary>
/// Get a users avatar from the Resgrid system based on their ID
/// </summary>
/// <param name="query">ID of the file</param>
/// <returns></returns>
[HttpHead("GetFile")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetFileHead(string query)
{
if (String.IsNullOrWhiteSpace(query))
return NotFound();
var decodedQuery = Encoding.UTF8.GetString(Convert.FromBase64String(query));
var decryptedQuery = SymmetricEncryption.Decrypt(decodedQuery, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase);
var items = decryptedQuery.Split(char.Parse("|"));
if (String.IsNullOrWhiteSpace(items[0]) || items[0] == "0" || String.IsNullOrWhiteSpace(items[1]))
return NotFound();
int departmentId = int.Parse(items[0].Trim());
string id = items[1].Trim();
var attachment = await _callsService.GetCallAttachmentAsync(int.Parse(id));
if (attachment == null)
return NotFound();
if (String.IsNullOrWhiteSpace(attachment.FileName) || attachment.Data == null || attachment.Data.Length == 0)
return NotFound();
var call = await _callsService.GetCallByIdAsync(attachment.CallId);
if (call.DepartmentId != departmentId)
return Unauthorized();
return Ok();
}
/// <summary>
/// Attaches a file to a call
/// </summary>
/// <param name="input">ID of the user</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns></returns>
[HttpPost("SaveCallFile")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status201Created)]
[Authorize(Policy = ResgridResources.Call_View)]
public async Task<ActionResult<SaveCallFileResult>> SaveCallFile(SaveCallFileInput input, CancellationToken cancellationToken)
{
var result = new SaveCallFileResult();
if (!ModelState.IsValid)
return BadRequest();
var call = await _callsService.GetCallByIdAsync(int.Parse(input.CallId));
if (call == null)
{
ResponseHelper.PopulateV4ResponseNotFound(result);
return Ok(result);
}
var effectiveDepartmentId = GetEffectiveDepartmentId(input.DepartmentId);
if (call.DepartmentId != effectiveDepartmentId)
return Unauthorized();
if (call.State != (int)CallStates.Active)
return BadRequest();
if (String.IsNullOrWhiteSpace(input.Data))
return BadRequest();
var callAttachment = new CallAttachment();
callAttachment.CallId = int.Parse(input.CallId);
callAttachment.CallAttachmentType = input.Type;
if (String.IsNullOrWhiteSpace(input.Name))
callAttachment.FileName = "cameraPhoneUpload.png";
else
callAttachment.FileName = input.Name;
callAttachment.UserId = input.UserId;
callAttachment.Timestamp = DateTime.UtcNow;
try
{
callAttachment.Data = Convert.FromBase64String(input.Data);
}
catch (Exception ex)
{
return BadRequest();
}
if (!String.IsNullOrWhiteSpace(input.Latitude))
{
callAttachment.Latitude = decimal.Parse(input.Latitude);
}
if (!String.IsNullOrWhiteSpace(input.Longitude))
{
callAttachment.Longitude = decimal.Parse(input.Longitude);
}
var saved = await _callsService.SaveCallAttachmentAsync(callAttachment, cancellationToken);
result.Id = saved.CallAttachmentId.ToString();
result.PageSize = 0;
result.Status = ResponseHelper.Created;
ResponseHelper.PopulateV4ResponseData(result);
return CreatedAtAction(nameof(GetFile), new { departmentId = call.DepartmentId, id = saved.CallAttachmentId }, result);
}
public static CallFileResultData ConvertCallFileData(CallAttachment attachment, Department department, bool includeData)
{
var file = new CallFileResultData();
file.Id = attachment.CallAttachmentId.ToString();
file.CallId = attachment.CallId.ToString();
file.FileName = attachment.FileName;
file.Type = attachment.CallAttachmentType;
var query = SymmetricEncryption.Encrypt($"{department.DepartmentId}|{attachment.CallAttachmentId}", Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase);
file.Url = Config.SystemBehaviorConfig.ResgridApiBaseUrl + "/api/v4/CallFiles/GetFile?query=" + Convert.ToBase64String(Encoding.UTF8.GetBytes(query));
file.Name = attachment.Name;
file.Size = attachment.Size.GetValueOrDefault();
file.Mime = FileHelper.GetContentTypeByExtension(Path.GetExtension(attachment.FileName));
if (attachment.Timestamp.HasValue)
file.Timestamp = attachment.Timestamp.Value.TimeConverterToString(department);
else
file.Timestamp = DateTime.UtcNow.TimeConverterToString(department);
if (!String.IsNullOrWhiteSpace(attachment.UserId))
file.UserId = attachment.UserId;
if (includeData)
file.Data = Convert.ToBase64String(attachment.Data);
return file;
}
}
}