video-media.controller.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts
  2. import {
  3. Controller,
  4. Get,
  5. Param,
  6. Query,
  7. Patch,
  8. Body,
  9. Post,
  10. Delete,
  11. Req,
  12. Res,
  13. BadRequestException,
  14. } from '@nestjs/common';
  15. import type { FastifyReply, FastifyRequest } from 'fastify';
  16. import {
  17. ApiTags,
  18. ApiOperation,
  19. ApiParam,
  20. ApiConsumes,
  21. ApiBody,
  22. ApiResponse,
  23. ApiOkResponse,
  24. ApiNotFoundResponse,
  25. ApiBadRequestResponse,
  26. } from '@nestjs/swagger';
  27. import { VideoMediaService } from './video-media.service';
  28. import {
  29. VideoMediaListQueryDto,
  30. UpdateVideoMediaTagsDto,
  31. UpdateVideoMediaStatusDto,
  32. BatchUpdateVideoMediaStatusDto,
  33. VideoMediaListItemDto,
  34. VideoMediaDetailDto,
  35. UpdateVideoMediaCoverResponseDto,
  36. } from './video-media.dto';
  37. @ApiTags('视频管理 (Video Media Management)')
  38. @Controller('video-media')
  39. export class VideoMediaController {
  40. constructor(private readonly videoMediaService: VideoMediaService) {}
  41. /**
  42. * 列表查询
  43. * POST /video-media/list
  44. */
  45. @ApiOperation({
  46. summary: '获取视频媒体列表',
  47. description: '分页查询视频列表,支持关键词搜索和上下架状态过滤',
  48. })
  49. @ApiOkResponse({
  50. description: '返回视频媒体分页列表',
  51. schema: {
  52. type: 'object',
  53. properties: {
  54. data: {
  55. type: 'array',
  56. items: { $ref: '#/components/schemas/VideoMediaListItemDto' },
  57. },
  58. total: { type: 'number', example: 100 },
  59. page: { type: 'number', example: 1 },
  60. pageSize: { type: 'number', example: 20 },
  61. },
  62. },
  63. })
  64. @ApiBadRequestResponse({
  65. description: '请求参数验证失败',
  66. })
  67. @Post('list')
  68. async findAll(@Body() dto: VideoMediaListQueryDto) {
  69. return this.videoMediaService.findAll(dto);
  70. }
  71. /**
  72. * 详情(管理弹窗)
  73. * GET /video-media/:id
  74. */
  75. @ApiOperation({
  76. summary: '获取视频媒体详情',
  77. description:
  78. '获取单个视频媒体的完整详细信息,包括基础属性、管理信息和反范式化数据',
  79. })
  80. @ApiParam({
  81. name: 'id',
  82. type: String,
  83. description: '视频媒体 MongoDB ID',
  84. example: '507f1f77bcf86cd799439011',
  85. })
  86. @ApiOkResponse({
  87. description: '返回视频媒体详情',
  88. type: VideoMediaDetailDto,
  89. })
  90. @ApiNotFoundResponse({
  91. description: '视频媒体不存在',
  92. })
  93. @Get(':id')
  94. async findOne(@Param('id') id: string) {
  95. return this.videoMediaService.findOne(id);
  96. }
  97. /**
  98. * 管理弹窗保存(标题 / 分类 / 标签 / 上下架)
  99. * PATCH /video-media/:id/manage
  100. */
  101. @ApiOperation({
  102. summary: '更新视频媒体管理信息',
  103. description: '更新视频的标题、分类、标签、上下架状态等管理级别信息',
  104. })
  105. @ApiBody({
  106. type: UpdateVideoMediaTagsDto,
  107. description: '更新的管理信息',
  108. })
  109. @Post('update-video-tags')
  110. async updateManage(@Body() dto: UpdateVideoMediaTagsDto) {
  111. return this.videoMediaService.updateVideoTags(dto);
  112. }
  113. /**
  114. * 单个上/下架
  115. * PATCH /video-media/:id/status
  116. */
  117. @ApiOperation({
  118. summary: '更新视频媒体上下架状态',
  119. description: '对单个视频进行上架(1)或下架(0)操作',
  120. })
  121. @ApiParam({
  122. name: 'id',
  123. type: String,
  124. description: '视频媒体 MongoDB ID',
  125. example: '507f1f77bcf86cd799439011',
  126. })
  127. @ApiBody({
  128. type: UpdateVideoMediaStatusDto,
  129. description: '上下架状态信息',
  130. })
  131. @ApiOkResponse({
  132. description: '状态更新成功',
  133. type: VideoMediaDetailDto,
  134. })
  135. @ApiNotFoundResponse({
  136. description: '视频媒体不存在',
  137. })
  138. @ApiBadRequestResponse({
  139. description: '请求参数验证失败',
  140. })
  141. @Patch(':id/status')
  142. async updateStatus(
  143. @Param('id') id: string,
  144. @Body() dto: UpdateVideoMediaStatusDto,
  145. ) {
  146. return this.videoMediaService.updateStatus(id, dto);
  147. }
  148. /**
  149. * 批量上/下架
  150. * POST /video-media/batch/status
  151. */
  152. @ApiOperation({
  153. summary: '批量更新视频媒体上下架状态',
  154. description: '对多个视频进行批量上架或下架操作',
  155. })
  156. @ApiBody({
  157. type: BatchUpdateVideoMediaStatusDto,
  158. description: '批量更新信息,包含 ID 列表和目标状态',
  159. })
  160. @ApiOkResponse({
  161. description: '批量更新成功',
  162. schema: {
  163. type: 'object',
  164. properties: {
  165. success: { type: 'number', example: 10 },
  166. failed: { type: 'number', example: 0 },
  167. },
  168. },
  169. })
  170. @ApiBadRequestResponse({
  171. description: '请求参数验证失败',
  172. })
  173. @Post('batch/status')
  174. async batchUpdateStatus(@Body() dto: BatchUpdateVideoMediaStatusDto) {
  175. return this.videoMediaService.batchUpdateStatus(dto);
  176. }
  177. /**
  178. * 封面上传:
  179. * - 前端通过 multipart/form-data 上传文件
  180. * - 这里示例使用 FileInterceptor;实际中你会上传到 S3,得到一个 URL / key
  181. * POST /video-media/:id/cover
  182. */
  183. @ApiOperation({
  184. summary: '上传视频封面',
  185. description: '为指定视频上传或更新自定义封面图片,支持上传至 S3 存储',
  186. })
  187. @ApiParam({
  188. name: 'id',
  189. type: String,
  190. description: '视频媒体 MongoDB ID',
  191. example: '507f1f77bcf86cd799439011',
  192. })
  193. @ApiConsumes('multipart/form-data')
  194. @ApiBody({
  195. description: '封面图片文件上传',
  196. schema: {
  197. type: 'object',
  198. properties: {
  199. file: {
  200. type: 'string',
  201. format: 'binary',
  202. description: '图片文件(支持 JPG、PNG 等常见格式)',
  203. },
  204. },
  205. required: ['file'],
  206. },
  207. })
  208. @ApiOkResponse({
  209. description: '封面上传成功',
  210. type: UpdateVideoMediaCoverResponseDto,
  211. })
  212. @ApiNotFoundResponse({
  213. description: '视频媒体不存在',
  214. })
  215. @ApiBadRequestResponse({
  216. description: '文件格式或大小不符合要求',
  217. })
  218. @Post(':id/cover')
  219. async updateCover(@Param('id') id: string, @Req() req: FastifyRequest) {
  220. const reqAny = req as any;
  221. const bodyFile = reqAny.body?.file;
  222. let mpFile = Array.isArray(bodyFile) ? bodyFile[0] : bodyFile;
  223. if (!mpFile && reqAny.isMultipart?.()) {
  224. mpFile = await reqAny.file();
  225. }
  226. if (!mpFile) {
  227. throw new BadRequestException('No file uploaded');
  228. }
  229. return this.videoMediaService.updateCover(id, mpFile);
  230. }
  231. // TODO: 删除视频媒体
  232. @ApiOperation({
  233. summary: '删除视频媒体',
  234. description: '根据 ID 删除指定的视频媒体',
  235. })
  236. @ApiParam({
  237. name: 'id',
  238. type: String,
  239. description: '视频媒体 MongoDB ID',
  240. example: '507f1f77bcf86cd799439011',
  241. })
  242. @ApiOkResponse({
  243. description: '删除成功',
  244. schema: {
  245. type: 'object',
  246. properties: {
  247. id: { type: 'string', example: '507f1f77bcf86cd799439011' },
  248. },
  249. },
  250. })
  251. @ApiNotFoundResponse({
  252. description: '视频媒体不存在',
  253. })
  254. @Delete(':id')
  255. async delete(@Param('id') id: string) {
  256. return this.videoMediaService.delete(id);
  257. }
  258. /**
  259. * 导入 Excel 标签
  260. * POST /video-media/import/excel-tags
  261. */
  262. @ApiOperation({
  263. summary: '导入视频标签',
  264. description: '从 Excel 文件导入视频标签并更新视频媒体',
  265. })
  266. @ApiConsumes('multipart/form-data')
  267. @ApiBody({
  268. schema: {
  269. type: 'object',
  270. properties: {
  271. file: { type: 'string', format: 'binary' },
  272. },
  273. required: ['file'],
  274. },
  275. })
  276. @Post('import/excel-tags')
  277. async importExcelTags(@Req() req: FastifyRequest) {
  278. const getBuffer = async (file: any): Promise<Buffer | undefined> => {
  279. if (!file) return undefined;
  280. if (Buffer.isBuffer(file)) return file;
  281. if (file instanceof Uint8Array) return Buffer.from(file);
  282. const candidate =
  283. file.buffer ?? file.data ?? file.value ?? file._buf ?? undefined;
  284. if (Buffer.isBuffer(candidate)) return candidate;
  285. if (candidate instanceof Uint8Array) return Buffer.from(candidate);
  286. if (typeof candidate === 'string') return Buffer.from(candidate);
  287. if (typeof file.toBuffer === 'function') {
  288. const buf = await file.toBuffer();
  289. if (Buffer.isBuffer(buf)) return buf;
  290. }
  291. if (file.file) {
  292. const chunks: Buffer[] = [];
  293. for await (const chunk of file.file) {
  294. chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
  295. }
  296. if (chunks.length > 0) {
  297. return Buffer.concat(chunks);
  298. }
  299. }
  300. return undefined;
  301. };
  302. // fastify multipart
  303. const reqAny = req as any;
  304. const bodyFile = reqAny.body?.file;
  305. let mpFile = Array.isArray(bodyFile) ? bodyFile[0] : bodyFile;
  306. if (!mpFile && reqAny.isMultipart?.()) {
  307. mpFile = await reqAny.file();
  308. }
  309. if (!mpFile) {
  310. throw new BadRequestException('No file uploaded');
  311. }
  312. const buf = await getBuffer(mpFile);
  313. if (!buf?.length) {
  314. throw new BadRequestException('Empty file');
  315. }
  316. return this.videoMediaService.importExcelTags(buf);
  317. }
  318. /**
  319. * 导出所有视频媒体为 Excel
  320. * GET /video-media/export/excel
  321. */
  322. @Get('export/excel')
  323. async exportExcel(@Res() res: FastifyReply) {
  324. const buffer = await this.videoMediaService.exportExcel();
  325. res
  326. .header(
  327. 'Content-Type',
  328. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  329. )
  330. .header('Content-Disposition', 'attachment; filename="video-media.xlsx"')
  331. .send(buffer);
  332. }
  333. }