✨ 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user