This commit is contained in:
梁朝伟 2022-09-07 12:00:31 +08:00
parent 23f3576720
commit f29f543ac6
4 changed files with 43 additions and 29 deletions

View File

@ -83,7 +83,7 @@ function getQiniuToken(config, params) {
*/ */
function getLivestream(params, context) { function getLivestream(params, context) {
return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__awaiter(this, void 0, void 0, function () {
var streamTitle, expireAt, config, hub, path, key, bodyStr, token, url, obj; var streamTitle, expireAt, config, hub, path, key, bodyStr, contentType, token, url, obj;
return tslib_1.__generator(this, function (_a) { return tslib_1.__generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
@ -100,30 +100,35 @@ function getLivestream(params, context) {
bodyStr = JSON.stringify({ bodyStr = JSON.stringify({
key: key, key: key,
}); });
contentType = 'application/json';
return [4 /*yield*/, getQiniuToken(config, { return [4 /*yield*/, getQiniuToken(config, {
method: 'POST', method: 'POST',
path: path, path: path,
contentType: 'application/json', contentType: contentType,
bodyStr: bodyStr, bodyStr: bodyStr,
})]; })];
case 2: case 2:
token = (_a.sent()).token; token = (_a.sent()).token;
url = "http://pili.qiniuapi.com/v2/hubs/".concat(hub, "/stearms"); url = 'http://pili.qiniuapi.com/v2/hubs/test-play-space/streams';
return [4 /*yield*/, fetch(url, { console.log(bodyStr, url, token);
fetch(url, {
method: 'POST', method: 'POST',
headers: new Headers({ headers: {
'Authorization': token, Authorization: token,
'Content-Type': 'application/json', 'Content-Type': contentType,
}), },
body: bodyStr, body: bodyStr,
mode: 'no-cors', mode: 'no-cors',
}).then(function (res) { })
.then(function (res) {
console.log(res.json()); console.log(res.json());
})]; }).then(function (res) {
case 3: console.log(res);
_a.sent(); }).catch(function (e) {
console.log(e);
});
return [4 /*yield*/, getStreamObj(config, streamTitle, expireAt)]; return [4 /*yield*/, getStreamObj(config, streamTitle, expireAt)];
case 4: case 3:
obj = _a.sent(); obj = _a.sent();
return [2 /*return*/, obj]; return [2 /*return*/, obj];
} }
@ -165,7 +170,7 @@ function getStreamObj(config, streamTitle, expireAt) {
hub = config.hub, publishDomain = config.publishDomain, rtmpPlayDomain = config.rtmpPlayDomain, publishKey = config.publishKey, playKey = config.playKey; hub = config.hub, publishDomain = config.publishDomain, rtmpPlayDomain = config.rtmpPlayDomain, publishKey = config.publishKey, playKey = config.playKey;
signStr = "/".concat(hub, "/").concat(streamTitle, "?expire=").concat(expireAt); signStr = "/".concat(hub, "/").concat(streamTitle, "?expire=").concat(expireAt);
sourcePath = "/".concat(hub, "/").concat(streamTitle); sourcePath = "/".concat(hub, "/").concat(streamTitle);
token = (0, sign_1.urlSafeBase64Encode)((0, sign_1.hmacSha1)(signStr, publishKey)); token = (0, sign_1.base64ToUrlSafe)((0, sign_1.hmacSha1)(signStr, publishKey));
rtmpPushUrl = "rtmp://".concat(publishDomain).concat(signStr, "&token=").concat(token); rtmpPushUrl = "rtmp://".concat(publishDomain).concat(signStr, "&token=").concat(token);
t = expireAt.toString(16).toLowerCase(); t = expireAt.toString(16).toLowerCase();
playSign = ts_md5_1.Md5.hashStr(playKey + sourcePath + t).toString().toLowerCase(); playSign = ts_md5_1.Md5.hashStr(playKey + sourcePath + t).toString().toLowerCase();

View File

@ -29,6 +29,7 @@ var QiniuLiveInstance = /** @class */ (function () {
if (bodyStr && contentType && contentType !== "application/octet-stream") { if (bodyStr && contentType && contentType !== "application/octet-stream") {
data += bodyStr; data += bodyStr;
} }
console.log('data', data);
var sign = (0, sign_1.hmacSha1)(data, secretKey); var sign = (0, sign_1.hmacSha1)(data, secretKey);
var encodedSign = (0, sign_1.base64ToUrlSafe)(sign); var encodedSign = (0, sign_1.base64ToUrlSafe)(sign);
var toke = "Qiniu " + accessKey + ":" + encodedSign; var toke = "Qiniu " + accessKey + ":" + encodedSign;

View File

@ -5,7 +5,7 @@ import { Schema as Livestream } from '../general-app-domain/Livestream/Schema';
import QiniuLive from '../utils/externalUpload/qiniu_live'; import QiniuLive from '../utils/externalUpload/qiniu_live';
import { Datetime } from 'oak-domain/lib/types/DataType'; import { Datetime } from 'oak-domain/lib/types/DataType';
import { Md5 } from 'ts-md5'; import { Md5 } from 'ts-md5';
import { hmacSha1, urlSafeBase64Encode } from '../utils/sign'; import { hmacSha1, base64ToUrlSafe } from '../utils/sign';
async function getQiniuUploadInfo<ED extends EntityDict, Cxt extends GeneralRuntimeContext<ED>>( async function getQiniuUploadInfo<ED extends EntityDict, Cxt extends GeneralRuntimeContext<ED>>(
context: Cxt context: Cxt
@ -103,24 +103,31 @@ export async function getLivestream<ED extends EntityDict, Cxt extends GeneralRu
const bodyStr = JSON.stringify({ const bodyStr = JSON.stringify({
key, key,
}) })
const contentType = 'application/json';
const { token } = await getQiniuToken(config, { const { token } = await getQiniuToken(config, {
method: 'POST', method: 'POST',
path, path,
contentType: 'application/json', contentType,
bodyStr, bodyStr,
}); });
const url = `http://pili.qiniuapi.com/v2/hubs/${hub}/stearms`; const url = 'http://pili.qiniuapi.com/v2/hubs/test-play-space/streams';
await fetch(url, { console.log(bodyStr, url, token);
fetch(url, {
method: 'POST', method: 'POST',
headers: new Headers({ headers: {
'Authorization': token, Authorization: token,
'Content-Type': 'application/json', 'Content-Type': contentType,
}), },
body: bodyStr, body: bodyStr,
mode: 'no-cors', mode: 'no-cors',
}).then((res) => { })
.then((res) => {
console.log(res.json()); console.log(res.json());
}).then((res) => {
console.log(res);
}).catch((e) => {
console.log(e);
}) })
const obj = await getStreamObj(config, streamTitle, expireAt); const obj = await getStreamObj(config, streamTitle, expireAt);
return obj; return obj;
@ -184,7 +191,7 @@ async function getStreamObj(
const {hub, publishDomain, rtmpPlayDomain, publishKey, playKey } = config; const {hub, publishDomain, rtmpPlayDomain, publishKey, playKey } = config;
const signStr = `/${hub}/${streamTitle}?expire=${expireAt}`; const signStr = `/${hub}/${streamTitle}?expire=${expireAt}`;
const sourcePath = `/${hub}/${streamTitle}`; const sourcePath = `/${hub}/${streamTitle}`;
const token = urlSafeBase64Encode(hmacSha1(signStr, publishKey)); const token = base64ToUrlSafe(hmacSha1(signStr, publishKey));
const rtmpPushUrl = `rtmp://${publishDomain}${signStr}&token=${token}` const rtmpPushUrl = `rtmp://${publishDomain}${signStr}&token=${token}`
// 生成播放地址 // 生成播放地址
const t = expireAt.toString(16).toLowerCase(); const t = expireAt.toString(16).toLowerCase();

View File

@ -48,6 +48,7 @@ export default class QiniuLiveInstance {
if(bodyStr && contentType && contentType !== "application/octet-stream") { if(bodyStr && contentType && contentType !== "application/octet-stream") {
data+=bodyStr; data+=bodyStr;
} }
console.log('data', data);
const sign = hmacSha1(data, secretKey); const sign = hmacSha1(data, secretKey);
const encodedSign = base64ToUrlSafe(sign); const encodedSign = base64ToUrlSafe(sign);
const toke = "Qiniu " + accessKey + ":" + encodedSign; const toke = "Qiniu " + accessKey + ":" + encodedSign;