|
|
@@ -2,6 +2,7 @@
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
import { HttpService } from '@nestjs/axios';
|
|
|
import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
|
|
|
+import { SysConfigReaderService } from '@box/core/sys-config/sys-config-reader.service';
|
|
|
import { firstValueFrom } from 'rxjs';
|
|
|
import { EntityType } from '@prisma/mongo/client';
|
|
|
|
|
|
@@ -136,10 +137,6 @@ export class ProviderVideoSyncService {
|
|
|
|
|
|
private lastSyncSummary: ProviderVideoSyncResult | null = null;
|
|
|
|
|
|
- private readonly PROVIDER_API_URL =
|
|
|
- 'https://vm.rvakc.xyz/api/web/mediafile/search';
|
|
|
-
|
|
|
- private readonly DEFAULT_PROVIDER_CODE = 'RVAKC';
|
|
|
private readonly MAX_PAGE_SIZE = 500;
|
|
|
private readonly DEFAULT_PAGE_SIZE = 500;
|
|
|
private readonly BATCH_SIZE = 100;
|
|
|
@@ -148,14 +145,23 @@ export class ProviderVideoSyncService {
|
|
|
constructor(
|
|
|
private readonly mongo: MongoPrismaService,
|
|
|
private readonly httpService: HttpService,
|
|
|
+ private readonly sysConfigReader: SysConfigReaderService,
|
|
|
) {}
|
|
|
|
|
|
async syncFromProvider(
|
|
|
options: ProviderVideoSyncOptions = {},
|
|
|
): Promise<ProviderVideoSyncResult> {
|
|
|
- const providerCode = options.providerCode ?? this.DEFAULT_PROVIDER_CODE;
|
|
|
+ const providerConfig = await this.sysConfigReader.getProviderConfig();
|
|
|
+ const providerCode = options.providerCode ?? providerConfig.providerCode;
|
|
|
+ const providerApiUrl = providerConfig.apiUrl;
|
|
|
+ if (!providerApiUrl) {
|
|
|
+ throw new Error(
|
|
|
+ 'sysConfig.provider.apiUrl is required for provider sync',
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- const requestedPageSize = options.pageSize ?? this.DEFAULT_PAGE_SIZE;
|
|
|
+ const defaultPageSize = providerConfig.itemsLimit ?? this.DEFAULT_PAGE_SIZE;
|
|
|
+ const requestedPageSize = options.pageSize ?? defaultPageSize;
|
|
|
const pageSize = this.clampInt(requestedPageSize, 1, this.MAX_PAGE_SIZE);
|
|
|
|
|
|
const fullSync = options.fullSync ?? false;
|
|
|
@@ -215,6 +221,7 @@ export class ProviderVideoSyncService {
|
|
|
cursor: initialCursor,
|
|
|
paramStatus,
|
|
|
optionsParam: options.param,
|
|
|
+ apiUrl: providerApiUrl,
|
|
|
});
|
|
|
if (baselineResult) {
|
|
|
this.lastSyncSummary = baselineResult;
|
|
|
@@ -236,7 +243,7 @@ export class ProviderVideoSyncService {
|
|
|
`[syncFromProvider] POST pageNum=${pageNum} pageSize=${cursor.pageSize} status=${paramStatus} updatedAt=${fullSync ? '(none)' : (body.param.updatedAt ?? '(none)')}`,
|
|
|
);
|
|
|
|
|
|
- const rawList = await this.fetchPage(body);
|
|
|
+ const rawList = await this.fetchPage(providerApiUrl, body);
|
|
|
if (!rawList.length) {
|
|
|
this.logger.log(
|
|
|
`[syncFromProvider] No more records (pageNum=${pageNum}). Stop.`,
|
|
|
@@ -325,6 +332,7 @@ export class ProviderVideoSyncService {
|
|
|
entity: EntityType;
|
|
|
cursor: SyncCursor;
|
|
|
paramStatus: string;
|
|
|
+ apiUrl: string;
|
|
|
optionsParam?: ProviderVideoSyncOptions['param'];
|
|
|
}): Promise<ProviderVideoSyncResult | null> {
|
|
|
if (!args.cursor || args.cursor.updatedAtCursor !== undefined) {
|
|
|
@@ -339,7 +347,10 @@ export class ProviderVideoSyncService {
|
|
|
extraParam: args.optionsParam,
|
|
|
});
|
|
|
|
|
|
- const pagination = await this.probeProviderForPaging(probeBody);
|
|
|
+ const pagination = await this.probeProviderForPaging(
|
|
|
+ args.apiUrl,
|
|
|
+ probeBody,
|
|
|
+ );
|
|
|
|
|
|
let totalPages = pagination.totalPages;
|
|
|
if (totalPages === undefined && pagination.total !== undefined) {
|
|
|
@@ -389,7 +400,7 @@ export class ProviderVideoSyncService {
|
|
|
|
|
|
this.logger.log(`[syncFromProvider] param body ${JSON.stringify(body)} `);
|
|
|
|
|
|
- const rawList = await this.fetchPage(body);
|
|
|
+ const rawList = await this.fetchPage(args.apiUrl, body);
|
|
|
if (!rawList.length) {
|
|
|
this.logger.log(
|
|
|
`[syncFromProvider] Baseline partial page ${page} returned 0 records; continuing.`,
|
|
|
@@ -425,14 +436,17 @@ export class ProviderVideoSyncService {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private async probeProviderForPaging(body: {
|
|
|
- pageNum: number;
|
|
|
- pageSize: number;
|
|
|
- param: Record<string, any>;
|
|
|
- }): Promise<ProviderPagingInfo> {
|
|
|
+ private async probeProviderForPaging(
|
|
|
+ apiUrl: string,
|
|
|
+ body: {
|
|
|
+ pageNum: number;
|
|
|
+ pageSize: number;
|
|
|
+ param: Record<string, any>;
|
|
|
+ },
|
|
|
+ ): Promise<ProviderPagingInfo> {
|
|
|
try {
|
|
|
const response = await firstValueFrom(
|
|
|
- this.httpService.post(this.PROVIDER_API_URL, body, {
|
|
|
+ this.httpService.post(apiUrl, body, {
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
timeout: 30000,
|
|
|
}),
|
|
|
@@ -519,11 +533,14 @@ export class ProviderVideoSyncService {
|
|
|
param,
|
|
|
};
|
|
|
}
|
|
|
- private async fetchPage(body: {
|
|
|
- pageNum: number;
|
|
|
- pageSize: number;
|
|
|
- param: Record<string, any>;
|
|
|
- }): Promise<RawProviderVideo[]> {
|
|
|
+ private async fetchPage(
|
|
|
+ apiUrl: string,
|
|
|
+ body: {
|
|
|
+ pageNum: number;
|
|
|
+ pageSize: number;
|
|
|
+ param: Record<string, any>;
|
|
|
+ },
|
|
|
+ ): Promise<RawProviderVideo[]> {
|
|
|
try {
|
|
|
// Provider expects { data: "<json string>" } (based on code=400 Field=data expecting string)
|
|
|
const wrappedBody = {
|
|
|
@@ -535,7 +552,7 @@ export class ProviderVideoSyncService {
|
|
|
};
|
|
|
|
|
|
const response = await firstValueFrom(
|
|
|
- this.httpService.post(this.PROVIDER_API_URL, wrappedBody, {
|
|
|
+ this.httpService.post(apiUrl, wrappedBody, {
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
timeout: 30_000,
|
|
|
}),
|