-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClipperController.cs
89 lines (81 loc) · 3.36 KB
/
ClipperController.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
using Microsoft.Extensions.Logging;
using Mikibot.AutoClipper.Abstract.Rquest;
using Mikibot.Database.Model;
using SimpleHttpServer.Pipeline;
using SimpleHttpServer.Pipeline.Middlewares;
using SimpleHttpServer.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Mikibot.AutoClipper.Service
{
public class ClipperController
{
public Func<RequestContext, Func<ValueTask>, ValueTask> Route { get; }
public ClipperController(ClipperService clipper, ILogger<ClipperController> logger)
{
Clipper = clipper;
Logger = logger;
Route = RouterMiddleware.Route("/api", (route) => route
.Get("/health", async (ctx) => await ctx.Http.Response.Ok("health"))
.Post("/loop-record", async (ctx) =>
{
if (int.TryParse(ctx.Http.Request.QueryString["Bid"], out var bid))
{
await ctx.Http.Response.Ok(await StartLoopRecording(bid, ctx.CancelToken));
}
else
{
await ctx.Http.Response.NotFound();
}
})
.Delete("/loop-record", async (ctx) =>
{
if (int.TryParse(ctx.Http.Request.QueryString["Bid"], out var bid))
{
await StopLoopRecording(bid, ctx.CancelToken);
await ctx.Http.Response.Ok(ctx.CancelToken);
}
else
{
await ctx.Http.Response.NotFound();
}
})
.Post("/danmaku-record", async (ctx) =>
{
if (int.TryParse(ctx.Http.Request.QueryString["Bid"], out var bid))
{
await ctx.Http.Response.Ok(await StartDanmakuRecording(bid, ctx.CancelToken));
}
else
{
await ctx.Http.Response.NotFound();
}
})
.Delete("/danmaku-record", async (ctx) =>
{
if (int.TryParse(ctx.Http.Request.QueryString["Bid"], out var bid))
{
await ctx.Http.Response.Ok(await StopDanmakuRecording(bid, ctx.CancelToken));
}
else
{
await ctx.Http.Response.NotFound();
}
}));
}
private ClipperService Clipper { get; }
public ILogger<ClipperController> Logger { get; }
private async ValueTask<StartRecordingResponse> StartDanmakuRecording(int bid, CancellationToken token)
=> new() { IsStarted = await Clipper.StartDanmakuRecording(bid, token) };
private async ValueTask<LiveStreamRecord> StopDanmakuRecording(int bid, CancellationToken token)
=> await Clipper.StopDanmakuRecording(bid, token);
private async ValueTask<StartRecordingResponse> StartLoopRecording(int bid, CancellationToken token)
=> new () { IsStarted = await Clipper.StartLoopRecording(bid, token) };
private async ValueTask StopLoopRecording(int bid, CancellationToken token)
=> await Clipper.CancelLoopRecording(bid, token);
}
}