feat: 新增图片资源上传和管理服务
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
NotFoundException,
|
||||
ConflictException,
|
||||
BadRequestException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, FindOptionsWhere } from 'typeorm';
|
||||
@@ -13,12 +14,16 @@ import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { QueryUserDto } from './dto/query-user.dto';
|
||||
import { ChangePasswordDto } from './dto/change-password.dto';
|
||||
import { PaginationInfo } from '@/common/dto/pagination.dto';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
private readonly logger = new Logger(UserService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
private readonly storageService: StorageService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -315,9 +320,27 @@ export class UserService {
|
||||
|
||||
/**
|
||||
* 删除用户(软删除,更新状态为 deleted)
|
||||
* 同时删除用户头像图片
|
||||
*/
|
||||
async remove(id: number): Promise<void> {
|
||||
const user = await this.findOneById(id);
|
||||
|
||||
// 删除用户头像图片
|
||||
if (user.avatarUrl) {
|
||||
try {
|
||||
const imagePath = this.storageService.extractStoragePath(
|
||||
user.avatarUrl,
|
||||
);
|
||||
if (imagePath) {
|
||||
await this.storageService.delete(imagePath);
|
||||
this.logger.log(`已删除用户头像: ${imagePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
// 图片删除失败不影响用户删除操作
|
||||
this.logger.warn(`删除用户头像失败: ${user.avatarUrl}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
user.status = 'deleted';
|
||||
await this.userRepository.save(user);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user