Skip to content

Commit

Permalink
optimize fastify ad logic code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram committed May 14, 2024
1 parent c531063 commit 8a21a6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/main/core/server/routes/lab/ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';
import logger from '../../../logger';

if (typeof Array.prototype.toReversed !== 'function') {
Array.prototype.toReversed = function() {
Array.prototype.toReversed = function () {
const clonedList = this.slice();
// 倒序新数组
const reversedList = clonedList.reverse();
Expand Down Expand Up @@ -37,9 +37,9 @@ const urljoin = (fromPath, nowPath) => {
* @param headers 自定义访问m3u8的请求头,可以不传
* @returns {string}
*/
const fixAdM3u8Ai = async (m3u8_url: string, headers: object = null) => {
const fixAdM3u8Ai = async (m3u8_url: string, headers: object | null = null) => {
let ts = new Date().getTime();
let option = headers ? { headers: headers } : {};
let option = headers ? { headers } : {};

function b(s1, s2) {
let i = 0;
Expand Down
82 changes: 15 additions & 67 deletions src/main/core/server/routes/lab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { nanoid } from 'nanoid';

import { fixAdM3u8Ai } from './ad';
import { site, setting } from '../../../db/service';
import logger from '../../../logger';


const API_VERSION = 'api/v1';

Expand All @@ -20,8 +18,7 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {
}
} catch (error) {}

if (action === 'all') {
res = db?.text || '';
Expand All @@ -47,8 +44,7 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {
}
} catch (error) {}
const { oldSiteId: dbOldSiteId, debugSiteId: dbDebugSiteId } = db;

if (dbDebugSiteId) {
Expand Down Expand Up @@ -89,8 +85,7 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {
}
} catch (error) {}

const { oldSiteId: dbOldSiteId, debugSiteId: dbDebugSiteId } = db;

Expand All @@ -113,83 +108,36 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
`/${API_VERSION}/lab/removeAd`,
async (req: FastifyRequest<{ Querystring: { [key: string]: string } }>, reply: FastifyReply) => {
try {
const { url, type, headers } = req.query;
let _headers = null;
if (headers) {
try {
_headers = JSON.parse(headers);
} catch (e) {
}
}
const { url = '', type = '', headers = null } = req.query;

let data = {
code: 500,
msg: 'fail',
url: url,
headers: _headers,
type: type,
url,
headers: headers ? JSON.parse(headers as string) : null,
type,
content: null,
};

if (!type || !type.includes('.m3u8')) {
data.msg = 'fail';
if (!type || !type.endsWith('.m3u8')) {
data.msg = 'Invalid type provided';
data.code = 500;
} else {
// 逻辑处理
const content = await fixAdM3u8Ai(url, _headers);
const content = (await fixAdM3u8Ai(url, data.headers)) || '';
// 结果处理
if (content.indexOf('.ts') > -1) {
// const id = nanoid();
// // @ts-ignore
// await req.server.db.push(`/ad/${id}`, content);
data.code = 200;
// data.url = `http://127.0.0.1:9978/api/v1/lab/stream/${id}.m3u8`;
data.msg = 'success';
data.content = content;
} else {
data.code = 500;
data.msg = 'fail';
}
data.code = content.includes('.ts') ? 200 : 500;
data.msg = content.includes('.ts') ? 'success' : 'fail';
data.content = content;
}
if (data.content) {

if (data.code === 200 && data.content) {
reply.code(data.code).header('Content-Type', 'application/vnd.apple.mpegurl').send(data.content);
} else if (url && url.startsWith('http')) {
reply.redirect(302, url);
} else {
reply.code(200).send(data);
}
// reply.code(200).send(data);
} catch (err) {
reply.code(500).send(err);
}
},
);
fastify.get(
`/${API_VERSION}/lab/stream/:id.m3u8`,
async (req: FastifyRequest<{ Querystring: { [key: string]: string } }>, reply: FastifyReply) => {
try {
let data = '';
let id = '';
let code = 200;
try {
// @ts-ignore
id = req.params.id;
} catch (err) {
code = 500;
}

try {
// @ts-ignore
const db = await req.server.db.getData(`/ad/${id}`);
logger.info(db);

data = db;
} catch (err) {
logger.info(err);
code = 500;
}

reply.code(code).header('Content-Type', 'application/vnd.apple.mpegurl').send(data);
} catch (err) {
reply.code(500).send(err);
}
Expand Down

0 comments on commit 8a21a6c

Please sign in to comment.