2025-08-12 21:50:58 +08:00
|
|
|
|
/* eslint-disable no-console, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
|
|
|
|
|
|
|
|
|
|
|
import { AdminConfig } from './admin.types';
|
2025-08-15 19:26:13 +08:00
|
|
|
|
import { KvrocksStorage } from './kvrocks.db';
|
2025-08-12 21:50:58 +08:00
|
|
|
|
import { RedisStorage } from './redis.db';
|
2025-12-03 21:34:05 +08:00
|
|
|
|
import { Favorite, IStorage, PlayRecord, SkipConfig, DanmakuFilterConfig } from './types';
|
2025-08-12 21:50:58 +08:00
|
|
|
|
import { UpstashRedisStorage } from './upstash.db';
|
|
|
|
|
|
|
|
|
|
|
|
// storage type 常量: 'localstorage' | 'redis' | 'upstash',默认 'localstorage'
|
|
|
|
|
|
const STORAGE_TYPE =
|
|
|
|
|
|
(process.env.NEXT_PUBLIC_STORAGE_TYPE as
|
|
|
|
|
|
| 'localstorage'
|
|
|
|
|
|
| 'redis'
|
|
|
|
|
|
| 'upstash'
|
2025-08-15 19:26:13 +08:00
|
|
|
|
| 'kvrocks'
|
2025-08-12 21:50:58 +08:00
|
|
|
|
| undefined) || 'localstorage';
|
|
|
|
|
|
|
|
|
|
|
|
// 创建存储实例
|
|
|
|
|
|
function createStorage(): IStorage {
|
|
|
|
|
|
switch (STORAGE_TYPE) {
|
|
|
|
|
|
case 'redis':
|
|
|
|
|
|
return new RedisStorage();
|
|
|
|
|
|
case 'upstash':
|
|
|
|
|
|
return new UpstashRedisStorage();
|
2025-08-15 19:26:13 +08:00
|
|
|
|
case 'kvrocks':
|
|
|
|
|
|
return new KvrocksStorage();
|
2025-08-12 21:50:58 +08:00
|
|
|
|
case 'localstorage':
|
|
|
|
|
|
default:
|
|
|
|
|
|
return null as unknown as IStorage;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 单例存储实例
|
|
|
|
|
|
let storageInstance: IStorage | null = null;
|
|
|
|
|
|
|
2025-12-18 23:31:10 +08:00
|
|
|
|
export function getStorage(): IStorage {
|
2025-08-12 21:50:58 +08:00
|
|
|
|
if (!storageInstance) {
|
|
|
|
|
|
storageInstance = createStorage();
|
|
|
|
|
|
}
|
|
|
|
|
|
return storageInstance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 工具函数:生成存储key
|
|
|
|
|
|
export function generateStorageKey(source: string, id: string): string {
|
|
|
|
|
|
return `${source}+${id}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出便捷方法
|
|
|
|
|
|
export class DbManager {
|
|
|
|
|
|
private storage: IStorage;
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.storage = getStorage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 播放记录相关方法
|
|
|
|
|
|
async getPlayRecord(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<PlayRecord | null> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
return this.storage.getPlayRecord(userName, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async savePlayRecord(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string,
|
|
|
|
|
|
record: PlayRecord
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
await this.storage.setPlayRecord(userName, key, record);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getAllPlayRecords(userName: string): Promise<{
|
|
|
|
|
|
[key: string]: PlayRecord;
|
|
|
|
|
|
}> {
|
|
|
|
|
|
return this.storage.getAllPlayRecords(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deletePlayRecord(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
await this.storage.deletePlayRecord(userName, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 收藏相关方法
|
|
|
|
|
|
async getFavorite(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<Favorite | null> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
return this.storage.getFavorite(userName, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async saveFavorite(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string,
|
|
|
|
|
|
favorite: Favorite
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
await this.storage.setFavorite(userName, key, favorite);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getAllFavorites(
|
|
|
|
|
|
userName: string
|
|
|
|
|
|
): Promise<{ [key: string]: Favorite }> {
|
|
|
|
|
|
return this.storage.getAllFavorites(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteFavorite(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
const key = generateStorageKey(source, id);
|
|
|
|
|
|
await this.storage.deleteFavorite(userName, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async isFavorited(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<boolean> {
|
|
|
|
|
|
const favorite = await this.getFavorite(userName, source, id);
|
|
|
|
|
|
return favorite !== null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 用户相关 ----------
|
|
|
|
|
|
async registerUser(userName: string, password: string): Promise<void> {
|
|
|
|
|
|
await this.storage.registerUser(userName, password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async verifyUser(userName: string, password: string): Promise<boolean> {
|
|
|
|
|
|
return this.storage.verifyUser(userName, password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查用户是否已存在
|
|
|
|
|
|
async checkUserExist(userName: string): Promise<boolean> {
|
|
|
|
|
|
return this.storage.checkUserExist(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 18:47:51 +08:00
|
|
|
|
async changePassword(userName: string, newPassword: string): Promise<void> {
|
|
|
|
|
|
await this.storage.changePassword(userName, newPassword);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteUser(userName: string): Promise<void> {
|
|
|
|
|
|
await this.storage.deleteUser(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 21:50:58 +08:00
|
|
|
|
// ---------- 搜索历史 ----------
|
|
|
|
|
|
async getSearchHistory(userName: string): Promise<string[]> {
|
|
|
|
|
|
return this.storage.getSearchHistory(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async addSearchHistory(userName: string, keyword: string): Promise<void> {
|
|
|
|
|
|
await this.storage.addSearchHistory(userName, keyword);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteSearchHistory(userName: string, keyword?: string): Promise<void> {
|
|
|
|
|
|
await this.storage.deleteSearchHistory(userName, keyword);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取全部用户名
|
|
|
|
|
|
async getAllUsers(): Promise<string[]> {
|
|
|
|
|
|
if (typeof (this.storage as any).getAllUsers === 'function') {
|
|
|
|
|
|
return (this.storage as any).getAllUsers();
|
|
|
|
|
|
}
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 管理员配置 ----------
|
|
|
|
|
|
async getAdminConfig(): Promise<AdminConfig | null> {
|
|
|
|
|
|
if (typeof (this.storage as any).getAdminConfig === 'function') {
|
|
|
|
|
|
return (this.storage as any).getAdminConfig();
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async saveAdminConfig(config: AdminConfig): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).setAdminConfig === 'function') {
|
|
|
|
|
|
await (this.storage as any).setAdminConfig(config);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 跳过片头片尾配置 ----------
|
|
|
|
|
|
async getSkipConfig(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<SkipConfig | null> {
|
|
|
|
|
|
if (typeof (this.storage as any).getSkipConfig === 'function') {
|
|
|
|
|
|
return (this.storage as any).getSkipConfig(userName, source, id);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async setSkipConfig(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string,
|
|
|
|
|
|
config: SkipConfig
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).setSkipConfig === 'function') {
|
|
|
|
|
|
await (this.storage as any).setSkipConfig(userName, source, id, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteSkipConfig(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
source: string,
|
|
|
|
|
|
id: string
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).deleteSkipConfig === 'function') {
|
|
|
|
|
|
await (this.storage as any).deleteSkipConfig(userName, source, id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getAllSkipConfigs(
|
|
|
|
|
|
userName: string
|
|
|
|
|
|
): Promise<{ [key: string]: SkipConfig }> {
|
|
|
|
|
|
if (typeof (this.storage as any).getAllSkipConfigs === 'function') {
|
|
|
|
|
|
return (this.storage as any).getAllSkipConfigs(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
2025-08-15 13:49:08 +08:00
|
|
|
|
|
2025-12-03 21:34:05 +08:00
|
|
|
|
// ---------- 弹幕过滤配置 ----------
|
|
|
|
|
|
async getDanmakuFilterConfig(userName: string): Promise<DanmakuFilterConfig | null> {
|
|
|
|
|
|
if (typeof (this.storage as any).getDanmakuFilterConfig === 'function') {
|
|
|
|
|
|
return (this.storage as any).getDanmakuFilterConfig(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async setDanmakuFilterConfig(
|
|
|
|
|
|
userName: string,
|
|
|
|
|
|
config: DanmakuFilterConfig
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).setDanmakuFilterConfig === 'function') {
|
|
|
|
|
|
await (this.storage as any).setDanmakuFilterConfig(userName, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteDanmakuFilterConfig(userName: string): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).deleteDanmakuFilterConfig === 'function') {
|
|
|
|
|
|
await (this.storage as any).deleteDanmakuFilterConfig(userName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 13:49:08 +08:00
|
|
|
|
// ---------- 数据清理 ----------
|
|
|
|
|
|
async clearAllData(): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).clearAllData === 'function') {
|
|
|
|
|
|
await (this.storage as any).clearAllData();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('存储类型不支持清空数据操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-18 21:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
// ---------- 通用键值存储 ----------
|
|
|
|
|
|
async getGlobalValue(key: string): Promise<string | null> {
|
|
|
|
|
|
if (typeof (this.storage as any).getGlobalValue === 'function') {
|
|
|
|
|
|
return (this.storage as any).getGlobalValue(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async setGlobalValue(key: string, value: string): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).setGlobalValue === 'function') {
|
|
|
|
|
|
await (this.storage as any).setGlobalValue(key, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async deleteGlobalValue(key: string): Promise<void> {
|
|
|
|
|
|
if (typeof (this.storage as any).deleteGlobalValue === 'function') {
|
|
|
|
|
|
await (this.storage as any).deleteGlobalValue(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-12 21:50:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出默认实例
|
|
|
|
|
|
export const db = new DbManager();
|