Skip to content

Let's use AI to Earn!

feat(material): Material controller with URL handling (#256)

nevin1fb12bdf9df5
diff --git a/project/backend/apps/aitoearn-server/src/content/common.ts b/project/backend/apps/aitoearn-server/src/content/common.ts
index 0d388d52..d5a7613c 100644
--- a/project/backend/apps/aitoearn-server/src/content/common.ts
+++ b/project/backend/apps/aitoearn-server/src/content/common.ts
@@ -1,4 +1,5 @@
import { UserType } from '@yikart/common'
+import { FileMetadata } from '@yikart/mongodb'
export enum PubStatus {
UNPUBLISH = 0, // Unpublished/Draft
@@ -54,8 +55,11 @@ export interface Material {
}
export interface MaterialMedia {
+ id?: string
url: string
type: MediaType
+ thumbUrl?: string
+ metadata?: FileMetadata
content?: string
}
diff --git a/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts b/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts
index 45af486d..9bba2d1e 100644
--- a/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts
+++ b/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts
@@ -8,8 +8,10 @@ const MaterialIdSchema = z.object({
export class MaterialIdDto extends createZodDto(MaterialIdSchema) {}
export const MaterialMediaSchema = z.object({
+ id: z.string().optional().describe('资源ID'),
url: z.string(),
type: z.enum(MediaType).describe('资源类型'),
+ thumbUrl: z.string().optional().describe('缩略图'),
content: z.string().optional().describe('文本内容'),
mediaId: z.string().optional().describe('资源ID'),
})
diff --git a/project/backend/apps/aitoearn-server/src/content/material.controller.ts b/project/backend/apps/aitoearn-server/src/content/material.controller.ts
index 92352209..90ef10ca 100644
--- a/project/backend/apps/aitoearn-server/src/content/material.controller.ts
+++ b/project/backend/apps/aitoearn-server/src/content/material.controller.ts
@@ -3,7 +3,7 @@
* @Date: 2024-06-17 19:19:20
* @LastEditTime: 2024-12-23 12:45:22
* @LastEditors: nevin
- * @Description: 草稿
+ * @Description: Material Controller
*/
import {
Body,
@@ -19,6 +19,7 @@ import { ApiTags } from '@nestjs/swagger'
import { GetToken, TokenInfo } from '@yikart/aitoearn-auth'
import { ApiDoc, AppException, ResponseCode, TableDto, UserType } from '@yikart/common'
import { MaterialStatus, MaterialType, MediaType } from '@yikart/mongodb'
+import { fileUtil } from '../util/file.util'
import {
CreateMaterialDto,
CreateMaterialTaskDto,
@@ -61,6 +62,7 @@ export class MaterialController {
if (!getInfo) {
throw new AppException(ResponseCode.MaterialGroupNotFound)
}
+
const res = await this.materialService.create({
...body,
userId: token.id,
@@ -197,6 +199,14 @@ export class MaterialController {
...(!query.groupId && { userId: token.id, userType: UserType.User }),
},
)
+
+ for (const item of res.list) {
+ item.coverUrl = fileUtil.buildUrl(item.coverUrl)
+ for (const media of item.mediaList) {
+ media.url = fileUtil.buildUrl(media.url)
+ media.thumbUrl = fileUtil.buildUrl(media.thumbUrl)
+ }
+ }
return res
}
}
diff --git a/project/backend/apps/aitoearn-server/src/content/material.service.ts b/project/backend/apps/aitoearn-server/src/content/material.service.ts
index 63b0b9f5..de9324be 100644
--- a/project/backend/apps/aitoearn-server/src/content/material.service.ts
+++ b/project/backend/apps/aitoearn-server/src/content/material.service.ts
@@ -11,21 +11,11 @@ export class MaterialService {
private readonly mediaService: MediaService,
) { }
- /**
- * 创建
- * @param newData
- * @returns
- */
async create(newData: NewMaterial) {
const res = await this.materialRepository.create(newData)
return res
}
- /**
- * delete material
- * @param id
- * @returns
- */
async del(id: string) {
const material = await this.getInfo(id)
if (!material)
@@ -33,7 +23,6 @@ export class MaterialService {
const res = await this.materialRepository.delOne(id)
if (!res)
return false
- // 删除媒体资源
if (material.autoDeleteMedia) {
for (const item of material.mediaList) {
if (!item.mediaId)
@@ -44,22 +33,11 @@ export class MaterialService {
return res
}
- /**
- * 批量删除素材
- * @param ids
- * @returns
- */
async delByIds(userId: string, ids: string[]): Promise<boolean> {
const res = await this.materialRepository.delByIds(ids, { userId })
return res
}
- /**
- * delete material (TODO: 待优化)
- * @param userId
- * @param filter
- * @returns
- */
async delByFilter(
userId: string,
inFilter: {
@@ -84,44 +62,21 @@ export class MaterialService {
return res
}
- /**
- * 更新素材信息
- * @param id
- * @param data
- * @returns
- */
async updateInfo(id: string, data: UpMaterial): Promise<boolean> {
const res = await this.materialRepository.updateInfo(id, data)
return res
}
- /**
- * 获取素材信息
- * @param id
- * @returns
- */
async getInfo(id: string): Promise<Material | null> {
const res = await this.materialRepository.getInfo(id)
return res
}
- /**
- * 获取组内最优素材
- * @param groupId
- * @returns
- */
async optimalInGroup(groupId: string): Promise<Material | null> {
const res = await this.materialRepository.optimalInGroup(groupId)
return res
}
- /**
- * 获取草稿列表
- * @param page
- * @param userId
- * @param filter
- * @returns
- */
async getList(
page: TableDto,
filter: {
@@ -138,41 +93,21 @@ export class MaterialService {
return res
}
- /**
- * 获取素材列表
- * @param materialIds
- * @returns
- */
async optimalByIds(materialIds: string[]) {
const res = await this.materialRepository.optimalByIds(materialIds)
return res
}
- /**
- * 草稿素材列表
- * @param ids
- * @returns
- */
async getListByIds(ids: string[]) {
const res = await this.materialRepository.listByIds(ids)
return res
}
- /**
- * 开始生成任务
- * @param id
- * @returns
- */
async updateStatus(id: string, status: MaterialStatus, message: string) {
const res = await this.materialRepository.updateStatus(id, status, message)
return res
}
- /**
- * 使用计数增加
- * @param id
- * @returns
- */
async addUseCount(id: string) {
const res = await this.materialRepository.addUseCount(id)
return res
diff --git a/project/backend/libs/mongodb/src/schemas/material.schema.ts b/project/backend/libs/mongodb/src/schemas/material.schema.ts
index 0191ed48..95814ddc 100644
--- a/project/backend/libs/mongodb/src/schemas/material.schema.ts
+++ b/project/backend/libs/mongodb/src/schemas/material.schema.ts
@@ -25,6 +25,11 @@ export class MaterialMedia {
})
url: string
+ @Prop({
+ required: false,
+ })
+ thumbUrl?: string // 缩略图
+
@Prop({
type: Object,
})