Docker Build & Test / build (push) Canceled after 0s
Docker Build & Test / test (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/amd64) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm/v7) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm64) (push) Canceled after 0s
Build & Push Docker image / merge-images (push) Canceled after 0s
Release / release (push) Canceled after 0s
Release / docker (push) Canceled after 0s
578 lines
19 KiB
TypeScript
578 lines
19 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any, no-console, @typescript-eslint/no-non-null-assertion */
|
|
|
|
import { AdminConfig } from './admin.types';
|
|
import { getStorage } from './db';
|
|
import runtimeConfig from './runtime';
|
|
|
|
export interface ApiSite {
|
|
key: string;
|
|
api: string;
|
|
name: string;
|
|
detail?: string;
|
|
}
|
|
|
|
interface ConfigFileStruct {
|
|
cache_time?: number;
|
|
api_site: {
|
|
[key: string]: ApiSite;
|
|
};
|
|
}
|
|
|
|
export const API_CONFIG = {
|
|
search: {
|
|
path: '?ac=videolist&wd=',
|
|
pagePath: '?ac=videolist&wd={query}&pg={page}',
|
|
headers: {
|
|
'User-Agent':
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
|
Accept: 'application/json',
|
|
},
|
|
},
|
|
detail: {
|
|
path: '?ac=videolist&ids=',
|
|
headers: {
|
|
'User-Agent':
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
|
Accept: 'application/json',
|
|
},
|
|
},
|
|
};
|
|
|
|
// 在模块加载时根据环境决定配置来源
|
|
let fileConfig: ConfigFileStruct;
|
|
let cachedConfig: AdminConfig;
|
|
|
|
async function initConfig() {
|
|
if (cachedConfig) {
|
|
return;
|
|
}
|
|
|
|
if (process.env.DOCKER_ENV === 'true') {
|
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
const _require = eval('require') as NodeRequire;
|
|
const fs = _require('fs') as typeof import('fs');
|
|
const path = _require('path') as typeof import('path');
|
|
|
|
const configPath = path.join(process.cwd(), 'config.json');
|
|
const raw = fs.readFileSync(configPath, 'utf-8');
|
|
fileConfig = JSON.parse(raw) as ConfigFileStruct;
|
|
console.log('load dynamic config success');
|
|
} else {
|
|
// 默认使用编译时生成的配置
|
|
fileConfig = runtimeConfig as unknown as ConfigFileStruct;
|
|
}
|
|
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
|
if (storageType !== 'localstorage') {
|
|
// 数据库存储,读取并补全管理员配置
|
|
const storage = getStorage();
|
|
|
|
try {
|
|
// 尝试从数据库获取管理员配置
|
|
let adminConfig: AdminConfig | null = null;
|
|
if (storage && typeof (storage as any).getAdminConfig === 'function') {
|
|
adminConfig = await (storage as any).getAdminConfig();
|
|
}
|
|
|
|
// 获取所有用户(含注册/最后登录时间),用于补全 Users
|
|
let userNames: string[] = [];
|
|
const userTimeMap = new Map<
|
|
string,
|
|
{
|
|
created_at?: string;
|
|
created_at_estimated?: boolean;
|
|
last_login_at?: string;
|
|
last_login_estimated?: boolean;
|
|
}
|
|
>();
|
|
if (storage && typeof (storage as any).getAllUsers === 'function') {
|
|
try {
|
|
const rawUsers = await (storage as any).getAllUsers();
|
|
// getAllUsers 返回的是 User 对象数组,这里统一取用户名字符串,
|
|
// 否则会把整个对象写入 Users[].username(导致管理页渲染对象报 React #31)
|
|
// 同时保留时间字段,供用户列表展示注册/最后登录时间
|
|
(Array.isArray(rawUsers) ? rawUsers : []).forEach((u: any) => {
|
|
const n =
|
|
typeof u === 'string'
|
|
? u
|
|
: u && typeof u.username === 'string'
|
|
? u.username
|
|
: '';
|
|
if (!n) return;
|
|
userNames.push(n);
|
|
if (typeof u === 'object' && u) {
|
|
userTimeMap.set(n, {
|
|
created_at: u.created_at || undefined,
|
|
created_at_estimated: !!u.created_at_estimated,
|
|
last_login_at: u.last_login_at || undefined,
|
|
last_login_estimated: !!u.last_login_estimated,
|
|
});
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('获取用户列表失败:', e);
|
|
}
|
|
}
|
|
|
|
// 从文件中获取源信息,用于补全源
|
|
const apiSiteEntries = Object.entries(fileConfig.api_site);
|
|
|
|
if (adminConfig) {
|
|
// 补全 SourceConfig
|
|
const existed = new Set(
|
|
(adminConfig.SourceConfig || []).map((s) => s.key)
|
|
);
|
|
apiSiteEntries.forEach(([key, site]) => {
|
|
if (!existed.has(key)) {
|
|
adminConfig!.SourceConfig.push({
|
|
key,
|
|
name: site.name,
|
|
api: site.api,
|
|
detail: site.detail,
|
|
from: 'config',
|
|
disabled: false,
|
|
is_adult: (site as any).is_adult || false, // 确保 is_adult 字段被正确处理
|
|
});
|
|
}
|
|
});
|
|
|
|
// 检查现有源是否在 fileConfig.api_site 中,如果不在则标记为 custom
|
|
const apiSiteKeys = new Set(apiSiteEntries.map(([key]) => key));
|
|
adminConfig.SourceConfig.forEach((source) => {
|
|
if (!apiSiteKeys.has(source.key)) {
|
|
source.from = 'custom';
|
|
} else {
|
|
// 更新现有源的 is_adult 字段
|
|
const siteConfig = fileConfig.api_site[source.key];
|
|
if (siteConfig) {
|
|
source.is_adult = (siteConfig as any).is_adult || false;
|
|
}
|
|
}
|
|
});
|
|
|
|
const existedUsers = new Set(
|
|
(adminConfig.UserConfig.Users || []).map((u) => u.username)
|
|
);
|
|
userNames.forEach((uname) => {
|
|
if (!existedUsers.has(uname)) {
|
|
adminConfig!.UserConfig.Users.push({
|
|
username: uname,
|
|
role: 'user',
|
|
...(userTimeMap.get(uname) || {}),
|
|
});
|
|
} else {
|
|
// 已存在用户也回填时间字段(老配置升级时补上)
|
|
const exist = adminConfig!.UserConfig.Users.find(
|
|
(u) => u.username === uname
|
|
);
|
|
const t = userTimeMap.get(uname);
|
|
if (exist && t) {
|
|
if (!exist.created_at && t.created_at) {
|
|
exist.created_at = t.created_at;
|
|
exist.created_at_estimated = t.created_at_estimated;
|
|
}
|
|
if (!exist.last_login_at && t.last_login_at) {
|
|
exist.last_login_at = t.last_login_at;
|
|
exist.last_login_estimated = t.last_login_estimated;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
// 站长
|
|
const ownerUser = process.env.USERNAME;
|
|
if (ownerUser) {
|
|
adminConfig!.UserConfig.Users = adminConfig!.UserConfig.Users.filter(
|
|
(u) => u.username !== ownerUser
|
|
);
|
|
adminConfig!.UserConfig.Users.unshift({
|
|
username: ownerUser,
|
|
role: 'owner',
|
|
});
|
|
}
|
|
} else {
|
|
// 数据库中没有配置,创建新的管理员配置
|
|
let allUsers = userNames.map((uname) => ({
|
|
username: uname,
|
|
role: 'user',
|
|
...(userTimeMap.get(uname) || {}),
|
|
}));
|
|
const ownerUser = process.env.USERNAME;
|
|
if (ownerUser) {
|
|
allUsers = allUsers.filter((u) => u.username !== ownerUser);
|
|
allUsers.unshift({
|
|
username: ownerUser,
|
|
role: 'owner',
|
|
});
|
|
}
|
|
adminConfig = {
|
|
SiteConfig: {
|
|
SiteName: process.env.SITE_NAME || '米视TV',
|
|
Announcement:
|
|
process.env.ANNOUNCEMENT ||
|
|
'本网站仅提供影视信息搜索服务,所有内容均来自第三方网站。本站不存储任何视频资源,不对任何内容的准确性、合法性、完整性负责。',
|
|
SearchDownstreamMaxPage:
|
|
Number(process.env.NEXT_PUBLIC_SEARCH_MAX_PAGE) || 5,
|
|
SiteInterfaceCacheTime: fileConfig.cache_time || 7200,
|
|
ImageProxy: process.env.NEXT_PUBLIC_IMAGE_PROXY || '',
|
|
DoubanProxy: process.env.NEXT_PUBLIC_DOUBAN_PROXY || '',
|
|
},
|
|
UserConfig: {
|
|
AllowRegister: process.env.NEXT_PUBLIC_ENABLE_REGISTER === 'true',
|
|
Users: allUsers as any,
|
|
},
|
|
SourceConfig: apiSiteEntries.map(([key, site]) => ({
|
|
key,
|
|
name: site.name,
|
|
api: site.api,
|
|
detail: site.detail,
|
|
from: 'config',
|
|
disabled: false,
|
|
is_adult: (site as any).is_adult || false, // 确保 is_adult 字段被正确处理
|
|
})),
|
|
};
|
|
}
|
|
|
|
// 写回数据库(更新/创建)
|
|
if (storage && typeof (storage as any).setAdminConfig === 'function') {
|
|
await (storage as any).setAdminConfig(adminConfig);
|
|
}
|
|
|
|
// 更新缓存
|
|
cachedConfig = adminConfig;
|
|
} catch (err) {
|
|
console.error('加载管理员配置失败:', err);
|
|
}
|
|
} else {
|
|
// 本地存储直接使用文件配置
|
|
cachedConfig = {
|
|
SiteConfig: {
|
|
SiteName: process.env.SITE_NAME || '米视TV',
|
|
Announcement:
|
|
process.env.ANNOUNCEMENT ||
|
|
'本网站仅提供影视信息搜索服务,所有内容均来自第三方网站。本站不存储任何视频资源,不对任何内容的准确性、合法性、完整性负责。',
|
|
SearchDownstreamMaxPage:
|
|
Number(process.env.NEXT_PUBLIC_SEARCH_MAX_PAGE) || 5,
|
|
SiteInterfaceCacheTime: fileConfig.cache_time || 7200,
|
|
ImageProxy: process.env.NEXT_PUBLIC_IMAGE_PROXY || '',
|
|
DoubanProxy: process.env.NEXT_PUBLIC_DOUBAN_PROXY || '',
|
|
},
|
|
UserConfig: {
|
|
AllowRegister: process.env.NEXT_PUBLIC_ENABLE_REGISTER === 'true',
|
|
Users: [],
|
|
},
|
|
SourceConfig: Object.entries(fileConfig.api_site).map(([key, site]) => ({
|
|
key,
|
|
name: site.name,
|
|
api: site.api,
|
|
detail: site.detail,
|
|
from: 'config',
|
|
disabled: false,
|
|
})),
|
|
} as AdminConfig;
|
|
}
|
|
}
|
|
|
|
export async function getConfig(): Promise<AdminConfig> {
|
|
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
|
|
|
if (process.env.DOCKER_ENV === 'true' || storageType === 'localstorage') {
|
|
await initConfig();
|
|
return cachedConfig;
|
|
}
|
|
|
|
// 非 docker 环境且 DB 存储,直接读 db 配置
|
|
try {
|
|
const storage = getStorage();
|
|
let adminConfig: AdminConfig | null = null;
|
|
|
|
if (storage && typeof (storage as any).getAdminConfig === 'function') {
|
|
adminConfig = await (storage as any).getAdminConfig();
|
|
}
|
|
|
|
if (adminConfig) {
|
|
// 合并一些环境变量配置
|
|
adminConfig.SiteConfig.SiteName = process.env.SITE_NAME || '米视TV';
|
|
adminConfig.SiteConfig.Announcement =
|
|
process.env.ANNOUNCEMENT ||
|
|
'本网站仅提供影视信息搜索服务,所有内容均来自第三方网站。本站不存储任何视频资源,不对任何内容的准确性、合法性、完整性负责。';
|
|
adminConfig.UserConfig.AllowRegister =
|
|
process.env.NEXT_PUBLIC_ENABLE_REGISTER === 'true';
|
|
adminConfig.SiteConfig.ImageProxy =
|
|
process.env.NEXT_PUBLIC_IMAGE_PROXY || '';
|
|
adminConfig.SiteConfig.DoubanProxy =
|
|
process.env.NEXT_PUBLIC_DOUBAN_PROXY || '';
|
|
|
|
// 合并文件中的源信息
|
|
fileConfig = runtimeConfig as unknown as ConfigFileStruct;
|
|
const apiSiteEntries = Object.entries(fileConfig.api_site);
|
|
const existed = new Set((adminConfig.SourceConfig || []).map((s) => s.key));
|
|
apiSiteEntries.forEach(([key, site]) => {
|
|
if (!existed.has(key)) {
|
|
adminConfig!.SourceConfig.push({
|
|
key,
|
|
name: site.name,
|
|
api: site.api,
|
|
detail: site.detail,
|
|
from: 'config',
|
|
disabled: false,
|
|
is_adult: (site as any).is_adult || false, // 确保处理 is_adult 字段
|
|
});
|
|
}
|
|
});
|
|
|
|
// 检查现有源是否在 fileConfig.api_site 中,如果不在则标记为 custom
|
|
const apiSiteKeys = new Set(apiSiteEntries.map(([key]) => key));
|
|
adminConfig.SourceConfig.forEach((source) => {
|
|
if (!apiSiteKeys.has(source.key)) {
|
|
source.from = 'custom';
|
|
} else {
|
|
// 更新现有源的 is_adult 字段
|
|
const siteConfig = fileConfig.api_site[source.key];
|
|
if (siteConfig) {
|
|
source.is_adult = (siteConfig as any).is_adult || false;
|
|
}
|
|
}
|
|
});
|
|
|
|
const ownerUser = process.env.USERNAME || '';
|
|
// 检查配置中的站长用户是否和 USERNAME 匹配,如果不匹配则降级为普通用户
|
|
let containOwner = false;
|
|
adminConfig.UserConfig.Users.forEach((user) => {
|
|
if (user.username !== ownerUser && user.role === 'owner') {
|
|
user.role = 'user';
|
|
}
|
|
if (user.username === ownerUser) {
|
|
containOwner = true;
|
|
user.role = 'owner';
|
|
}
|
|
});
|
|
|
|
// 如果不在则添加
|
|
if (!containOwner) {
|
|
adminConfig.UserConfig.Users.unshift({
|
|
username: ownerUser,
|
|
role: 'owner',
|
|
});
|
|
}
|
|
cachedConfig = adminConfig;
|
|
} else {
|
|
// DB 无配置,执行一次初始化
|
|
await initConfig();
|
|
}
|
|
return cachedConfig;
|
|
} catch (error) {
|
|
// 如果数据库访问失败,回退到默认配置
|
|
await initConfig();
|
|
return cachedConfig;
|
|
}
|
|
}
|
|
|
|
export async function resetConfig() {
|
|
const storage = getStorage();
|
|
// 获取所有用户(含注册/最后登录时间),用于补全 Users
|
|
let userNames: string[] = [];
|
|
const userTimeMap = new Map<
|
|
string,
|
|
{
|
|
created_at?: string;
|
|
created_at_estimated?: boolean;
|
|
last_login_at?: string;
|
|
last_login_estimated?: boolean;
|
|
}
|
|
>();
|
|
if (storage && typeof (storage as any).getAllUsers === 'function') {
|
|
try {
|
|
const rawUsers = await (storage as any).getAllUsers();
|
|
(Array.isArray(rawUsers) ? rawUsers : []).forEach((u: any) => {
|
|
const n =
|
|
typeof u === 'string'
|
|
? u
|
|
: u && typeof u.username === 'string'
|
|
? u.username
|
|
: '';
|
|
if (!n) return;
|
|
userNames.push(n);
|
|
if (typeof u === 'object' && u) {
|
|
userTimeMap.set(n, {
|
|
created_at: u.created_at || undefined,
|
|
created_at_estimated: !!u.created_at_estimated,
|
|
last_login_at: u.last_login_at || undefined,
|
|
last_login_estimated: !!u.last_login_estimated,
|
|
});
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('获取用户列表失败:', e);
|
|
}
|
|
}
|
|
|
|
if (process.env.DOCKER_ENV === 'true') {
|
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
const _require = eval('require') as NodeRequire;
|
|
const fs = _require('fs') as typeof import('fs');
|
|
const path = _require('path') as typeof import('path');
|
|
|
|
const configPath = path.join(process.cwd(), 'config.json');
|
|
const raw = fs.readFileSync(configPath, 'utf-8');
|
|
fileConfig = JSON.parse(raw) as ConfigFileStruct;
|
|
console.log('load dynamic config success');
|
|
} else {
|
|
// 默认使用编译时生成的配置
|
|
fileConfig = runtimeConfig as unknown as ConfigFileStruct;
|
|
}
|
|
|
|
// 从文件中获取源信息,用于补全源
|
|
const apiSiteEntries = Object.entries(fileConfig.api_site);
|
|
let allUsers = userNames.map((uname) => ({
|
|
username: uname,
|
|
role: 'user',
|
|
...(userTimeMap.get(uname) || {}),
|
|
}));
|
|
const ownerUser = process.env.USERNAME;
|
|
if (ownerUser) {
|
|
allUsers = allUsers.filter((u) => u.username !== ownerUser);
|
|
allUsers.unshift({
|
|
username: ownerUser,
|
|
role: 'owner',
|
|
});
|
|
}
|
|
const adminConfig = {
|
|
SiteConfig: {
|
|
SiteName: process.env.SITE_NAME || '米视TV',
|
|
Announcement:
|
|
process.env.ANNOUNCEMENT ||
|
|
'本网站仅提供影视信息搜索服务,所有内容均来自第三方网站。本站不存储任何视频资源,不对任何内容的准确性、合法性、完整性负责。',
|
|
SearchDownstreamMaxPage:
|
|
Number(process.env.NEXT_PUBLIC_SEARCH_MAX_PAGE) || 5,
|
|
SiteInterfaceCacheTime: fileConfig.cache_time || 7200,
|
|
ImageProxy: process.env.NEXT_PUBLIC_IMAGE_PROXY || '',
|
|
DoubanProxy: process.env.NEXT_PUBLIC_DOUBAN_PROXY || '',
|
|
},
|
|
UserConfig: {
|
|
AllowRegister: process.env.NEXT_PUBLIC_ENABLE_REGISTER === 'true',
|
|
Users: allUsers as any,
|
|
},
|
|
SourceConfig: apiSiteEntries.map(([key, site]) => ({
|
|
key,
|
|
name: site.name,
|
|
api: site.api,
|
|
detail: site.detail,
|
|
from: 'config',
|
|
disabled: false,
|
|
})),
|
|
} as AdminConfig;
|
|
|
|
if (storage && typeof (storage as any).setAdminConfig === 'function') {
|
|
await (storage as any).setAdminConfig(adminConfig);
|
|
}
|
|
if (cachedConfig == null) {
|
|
// serverless 环境,直接使用 adminConfig
|
|
cachedConfig = adminConfig;
|
|
}
|
|
cachedConfig.SiteConfig = adminConfig.SiteConfig;
|
|
cachedConfig.UserConfig = adminConfig.UserConfig;
|
|
cachedConfig.SourceConfig = adminConfig.SourceConfig;
|
|
}
|
|
|
|
export async function getCacheTime(): Promise<number> {
|
|
const config = await getConfig();
|
|
return config.SiteConfig.SiteInterfaceCacheTime || 7200;
|
|
}
|
|
|
|
export async function getAvailableApiSites(filterAdult = false): Promise<ApiSite[]> {
|
|
const config = await getConfig();
|
|
|
|
// 防御性检查:确保 SourceConfig 存在且为数组
|
|
if (!config.SourceConfig || !Array.isArray(config.SourceConfig)) {
|
|
console.warn('SourceConfig is missing or not an array, returning empty array');
|
|
return [];
|
|
}
|
|
|
|
// 防御性处理:为每个源确保 is_adult 字段存在
|
|
let sites = config.SourceConfig
|
|
.filter((s) => !s.disabled)
|
|
.map((s) => ({
|
|
...s,
|
|
is_adult: s.is_adult === true // 严格检查,只有明确为 true 的才是成人内容
|
|
}));
|
|
|
|
// 如果需要过滤成人内容,则排除标记为成人内容的资源站
|
|
if (filterAdult) {
|
|
sites = sites.filter((s) => !s.is_adult);
|
|
}
|
|
|
|
return sites.map((s) => ({
|
|
key: s.key,
|
|
name: s.name,
|
|
api: s.api,
|
|
detail: s.detail,
|
|
}));
|
|
}
|
|
|
|
// 根据用户设置动态获取可用资源站(你的想法实现)
|
|
export async function getFilteredApiSites(userName?: string): Promise<ApiSite[]> {
|
|
const config = await getConfig();
|
|
|
|
// 防御性检查:确保 SourceConfig 存在且为数组
|
|
if (!config.SourceConfig || !Array.isArray(config.SourceConfig)) {
|
|
console.warn('SourceConfig is missing or not an array, returning empty array');
|
|
return [];
|
|
}
|
|
|
|
// 默认过滤成人内容
|
|
let shouldFilterAdult = true;
|
|
|
|
// 如果提供了用户名,获取用户设置
|
|
if (userName) {
|
|
try {
|
|
const storage = getStorage();
|
|
const userSettings = await storage.getUserSettings(userName);
|
|
shouldFilterAdult = userSettings?.filter_adult_content !== false; // 默认为 true
|
|
} catch (error) {
|
|
// 获取用户设置失败时,默认过滤成人内容
|
|
console.warn('Failed to get user settings, using default filter:', error);
|
|
}
|
|
}
|
|
|
|
// 防御性处理:为每个源确保 is_adult 字段存在
|
|
let sites = config.SourceConfig
|
|
.filter((s) => !s.disabled)
|
|
.map((s) => ({
|
|
...s,
|
|
is_adult: s.is_adult === true // 严格检查,只有明确为 true 的才是成人内容
|
|
}));
|
|
|
|
// 根据用户设置动态过滤成人内容源
|
|
if (shouldFilterAdult) {
|
|
sites = sites.filter((s) => !s.is_adult);
|
|
}
|
|
|
|
return sites.map((s) => ({
|
|
key: s.key,
|
|
name: s.name,
|
|
api: s.api,
|
|
detail: s.detail,
|
|
}));
|
|
}
|
|
|
|
// 获取成人内容资源站
|
|
export async function getAdultApiSites(): Promise<ApiSite[]> {
|
|
const config = await getConfig();
|
|
|
|
// 防御性检查:确保 SourceConfig 存在且为数组
|
|
if (!config.SourceConfig || !Array.isArray(config.SourceConfig)) {
|
|
console.warn('SourceConfig is missing or not an array, returning empty array');
|
|
return [];
|
|
}
|
|
|
|
// 防御性处理:严格检查成人内容标记
|
|
const adultSites = config.SourceConfig
|
|
.filter((s) => !s.disabled && s.is_adult === true); // 只有明确为 true 的才被认为是成人内容
|
|
|
|
return adultSites.map((s) => ({
|
|
key: s.key,
|
|
name: s.name,
|
|
api: s.api,
|
|
detail: s.detail,
|
|
}));
|
|
}
|