-
Notifications
You must be signed in to change notification settings - Fork 3
/
exercise5.cs
110 lines (88 loc) · 3.15 KB
/
exercise5.cs
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
[AllowAnonymous]
[HttpPost("Login")]
public async Task<IActionResult> Login(LoginModel model)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, model.Username),
new Claim(ClaimTypes.Role, "Admin"),
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties();
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
return RedirectToAction("Index", "Home");
}
[Authorize(Roles = "Admin")]
[HttpPost("ApplyGrayscale")]
public async Task<IActionResult> ApplyGrayscale()
{
[Authorize]
[HttpPost("ApplyGrayscale")]
public async Task<IActionResult> ApplyGrayscale()
{
using Microsoft.Extensions.Caching.Memory;
[ApiController]
[Route("[controller]")]
public class ImageProcessingController : ControllerBase
{
private readonly IMemoryCache _cache;
public ImageProcessingController(IMemoryCache cache)
{
_cache = cache;
}
[HttpPost("ApplyGrayscale")]
public async Task<IActionResult> ApplyGrayscale()
{
var file = Request.Form.Files[0];
if (file.Length > MAX_FILE_SIZE)
{
return BadRequest("File size is too large.");
}
if (!file.ContentType.StartsWith("image/"))
{
return BadRequest("Invalid file type.");
}
byte[] resultBytes;
if (!_cache.TryGetValue(file.FileName, out resultBytes))
{
try
{
using var memoryStream = new MemoryStream();
await file.CopyToAsync(memoryStream);
var bitmap = new Bitmap(memoryStream);
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var data = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
unsafe
{
var ptr = (byte*)data.Scan0;
for (int i = 0; i < data.Height; i++)
{
for (int j = 0; j < data.Width; j++)
{
var pixelAddress = ptr + (i * data.Stride) + (j * 3);
var brightness = (byte)((*pixelAddress + *(pixelAddress + 1) + *(pixelAddress + 2)) / 3);
*pixelAddress = brightness;
*(pixelAddress + 1) = brightness;
*(pixelAddress + 2) = brightness;
}
}
}
bitmap.UnlockBits(data);
using var resultStream = new MemoryStream();
bitmap.Save(resultStream, ImageFormat.Png);
resultBytes = resultStream.ToArray();
_cache.Set(file.FileName, resultBytes, TimeSpan.FromHours(1));
}
catch (Exception ex)
{
return StatusCode(500, "An error occurred while processing the image.");
}
}
return File(resultBytes, "image/png");
}
}
}
}