A decentralized music tracking and discovery platform built on AT Protocol 馃幍
rocksky.app
spotify
atproto
lastfm
musicbrainz
scrobbling
listenbrainz
1/**
2 * Welcome to Cloudflare Workers! This is your first worker.
3 *
4 * - Run `npm run dev` in your terminal to start a development server
5 * - Open a browser tab at http://localhost:8787/ to see your worker in action
6 * - Run `npm run deploy` to publish your worker
7 *
8 * Bind resources to your worker in `wrangler.json`. After adding bindings, a type definition for the
9 * `Env` object can be regenerated with `npm run cf-typegen`.
10 *
11 * Learn more at https://developers.cloudflare.com/workers/
12 */
13import jwt from '@tsndr/cloudflare-worker-jwt';
14
15export default {
16 async fetch(request, env, ctx): Promise<Response> {
17 if (request.method === 'POST') {
18 const formData = await request.formData();
19 const file = formData.get('file') as File;
20
21 if (!file) {
22 return new Response('No file found in request!', { status: 400 });
23 }
24
25 const authorization = request.headers.get('Authorization');
26 const token = authorization?.replace('Bearer ', '');
27
28 if (!token) {
29 return new Response('No token found in request!', { status: 401 });
30 }
31
32 await jwt.verify(token, env.JWT_SECRET);
33
34 const fileBuffer = await file.arrayBuffer();
35 const fileName = file.name.includes('/') ? file.name.split('/').pop() : file.name;
36
37 await env.ROCKSKY_BUCKET.put(`covers/${fileName}`, fileBuffer);
38
39 return new Response('Post operation successful!');
40 }
41
42 // return json info about this worker
43 return new Response(
44 JSON.stringify({
45 name: 'RockSky Upload Worker',
46 version: '0.1.0',
47 description: 'A Cloudflare Worker for uploading files to RockSky',
48 }),
49 {
50 headers: {
51 'content-type': 'application/json',
52 },
53 }
54 );
55 },
56} satisfies ExportedHandler<Env>;