|
|
@@ -124,7 +124,7 @@ export class WebdavManager {
|
|
|
|
|
|
// 创建WebDAV账户表
|
|
|
public createWebDavTableInDB(): Promise<void> {
|
|
|
- const sql = `CREATE TABLE IF NOT EXISTS ${this.webDavTable} (
|
|
|
+ const createTableSql = `CREATE TABLE IF NOT EXISTS ${this.webDavTable} (
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
name TEXT,
|
|
|
isActivate INTEGER DEFAULT 1,
|
|
|
@@ -138,12 +138,16 @@ export class WebdavManager {
|
|
|
uploadFilePath TEXT,
|
|
|
account TEXT,
|
|
|
password TEXT,
|
|
|
- enableHttps INTEGER DEFAULT 0
|
|
|
+ enableHttps INTEGER DEFAULT 0,
|
|
|
+ coverPath TEXT
|
|
|
)`;
|
|
|
|
|
|
- return this.dataBaseUtil.executeSql(sql)
|
|
|
+ return this.dataBaseUtil.executeSql(createTableSql)
|
|
|
.then(() => {
|
|
|
Logger.info(TAG, 'WebDAV账户表创建成功');
|
|
|
+
|
|
|
+ // 检查并添加新字段(用于数据库升级)
|
|
|
+ return this.upgradeWebDavTable();
|
|
|
})
|
|
|
.catch((err: Error) => {
|
|
|
Logger.error(TAG, '创建WebDAV账户表失败:', err.message);
|
|
|
@@ -151,15 +155,34 @@ export class WebdavManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ // 升级WebDAV表结构
|
|
|
+ private async upgradeWebDavTable(): Promise<void> {
|
|
|
+ try {
|
|
|
+ // 直接尝试添加coverPath字段,如果字段已存在会失败但不影响应用运行
|
|
|
+ Logger.info(TAG, '检查数据库表结构,尝试添加coverPath字段...');
|
|
|
+ const addColumnSql = `ALTER TABLE ${this.webDavTable} ADD COLUMN coverPath TEXT`;
|
|
|
+
|
|
|
+ await this.dataBaseUtil.executeSql(addColumnSql);
|
|
|
+ Logger.info(TAG, 'coverPath字段添加成功,数据库升级完成');
|
|
|
+ } catch (error) {
|
|
|
+ // 如果添加字段失败(可能字段已存在),记录但不阻止应用启动
|
|
|
+ Logger.info(TAG, 'coverPath字段可能已存在或添加失败,继续正常运行');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 从数据库查询所有账户
|
|
|
public async queryWebDavAccountsFromDB(): Promise<void> {
|
|
|
try {
|
|
|
+ // 确保表结构是最新的
|
|
|
+ await this.upgradeWebDavTable();
|
|
|
+
|
|
|
const predicates = new relationalStore.RdbPredicates(this.webDavTable);
|
|
|
- const columns = ['id', 'name', 'isActivate', 'host', 'localHost', 'isUseLocalHost',
|
|
|
+ // 先查询基础字段(确保这些字段在旧版本中存在)
|
|
|
+ const baseColumns = ['id', 'name', 'isActivate', 'host', 'localHost', 'isUseLocalHost',
|
|
|
'port', 'filepath', 'imageFilePath', 'lyricFilePath', 'uploadFilePath',
|
|
|
'account', 'password', 'enableHttps'];
|
|
|
|
|
|
- const resultSet = await this.dataBaseUtil.queryData(this.webDavTable, columns, predicates);
|
|
|
+ const resultSet = await this.dataBaseUtil.queryData(this.webDavTable, baseColumns, predicates);
|
|
|
|
|
|
this.webDavAccounts = [];
|
|
|
while (resultSet.goToNextRow()) {
|
|
|
@@ -178,11 +201,41 @@ export class WebdavManager {
|
|
|
account.account = resultSet.getString(resultSet.getColumnIndex('account'));
|
|
|
account.password = resultSet.getString(resultSet.getColumnIndex('password'));
|
|
|
account.enableHttps = resultSet.getLong(resultSet.getColumnIndex('enableHttps')) === 1;
|
|
|
+ // 设置coverPath为默认值undefined,稍后会尝试更新
|
|
|
+ account.coverPath = undefined;
|
|
|
|
|
|
this.webDavAccounts.push(account);
|
|
|
}
|
|
|
resultSet.close();
|
|
|
|
|
|
+ // 尝试查询coverPath字段(如果升级成功)
|
|
|
+ try {
|
|
|
+ const coverPathResultSet = await this.dataBaseUtil.queryData(this.webDavTable, ['id', 'coverPath'], predicates);
|
|
|
+ if (coverPathResultSet.goToFirstRow()) {
|
|
|
+ // 创建一个映射来存储coverPath
|
|
|
+ const coverPathMap = new Map<number, string>();
|
|
|
+ do {
|
|
|
+ const accountId = coverPathResultSet.getLong(coverPathResultSet.getColumnIndex('id'));
|
|
|
+ const coverPathIndex = coverPathResultSet.getColumnIndex('coverPath');
|
|
|
+ const coverPath = coverPathIndex >= 0 ? coverPathResultSet.getString(coverPathIndex) : undefined;
|
|
|
+ if (coverPath) {
|
|
|
+ coverPathMap.set(accountId, coverPath);
|
|
|
+ }
|
|
|
+ } while (coverPathResultSet.goToNextRow());
|
|
|
+
|
|
|
+ // 将coverPath值赋给对应的账户
|
|
|
+ for (const account of this.webDavAccounts) {
|
|
|
+ if (coverPathMap.has(account.id)) {
|
|
|
+ account.coverPath = coverPathMap.get(account.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ coverPathResultSet.close();
|
|
|
+ } catch (error) {
|
|
|
+ // 如果查询coverPath失败,说明字段可能不存在,忽略错误
|
|
|
+ Logger.info(TAG, 'coverPath字段不存在或查询失败,使用默认值');
|
|
|
+ }
|
|
|
+
|
|
|
Logger.info(TAG, '从数据库加载了', this.webDavAccounts.length.toString(), '个WebDAV账户');
|
|
|
this.notifyObservers(WebdavManagerStates.QueryAccountsSucceed);
|
|
|
} catch (err) {
|
|
|
@@ -206,7 +259,8 @@ export class WebdavManager {
|
|
|
imageFilePath: string,
|
|
|
account: string,
|
|
|
password: string,
|
|
|
- enableHttps: boolean
|
|
|
+ enableHttps: boolean,
|
|
|
+ coverPath?: string
|
|
|
): Promise<void> {
|
|
|
try {
|
|
|
const values: relationalStore.ValuesBucket = {
|
|
|
@@ -222,7 +276,8 @@ export class WebdavManager {
|
|
|
'uploadFilePath': uploadFilePath,
|
|
|
'account': account,
|
|
|
'password': password,
|
|
|
- 'enableHttps': enableHttps ? 1 : 0
|
|
|
+ 'enableHttps': enableHttps ? 1 : 0,
|
|
|
+ 'coverPath': coverPath || null
|
|
|
};
|
|
|
|
|
|
await this.dataBaseUtil.insertData(this.webDavTable, values);
|
|
|
@@ -254,7 +309,8 @@ export class WebdavManager {
|
|
|
'uploadFilePath': account.uploadFilePath,
|
|
|
'account': account.account,
|
|
|
'password': account.password,
|
|
|
- 'enableHttps': account.enableHttps ? 1 : 0
|
|
|
+ 'enableHttps': account.enableHttps ? 1 : 0,
|
|
|
+ 'coverPath': account.coverPath || null
|
|
|
};
|
|
|
|
|
|
const predicates = new relationalStore.RdbPredicates(this.webDavTable);
|