Basic setup for R2 verification presigning
This commit is contained in:
+30
-7
@@ -3,6 +3,8 @@ import {
|
||||
ActivityVerificationsSchema,
|
||||
ActivityMediaKeysSchema,
|
||||
PlayerSchema,
|
||||
minToMs,
|
||||
Activity,
|
||||
} from "@blade-and-brawn/domain"
|
||||
import { cors } from "@elysiajs/cors";
|
||||
import { Elysia, redirect, status, t } from "elysia";
|
||||
@@ -17,7 +19,7 @@ import serverTiming from "@elysia/server-timing";
|
||||
import jwt from "@elysia/jwt";
|
||||
import { CommerceService, WOrderStatusSchema } from "./services/commerce";
|
||||
import cluster from "node:cluster";
|
||||
import { randomUUIDv7, sleep } from "bun";
|
||||
import { randomUUIDv7, sleep, S3Client } from "bun";
|
||||
import { DatabaseError } from "pg";
|
||||
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
@@ -47,6 +49,17 @@ const s = (() => {
|
||||
return { Standards, Calculator, Commerce, Accounts, Events, Assessments, Verifications };
|
||||
})();
|
||||
|
||||
// R2 BUCKETS
|
||||
// -----------------------
|
||||
const r2 = {
|
||||
verificationMedia: new S3Client({
|
||||
accessKeyId: env.R2_ACCESS_KEY_ID,
|
||||
secretAccessKey: env.R2_SECRET_ACCESS_KEY,
|
||||
bucket: "verification-media",
|
||||
endpoint: env.R2_URL,
|
||||
})
|
||||
};
|
||||
|
||||
// QUEUES
|
||||
// -----------------------
|
||||
const queues = [
|
||||
@@ -506,20 +519,30 @@ export const app = new Elysia()
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.post("/verifications", async ({ body: { assessmentId, activityMediaKeys }, accountId }) => {
|
||||
.post("/verifications", async ({ body: { assessmentId }, accountId }) => {
|
||||
const assessment = await s.Assessments.get(assessmentId, accountId);
|
||||
if (!assessment) return status(404, { error: "Assessment not found" });
|
||||
|
||||
// TODO: only allow a single active non-completed verification at a time
|
||||
// TODO: return R2 POST urls for the media
|
||||
|
||||
const created = await s.Verifications.create(assessmentId, activityMediaKeys);
|
||||
const created = await s.Verifications.create(assessmentId);
|
||||
if (!created) return status(409, { error: "This assessment already has a verification" });
|
||||
return created;
|
||||
|
||||
// Generate upload urls
|
||||
const activityMediaUrls: Record<string, string> = {};
|
||||
for (const activity of Object.values(Activity)) {
|
||||
activityMediaUrls[activity] = r2.verificationMedia.presign(
|
||||
`${assessmentId}/${activity}-${crypto.randomUUID()}`,
|
||||
{
|
||||
method: "PUT",
|
||||
expiresIn: 30 * 60 // 30 minutes
|
||||
});
|
||||
}
|
||||
|
||||
return { created, activityMediaUrls };
|
||||
}, {
|
||||
body: t.Object({
|
||||
assessmentId: BigIntIdSchema,
|
||||
activityMediaKeys: ActivityMediaKeysSchema,
|
||||
assessmentId: BigIntIdSchema
|
||||
})
|
||||
})
|
||||
.patch("/verifications/:id", async ({ params: { id }, body: { assessmentId, activityMediaKeys }, accountId }) => {
|
||||
|
||||
Reference in New Issue
Block a user