feat: 文件添加哈希计算

master
luoer 2023-11-03 17:31:42 +08:00
parent 26013a9f92
commit 2fd52a3d98
36 changed files with 164 additions and 6 deletions

View File

@ -38,6 +38,11 @@
- .dockerignore 配置哪些文件应该被忽略掉 - .dockerignore 配置哪些文件应该被忽略掉
- .gitea/workflows/depoy.yaml 流水线任务的配置文件,语法上与 Github Actions 一致 - .gitea/workflows/depoy.yaml 流水线任务的配置文件,语法上与 Github Actions 一致
## 计划
- 双token无感刷新
- session/cookiejwtsso单点登陆
- 大文件上传,断点续传
## 笔记 ## 笔记
- createUserDto与User分开 - createUserDto与User分开

Binary file not shown.

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 473 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B

View File

@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

View File

@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Leckerli One
- Font Author: Copyright (c) 2011 Gesine Todt (www.gesine-todt.de hallo@gesine-todt.de), with Reserved Font Names "Leckerli"
- Font Source: http://fonts.gstatic.com/s/leckerlione/v20/V8mCoQH8VCsNttEnxnGQ-1itLZxcBtItFw.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -0,0 +1 @@
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -1,6 +1,7 @@
import { BaseEntity } from '@/database'; import { BaseEntity } from '@/database';
import { FileCategory } from '@/storage/fileCategory'; import { FileCategory } from '@/storage/fileCategory';
import { ApiHideProperty } from '@nestjs/swagger'; import { ApiHideProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'; import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
@Entity({ orderBy: { id: 'DESC' } }) @Entity({ orderBy: { id: 'DESC' } })
@ -54,6 +55,13 @@ export class File extends BaseEntity {
@Column({ comment: '文件后缀' }) @Column({ comment: '文件后缀' })
extension: string; extension: string;
/**
*
* @example 1
*/
@Column({ comment: '类型(1: 文本2: 图片3: 音频4: 视频5: 其他)', nullable: true })
type: number;
/** /**
* *
*/ */
@ -66,6 +74,8 @@ export class File extends BaseEntity {
* ID * ID
* @example 0 * @example 0
*/ */
@Exclude()
@ApiHideProperty()
@Column({ comment: '分类ID', nullable: true }) @Column({ comment: '分类ID', nullable: true })
categoryId: number; categoryId: number;
} }

View File

@ -7,6 +7,9 @@ import { UpdateFileDto } from './dto/update-file.dto';
import { File } from './entities/file.entity'; import { File } from './entities/file.entity';
import { FindFileDto } from './dto/find-file.dto'; import { FindFileDto } from './dto/find-file.dto';
import { FileCategoryService } from '../fileCategory'; import { FileCategoryService } from '../fileCategory';
import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import { getTypeByMimetype } from './util';
@Injectable() @Injectable()
export class FileService extends BaseService { export class FileService extends BaseService {
@ -23,13 +26,17 @@ export class FileService extends BaseService {
* @returns * @returns
*/ */
async create(uploadFile: Express.Multer.File) { async create(uploadFile: Express.Multer.File) {
const { originalname: name, mimetype, size, path: hash } = uploadFile; const { originalname: name, mimetype, size } = uploadFile;
const relativePath = relative(this.config.uploadDir, uploadFile.path).split(sep).join('/'); const { uploadDir, uploadPrefix } = this.config;
const path = `${this.config.uploadPrefix}/${relativePath}`; const relativePath = relative(uploadDir, uploadFile.path).split(sep).join('/');
const path = `${uploadPrefix}/${relativePath}`;
const extension = extname(uploadFile.originalname); const extension = extname(uploadFile.originalname);
const description = ''; const description = '';
const hash = await this.hashByFilePath(uploadFile.path);
const type = getTypeByMimetype(mimetype)
const file = this.repository.create({ const file = this.repository.create({
name, name,
type,
mimetype, mimetype,
size, size,
hash, hash,
@ -41,7 +48,28 @@ export class FileService extends BaseService {
return file.id; return file.id;
} }
findMany(findFileDto: FindFileDto) { /**
* MD5
* @param path
* @returns
*/
hashByFilePath(path: string): Promise<string> {
const hash = createHash('md5');
const stream = createReadStream(path);
return new Promise((res, rej) => {
stream.on('data', (chunk) => {
hash.update(chunk);
});
stream.on('end', () => {
res(hash.digest('hex'));
});
stream.on('error', () => {
rej('获取文件哈希值失败');
});
});
}
async findMany(findFileDto: FindFileDto) {
const { page, size, name, categoryId } = findFileDto; const { page, size, name, categoryId } = findFileDto;
const { skip, take } = this.formatPagination(page, size, true); const { skip, take } = this.formatPagination(page, size, true);
const where: FindOptionsWhere<File> = {}; const where: FindOptionsWhere<File> = {};
@ -51,7 +79,20 @@ export class FileService extends BaseService {
if (categoryId) { if (categoryId) {
where.categoryId = categoryId; where.categoryId = categoryId;
} }
return this.repository.findAndCount({ skip, take, where }); return this.repository.findAndCount({
skip,
take,
where,
relations: {
category: true,
},
select: {
category: {
id: true,
name: true,
},
},
});
} }
findOne(id: number) { findOne(id: number) {

30
src/storage/file/util.ts Normal file
View File

@ -0,0 +1,30 @@
/**
*
*/
enum FileType {
/**
*
*/
TEXT = 1,
/**
*
*/
IMAGE = 2,
/**
*
*/
AUDIO = 3,
/**
*
*/
VIDEO = 4,
/**
*
*/
OTHER = 5,
}
export function getTypeByMimetype(mimetype: string) {
const [type] = mimetype.split('/');
return FileType[type.toLowerCase()] ?? FileType.OTHER;
}

View File

@ -29,5 +29,5 @@ export class CreateRoleDto {
*/ */
@IsOptional() @IsOptional()
@IsInt({ each: true }) @IsInt({ each: true })
menuIds: number[]; menuIds?: number[];
} }

View File

@ -43,6 +43,7 @@ export class Role extends BaseEntity {
/** /**
* ID * ID
* @example [1]
*/ */
@RelationId('menus') @RelationId('menus')
menuIds: number[]; menuIds: number[];