Skip to content

Commit

Permalink
Merge branch 'master' of github.com:xiangmy21/api into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangmy21 committed Oct 22, 2023
2 parents 6b375ca + a9e0f8f commit d95eb27
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
75 changes: 58 additions & 17 deletions src/routes/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<boolean> {
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')
}
});
};

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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}`);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}

Expand Down

0 comments on commit d95eb27

Please sign in to comment.