mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-13 01:17:29 +08:00
feat: data export and import
This commit is contained in:
58
src/lib/crypto.ts
Normal file
58
src/lib/crypto.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
|
||||
/**
|
||||
* 简单的对称加密工具
|
||||
* 使用 AES 加密算法
|
||||
*/
|
||||
export class SimpleCrypto {
|
||||
/**
|
||||
* 加密数据
|
||||
* @param data 要加密的数据
|
||||
* @param password 加密密码
|
||||
* @returns 加密后的字符串
|
||||
*/
|
||||
static encrypt(data: string, password: string): string {
|
||||
try {
|
||||
const encrypted = CryptoJS.AES.encrypt(data, password).toString();
|
||||
return encrypted;
|
||||
} catch (error) {
|
||||
throw new Error('加密失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密数据
|
||||
* @param encryptedData 加密的数据
|
||||
* @param password 解密密码
|
||||
* @returns 解密后的字符串
|
||||
*/
|
||||
static decrypt(encryptedData: string, password: string): string {
|
||||
try {
|
||||
const bytes = CryptoJS.AES.decrypt(encryptedData, password);
|
||||
const decrypted = bytes.toString(CryptoJS.enc.Utf8);
|
||||
|
||||
if (!decrypted) {
|
||||
throw new Error('解密失败,请检查密码是否正确');
|
||||
}
|
||||
|
||||
return decrypted;
|
||||
} catch (error) {
|
||||
throw new Error('解密失败,请检查密码是否正确');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证密码是否能正确解密数据
|
||||
* @param encryptedData 加密的数据
|
||||
* @param password 密码
|
||||
* @returns 是否能正确解密
|
||||
*/
|
||||
static canDecrypt(encryptedData: string, password: string): boolean {
|
||||
try {
|
||||
const decrypted = this.decrypt(encryptedData, password);
|
||||
return decrypted.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,6 +218,15 @@ export class DbManager {
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// ---------- 数据清理 ----------
|
||||
async clearAllData(): Promise<void> {
|
||||
if (typeof (this.storage as any).clearAllData === 'function') {
|
||||
await (this.storage as any).clearAllData();
|
||||
} else {
|
||||
throw new Error('存储类型不支持清空数据操作');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出默认实例
|
||||
|
||||
@@ -362,6 +362,27 @@ export class RedisStorage implements IStorage {
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
// 清空所有数据
|
||||
async clearAllData(): Promise<void> {
|
||||
try {
|
||||
// 获取所有用户
|
||||
const allUsers = await this.getAllUsers();
|
||||
|
||||
// 删除所有用户及其数据
|
||||
for (const username of allUsers) {
|
||||
await this.deleteUser(username);
|
||||
}
|
||||
|
||||
// 删除管理员配置
|
||||
await withRetry(() => this.client.del(this.adminConfigKey()));
|
||||
|
||||
console.log('所有数据已清空');
|
||||
} catch (error) {
|
||||
console.error('清空数据失败:', error);
|
||||
throw new Error('清空数据失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 单例 Redis 客户端
|
||||
|
||||
@@ -79,6 +79,9 @@ export interface IStorage {
|
||||
): Promise<void>;
|
||||
deleteSkipConfig(userName: string, source: string, id: string): Promise<void>;
|
||||
getAllSkipConfigs(userName: string): Promise<{ [key: string]: SkipConfig }>;
|
||||
|
||||
// 数据清理相关
|
||||
clearAllData(): Promise<void>;
|
||||
}
|
||||
|
||||
// 搜索结果数据结构
|
||||
|
||||
@@ -343,6 +343,27 @@ export class UpstashRedisStorage implements IStorage {
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
// 清空所有数据
|
||||
async clearAllData(): Promise<void> {
|
||||
try {
|
||||
// 获取所有用户
|
||||
const allUsers = await this.getAllUsers();
|
||||
|
||||
// 删除所有用户及其数据
|
||||
for (const username of allUsers) {
|
||||
await this.deleteUser(username);
|
||||
}
|
||||
|
||||
// 删除管理员配置
|
||||
await withRetry(() => this.client.del(this.adminConfigKey()));
|
||||
|
||||
console.log('所有数据已清空');
|
||||
} catch (error) {
|
||||
console.error('清空数据失败:', error);
|
||||
throw new Error('清空数据失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 单例 Upstash Redis 客户端
|
||||
|
||||
@@ -1,151 +1,6 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use client';
|
||||
|
||||
const CURRENT_VERSION = '2.2.1';
|
||||
|
||||
// 版本检查结果枚举
|
||||
export enum UpdateStatus {
|
||||
HAS_UPDATE = 'has_update', // 有新版本
|
||||
NO_UPDATE = 'no_update', // 无新版本
|
||||
FETCH_FAILED = 'fetch_failed', // 获取失败
|
||||
}
|
||||
|
||||
// 远程版本检查URL配置
|
||||
const VERSION_CHECK_URLS = [
|
||||
'https://raw.githubusercontent.com/MoonTechLab/LunaTV/main/VERSION.txt',
|
||||
];
|
||||
|
||||
/**
|
||||
* 检查是否有新版本可用
|
||||
* @returns Promise<UpdateStatus> - 返回版本检查状态
|
||||
*/
|
||||
export async function checkForUpdates(): Promise<UpdateStatus> {
|
||||
try {
|
||||
// 尝试从主要URL获取版本信息
|
||||
const primaryVersion = await fetchVersionFromUrl(VERSION_CHECK_URLS[0]);
|
||||
if (primaryVersion) {
|
||||
return compareVersions(primaryVersion);
|
||||
}
|
||||
|
||||
// 如果主要URL失败,尝试备用URL
|
||||
const backupVersion = await fetchVersionFromUrl(VERSION_CHECK_URLS[1]);
|
||||
if (backupVersion) {
|
||||
return compareVersions(backupVersion);
|
||||
}
|
||||
|
||||
// 如果两个URL都失败,返回获取失败状态
|
||||
return UpdateStatus.FETCH_FAILED;
|
||||
} catch (error) {
|
||||
console.error('版本检查失败:', error);
|
||||
return UpdateStatus.FETCH_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定URL获取版本信息
|
||||
* @param url - 版本信息URL
|
||||
* @returns Promise<string | null> - 版本字符串或null
|
||||
*/
|
||||
async function fetchVersionFromUrl(url: string): Promise<string | null> {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时
|
||||
|
||||
// 添加时间戳参数以避免缓存
|
||||
const timestamp = Date.now();
|
||||
const urlWithTimestamp = url.includes('?')
|
||||
? `${url}&_t=${timestamp}`
|
||||
: `${url}?_t=${timestamp}`;
|
||||
|
||||
const response = await fetch(urlWithTimestamp, {
|
||||
method: 'GET',
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
},
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const version = await response.text();
|
||||
return version.trim();
|
||||
} catch (error) {
|
||||
console.warn(`从 ${url} 获取版本信息失败:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较版本号
|
||||
* @param remoteVersion - 远程版本号
|
||||
* @returns UpdateStatus - 返回版本比较结果
|
||||
*/
|
||||
function compareVersions(remoteVersion: string): UpdateStatus {
|
||||
// 如果版本号相同,无需更新
|
||||
if (remoteVersion === CURRENT_VERSION) {
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
|
||||
try {
|
||||
// 解析版本号为数字数组 [X, Y, Z]
|
||||
const currentParts = CURRENT_VERSION.split('.').map((part) => {
|
||||
const num = parseInt(part, 10);
|
||||
if (isNaN(num) || num < 0) {
|
||||
throw new Error(`无效的版本号格式: ${CURRENT_VERSION}`);
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
const remoteParts = remoteVersion.split('.').map((part) => {
|
||||
const num = parseInt(part, 10);
|
||||
if (isNaN(num) || num < 0) {
|
||||
throw new Error(`无效的版本号格式: ${remoteVersion}`);
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
// 标准化版本号到3个部分
|
||||
const normalizeVersion = (parts: number[]) => {
|
||||
if (parts.length >= 3) {
|
||||
return parts.slice(0, 3); // 取前三个元素
|
||||
} else {
|
||||
// 不足3个的部分补0
|
||||
const normalized = [...parts];
|
||||
while (normalized.length < 3) {
|
||||
normalized.push(0);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
};
|
||||
|
||||
const normalizedCurrent = normalizeVersion(currentParts);
|
||||
const normalizedRemote = normalizeVersion(remoteParts);
|
||||
|
||||
// 逐级比较版本号
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (normalizedRemote[i] > normalizedCurrent[i]) {
|
||||
return UpdateStatus.HAS_UPDATE;
|
||||
} else if (normalizedRemote[i] < normalizedCurrent[i]) {
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
// 如果当前级别相等,继续比较下一级
|
||||
}
|
||||
|
||||
// 所有级别都相等,无需更新
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
} catch (error) {
|
||||
console.error('版本号比较失败:', error);
|
||||
// 如果版本号格式无效,回退到字符串比较
|
||||
return remoteVersion !== CURRENT_VERSION
|
||||
? UpdateStatus.HAS_UPDATE
|
||||
: UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
}
|
||||
|
||||
// 导出当前版本号供其他地方使用
|
||||
export { compareVersions, CURRENT_VERSION };
|
||||
export { CURRENT_VERSION };
|
||||
|
||||
148
src/lib/version_check.ts
Normal file
148
src/lib/version_check.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use client';
|
||||
|
||||
import { CURRENT_VERSION } from "@/lib/version";
|
||||
|
||||
// 版本检查结果枚举
|
||||
export enum UpdateStatus {
|
||||
HAS_UPDATE = 'has_update', // 有新版本
|
||||
NO_UPDATE = 'no_update', // 无新版本
|
||||
FETCH_FAILED = 'fetch_failed', // 获取失败
|
||||
}
|
||||
|
||||
// 远程版本检查URL配置
|
||||
const VERSION_CHECK_URLS = [
|
||||
'https://raw.githubusercontent.com/MoonTechLab/LunaTV/main/VERSION.txt',
|
||||
];
|
||||
|
||||
/**
|
||||
* 检查是否有新版本可用
|
||||
* @returns Promise<UpdateStatus> - 返回版本检查状态
|
||||
*/
|
||||
export async function checkForUpdates(): Promise<UpdateStatus> {
|
||||
try {
|
||||
// 尝试从主要URL获取版本信息
|
||||
const primaryVersion = await fetchVersionFromUrl(VERSION_CHECK_URLS[0]);
|
||||
if (primaryVersion) {
|
||||
return compareVersions(primaryVersion);
|
||||
}
|
||||
|
||||
// 如果主要URL失败,尝试备用URL
|
||||
const backupVersion = await fetchVersionFromUrl(VERSION_CHECK_URLS[1]);
|
||||
if (backupVersion) {
|
||||
return compareVersions(backupVersion);
|
||||
}
|
||||
|
||||
// 如果两个URL都失败,返回获取失败状态
|
||||
return UpdateStatus.FETCH_FAILED;
|
||||
} catch (error) {
|
||||
console.error('版本检查失败:', error);
|
||||
return UpdateStatus.FETCH_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定URL获取版本信息
|
||||
* @param url - 版本信息URL
|
||||
* @returns Promise<string | null> - 版本字符串或null
|
||||
*/
|
||||
async function fetchVersionFromUrl(url: string): Promise<string | null> {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时
|
||||
|
||||
// 添加时间戳参数以避免缓存
|
||||
const timestamp = Date.now();
|
||||
const urlWithTimestamp = url.includes('?')
|
||||
? `${url}&_t=${timestamp}`
|
||||
: `${url}?_t=${timestamp}`;
|
||||
|
||||
const response = await fetch(urlWithTimestamp, {
|
||||
method: 'GET',
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
},
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const version = await response.text();
|
||||
return version.trim();
|
||||
} catch (error) {
|
||||
console.warn(`从 ${url} 获取版本信息失败:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较版本号
|
||||
* @param remoteVersion - 远程版本号
|
||||
* @returns UpdateStatus - 返回版本比较结果
|
||||
*/
|
||||
export function compareVersions(remoteVersion: string): UpdateStatus {
|
||||
// 如果版本号相同,无需更新
|
||||
if (remoteVersion === CURRENT_VERSION) {
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
|
||||
try {
|
||||
// 解析版本号为数字数组 [X, Y, Z]
|
||||
const currentParts = CURRENT_VERSION.split('.').map((part) => {
|
||||
const num = parseInt(part, 10);
|
||||
if (isNaN(num) || num < 0) {
|
||||
throw new Error(`无效的版本号格式: ${CURRENT_VERSION}`);
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
const remoteParts = remoteVersion.split('.').map((part) => {
|
||||
const num = parseInt(part, 10);
|
||||
if (isNaN(num) || num < 0) {
|
||||
throw new Error(`无效的版本号格式: ${remoteVersion}`);
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
// 标准化版本号到3个部分
|
||||
const normalizeVersion = (parts: number[]) => {
|
||||
if (parts.length >= 3) {
|
||||
return parts.slice(0, 3); // 取前三个元素
|
||||
} else {
|
||||
// 不足3个的部分补0
|
||||
const normalized = [...parts];
|
||||
while (normalized.length < 3) {
|
||||
normalized.push(0);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
};
|
||||
|
||||
const normalizedCurrent = normalizeVersion(currentParts);
|
||||
const normalizedRemote = normalizeVersion(remoteParts);
|
||||
|
||||
// 逐级比较版本号
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (normalizedRemote[i] > normalizedCurrent[i]) {
|
||||
return UpdateStatus.HAS_UPDATE;
|
||||
} else if (normalizedRemote[i] < normalizedCurrent[i]) {
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
// 如果当前级别相等,继续比较下一级
|
||||
}
|
||||
|
||||
// 所有级别都相等,无需更新
|
||||
return UpdateStatus.NO_UPDATE;
|
||||
} catch (error) {
|
||||
console.error('版本号比较失败:', error);
|
||||
// 如果版本号格式无效,回退到字符串比较
|
||||
return remoteVersion !== CURRENT_VERSION
|
||||
? UpdateStatus.HAS_UPDATE
|
||||
: UpdateStatus.NO_UPDATE;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user