diff --git a/config.example.json b/config.example.json index 2297f94..dd7aff1 100644 --- a/config.example.json +++ b/config.example.json @@ -1,7 +1,4 @@ { "cache_time": 7200, - "api_site": {}, - "storage": { - "type": "localstorage" - } + "api_site": {} } diff --git a/config.json b/config.json index 90ade99..24d5e68 100644 --- a/config.json +++ b/config.json @@ -81,8 +81,5 @@ "api": "https://ikunzyapi.com/api.php/provide/vod", "name": "iKun资源" } - }, - "storage": { - "type": "localstorage" } } diff --git a/next.config.js b/next.config.js index 152688e..76b805a 100644 --- a/next.config.js +++ b/next.config.js @@ -9,24 +9,14 @@ const nextConfig = { swcMinify: true, /** - * 在编译阶段把 storage.type 写入环境变量,供浏览器端动态切换存储方案。 + * 在编译阶段将 STORAGE_TYPE 写入环境变量,供浏览器端与服务端统一读取。 */ env: (function () { - const fs = require('fs'); - const path = require('path'); - - let storageType = 'localstorage'; - try { - const json = JSON.parse( - fs.readFileSync(path.join(__dirname, 'config.json'), 'utf-8') - ); - storageType = json?.storage?.type ?? 'localstorage'; - } catch { - // ignore – 保持默认值 - } + // 编译阶段优先使用传入的环境变量;默认 localstorage + const storageType = process.env.STORAGE_TYPE || 'localstorage'; return { - NEXT_PUBLIC_STORAGE_TYPE: storageType, + STORAGE_TYPE: storageType, }; })(), diff --git a/src/lib/config.ts b/src/lib/config.ts index bd219a8..fede9ed 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -74,8 +74,3 @@ export function getApiSites(): ApiSite[] { key, })); } - -export function getStorageConfig(): StorageConfig { - const config = getConfig(); - return config.storage || { type: 'localstorage' }; -} diff --git a/src/lib/db.client.ts b/src/lib/db.client.ts index 306809a..0d88d5d 100644 --- a/src/lib/db.client.ts +++ b/src/lib/db.client.ts @@ -31,10 +31,8 @@ const PLAY_RECORDS_KEY = 'moontv_play_records'; // ---- 环境变量 ---- const STORAGE_TYPE = - (process.env.NEXT_PUBLIC_STORAGE_TYPE as - | 'localstorage' - | 'database' - | undefined) || 'localstorage'; + (process.env.STORAGE_TYPE as 'localstorage' | 'database' | undefined) || + 'localstorage'; // ---------------- 搜索历史相关常量 ---------------- const SEARCH_HISTORY_KEY = 'moontv_search_history'; diff --git a/src/lib/db.ts b/src/lib/db.ts index 2fd789e..2e4791f 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,6 +1,9 @@ /* eslint-disable no-console */ -import { getStorageConfig } from './config'; +// storage type 常量: 'localstorage' | 'database',默认 'localstorage' +const STORAGE_TYPE = + (process.env.STORAGE_TYPE as 'localstorage' | 'database' | undefined) || + 'localstorage'; // 播放记录数据结构 export interface PlayRecord { @@ -85,9 +88,7 @@ class DatabaseStorage implements IStorage { // 创建存储实例 function createStorage(): IStorage { - const config = getStorageConfig(); - - switch (config.type) { + switch (STORAGE_TYPE) { case 'database': return new DatabaseStorage(); case 'localstorage':