Skip to content

Commit

Permalink
文件上传与前端整合
Browse files Browse the repository at this point in the history
  • Loading branch information
houdunwang committed May 11, 2023
1 parent fa2b0b2 commit fdc1882
Show file tree
Hide file tree
Showing 173 changed files with 18,884 additions and 56 deletions.
5 changes: 3 additions & 2 deletions hdcms/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ prisma/*
!prisma/schema.prisma
.env
# compiled output
/dist
/node_modules
dist
node_modules
/uploads

# Logs
logs
Expand Down
5 changes: 4 additions & 1 deletion hdcms/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"files.exclude": {
".vscode": true,
"test": true,
"dist": true
"dist": true,
"vue/core": true,
"vue/.vscode": true,
"vue/public": true
}
}
3 changes: 0 additions & 3 deletions hdcms/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ model User {
avatar String?
createAt DateTime @default(now())
updateAt DateTime @updatedAt
App Soft[]
}

model Soft {
Expand All @@ -31,6 +30,4 @@ model Soft {
is_free Boolean @default(true)
createAt DateTime @default(now())
updateAt DateTime @updatedAt
User User? @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int?
}
20 changes: 2 additions & 18 deletions hdcms/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import { PrismaClient } from '@prisma/client'
import { Random } from 'mockjs'
import { soft } from './seed/soft'
import user from './seed/user'
const prisma = new PrismaClient()

async function run() {
for (let i = 0; i < 30; i++) {
await prisma.user.create({
data: {
name: Random.cname(),
password: Random.string(),
App: {
create: {
title: Random.csentence(),
content: Random.cparagraph(),
preview: Random.image('300x300'),
},
},
},
})
}
await user()
await soft()
}

run()
4 changes: 3 additions & 1 deletion hdcms/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { AppController } from './app.controller'
import { CommonModule } from './common/common.module'
import { AuthModule } from './auth/auth.module'
import { SoftModule } from './soft/soft.module'
import { UploadModule } from './upload/upload.module'
import { UserModule } from './user/user.module';
@Module({
imports: [CommonModule, AuthModule, SoftModule],
imports: [CommonModule, AuthModule, SoftModule, UploadModule, UserModule],
controllers: [AppController],
providers: [],
})
Expand Down
7 changes: 0 additions & 7 deletions hdcms/src/auth/admin.decorator.spec.ts

This file was deleted.

7 changes: 5 additions & 2 deletions hdcms/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { ValidatePipeCustom } from './pipe/validate-pipe-custom'
import { NestExpressApplication } from '@nestjs/platform-express'
import { HttpStatus } from '@nestjs/common'

async function bootstrap() {
const app = await NestFactory.create(AppModule)
const app = await NestFactory.create<NestExpressApplication>(AppModule)
app.setGlobalPrefix('api')
app.useGlobalPipes(new ValidatePipeCustom())
app.useStaticAssets('uploads', { prefix: '/uploads' })
app.useGlobalPipes(new ValidatePipeCustom({ errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY }))
await app.listen(3000)
}
bootstrap()
13 changes: 12 additions & 1 deletion hdcms/src/soft/dto/create-soft.dto.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export class CreateSoftDto {}
import { IsNotEmpty } from 'class-validator'

export class CreateSoftDto {
@IsNotEmpty()
title: string

@IsNotEmpty()
content: string

@IsNotEmpty()
preview: string
}
4 changes: 2 additions & 2 deletions hdcms/src/soft/dto/update-soft.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateSoftDto } from './create-soft.dto';
import { PartialType } from '@nestjs/mapped-types'
import { CreateSoftDto } from './create-soft.dto'

export class UpdateSoftDto extends PartialType(CreateSoftDto) {}
18 changes: 12 additions & 6 deletions hdcms/src/soft/soft.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'
import { SoftService } from './soft.service'
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common'
import { User } from '@prisma/client'
import { Admin } from 'src/auth/admin.decorator'
import { CurrentUser } from 'src/auth/current-user.decorator'
import { CreateSoftDto } from './dto/create-soft.dto'
import { UpdateSoftDto } from './dto/update-soft.dto'
import { SoftService } from './soft.service'

@Controller('soft')
export class SoftController {
constructor(private readonly softService: SoftService) {}

@Post()
create(@Body() createSoftDto: CreateSoftDto) {
return this.softService.create(createSoftDto)
@Admin()
create(@Body() createSoftDto: CreateSoftDto, @CurrentUser() user: User) {
return this.softService.create(createSoftDto, user)
}

@Get()
findAll() {
return this.softService.findAll()
findAll(@Query('page') page: number) {
return this.softService.findAll(+page)
}

@Get(':id')
Expand All @@ -23,11 +27,13 @@ export class SoftController {
}

@Patch(':id')
@Admin()
update(@Param('id') id: string, @Body() updateSoftDto: UpdateSoftDto) {
return this.softService.update(+id, updateSoftDto)
}

@Delete(':id')
@Admin()
remove(@Param('id') id: string) {
return this.softService.remove(+id)
}
Expand Down
40 changes: 29 additions & 11 deletions hdcms/src/soft/soft.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import { Injectable } from '@nestjs/common';
import { CreateSoftDto } from './dto/create-soft.dto';
import { UpdateSoftDto } from './dto/update-soft.dto';
import { Inject, Injectable } from '@nestjs/common'
import { User } from '@prisma/client'
import { PrismaService } from 'src/common/prisma.service'
import { CreateSoftDto } from './dto/create-soft.dto'
import { UpdateSoftDto } from './dto/update-soft.dto'
import { REQUEST } from '@nestjs/core'

@Injectable()
export class SoftService {
create(createSoftDto: CreateSoftDto) {
return 'This action adds a new soft';
constructor(private prisma: PrismaService) {}
create(data: CreateSoftDto, user: User) {
return this.prisma.soft.create({
data,
})
}

findAll() {
return `This action returns all soft`;
async findAll(page = 1) {
const row = 10
const data = await this.prisma.soft.findMany({
skip: (page - 1) * row,
take: row,
})

return {
meta: { page, row, total: await this.prisma.soft.count() },
data,
}
}

findOne(id: number) {
return `This action returns a #${id} soft`;
return this.prisma.soft.findFirst({ where: { id } })
}

update(id: number, updateSoftDto: UpdateSoftDto) {
return `This action updates a #${id} soft`;
update(id: number, dto: UpdateSoftDto) {
return this.prisma.soft.update({
where: { id },
data: { ...dto },
})
}

remove(id: number) {
return `This action removes a #${id} soft`;
return this.prisma.soft.deleteMany({ where: { id } })
}
}
1 change: 1 addition & 0 deletions hdcms/src/upload/dto/create-upload.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateUploadDto {}
4 changes: 4 additions & 0 deletions hdcms/src/upload/dto/update-upload.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateUploadDto } from './create-upload.dto';

export class UpdateUploadDto extends PartialType(CreateUploadDto) {}
13 changes: 13 additions & 0 deletions hdcms/src/upload/upload.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Post, UploadedFile } from '@nestjs/common'
import { Uploader } from './upload.decorator'
import { Auth } from 'src/auth/auth.decorator'

@Controller('upload')
export class UploadController {
@Post('image')
@Uploader('image')
@Auth()
image(@UploadedFile() file: Express.Multer.File) {
return file
}
}
25 changes: 25 additions & 0 deletions hdcms/src/upload/upload.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UnsupportedMediaTypeException, UseInterceptors, applyDecorators } from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'

export function Uploader(mime: string, field = 'file') {
return applyDecorators(
UseInterceptors(
FileInterceptor(field, {
limits: {
fileSize: Math.pow(1024, 2) * 2,
},
fileFilter(
req: Request,
file: Express.Multer.File,
callback: (error: Error | null, acceptFile: boolean) => void,
) {
if (!file.mimetype.includes(mime)) {
callback(new UnsupportedMediaTypeException('文件类型错误'), false)
} else {
callback(null, true)
}
},
}),
),
)
}
30 changes: 30 additions & 0 deletions hdcms/src/upload/upload.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Module } from '@nestjs/common'
import { UploadService } from './upload.service'
import { UploadController } from './upload.controller'
import { MulterModule } from '@nestjs/platform-express'
import { diskStorage } from 'multer'
import dayjs from 'dayjs'
import { extname } from 'path'

@Module({
imports: [
MulterModule.registerAsync({
useFactory() {
return {
storage: diskStorage({
//文件储存位置
destination: 'uploads/' + dayjs().format('YYYY/MM'),
//文件名定制
filename: (req, file, callback) => {
const path = Date.now() + '-' + Math.round(Math.random() * 1e10) + extname(file.originalname)
callback(null, path)
},
}),
}
},
}),
],
controllers: [UploadController],
providers: [UploadService],
})
export class UploadModule {}
26 changes: 26 additions & 0 deletions hdcms/src/upload/upload.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreateUploadDto } from './dto/create-upload.dto';
import { UpdateUploadDto } from './dto/update-upload.dto';

@Injectable()
export class UploadService {
create(createUploadDto: CreateUploadDto) {
return 'This action adds a new upload';
}

findAll() {
return `This action returns all upload`;
}

findOne(id: number) {
return `This action returns a #${id} upload`;
}

update(id: number, updateUploadDto: UpdateUploadDto) {
return `This action updates a #${id} upload`;
}

remove(id: number) {
return `This action removes a #${id} upload`;
}
}
1 change: 1 addition & 0 deletions hdcms/src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateUserDto {}
4 changes: 4 additions & 0 deletions hdcms/src/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateUserDto } from './create-user.dto';

export class UpdateUserDto extends PartialType(CreateUserDto) {}
1 change: 1 addition & 0 deletions hdcms/src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class User {}
16 changes: 16 additions & 0 deletions hdcms/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller, Get } from '@nestjs/common'
import { User } from '@prisma/client'
import { Auth } from 'src/auth/auth.decorator'
import { CurrentUser } from 'src/auth/current-user.decorator'
import { UserService } from './user.service'

@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}

@Get('current')
@Auth()
info(@CurrentUser() user: User) {
return user
}
}
9 changes: 9 additions & 0 deletions hdcms/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';

@Module({
controllers: [UserController],
providers: [UserService]
})
export class UserModule {}
26 changes: 26 additions & 0 deletions hdcms/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

@Injectable()
export class UserService {
create(createUserDto: CreateUserDto) {
return 'This action adds a new user';
}

findAll() {
return `This action returns all user`;
}

findOne(id: number) {
return `This action returns a #${id} user`;
}

update(id: number, updateUserDto: UpdateUserDto) {
return `This action updates a #${id} user`;
}

remove(id: number) {
return `This action removes a #${id} user`;
}
}
2 changes: 1 addition & 1 deletion hdcms/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "vue"]
}
Loading

0 comments on commit fdc1882

Please sign in to comment.