2024-08-03 12:46:12 +08:00
|
|
|
|
import { BookType, OperateBookType, TagDefineType } from '../../../define/enum/bookEnum'
|
|
|
|
|
|
import { errorMessage, successMessage } from '../../Public/generalTools'
|
|
|
|
|
|
import { BookService } from '../../../define/db/service/Book/bookService'
|
|
|
|
|
|
import path from 'path'
|
2024-08-18 16:22:19 +08:00
|
|
|
|
import { CheckFileOrDirExist, CheckFolderExistsOrCreate, CopyFileOrFolder, DeleteFolderAllFile, GetSubdirectories } from '../../../define/Tools/file'
|
2024-08-03 12:46:12 +08:00
|
|
|
|
import { GeneralResponse } from '../../../model/generalResponse'
|
2024-08-18 16:22:19 +08:00
|
|
|
|
import { BookServiceBasic } from '../ServiceBasic/bookServiceBasic'
|
|
|
|
|
|
import { BookTask } from './bookTask'
|
|
|
|
|
|
import fs from 'fs'
|
2024-08-03 12:46:12 +08:00
|
|
|
|
|
|
|
|
|
|
export class BookBasic {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
bookServiceBasic: BookServiceBasic
|
|
|
|
|
|
bookTask: BookTask
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.bookServiceBasic = new BookServiceBasic();
|
|
|
|
|
|
this.bookTask = new BookTask()
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#region 小说相关操作
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 新增或者是修小说数据
|
|
|
|
|
|
* @param {*} book 小说信息
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
async AddOrModifyBook(book) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (book == null) {
|
|
|
|
|
|
return errorMessage('小说数据为空,无法修改')
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理一下数据,处理文件地址(删除前缀,转换为默认地址)
|
|
|
|
|
|
// 当前的小说的名字是不是在数据库中以存在
|
|
|
|
|
|
let _bookService = await BookService.getInstance()
|
|
|
|
|
|
let res = await _bookService.AddOrModifyBook(book)
|
|
|
|
|
|
return res
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return errorMessage(
|
|
|
|
|
|
'修改数据错误,错误信息如下:' + error.message,
|
|
|
|
|
|
'BookBasic_AddOrModifyBook'
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 小说类型返回
|
|
|
|
|
|
GetBookType() {
|
|
|
|
|
|
return successMessage(
|
|
|
|
|
|
[
|
|
|
|
|
|
{
|
|
|
|
|
|
label: 'SD反推',
|
|
|
|
|
|
value: BookType.SD_REVERSE
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
label: 'MJ反推',
|
|
|
|
|
|
value: BookType.MJ_REVERSE
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
label: '原创',
|
|
|
|
|
|
value: BookType.ORIGINAL
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
'获取小说类型成功'
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
//#region 小说相关操作
|
2024-08-03 12:46:12 +08:00
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重置小说数据
|
|
|
|
|
|
* @param bookId 小说ID
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
async ResetBookData(bookId: string): Promise<GeneralResponse.ErrorItem | GeneralResponse.SuccessItem> {
|
2024-08-03 12:46:12 +08:00
|
|
|
|
try {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
let book = await this.bookServiceBasic.GetBookDataById(bookId)
|
|
|
|
|
|
// 获取所有的小说批次
|
|
|
|
|
|
let bookTasks = (await this.bookServiceBasic.GetBookTaskData({
|
|
|
|
|
|
bookId: bookId
|
|
|
|
|
|
})).bookTasks;
|
|
|
|
|
|
// 重置批次任务
|
|
|
|
|
|
for (let i = 0; i < bookTasks.length; i++) {
|
|
|
|
|
|
const element = bookTasks[i];
|
|
|
|
|
|
// 第一个重置,后面的删除
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
|
let resetBookTaskData = await this.bookTask.ReSetBookTask(element.id);
|
|
|
|
|
|
if (resetBookTaskData.code == 0) {
|
|
|
|
|
|
throw new Error(resetBookTaskData.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let deleteBookTaskData = await this.bookTask.DeleteBookTask(element.id);
|
|
|
|
|
|
if (deleteBookTaskData.code == 0) {
|
|
|
|
|
|
throw new Error(deleteBookTaskData.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
// 开始重置小说数据
|
|
|
|
|
|
await this.bookServiceBasic.UpdateBookData(bookId, {
|
|
|
|
|
|
srtPath: undefined,
|
|
|
|
|
|
audioPath: undefined,
|
|
|
|
|
|
subtitlePosition: undefined,
|
|
|
|
|
|
imageStyle: undefined,
|
|
|
|
|
|
autoAnalyzeCharacter: undefined,
|
|
|
|
|
|
customizeImageStyle: undefined,
|
|
|
|
|
|
videoConfig: undefined,
|
|
|
|
|
|
prefixPrompt: undefined,
|
|
|
|
|
|
suffixPrompt: undefined,
|
|
|
|
|
|
draftSrtStyle: undefined,
|
|
|
|
|
|
backgroundMusic: undefined,
|
|
|
|
|
|
friendlyReminder: undefined,
|
|
|
|
|
|
watermarkPosition: undefined,
|
|
|
|
|
|
})
|
|
|
|
|
|
// 文件重置,获取data下面的所有的子文件夹,删除所有的文件夹
|
|
|
|
|
|
let dirs = await GetSubdirectories(path.join(book.bookFolderPath, 'data'))
|
|
|
|
|
|
for (let i = 0; i < dirs.length; i++) {
|
|
|
|
|
|
const element = dirs[i];
|
|
|
|
|
|
await DeleteFolderAllFile(element, true)
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
2024-08-18 16:22:19 +08:00
|
|
|
|
let scriptPath = path.join(book.bookFolderPath, 'script')
|
|
|
|
|
|
if (await CheckFileOrDirExist(scriptPath)) {
|
|
|
|
|
|
await DeleteFolderAllFile(scriptPath, true)
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
2024-08-18 16:22:19 +08:00
|
|
|
|
// 删掉输入的备份文件和input文件
|
|
|
|
|
|
let bakPath = path.join(book.bookFolderPath, 'tmp/bak');
|
|
|
|
|
|
if (await CheckFileOrDirExist(bakPath)) {
|
|
|
|
|
|
await DeleteFolderAllFile(bakPath, true)
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
2024-08-18 16:22:19 +08:00
|
|
|
|
let inputPath = path.join(book.bookFolderPath, 'tmp/input');
|
|
|
|
|
|
if (await CheckFileOrDirExist(inputPath)) {
|
|
|
|
|
|
await DeleteFolderAllFile(inputPath, true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重置完毕,开始返回
|
|
|
|
|
|
return successMessage('重置小说数据成功', 'BookBasic_ResetBookData');
|
2024-08-03 12:46:12 +08:00
|
|
|
|
} catch (error) {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
return errorMessage('重置小说数据失败,失败信息如下:' + error.message, 'BookBasic_ResetBookData');
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-18 16:22:19 +08:00
|
|
|
|
* 删除指定小说数据
|
|
|
|
|
|
* @param bookId 要删除的小说ID
|
2024-08-03 12:46:12 +08:00
|
|
|
|
*/
|
2024-08-18 16:22:19 +08:00
|
|
|
|
async DeleteBookData(bookId: string): Promise<GeneralResponse.ErrorItem | GeneralResponse.SuccessItem> {
|
2024-08-03 12:46:12 +08:00
|
|
|
|
try {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
let book = await this.bookServiceBasic.GetBookDataById(bookId);
|
|
|
|
|
|
// 先将所有的数据重置
|
|
|
|
|
|
let resetRes = await this.ResetBookData(bookId);
|
|
|
|
|
|
if (resetRes.code == 0) {
|
|
|
|
|
|
throw new Error(resetRes.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
let bookTasks = (await this.bookServiceBasic.GetBookTaskData({
|
|
|
|
|
|
bookId: bookId
|
|
|
|
|
|
})).bookTasks;
|
|
|
|
|
|
// 删除遗留重置的小说批次任务
|
|
|
|
|
|
for (let i = 0; i < bookTasks.length; i++) {
|
|
|
|
|
|
const element = bookTasks[i];
|
|
|
|
|
|
await this.bookServiceBasic.DeleteBookTaskData(element.id);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
// 开始删除数据
|
|
|
|
|
|
await this.bookServiceBasic.DeleteBookData(bookId);
|
2024-08-03 12:46:12 +08:00
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
// 开始删除文件
|
|
|
|
|
|
let bookPath = book.bookFolderPath;
|
|
|
|
|
|
if (await CheckFileOrDirExist(bookPath)) {
|
|
|
|
|
|
await DeleteFolderAllFile(bookPath, true)
|
|
|
|
|
|
}
|
|
|
|
|
|
return successMessage(null, '删除小说数据成功', 'BookBasic_DeleteBookData');
|
2024-08-03 12:46:12 +08:00
|
|
|
|
} catch (error) {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
return errorMessage('删除小说数据失败,失败信息如下:' + error.message, 'BookBasic_DeleteBookData');
|
2024-08-03 12:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
}
|