This commit is contained in:
2026-07-01 20:21:51 +08:00
parent aea1513836
commit ef6cc18fb2
8 changed files with 178 additions and 20 deletions
@@ -1,31 +1,22 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import type { ResourceBizType, ResourceMediaType, ResourceOwnerType } from '@prisma/client';
import { Prisma } from '@prisma/client';
import { randomUUID } from 'crypto';
import { PrismaService } from '../../common/prisma/prisma.module';
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
import { OSS_PROVIDER } from '../../integrations/integrations.constants';
import type { IOssProvider } from '../../integrations/oss/oss.interface';
import type { ResourceListQueryDto } from './dto/common-query.dto';
import type { RegisterResourceDto, UpdateResourceDto, UploadTokenDto } from './dto/common-mutate.dto';
@Injectable()
export class ResourceService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
@Inject(OSS_PROVIDER) private readonly oss: IOssProvider,
) {}
getUploadToken(dto: UploadTokenDto) {
const bucket = process.env.OSS_BUCKET || 'mock-dukang';
const ext = dto.fileName.includes('.') ? dto.fileName.split('.').pop() : 'bin';
const key = `uploads/${dto.bizType.toLowerCase()}/${Date.now()}-${randomUUID().slice(0, 8)}.${ext}`;
const cdnBase = process.env.OSS_CDN_BASE || 'https://mock-cdn.dukang.local';
return {
bucket,
region: process.env.OSS_REGION || 'oss-cn-hangzhou',
ossKey: key,
url: `${cdnBase}/${key}`,
mock: true,
expireAt: new Date(Date.now() + 15 * 60 * 1000).toISOString(),
mediaType: dto.mediaType,
bizType: dto.bizType,
};
return this.oss.getUploadToken(dto);
}
async register(dto: RegisterResourceDto) {
@@ -37,7 +28,7 @@ export class ResourceService {
mediaType: dto.mediaType as ResourceMediaType,
ossBucket: dto.ossBucket ?? process.env.OSS_BUCKET ?? 'mock-dukang',
ossKey: dto.ossKey,
url: dto.url,
url: dto.url || this.oss.buildPublicUrl(dto.ossKey),
fileName: dto.fileName,
sortOrder: dto.sortOrder ?? 0,
},