-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageAPI.cs
54 lines (48 loc) · 1.87 KB
/
ImageAPI.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
using AndrealImageGenerator.Beans;
using AndrealImageGenerator.Beans.Json;
using AndrealImageGenerator.Graphics;
using AndrealImageGenerator.Graphics.Generators;
using Microsoft.AspNetCore.Mvc;
namespace AndrealImageGenerator;
[ApiController]
public sealed class ImageAPI : ControllerBase
{
private static Task<BackGround> GetBestsGeneratorImage(UserBestsContent content, ImgVersion imgVersion)
=> imgVersion switch
{
ImgVersion.ImgV2 => new Best40Generator(content).Generate(),
_ => new Best30Generator(content).Generate()
};
private static FileStreamResult FileStreamResult(BackGround backGround)
{
var ms = new MemoryStream();
backGround.SaveAsJpgWithQuality(ms);
ms.Position = 0;
backGround.Dispose();
return new(ms, "image/jpeg");
}
[HttpPost("user/best30")]
public async Task<FileStreamResult> GetUserBest30(
[FromBody] ResponseRoot<UserBestsContent> json,
[FromQuery(Name = "image_version")] ImgVersion imgVersion)
{
var backGround = await GetBestsGeneratorImage(json.Content, imgVersion);
return FileStreamResult(backGround);
}
[HttpPost("user/best")]
public async Task<FileStreamResult> GetUserBest(
[FromBody] ResponseRoot<UserBestContent> json,
[FromQuery(Name = "image_version")] ImgVersion imgVersion)
{
var backGround = await RecordGenerator.Generate(json.Content, imgVersion);
return FileStreamResult(backGround);
}
[HttpPost("user/info")]
public async Task<FileStreamResult> GetUserInfo(
[FromBody] ResponseRoot<UserInfoContent> json,
[FromQuery(Name = "image_version")] ImgVersion imgVersion)
{
var backGround = await RecordGenerator.Generate(json.Content, imgVersion);
return FileStreamResult(backGround);
}
}