V 2.2.10 新增MJ的代理模式
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
import Realm, { UpdateMode } from 'realm'
|
||||
import { BookModel } from '../../model/Book/book.js'
|
||||
import path from 'path'
|
||||
import { BaseService } from '../baseService.js'
|
||||
import { define } from '../../../define.js'
|
||||
import { BookTaskStatus, BookType } from '../../../enum/bookEnum.js'
|
||||
import { successMessage } from '../../../../main/generalTools.js'
|
||||
import { CheckFolderExistsOrCreate, CopyFileOrFolder } from '../../../Tools/file.js'
|
||||
const { v4: uuidv4 } = require('uuid')
|
||||
import { BookTaskService } from './bookTaskService'
|
||||
import { BaseRealmService } from './bookBasic.js'
|
||||
import { isEmpty } from 'lodash'
|
||||
|
||||
class BooKService extends BaseRealmService {
|
||||
static instance: BooKService | null = null
|
||||
realm: Realm
|
||||
|
||||
private constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前实例对象,为空则创建一个新的
|
||||
* @returns
|
||||
*/
|
||||
public static async getInstance() {
|
||||
if (BooKService.instance === null) {
|
||||
BooKService.instance = new BooKService()
|
||||
await super.getInstance()
|
||||
}
|
||||
return BooKService.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小说信息,通过参数查询
|
||||
* @returns
|
||||
*/
|
||||
async GetBookData(bookQuery) {
|
||||
try {
|
||||
await this.open()
|
||||
// 获取所有的小说数据,并进行时间降序排序
|
||||
let books = this.realm.objects<BookModel>('Book')
|
||||
let book_length = books.length
|
||||
|
||||
// 开始开始筛选
|
||||
if (bookQuery.bookId) {
|
||||
// 查询对应的小说ID的数据
|
||||
books = books.filtered('id = $0', bookQuery.bookId)
|
||||
}
|
||||
|
||||
books = books.sorted('updateTime', true)
|
||||
// 判断是不是有page和pageSize,有的话对查询返回的信息做分页
|
||||
if (bookQuery.page && bookQuery.pageSize) {
|
||||
books = books.slice(
|
||||
(bookQuery.page - 1) * bookQuery.pageSize,
|
||||
bookQuery.page * bookQuery.pageSize
|
||||
) as unknown as Realm.Results<BookModel>
|
||||
}
|
||||
if (books.length <= 0) {
|
||||
return successMessage(
|
||||
{
|
||||
res_book: [],
|
||||
book_length: 0
|
||||
},
|
||||
'没有数据',
|
||||
'ReverseBook_GetBookData'
|
||||
)
|
||||
}
|
||||
|
||||
// 将realm对象数组转换为普通对象数组
|
||||
let res_book = Array.from(books).map((book) => {
|
||||
// 这里可以直接操作普通对象
|
||||
let bookObj = {
|
||||
...book,
|
||||
bookFolderPath: path.resolve(
|
||||
define.project_path,
|
||||
book.bookFolderPath.replace(/\\/g, '/')
|
||||
),
|
||||
oldVideoPath: book.oldVideoPath
|
||||
? path.resolve(define.project_path, book.oldVideoPath.replace(/\\/g, '/'))
|
||||
: '',
|
||||
imageFolder: book.imageFolder
|
||||
? path.resolve(define.project_path, book.imageFolder.replace(/\\/g, '/'))
|
||||
: ''
|
||||
}
|
||||
return bookObj
|
||||
})
|
||||
|
||||
return successMessage(
|
||||
{
|
||||
res_book,
|
||||
book_length
|
||||
},
|
||||
'获取成功',
|
||||
'ReverseBook_GetBookData'
|
||||
)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增或者是修小说数据
|
||||
* @param {*} book 小说信息
|
||||
* @returns
|
||||
*/
|
||||
async AddOrModifyBook(book) {
|
||||
try {
|
||||
await this.open()
|
||||
if (book == null) {
|
||||
throw new Error('小说数据为空,无法修改')
|
||||
}
|
||||
|
||||
// 当小说的类型是反推的时候,必须传入视频
|
||||
if (book.type == BookType.MJ_REVERSE || book.type == BookType.SD_REVERSE) {
|
||||
// 判断视频是否存在
|
||||
if (book.oldVideoPath == null || book.oldVideoPath == '') {
|
||||
throw new Error('反推必须传入视频')
|
||||
}
|
||||
}
|
||||
|
||||
if (book.id == null) {
|
||||
// 新增
|
||||
// 判断指定的名字在数据库中是否存在
|
||||
let books = this.realm.objects('Book').filtered('name = $0', book.name)
|
||||
if (books.length > 0) {
|
||||
throw new Error(`小说名字 ${book.name} 已经存在,请更换小说名字`)
|
||||
}
|
||||
console.log(this)
|
||||
// 新增数据
|
||||
book.id = uuidv4()
|
||||
book.createTime = new Date()
|
||||
book.updateTime = new Date()
|
||||
// 检查传入的视频文件是不是存在
|
||||
// 获取当前最大的no
|
||||
let maxNo = this.realm.objects('Book').max('no')
|
||||
book.no = maxNo == null ? 1 : Number(maxNo) + 1
|
||||
// 拼接项目文件夹
|
||||
book.bookFolderPath = book.id
|
||||
book.imageFolder = `${book.id}/tmp`
|
||||
let bookFolderPath = path.resolve(define.project_path, book.id)
|
||||
let imageFolder = path.resolve(define.project_path, `${book.id}/tmp`)
|
||||
let oldVideoPath = path.resolve(define.project_path, `${book.id}/data/${book.id}.mp4`)
|
||||
|
||||
// 将视频拷贝一个到项目文件下面
|
||||
if (book.oldVideoPath) {
|
||||
await CopyFileOrFolder(book.oldVideoPath, oldVideoPath)
|
||||
}
|
||||
|
||||
// 创建对应的文件夹
|
||||
await CheckFolderExistsOrCreate(bookFolderPath)
|
||||
await CheckFolderExistsOrCreate(imageFolder)
|
||||
// 修改数据
|
||||
book.oldVideoPath = path.relative(define.project_path, oldVideoPath)
|
||||
this.realm.write(() => {
|
||||
this.realm.create('Book', book)
|
||||
let bookTaskImageFolder = path.resolve(imageFolder, 'output_00001')
|
||||
// 添加一个任务
|
||||
let bookTask = {
|
||||
id: uuidv4(),
|
||||
bookId: book.id,
|
||||
no: 1,
|
||||
name: 'output_00001',
|
||||
generateVideoPath: null,
|
||||
srtPath: null,
|
||||
audioPath: null,
|
||||
imageFolder: path.relative(define.project_path, bookTaskImageFolder), // 获取文件输出对于项目的相对路径
|
||||
styleList: [],
|
||||
prefix: null,
|
||||
status: BookTaskStatus.WAIT,
|
||||
errorMsg: null,
|
||||
isAuto: false,
|
||||
updateTime: new Date(),
|
||||
createTime: new Date()
|
||||
}
|
||||
|
||||
// 添加任务
|
||||
this.realm.create('BookTask', bookTask)
|
||||
})
|
||||
|
||||
// 保存成功,返回数据,但是要做处理
|
||||
book.bookFolderPath = bookFolderPath
|
||||
book.imageFolder = imageFolder
|
||||
book.oldVideoPath = oldVideoPath
|
||||
|
||||
return successMessage(book, '新增成功', 'BookBasic_AddOrModifyBook')
|
||||
} else {
|
||||
// 修改
|
||||
// 判断指定的名字在数据库中是否存在(不算自己)
|
||||
let books = this.realm
|
||||
.objects('Book')
|
||||
.filtered('name = $0 AND id != $1', book.name, book.id)
|
||||
if (books.length > 0) {
|
||||
throw new Error(`小说名字 ${book.name} 已经存在,请更换小说名字`)
|
||||
}
|
||||
// 两个文件夹地址不能改,删除两个属性
|
||||
delete book.bookFolderPath
|
||||
delete book.imageFolder
|
||||
// 修改数据
|
||||
book.updateTime = new Date()
|
||||
this.realm.write(() => {
|
||||
this.realm.create('Book', book, UpdateMode.Modified)
|
||||
})
|
||||
|
||||
// 保存成功,返回数据,但是要做处理
|
||||
return successMessage(null, '修改成功', 'BookBasic_AddOrModifyBook')
|
||||
}
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default BooKService
|
||||
Reference in New Issue
Block a user