From a9e0f8f36a1cfd2cf270673255180a5ef9311888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E7=85=9C=E6=9B=88?= <115681059+zou-y-t@users.noreply.github.com> Date: Sun, 22 Oct 2023 22:40:15 +0800 Subject: [PATCH] feat(code.ts): add error info when file not exist (#1285) * feat(code.ts): add error info when file not exist * lint --------- Co-authored-by: zou-y-t <1354410159@qq.com> --- src/routes/code.ts | 75 +++++++++++++++++++++++++++++++++++---------- src/routes/users.ts | 4 +-- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/routes/code.ts b/src/routes/code.ts index c7c0b388..506b5751 100644 --- a/src/routes/code.ts +++ b/src/routes/code.ts @@ -9,9 +9,10 @@ import getSTS from "../helpers/sts"; import fStream from 'fs'; import COS from "cos-nodejs-sdk-v5"; import { join } from "path"; + const router = express.Router(); -const base_directory = process.env.NODE_ENV === "production" ? '/data/thuai6/' : '/home/guoyun/thuai6'; +const base_directory = process.env.NODE_ENV === "production" ? '/data/thuai6/' : '/home/alan/thuai6'; interface JwtCompilerPayload { team_id: string; @@ -145,7 +146,9 @@ router.post("/compile", async (req, res) => { "name/cos:AbortMultipartUpload", "name/cos:GetObject", "name/cos:DeleteObject", + "name/cos:GetBucket" ], "*"); + const cos = new COS({ getAuthorization: async (options: object, callback: ( params: COS.GetAuthorizationCallbackParams @@ -164,26 +167,61 @@ router.post("/compile", async (req, res) => { } } }); + const config = { bucket: 'eesast-1255334966', region: 'ap-beijing', }; + + const downloadObject = async function downloadObject(key: string, outputPath: string): Promise { return new Promise((resolve, reject) => { - cos.getObject({ - Bucket: config.bucket, - Region: config.region, - Key: key, - Output: fStream.createWriteStream(outputPath), - }, (err) => { - if (err) { - reject(err); - } else { - // console.log(data); - resolve(true); - } - }); + // console.log(key) + const key_prefix = key.match(/THUAI6\/.*\//i); + if (key_prefix) { + cos.getBucket({ + Bucket: config.bucket, + Region: config.region, + Prefix: key_prefix[0], /* Prefix表示列出的object的key以prefix开始,非必须 */ + Delimiter: "/" + }, function(err, data) { + if (err) { + reject(err); + } else { + // console.log(data.Contents.length); + let find_flag = false; + for (let i = 0;i < data.Contents.length && !find_flag;i++) { + // console.log("content: " + data.Contents[i].Key) + if (data.Contents[i].Key == key) { + // console.log('find key') + find_flag = true; + } + } + if (find_flag) { + cos.getObject({ + Bucket: config.bucket, + Region: config.region, + Key: key, + Output: fStream.createWriteStream(outputPath), + }, (err) => { + if (err) { + reject(err); + } else { + // console.log(data); + resolve(true); + } + }); + // resolve(true); + } else { + reject('not find key') + } + + } + }); + } else { + reject('key prefix error') + } }); }; @@ -228,8 +266,8 @@ router.post("/compile", async (req, res) => { // }); // return res.status(200).send("ok"); - deleteFile(`${base_directory}/${team_id}`).then(() => { - return downloadAllFiles(); + await deleteFile(`${base_directory}/${team_id}`).then(() => { + return downloadAllFiles(); }).then(async () => { // console.log('所有文件已下载完成'); // console.log("@@ files downloaded"); @@ -306,7 +344,10 @@ router.post("/compile", async (req, res) => { return res.status(400).send(err); } }).catch((err) => { - console.error('下载文件失败:', err); + // return err; + return new Promise((resolve, reject) => { + reject(err) + }) }); } catch (err) { return res.status(400).send(`STS选手代码下载失败:${err}`); diff --git a/src/routes/users.ts b/src/routes/users.ts index 25a674a8..bef6ccbc 100644 --- a/src/routes/users.ts +++ b/src/routes/users.ts @@ -15,7 +15,6 @@ import hasura from "../middlewares/hasura"; import type { MongoError } from "mongodb"; import { gql } from "graphql-request"; import { client } from ".."; - const router = express.Router(); router.put("/delete", async(req, res) => { @@ -160,7 +159,6 @@ router.put("/", authenticate(), async (req, res) => { router.post("/login", async (req, res) => { const { email, password } = req.body; - if (!email || !password) { return res .status(422) @@ -172,11 +170,13 @@ router.post("/login", async (req, res) => { if (!user) { // 没有 recaptcha 保护,不提示“用户不存在” + console.log("user missing") return res.status(401).end(); } const valid = await bcrypt.compare(password, user.password); if (!valid) { + console.log("password wrong") return res.status(401).end(); }