✨ feat(battle): 添加录像上传接口
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import { STATUS } from '@consts';
|
||||
import { UserModel } from '@db/User';
|
||||
import { LadderMatchRecModel } from '@db/LadderMatchRec';
|
||||
import { PvpRecordModel } from '@db/PvpRecord';
|
||||
import { BattleRecordModel } from '@db/BattleRecord';
|
||||
import { STATUS, WAR_TYPE } from '@consts';
|
||||
import { Controller } from 'egg';
|
||||
import * as fs from 'fs';
|
||||
import { RoleModel } from '@db/Role';
|
||||
import { NoticeModel } from '@db/Notice';
|
||||
import { ServerParamWithRole, GroupParam } from '../domain/gameField/serverlist';
|
||||
@@ -10,7 +15,10 @@ import { RedisClient } from 'redis';
|
||||
import { REDIS_KEY } from '@consts';
|
||||
import { RegionModel } from '@db/Region';
|
||||
import { getRandEelmWithWeight } from 'app/pubUtils/util';
|
||||
import { getLocalRplUrl, getRemoteRplUrl } from 'app/pubUtils/battleUtils'
|
||||
import { ChannelInfoModel } from '@db/ChannelInfo';
|
||||
const sendToWormhole = require('stream-wormhole');
|
||||
const pump = require('mz-modules/pump');
|
||||
|
||||
export default class GameController extends Controller {
|
||||
|
||||
@@ -178,4 +186,75 @@ export default class GameController extends Controller {
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.SUCCESS, { host: res.clientHost, port: res.clientPort });
|
||||
return
|
||||
}
|
||||
|
||||
public async upload() {
|
||||
const { ctx } = this;
|
||||
const parts = ctx.multipart();
|
||||
let part;
|
||||
let [writePath, token, battleCode, fullPath, remoteUrl] = ['', '', '', '', ''];
|
||||
while ((part = await parts()) != null) {
|
||||
if (part.length) {
|
||||
console.log('kv: ', `${part[0]}: ${part[1]}`);
|
||||
if (part[0] === 'token') {
|
||||
token = part[1];
|
||||
} else if (part[0] === 'battleCode') {
|
||||
battleCode = part[1];
|
||||
}
|
||||
} else {
|
||||
if (!part.filename) {
|
||||
continue;
|
||||
}
|
||||
console.log(part);
|
||||
if (part.fieldname === 'rpl' && battleCode !== '') {
|
||||
console.log('field: ', part.fieldname);
|
||||
console.log('filename: ', part.filename);
|
||||
if (token === '') {
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||
return;
|
||||
}
|
||||
const user = await UserModel.findUserByToken(token);
|
||||
if (!user) {
|
||||
console.error('token invalid');
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.TOKEN_ERR);
|
||||
return;
|
||||
}
|
||||
|
||||
let battleRec = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
||||
if (!battleRec) return ctx.body = ctx.service.utils.resResult(STATUS.BATTLE_NOT_FOUND);
|
||||
|
||||
let { warType, roleId } = battleRec;
|
||||
if (warType !== WAR_TYPE.PVP && warType !== WAR_TYPE.LADDER) return ctx.body = ctx.service.utils.resResult(STATUS.BATTLE_RPL_NOT_SUPPORT);
|
||||
|
||||
writePath = getLocalRplUrl(roleId, warType, battleCode);
|
||||
try {
|
||||
fs.accessSync(writePath);
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
fs.mkdirSync(writePath, { recursive: true });
|
||||
}
|
||||
}
|
||||
fullPath = `${writePath}/${battleCode}.bin`
|
||||
console.log(fullPath);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
fs.writeFileSync(fullPath, '');
|
||||
}
|
||||
const writeStream = fs.createWriteStream(fullPath);
|
||||
await pump(part, writeStream);
|
||||
|
||||
let updateDBRes;
|
||||
if (warType === WAR_TYPE.PVP) {
|
||||
updateDBRes = await PvpRecordModel.updateRplStatus(battleCode, true);
|
||||
} else if (warType === WAR_TYPE.LADDER) {
|
||||
updateDBRes = await LadderMatchRecModel.updateRplStatus(battleCode, true);
|
||||
}
|
||||
if (!updateDBRes) return ctx.body = ctx.service.utils.resResult(STATUS.BATTLE_RPL_UPDATE_ERR);
|
||||
remoteUrl = `${getRemoteRplUrl(ctx.app.config.realEnv, roleId, warType, battleCode)}/${battleCode}.bin`;
|
||||
} else {
|
||||
await sendToWormhole(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.SUCCESS, { rplUrl: remoteUrl });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ export default (app: Application) => {
|
||||
router.post('/update/getversion', controller.update.getversion);
|
||||
router.post('/update/getupdateurl', controller.update.getUpdateUrl);
|
||||
router.post('/web/reloadresource', app.middleware.gmTokenParser(), controller.game.reloadResource);
|
||||
router.post('/web/upload', controller.game.upload);
|
||||
|
||||
|
||||
// sdk 回调
|
||||
|
||||
@@ -55,6 +55,11 @@ export default (appInfo: EggAppInfo) => {
|
||||
dir: path.join(appInfo.baseDir, '/app/public'),
|
||||
};
|
||||
|
||||
config.multipart = {
|
||||
fileSize: '10mb',
|
||||
fileExtensions: ['.bin'], // 支持上传 bin 类型文件
|
||||
};
|
||||
|
||||
config.customLogger = {
|
||||
linkLogger: {
|
||||
file: path.join(appInfo.root, 'logs/web-server/link-log.log'),
|
||||
|
||||
@@ -49,12 +49,13 @@
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"request": "^2.88.2",
|
||||
"request-promise": "^4.2.6",
|
||||
"stream-to-array": "^2.3.0",
|
||||
"thinkingdata-node": "^1.2.2",
|
||||
"underscore": "^1.13.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^2.2.40",
|
||||
"@types/node": "^7.0.12",
|
||||
"@types/node": "^10.0.0",
|
||||
"@types/redis": "^2.8.31",
|
||||
"@types/request-promise": "^4.1.47",
|
||||
"@types/supertest": "^2.0.0",
|
||||
@@ -69,7 +70,7 @@
|
||||
"typescript": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.9.0"
|
||||
"node": ">=10.20.1"
|
||||
},
|
||||
"ci": {
|
||||
"version": "8"
|
||||
|
||||
Reference in New Issue
Block a user