localhost_oa_based/src/modules/tools/storage/storage.service.ts

99 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Between, Like, Repository } from 'typeorm';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { paginateRaw } from '~/helper/paginate';
import { PaginationTypeEnum } from '~/helper/paginate/interface';
import { Pagination } from '~/helper/paginate/pagination';
import { Storage } from '~/modules/tools/storage/storage.entity';
import { UserEntity } from '~/modules/user/user.entity';
import { deleteFile } from '~/utils';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { StorageCreateDto, StoragePageDto } from './storage.dto';
import { StorageInfo } from './storage.modal';
2024-02-28 08:32:35 +08:00
@Injectable()
export class StorageService {
constructor(
@InjectRepository(Storage)
private storageRepository: Repository<Storage>,
@InjectRepository(UserEntity)
2024-02-28 17:02:46 +08:00
private userRepository: Repository<UserEntity>
2024-02-28 08:32:35 +08:00
) {}
async create(dto: StorageCreateDto, userId: number): Promise<void> {
await this.storageRepository.save({
...dto,
2024-02-29 09:29:03 +08:00
userId
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
}
/**
*
*/
async delete(fileIds: number[]): Promise<void> {
2024-02-28 17:02:46 +08:00
const items = await this.storageRepository.findByIds(fileIds);
await this.storageRepository.delete(fileIds);
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
items.forEach(el => {
deleteFile(el.path);
});
2024-02-28 08:32:35 +08:00
}
async list({
page,
pageSize,
name,
type,
size,
extName,
time,
2024-02-29 09:29:03 +08:00
username
2024-02-28 08:32:35 +08:00
}: StoragePageDto): Promise<Pagination<StorageInfo>> {
const queryBuilder = this.storageRepository
.createQueryBuilder('storage')
.leftJoinAndSelect('sys_user', 'user', 'storage.user_id = user.id')
.where({
...(name && { name: Like(`%${name}%`) }),
...(type && { type }),
...(extName && { extName }),
...(size && { size: Between(size[0], size[1]) }),
...(time && { createdAt: Between(time[0], time[1]) }),
...(username && {
2024-02-29 09:29:03 +08:00
userId: await (await this.userRepository.findOneBy({ username })).id
})
2024-02-28 08:32:35 +08:00
})
2024-02-28 17:02:46 +08:00
.orderBy('storage.created_at', 'DESC');
2024-02-28 08:32:35 +08:00
const { items, ...rest } = await paginateRaw<Storage>(queryBuilder, {
page,
pageSize,
2024-02-29 09:29:03 +08:00
paginationType: PaginationTypeEnum.LIMIT_AND_OFFSET
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
function formatResult(result: Storage[]) {
return result.map((e: any) => {
return {
id: e.storage_id,
name: e.storage_name,
extName: e.storage_ext_name,
path: e.storage_path,
type: e.storage_type,
size: e.storage_size,
createdAt: e.storage_created_at,
2024-02-29 09:29:03 +08:00
username: e.user_username
2024-02-28 17:02:46 +08:00
};
});
2024-02-28 08:32:35 +08:00
}
return {
items: formatResult(items),
2024-02-29 09:29:03 +08:00
...rest
2024-02-28 17:02:46 +08:00
};
2024-02-28 08:32:35 +08:00
}
async count(): Promise<number> {
2024-02-28 17:02:46 +08:00
return this.storageRepository.count();
2024-02-28 08:32:35 +08:00
}
}