Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding Games (Backblaze B2 + Cloudflare Integration) #36

Merged
merged 25 commits into from
Feb 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for deleting builds
nathangong committed Feb 21, 2024

Verified

This commit was signed with the committer’s verified signature.
BoBoBaSs84 Robert Peter Meyer
commit 26152fb142cac75874d209823cecec99b5009278
19 changes: 12 additions & 7 deletions src/pages/api/games/[id]/builds/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
deleteBuild,
getBuildUploadUrl,
} from "@/server/db/actions/BuildAction";
import { getGameById } from "@/server/db/actions/GameAction";
import connectB2 from "@/server/db/b2";
import { NextApiResponse } from "next";

export default async function handler(req: any, res: NextApiResponse) {
@@ -14,18 +17,20 @@ export default async function handler(req: any, res: NextApiResponse) {

switch (req.method) {
case "POST":
const b2 = await connectB2();

const bucketId = process.env.B2_BUCKET_ID;
const response = await b2.getUploadUrl({ bucketId });
const uploadUrl = response.data.uploadUrl;
const uploadAuthToken = response.data.authorizationToken;
const { uploadUrl, uploadAuthToken } = await getBuildUploadUrl();

return res.status(200).send({
success: true,
message: "URL and auth token generated successfully",
data: { uploadUrl, uploadAuthToken },
});
case "DELETE":
await deleteBuild(gameId);

return res.status(200).send({
success: true,
message: "Builds successfully deleted!",
});
}

return res.status(405).send({
32 changes: 32 additions & 0 deletions src/server/db/actions/BuildAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import connectB2 from "../b2";

export async function deleteBuild(id: string) {
const b2 = await connectB2();

const bucketId = process.env.B2_BUCKET_ID;
const response = await b2.listFileNames({
bucketId,
prefix: id + "/",
delimiter: "",
startFileName: "",
maxFileCount: 1000,
});

for (const file of response.data.files) {
await b2.deleteFileVersion({
fileId: file.fileId,
fileName: file.fileName,
});
}
}

export async function getBuildUploadUrl() {
const b2 = await connectB2();

const bucketId = process.env.B2_BUCKET_ID;
const response = await b2.getUploadUrl({ bucketId });
const uploadUrl = response.data.uploadUrl;
const uploadAuthToken = response.data.authorizationToken;

return { uploadUrl, uploadAuthToken };
}
6 changes: 4 additions & 2 deletions src/server/db/actions/GameAction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import GameModel from "../models/GameModel";
import connectMongoDB from "../mongodb";
import { deleteBuild } from "./BuildAction";

export async function createGame(data: any) {
await connectMongoDB();
@@ -16,8 +17,9 @@ export async function deleteGame(data: any) {
await connectMongoDB();
try {
const result = await GameModel.findByIdAndDelete(data);

// todo: delete build files from B2
if (result?.get("webGLBuild")) {
await deleteBuild(data);
}
if (!result) {
throw new ReferenceError("Game with given ID does not exist.");
}