Skip to content

Commit

Permalink
feat: add playback get route (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangmy21 authored May 8, 2024
1 parent 664f07b commit c730aa4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/helpers/docker_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const upload_contest_files = async (sub_base_dir: string, queue_front: queue_ele
const file_name = await fs_promises.readdir(`${sub_base_dir}/${queue_front.room_id}/output`);
const upload_file_promises = file_name.map(filename => {
console.log("filename: " + filename);
const key = `${contest_name}/arena/${queue_front.room_id}/${queue_front.room_id}.${filename}`;
const key = `${contest_name}/arena/${queue_front.room_id}/${filename}`;
const localFilePath = `${sub_base_dir}/${queue_front.room_id}/output/${filename}`;
return utils.uploadObject(localFilePath, key, cos, config)
.then(() => {
Expand Down
23 changes: 22 additions & 1 deletion src/helpers/hasura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,28 @@ export const get_map_name: any = async (map_id: string) => {
return query_map.contest_map[0].filename ?? null;
}


/**
* get contest_name by room_id
* @param {uuid} room_id
* @returns {string} contest_name
*/
export const get_contest_name_by_room: any = async (room_id: string) => {
const query_contest_name = await client.request(
gql`
query get_contest_name($room_id: uuid!) {
contest_room(where: {room_id: {_eq: $room_id}}) {
contest {
name
}
}
}
`,
{
room_id: room_id
}
);
return query_contest_name.contest_room[0]?.contest?.name ?? null;
}

/**
============================================================================
Expand Down
30 changes: 30 additions & 0 deletions src/routes/arena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,5 +501,35 @@ router.post("/finish", async (req, res) => {
}
});

/**
* @param {uuid} room_id
* 用于获取回放的路由,直接返回文件。
*/
router.get("/playback", async (req, res) => {
try {
const room_id = req.body.room_id;

const contest_name = await hasura.get_contest_name_by_room(room_id);
const base_directory = await utils.get_base_directory();
const playbackLocalPath = `${base_directory}/temp/${room_id}/playback.thuaipb`;
const playbackCOSPath = `${contest_name}/arena/${room_id}/playback.thuaipb`;

const cos = await utils.initCOS();
const config = await utils.getConfig();
await fs.mkdir(`${base_directory}/temp/${room_id}`, { recursive: true });
await utils.downloadObject(playbackCOSPath, playbackLocalPath, cos, config);

res.setHeader(
"Content-Disposition",
"attachment;filename=playback.thuaipb"
);
res.status(200).sendFile(playbackLocalPath, () => {
utils.deleteAllFilesInDir(`${base_directory}/temp/${room_id}`);
});
} catch (err) {
console.log(err);
return res.status(404).send("404 Not Found: Playback not found");
}
});

export default router;
31 changes: 31 additions & 0 deletions src/routes/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,5 +799,36 @@ router.post("/finish-one", async (req, res) => {
}
});

/**
* @param {uuid} room_id
* 用于获取回放的路由,直接返回文件。
*/
router.get("/playback", async (req, res) => {
try {
const room_id = req.body.room_id;

const contest_name = await hasura.get_contest_name_by_room(room_id);
const base_directory = await utils.get_base_directory();
const playbackLocalPath = `${base_directory}/temp/${room_id}/playback.thuaipb`;
const playbackCOSPath = `${contest_name}/competition/${room_id}/playback.thuaipb`;

const cos = await utils.initCOS();
const config = await utils.getConfig();
await fs.mkdir(`${base_directory}/temp/${room_id}`, { recursive: true });
await utils.downloadObject(playbackCOSPath, playbackLocalPath, cos, config);

res.setHeader(
"Content-Disposition",
"attachment;filename=playback.thuaipb"
);
res.status(200).sendFile(playbackLocalPath, () => {
utils.deleteAllFilesInDir(`${base_directory}/temp/${room_id}`);
});
} catch (err) {
console.log(err);
return res.status(404).send("404 Not Found: Playback not found");
}
});


export default router;

0 comments on commit c730aa4

Please sign in to comment.