|
| 1 | +function assetAuth(ctx: Context) { |
| 2 | + const body = ctx.req.body |
| 3 | + const authKey = ctx.secret.key |
| 4 | + if (ctx.isAuthenticated) return |
| 5 | + if (body.key !== authKey) { |
| 6 | + ctx.throws(401, 'Unauthorized') |
| 7 | + } |
| 8 | +} |
| 9 | + |
1 | 10 | export default async function handler(ctx: Context) { |
2 | | - const { |
3 | | - timestamp, |
4 | | - process: processName, |
5 | | - key, |
6 | | - media, |
7 | | - meta, |
8 | | - } = ctx.req.body || {} |
9 | | - // handle GET |
10 | | - { |
11 | | - const [processInfo, mediaInfo] = await Promise.all([ |
12 | | - ctx.storage.cache.get('ps') as any as Promise<Process | undefined>, |
13 | | - ctx.storage.cache.get('media') as any as Promise<Media | undefined>, |
14 | | - ]) |
15 | | - if (!key) { |
16 | | - return { |
17 | | - processName: processInfo?.name, |
18 | | - processInfo, |
19 | | - mediaInfo, |
20 | | - } |
| 11 | + const method = ctx.req.method.toLowerCase() |
| 12 | + |
| 13 | + switch (method) { |
| 14 | + case 'get': { |
| 15 | + return GET(ctx) |
| 16 | + } |
| 17 | + case 'post': { |
| 18 | + assetAuth(ctx) |
| 19 | + return POST(ctx) |
| 20 | + } |
| 21 | + |
| 22 | + default: { |
| 23 | + ctx.throws(405, 'Method Not Allowed') |
21 | 24 | } |
22 | 25 | } |
| 26 | +} |
23 | 27 |
|
24 | | - const ts = +new Date() |
25 | | - // if (Math.abs(ts - timestamp) > 1000 * 10) { |
26 | | - // ctx.throws(400, 'this request is outdate') |
27 | | - // return |
28 | | - // } |
| 28 | +interface Media { |
| 29 | + title: string |
| 30 | + artist: string |
| 31 | +} |
29 | 32 |
|
30 | | - const processInfo: Process = { |
31 | | - name: processName, |
32 | | - ...meta, |
| 33 | +interface Process { |
| 34 | + name: string |
| 35 | + iconBase64?: string |
| 36 | + iconUrl?: string |
| 37 | + description?: string |
| 38 | +} |
| 39 | + |
| 40 | +async function GET(ctx: Context) { |
| 41 | + const [processInfo, mediaInfo] = await Promise.all([ |
| 42 | + ctx.storage.cache.get<Process>('ps'), |
| 43 | + ctx.storage.cache.get<Media>('media'), |
| 44 | + ]) |
| 45 | + |
| 46 | + return { |
| 47 | + processName: processInfo?.name, |
| 48 | + processInfo, |
| 49 | + mediaInfo, |
33 | 50 | } |
| 51 | +} |
| 52 | +async function POST(ctx: Context) { |
| 53 | + const [cachedProcessInfo, cachedMediaInfo] = await Promise.all([ |
| 54 | + ctx.storage.cache.get<Process>('ps'), |
| 55 | + ctx.storage.cache.get<Media>('media'), |
| 56 | + ]) |
34 | 57 |
|
35 | | - const validKey = (await ctx.secret.key) || 'testing' |
36 | | - if (key != validKey) |
37 | | - ctx.throws(401, "You haven't permission to update process info") |
| 58 | + const ts = +new Date() |
38 | 59 |
|
39 | | - const originalPsInfo: Process | null = (await ctx.storage.cache.get( |
40 | | - 'ps', |
41 | | - )) as any |
42 | | - await ctx.storage.cache.set('ps', processInfo, 300) |
| 60 | + const psInfo = ctx.req.body.process as Process |
| 61 | + const mediaInfo = ctx.req.body.media as Media |
43 | 62 |
|
44 | | - if (originalPsInfo?.name !== processName) |
| 63 | + if (psInfo?.name !== cachedProcessInfo?.name) |
45 | 64 | ctx?.broadcast?.('ps-update', { |
46 | | - processInfo, |
47 | | - process: processInfo.name, |
| 65 | + processInfo: psInfo, |
| 66 | + process: psInfo.name, |
48 | 67 | ts, |
49 | 68 | }) |
50 | | - if (media) { |
51 | | - await ctx.storage.cache.set('media', media, 10) |
52 | | - } |
53 | 69 |
|
54 | | - const mediaInfo: Media | undefined = (await ctx.storage.cache.get( |
55 | | - 'media', |
56 | | - )) as any |
57 | | - if (mediaInfo?.title !== media?.title) |
58 | | - ctx?.broadcast?.('media-update', media || null) |
| 70 | + if (cachedMediaInfo?.title !== mediaInfo?.title) |
| 71 | + ctx?.broadcast?.('media-update', mediaInfo || null) |
| 72 | + |
| 73 | + if (mediaInfo) { |
| 74 | + await ctx.storage.cache.set('media', mediaInfo, 10) |
| 75 | + } |
| 76 | + await ctx.storage.cache.set('ps', psInfo, 300) |
59 | 77 |
|
60 | 78 | return { |
61 | 79 | ok: 1, |
62 | | - mediaInfo, |
63 | | - process: processInfo.name, |
64 | | - processInfo, |
| 80 | + mediaInfo: cachedMediaInfo, |
| 81 | + process: cachedProcessInfo.name, |
| 82 | + processInfo: cachedProcessInfo, |
65 | 83 | timestamp: +new Date(), |
66 | 84 | } |
67 | 85 | } |
68 | | - |
69 | | -interface Media { |
70 | | - title: string |
71 | | - artist: string |
72 | | -} |
73 | | - |
74 | | -interface Process { |
75 | | - name: string |
76 | | - iconBase64?: string |
77 | | - iconUrl?: string |
78 | | - description?: string |
79 | | -} |
|
0 commit comments