|
|
@@ -37,6 +37,7 @@ import {
|
|
|
} from '../homepage/homepage.constants';
|
|
|
import { CategoryDto } from '../homepage/dto/homepage.dto';
|
|
|
import { VideoListItemDto } from './dto/video-list-response.dto';
|
|
|
+import { VideoListItemDto } from './dto/video-list-response.dto';
|
|
|
|
|
|
/**
|
|
|
* VideoService provides read-only access to video data from Redis cache.
|
|
|
@@ -102,17 +103,18 @@ export class VideoService {
|
|
|
* Reads video IDs from Redis cache, fetches full details from MongoDB,
|
|
|
* and returns paginated results.
|
|
|
*/
|
|
|
- async getVideoList(dto: VideoListRequestDto, ttl?: number): Promise<VideoListItemDto[]> {
|
|
|
+ async getVideoList(dto: VideoListRequestDto): Promise<VideoListItemDto[]> {
|
|
|
const { page, size, tag, keyword, random } = dto;
|
|
|
const start = (page - 1) * size;
|
|
|
|
|
|
const cacheKey = `video:list:${Buffer.from(
|
|
|
JSON.stringify(dto),
|
|
|
).toString('base64')}`;
|
|
|
+ const cacheKey = `video:list:${Buffer.from(
|
|
|
+ JSON.stringify(dto),
|
|
|
+ ).toString('base64')}`;
|
|
|
|
|
|
- if(!ttl){
|
|
|
- ttl = random ? 15 : 300;
|
|
|
- }
|
|
|
+ const ttl = random ? 15 : 300;
|
|
|
|
|
|
let fallbackRecords: VideoListItemDto[] = [];
|
|
|
try {
|
|
|
@@ -125,18 +127,19 @@ export class VideoService {
|
|
|
status: 'Completed'
|
|
|
};
|
|
|
|
|
|
- if(random){
|
|
|
- if (tag) {
|
|
|
- where.secondTags = tag;
|
|
|
- }
|
|
|
+ if (tag) {
|
|
|
+ where.secondTags = {
|
|
|
+ has: tag,
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- if (keyword) {
|
|
|
- where.title = {
|
|
|
- $regex: keyword,
|
|
|
- $options: 'i',
|
|
|
- }
|
|
|
+ if (keyword) {
|
|
|
+ where.title = {
|
|
|
+ contains: keyword, mode: 'insensitive'
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ if(random){
|
|
|
fallbackRecords = (await this.mongoPrisma.videoMedia.aggregateRaw({
|
|
|
pipeline: [
|
|
|
{ $match: where },
|
|
|
@@ -154,23 +157,13 @@ export class VideoService {
|
|
|
],
|
|
|
})) as unknown as VideoListItemDto[];
|
|
|
}else{
|
|
|
- if (tag) {
|
|
|
- where.secondTags = {
|
|
|
- has: tag,
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- if (keyword) {
|
|
|
- where.title = {
|
|
|
- contains: keyword, mode: 'insensitive'
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
fallbackRecords = (await this.mongoPrisma.videoMedia.findMany({
|
|
|
where,
|
|
|
orderBy: [{ addedTime: 'desc' }, { createdAt: 'desc' }],
|
|
|
skip: start,
|
|
|
take: size,
|
|
|
+ skip: start,
|
|
|
+ take: size,
|
|
|
select: {
|
|
|
id: true,
|
|
|
title: true,
|
|
|
@@ -180,10 +173,13 @@ export class VideoService {
|
|
|
preFileName: true,
|
|
|
},
|
|
|
})) as VideoListItemDto[];
|
|
|
+ })) as VideoListItemDto[];
|
|
|
}
|
|
|
|
|
|
if (fallbackRecords.length > 0) {
|
|
|
await this.redis.setJson(cacheKey, fallbackRecords, ttl);
|
|
|
+ if (fallbackRecords.length > 0) {
|
|
|
+ await this.redis.setJson(cacheKey, fallbackRecords, ttl);
|
|
|
}
|
|
|
|
|
|
return fallbackRecords;
|
|
|
@@ -194,6 +190,14 @@ export class VideoService {
|
|
|
);
|
|
|
return [];
|
|
|
}
|
|
|
+ return fallbackRecords;
|
|
|
+ } catch (err) {
|
|
|
+ this.logger.error(
|
|
|
+ `Error fetching videos from MongoDB`,
|
|
|
+ err instanceof Error ? err.stack : String(err),
|
|
|
+ );
|
|
|
+ return [];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -469,7 +473,7 @@ export class VideoService {
|
|
|
async getGuessLikeVideos(tag: string): Promise<VideoItemDto[]> {
|
|
|
try {
|
|
|
// Try to fetch from Redis cache first
|
|
|
- const cached = await this.readCachedVideoList(tsCacheKeys.video.guess() + encodeURIComponent(tag), 'guess like videos');
|
|
|
+ const cached = this.readCachedVideoList(tsCacheKeys.video.guess() + tag, 'guess like videos');
|
|
|
|
|
|
if (cached && Array.isArray(cached) && cached.length > 0) {
|
|
|
return cached;
|
|
|
@@ -491,7 +495,7 @@ export class VideoService {
|
|
|
this.mapVideoToDto(v),
|
|
|
);
|
|
|
|
|
|
- this.redis.setJson(tsCacheKeys.video.guess() + encodeURIComponent(tag), items, 3600).catch(err => {
|
|
|
+ this.redis.setJson(tsCacheKeys.video.guess() + tag, items, 3600).catch(err => {
|
|
|
this.logger.warn("Redis setJson video.guess failed", err);
|
|
|
});
|
|
|
|