-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathImageController.cs
More file actions
165 lines (133 loc) · 5.42 KB
/
Copy pathImageController.cs
File metadata and controls
165 lines (133 loc) · 5.42 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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.IO;
using TransAction.Data.Models;
using TransAction.Data.Repositories.Interfaces;
using AutoMapper;
using TransAction.API.Responses;
namespace TransAction.API.Controllers
{
[Route("api/images")]
[ApiController]
public class ImageController : BaseController
{
private static readonly string JPEG = "image/jpeg";
private static readonly string PNG = "image/png";
private static readonly double MAX_SIZE = 512.0;
public ImageController(IHttpContextAccessor httpContextAccessor, ILogger<ActivityController> logger, IUnitOfWork unitOfWork, IMapper mapper) :
base(httpContextAccessor, logger, unitOfWork, mapper)
{ }
[AllowAnonymous]
[HttpGet("{guid}", Name = "GetImageByGuid")]
public IActionResult GetImageByGuid(string guid)
{
TraImage image = _unitOfWork.Image.GetProfileImage(guid);
if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
return File(image.Data, image.ContentType, image.Filename);
}
[HttpGet("user/{id}", Name = "GetImageByUserId")]
public IActionResult GetImageByUserId(int id)
{
TraImage image = _unitOfWork.Image.GetUserProfileImage(id);
if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
return File(image.Data, image.ContentType, image.Filename);
}
[HttpGet("user/{id}", Name = "GetImageByTeamId")]
public IActionResult GetImageByTeamId(int id)
{
TraImage image = _unitOfWork.Image.GetTeamProfileImage(id);
if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
return File(image.Data, image.ContentType, image.Filename);
}
[HttpPost()]
public IActionResult UploadProfileImage([FromForm]ImagePostDto model)
{
bool newRecord = true;
if (model.TeamId == null && model.UserId == null)
{
return BadRequest(new TransActionResponse("Need to specify either User Id or Team Id."));
}
if (model.TeamId != null && model.UserId != null)
{
return BadRequest(new TransActionResponse("Do not specify both User Id and Team Id."));
}
if (model.Data.ContentType != JPEG && model.Data.ContentType != PNG)
{
return BadRequest(new TransActionResponse("Unsupported file format. Only jpeg or png are supported."));
}
if (!ModelState.IsValid)
{
return BadRequest(new TransActionResponse(ModelState));
}
TraImage traImage = new TraImage();
if (model.UserId != null)
{
var image = _unitOfWork.Image.GetUserProfileImage(model.UserId.Value);
if (image != null)
{
traImage = image;
newRecord = false;
}
}
if (model.TeamId != null)
{
var image = _unitOfWork.Image.GetTeamProfileImage(model.TeamId.Value);
if (image != null)
{
traImage = image;
newRecord = false;
}
}
byte[] bytes = null;
using (var memoryStream = new MemoryStream())
{
model.Data.CopyTo(memoryStream);
bytes = memoryStream.ToArray();
using (Image<Rgba32> image = Image.Load<Rgba32>(bytes))
{
int maxWidthOrLength = Math.Max(image.Width, image.Height);
if (maxWidthOrLength > MAX_SIZE)
{
double ratio = MAX_SIZE / maxWidthOrLength;
image.Mutate(x => x.Resize(Convert.ToInt32(image.Width * ratio), Convert.ToInt32(image.Height * ratio)));
}
memoryStream.SetLength(0);
if (model.Data.ContentType == JPEG)
{
image.SaveAsJpeg(memoryStream);
}
else
{
image.SaveAsPng(memoryStream);
}
bytes = memoryStream.ToArray();
traImage.Width = image.Width;
traImage.Height = image.Height;
}
}
traImage.UserId = model.UserId;
traImage.TeamId = model.TeamId;
traImage.Data = bytes;
traImage.Guid = Guid.NewGuid().ToString();
traImage.Filename = model.Data.FileName;
traImage.Filesize = model.Data.Length;
traImage.ContentType = model.Data.ContentType;
if (newRecord)
_unitOfWork.Image.Create(traImage);
if (!_unitOfWork.Save())
{
return StatusCode(500, new TransActionResponse("Unable to save image to database."));
}
return Ok();
}
}
}