160 lines
4.3 KiB
Plaintext
160 lines
4.3 KiB
Plaintext
import { preferences } from '@kit.ArkData';
|
|
import { fileIo as fs } from '@kit.CoreFileKit';
|
|
import { AppHub } from '../common/AppHub';
|
|
|
|
/**
|
|
* 书库记录:已生成书籍的持久化索引。
|
|
*/
|
|
|
|
export interface BookRecord {
|
|
id: string; // 原书下载 = 详情页链接;选章/自定义标题下载 = 链接#《自定义名》作者
|
|
url: string; // 详情页链接
|
|
name: string;
|
|
author: string;
|
|
date: string; // 更新日期
|
|
baseName: string; // 不含扩展名的文件名(《书名》作者)
|
|
epubPath: string;
|
|
txtPath: string;
|
|
chapters: number;
|
|
createdAt: number;
|
|
}
|
|
|
|
export class LibraryStore {
|
|
private static prefs: preferences.Preferences | null = null;
|
|
private static records: BookRecord[] = [];
|
|
|
|
static async init(): Promise<void> {
|
|
LibraryStore.prefs = await preferences.getPreferences(AppHub.uiContext(), 'esj_library');
|
|
const raw = LibraryStore.prefs.getSync('library_json', '') as string;
|
|
LibraryStore.records = [];
|
|
if (raw.length > 0) {
|
|
try {
|
|
const arr = JSON.parse(raw) as BookRecord[];
|
|
for (const r of arr) {
|
|
LibraryStore.records.push(r);
|
|
}
|
|
} catch (e) {
|
|
LibraryStore.records = [];
|
|
}
|
|
}
|
|
ensureLibraryDir();
|
|
}
|
|
|
|
static list(): BookRecord[] {
|
|
return LibraryStore.records.slice().sort((a: BookRecord, b: BookRecord): number => b.createdAt - a.createdAt);
|
|
}
|
|
|
|
static save(): void {
|
|
if (LibraryStore.prefs === null) {
|
|
return;
|
|
}
|
|
LibraryStore.prefs.putSync('library_json', JSON.stringify(LibraryStore.records));
|
|
LibraryStore.prefs.flush();
|
|
}
|
|
|
|
static findByUrl(url: string): BookRecord | null {
|
|
// 仅匹配"原书下载"记录(id === 链接),批量模式的日期去重使用
|
|
for (const r of LibraryStore.records) {
|
|
if (r.id === url) {
|
|
return r;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static findById(id: string): BookRecord | null {
|
|
for (const r of LibraryStore.records) {
|
|
if (r.id === id) {
|
|
return r;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** 同一 id 重新下载时替换记录并清理旧文件;不同 id(如选章自定义名)各自独立保留 */
|
|
static upsert(record: BookRecord): void {
|
|
const idx = LibraryStore.records.findIndex((r: BookRecord): boolean => r.id === record.id);
|
|
if (idx >= 0) {
|
|
const old = LibraryStore.records[idx];
|
|
if (old.baseName !== record.baseName) {
|
|
// 文件名变了:清理旧文件,避免残留
|
|
deleteQuietly(old.epubPath);
|
|
deleteQuietly(old.txtPath);
|
|
}
|
|
LibraryStore.records.splice(idx, 1);
|
|
}
|
|
LibraryStore.records.push(record);
|
|
LibraryStore.save();
|
|
}
|
|
|
|
static remove(id: string): void {
|
|
const idx = LibraryStore.records.findIndex((r: BookRecord): boolean => r.id === id);
|
|
if (idx >= 0) {
|
|
const old = LibraryStore.records[idx];
|
|
deleteQuietly(old.epubPath);
|
|
deleteQuietly(old.txtPath);
|
|
LibraryStore.records.splice(idx, 1);
|
|
LibraryStore.save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 为书籍生成不冲突的基础文件名。
|
|
* 同一 id(原书刷新 / 同名选章重下)复用旧文件名以便覆盖;
|
|
* 不同内容若与磁盘文件重名则追加序号。
|
|
*/
|
|
static uniqueName(base: string, id: string): string {
|
|
const exist = LibraryStore.findById(id);
|
|
if (exist !== null && exist.baseName.length > 0) {
|
|
return exist.baseName;
|
|
}
|
|
const dir = AppHub.uiContext().filesDir + '/library';
|
|
let name = base;
|
|
let n = 2;
|
|
while (LibraryStore.existsFile(dir + '/' + name + '.epub') || LibraryStore.existsFile(dir + '/' + name + '.txt')) {
|
|
name = base + `(${n})`;
|
|
n++;
|
|
if (n > 50) {
|
|
break;
|
|
}
|
|
}
|
|
return name;
|
|
}
|
|
|
|
static existsFile(path: string): boolean {
|
|
try {
|
|
return fs.accessSync(path);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
function deleteQuietly(path: string): void {
|
|
if (path.length === 0) {
|
|
return;
|
|
}
|
|
try {
|
|
if (fs.accessSync(path)) {
|
|
fs.unlinkSync(path);
|
|
}
|
|
} catch (e) {
|
|
// 忽略
|
|
}
|
|
}
|
|
|
|
export function ensureLibraryDir(): void {
|
|
const dir = AppHub.uiContext().filesDir + '/library';
|
|
try {
|
|
if (!fs.accessSync(dir)) {
|
|
fs.mkdirSync(dir, true);
|
|
}
|
|
} catch (e) {
|
|
try {
|
|
fs.mkdirSync(dir, true);
|
|
} catch (e2) {
|
|
// 忽略
|
|
}
|
|
}
|
|
}
|