mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-18 22:33:26 +08:00
fix: middleware errors
This commit is contained in:
@@ -1,21 +1,15 @@
|
||||
import render from '@/utils/render';
|
||||
import * as path from 'node:path';
|
||||
import { config } from '@/config';
|
||||
const typeRegex = /\.(atom|rss|ums|debug\.json|json|\d+\.debug\.html)$/;
|
||||
import utils from '@/utils/common-utils';
|
||||
import type { MiddlewareHandler } from 'hono';
|
||||
import { Data } from '@/types';
|
||||
|
||||
const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
if (ctx.req.header('user-agent')?.includes('Reeder')) {
|
||||
ctx.req.path = ctx.req.path.replace(/.com$/, '');
|
||||
}
|
||||
|
||||
ctx.set('type', ctx.req.path.match(typeRegex) || ['', '']);
|
||||
ctx.req.path = ctx.req.path.replace(typeRegex, '');
|
||||
|
||||
await next();
|
||||
|
||||
const outputType = ctx.get('type')[1] || 'rss';
|
||||
const data: Data = ctx.get('data');
|
||||
const outputType = ctx.req.query('format') || 'rss';
|
||||
|
||||
// only enable when debugInfo=true
|
||||
if (config.debugInfo) {
|
||||
@@ -24,87 +18,83 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
return ctx.body(ctx.get('json') ? JSON.stringify(ctx.get('json'), null, 4) : JSON.stringify({ message: 'plugin does not set debug json' }));
|
||||
}
|
||||
|
||||
if (outputType.endsWith('.debug.html')) {
|
||||
if (outputType === 'debug.html') {
|
||||
ctx.header('Content-Type', 'text/html; charset=UTF-8');
|
||||
|
||||
const index = Number.parseInt(outputType.match(/(\d+)\.debug\.html$/)[1]);
|
||||
return ctx.body(ctx.get('data')?.item?.[index]?.description || `ctx.get('data')?.item?.[${index}]?.description not found`);
|
||||
const index = Number.parseInt(outputType.match(/(\d+)\.debug\.html$/)?.[1] || '0');
|
||||
return ctx.body(data?.item?.[index]?.description || `data?.item?.[${index}]?.description not found`);
|
||||
}
|
||||
}
|
||||
|
||||
const templateName = outputType === 'atom' ? 'atom.art' : 'rss.art';
|
||||
const template = path.resolve(__dirname, `../views/${templateName}`);
|
||||
const templateName = outputType === 'atom' ? 'atom.art' : 'rss.art';
|
||||
const template = path.resolve(__dirname, `../views/${templateName}`);
|
||||
|
||||
if (ctx.get('data')) {
|
||||
for (const prop of ['title', 'subtitle', 'author']) {
|
||||
if (ctx.get('data')[prop]) {
|
||||
ctx.get('data')[prop] = utils.collapseWhitespace(ctx.get('data')[prop]);
|
||||
if (data) {
|
||||
data.title = utils.collapseWhitespace(data.title);
|
||||
data.description && (data.description = utils.collapseWhitespace(data.description));
|
||||
data.author && (data.author = utils.collapseWhitespace(data.author));
|
||||
|
||||
if (data.item) {
|
||||
for (const item of data.item) {
|
||||
if (item.title) {
|
||||
item.title = utils.collapseWhitespace(item.title);
|
||||
// trim title length
|
||||
for (let length = 0, i = 0; i < item.title.length; i++) {
|
||||
length += Buffer.from(item.title[i]).length === 1 ? 1 : 2;
|
||||
if (length > config.titleLengthLimit) {
|
||||
item.title = `${item.title.slice(0, i)}...`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.get('data').item) {
|
||||
for (const item of ctx.get('data').item) {
|
||||
if (item.title) {
|
||||
item.title = utils.collapseWhitespace(item.title);
|
||||
// trim title length
|
||||
for (let length = 0, i = 0; i < item.title.length; i++) {
|
||||
length += Buffer.from(item.title[i]).length === 1 ? 1 : 2;
|
||||
if (length > config.titleLengthLimit) {
|
||||
item.title = `${item.title.slice(0, i)}...`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (typeof item.author === 'string') {
|
||||
item.author = utils.collapseWhitespace(item.author);
|
||||
} else if (typeof item.author === 'object' && item.author !== null) {
|
||||
for (const a of item.author) {
|
||||
a.name = utils.collapseWhitespace(a.name);
|
||||
}
|
||||
if (outputType !== 'json') {
|
||||
item.author = item.author.map((a: { name: string }) => a.name).join(', ');
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof item.author === 'string') {
|
||||
item.author = utils.collapseWhitespace(item.author);
|
||||
} else if (typeof item.author === 'object' && item.author !== null) {
|
||||
for (const a of item.author) {
|
||||
a.name = utils.collapseWhitespace(a.name);
|
||||
}
|
||||
if (outputType !== 'json') {
|
||||
item.author = item.author.map((a: {
|
||||
name: string;
|
||||
}) => a.name).join(', ');
|
||||
}
|
||||
}
|
||||
if (item.itunes_duration && ((typeof item.itunes_duration === 'string' && !item.itunes_duration.includes(':')) || (typeof item.itunes_duration === 'number' && !isNaN(item.itunes_duration)))) {
|
||||
item.itunes_duration = +item.itunes_duration;
|
||||
item.itunes_duration =
|
||||
Math.floor(item.itunes_duration / 3600) + ':' + (Math.floor((item.itunes_duration % 3600) / 60) / 100).toFixed(2).slice(-2) + ':' + (((item.itunes_duration % 3600) % 60) / 100).toFixed(2).slice(-2);
|
||||
}
|
||||
|
||||
if (item.itunes_duration && ((typeof item.itunes_duration === 'string' && !item.itunes_duration.includes(':')) || (typeof item.itunes_duration === 'number' && !isNaN(item.itunes_duration)))) {
|
||||
item.itunes_duration = +item.itunes_duration;
|
||||
item.itunes_duration =
|
||||
Math.floor(item.itunes_duration / 3600) + ':' + (Math.floor((item.itunes_duration % 3600) / 60) / 100).toFixed(2).slice(-2) + ':' + (((item.itunes_duration % 3600) % 60) / 100).toFixed(2).slice(-2);
|
||||
}
|
||||
|
||||
if (outputType !== 'rss') {
|
||||
item.pubDate = utils.convertDateToISO8601(item.pubDate);
|
||||
item.updated = utils.convertDateToISO8601(item.updated);
|
||||
}
|
||||
if (outputType !== 'rss') {
|
||||
item.pubDate && (item.pubDate = utils.convertDateToISO8601(item.pubDate));
|
||||
item.updated && (item.updated = utils.convertDateToISO8601(item.updated));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const currentDate = new Date();
|
||||
const data = {
|
||||
lastBuildDate: currentDate.toUTCString(),
|
||||
updated: currentDate.toISOString(),
|
||||
ttl: Math.trunc(config.cache.routeExpire / 60),
|
||||
atomlink: ctx.req.url,
|
||||
...ctx.get('data'),
|
||||
};
|
||||
const currentDate = new Date();
|
||||
const result = {
|
||||
lastBuildDate: currentDate.toUTCString(),
|
||||
updated: currentDate.toISOString(),
|
||||
ttl: Math.trunc(config.cache.routeExpire / 60),
|
||||
atomlink: ctx.req.url,
|
||||
...data,
|
||||
};
|
||||
|
||||
if (config.isPackage) {
|
||||
return ctx.body(data);
|
||||
}
|
||||
if (config.isPackage) {
|
||||
return ctx.json(result);
|
||||
}
|
||||
|
||||
if (outputType === 'ums') {
|
||||
ctx.header('Content-Type', 'application/json; charset=UTF-8');
|
||||
return ctx.body(render.rss3Ums(data));
|
||||
} else if (outputType === 'json') {
|
||||
ctx.header('Content-Type', 'application/feed+json; charset=UTF-8');
|
||||
return ctx.body(render.json(data));
|
||||
} else {
|
||||
return ctx.body(render.art(template, data));
|
||||
}
|
||||
if (outputType === 'ums') {
|
||||
ctx.header('Content-Type', 'application/json; charset=UTF-8');
|
||||
return ctx.body(render.rss3Ums(data));
|
||||
} else if (outputType === 'json') {
|
||||
ctx.header('Content-Type', 'application/feed+json; charset=UTF-8');
|
||||
return ctx.body(render.json(data));
|
||||
} else {
|
||||
return ctx.body(render.art(template, data));
|
||||
}
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
|
||||
Reference in New Issue
Block a user