Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
node
/
app-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 6806a535
authored
Sep 29, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
修改日志文件上传的BUG
1 parent
01ea76a8
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
117 additions
and
233 deletions
prisma/schema.prisma
src/config/file.ts
src/dto/base.dto.ts
src/dto/config.dto.ts
src/dto/device.dto.ts
src/dto/device.status.dto.ts
src/dto/field.dto.ts
src/modules/config/config.service.ts
src/modules/device/device.controller.ts
src/modules/device/device.service.ts
src/modules/file/file.controller.ts
src/modules/file/file.service.ts
src/typings/broadcast.d.ts
src/typings/file.d.ts
src/typings/ws.d.ts
test.http
prisma/schema.prisma
View file @
6806a53
...
@@ -47,6 +47,7 @@ model file {
...
@@ -47,6 +47,7 @@ model file {
filename String? @db.VarChar(100)
filename String? @db.VarChar(100)
originalname String? @db.VarChar(255)
originalname String? @db.VarChar(255)
size Int? @default(0)
size Int? @default(0)
destination String? @db.VarChar(500)
created_at DateTime @default(now()) @db.Timestamp(0)
created_at DateTime @default(now()) @db.Timestamp(0)
type String @default("other") @db.VarChar(10)
type String @default("other") @db.VarChar(10)
device device @relation(fields: [device_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_log_device")
device device @relation(fields: [device_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_log_device")
...
...
src/config/file.ts
View file @
6806a53
import
{
join
,
extname
}
from
'path'
;
import
{
join
,
extname
}
from
'path'
;
import
{
diskStorage
}
from
'multer'
;
import
{
diskStorage
}
from
'multer'
;
export
default
{
export
default
{
root
:
join
(
__dirname
,
'..'
,
'..'
,
process
.
env
.
STATIC_PATH
),
root
:
join
(
__dirname
,
'..'
,
'..'
,
process
.
env
.
STATIC_PATH
),
storage
:
diskStorage
({
storage
:
diskStorage
({
...
...
src/dto/base.dto.ts
View file @
6806a53
import
{
ApiProperty
}
from
'@nestjs/swagger'
import
{
ApiProperty
}
from
'@nestjs/swagger'
import
{
Transform
}
from
'class-transformer'
import
{
Transform
}
from
'class-transformer'
import
{
IsOptional
,
isString
,
Matches
,
registerDecorator
,
ValidationArguments
,
ValidationOptions
}
from
'class-validator'
import
{
Is
MobilePhone
,
Is
Optional
,
isString
,
Matches
,
registerDecorator
,
ValidationArguments
,
ValidationOptions
}
from
'class-validator'
// 自定义验证装饰器
// 自定义验证装饰器
export
function
IsNumberOrNumberString
(
validationOptions
?:
ValidationOptions
)
{
export
function
IsNumberOrNumberString
(
validationOptions
?:
ValidationOptions
)
{
return
function
(
object
:
Object
,
propertyName
:
string
)
{
return
function
(
object
:
Object
,
propertyName
:
string
)
{
...
@@ -60,3 +60,59 @@ export class BaseDto {
...
@@ -60,3 +60,59 @@ export class BaseDto {
@
IsNumberOrNumberString
()
@
IsNumberOrNumberString
()
pageIndex
?:
number
pageIndex
?:
number
}
}
export
class
TimeDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'创建时间'
,
default
:
''
})
created_at
:
Date
@
ApiProperty
({
type
:
Date
,
description
:
'更新时间'
,
default
:
''
})
updated_at
:
Date
}
export
class
UserBaseDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
default
:
''
})
user
:
string
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
default
:
''
})
mobile
:
string
@
ApiProperty
({
type
:
String
,
description
:
'使用者商户名称'
,
default
:
''
})
merchant_name
:
string
}
export
class
SnDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备SN'
,
})
sn
:
string
}
export
class
DeviceIdDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
})
device_id
:
number
}
\ No newline at end of file
\ No newline at end of file
src/dto/config.dto.ts
View file @
6806a53
import
{
ApiProperty
}
from
"@nestjs/swagger"
import
{
ApiProperty
,
IntersectionType
,
PartialType
,
PickType
}
from
'@nestjs/swagger'
import
{
Transform
}
from
"class-transformer"
import
{
Transform
}
from
'class-transformer'
import
{
IsNotEmpty
,
IsNumber
,
isString
,
Matches
}
from
"class-validator"
import
{
IsNotEmpty
,
IsNumber
,
isString
,
Matches
}
from
'class-validator'
import
{
DeviceIdDto
,
TimeDto
}
from
'./base.dto'
/*
/*
model log_config {
model log_config {
...
@@ -13,50 +14,13 @@ model log_config {
...
@@ -13,50 +14,13 @@ model log_config {
device_info device_info @relation(fields: [device_id], references: [device_id], onDelete: NoAction, onUpdate: NoAction, map: "FK_log_config_device_info")
device_info device_info @relation(fields: [device_id], references: [device_id], onDelete: NoAction, onUpdate: NoAction, map: "FK_log_config_device_info")
user_fields log_config_userfield_relation[]
user_fields log_config_userfield_relation[]
}*/
}*/
export
class
LogConfigDto
{
export
class
LogConfigDto
extends
IntersectionType
(
DeviceIdDto
,
PartialType
(
TimeDto
))
{
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
required
:
true
,
default
:
0
})
@
IsNotEmpty
({
message
:
'设备ID不能为空'
})
device_id
:
number
@
ApiProperty
({
@
ApiProperty
({
type
:
Number
,
type
:
Number
,
description
:
'日志保存天数'
,
description
:
'日志保存天数'
,
required
:
false
,
required
:
false
,
default
:
''
default
:
''
})
})
log_saved_days
:
number
@
ApiProperty
({
type
:
Number
,
description
:
'创建时间'
,
required
:
false
,
default
:
''
})
created_at
:
Date
|
string
@
ApiProperty
({
type
:
Date
,
description
:
'更新时间'
,
required
:
false
,
default
:
''
})
updated_at
:
Date
|
string
}
export
class
LogConfigUpdateDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'日志保存天数'
,
required
:
false
,
default
:
''
})
log_saved_days
:
number
}
\ No newline at end of file
\ No newline at end of file
log_saved_days
?:
number
}
export
class
LogConfigUpdateDto
extends
PickType
(
LogConfigDto
,
[
'log_saved_days'
])
{}
src/dto/device.dto.ts
View file @
6806a53
import
{
IsString
,
IsNumber
,
IsNotEmpty
,
MaxLength
,
IsMobilePhone
,
IsOptional
,
isNotEmpty
}
from
'class-validator'
import
{
IsString
,
IsNumber
,
IsNotEmpty
,
MaxLength
,
IsMobilePhone
,
IsOptional
,
isNotEmpty
}
from
'class-validator'
import
{
ApiProperty
}
from
'@nestjs/swagger'
import
{
ApiProperty
,
IntersectionType
,
PartialType
,
PickType
}
from
'@nestjs/swagger'
import
{
BaseDto
}
from
'./base.dto'
import
{
BaseDto
,
DeviceIdDto
,
SnDto
,
TimeDto
,
UserBaseDto
}
from
'./base.dto'
import
{
LogConfigUpdateDto
}
from
'./config.dto'
import
{
LogConfigUpdateDto
}
from
'./config.dto'
export
class
UserBaseDto
{
export
class
DeviceQueryDto
extends
PartialType
(
UserBaseDto
)
{
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
default
:
''
})
user
?:
string
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
default
:
''
})
mobile
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'使用者商户名称'
,
default
:
''
})
merchant_name
?:
string
}
export
class
DeviceQueryDto
extends
BaseDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备SN'
,
required
:
false
,
default
:
''
})
sn
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
required
:
false
,
default
:
''
})
user
?:
string
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
required
:
false
,
default
:
''
})
mobile
?:
string
@
ApiProperty
({
@
ApiProperty
({
type
:
Number
,
type
:
Number
,
description
:
'在线状态 1:在线,2:离线,0:弃用'
,
description
:
'在线状态 1:在线,2:离线,0:弃用'
,
...
@@ -63,91 +12,9 @@ export class DeviceQueryDto extends BaseDto {
...
@@ -63,91 +12,9 @@ export class DeviceQueryDto extends BaseDto {
status
?:
number
status
?:
number
}
}
export
class
DeviceFindOneDto
{
export
class
DeviceFindOneDto
extends
PickType
(
UserBaseDto
,
[
'user'
,
'mobile'
])
{}
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
required
:
true
,
default
:
''
})
@
IsNotEmpty
({
message
:
'用户不能为空'
})
user
:
string
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
required
:
true
,
default
:
''
})
@
IsNotEmpty
({
message
:
'手机号不能为空'
})
mobile
:
string
}
export
class
AddDeviceDto
extends
UserBaseDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备SN'
,
required
:
true
,
default
:
''
})
@
MaxLength
(
20
,
{
message
:
'设备SN不能超过20个字符'
})
@
IsNotEmpty
({
message
:
'设备SN不能为空'
})
sn
:
string
}
export
class
AddBaseDto
{
}
export
class
AddDeviceInfoDto
{
@
ApiProperty
({
description
:
'SN 设备表的SN'
})
sn
?:
string
@
ApiProperty
({
description
:
'设备信息 包括 user mobile merchant_name'
})
device_info
?:
UserBaseDto
@
ApiProperty
({
description
:
'设备配置信息 包括 log_saved_days'
})
log_config
?:
LogConfigUpdateDto
@
ApiProperty
({
description
:
'设备对应的日志字段集合 '
})
user_fields
?:
Array
<
Number
>
@
ApiProperty
({
type
:
Number
,
description
:
'创建时间'
,
default
:
''
})
created_at
:
Date
@
ApiProperty
({
type
:
Date
,
description
:
'更新时间'
,
default
:
''
})
updated_at
:
Date
}
export
class
UpdateDeviceInfoDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
default
:
0
})
@
IsNumber
({},
{
message
:
'设备ID必须是数字'
})
@
IsNotEmpty
({
message
:
'设备ID不能为空'
})
device_id
:
number
@
ApiProperty
({
export
class
AddDeviceInfoDto
extends
IntersectionType
(
PartialType
(
SnDto
),
TimeDto
)
{
description
:
'SN 设备表的SN'
})
sn
?:
string
@
ApiProperty
({
@
ApiProperty
({
description
:
'设备信息 包括 user mobile merchant_name'
description
:
'设备信息 包括 user mobile merchant_name'
...
@@ -162,19 +29,6 @@ export class UpdateDeviceInfoDto {
...
@@ -162,19 +29,6 @@ export class UpdateDeviceInfoDto {
description
:
'设备对应的日志字段集合 '
description
:
'设备对应的日志字段集合 '
})
})
user_fields
?:
Array
<
Number
>
user_fields
?:
Array
<
Number
>
@
ApiProperty
({
type
:
Number
,
description
:
'创建时间'
,
default
:
''
})
created_at
:
Date
@
ApiProperty
({
type
:
Date
,
description
:
'更新时间'
,
default
:
''
})
updated_at
:
Date
}
}
export
class
UpdateDeviceInfoDto
extends
IntersectionType
(
PartialType
(
AddDeviceInfoDto
),
DeviceIdDto
)
{}
src/dto/device.status.dto.ts
View file @
6806a53
src/dto/field.dto.ts
View file @
6806a53
import
{
ApiProperty
}
from
'@nestjs/swagger'
import
{
ApiProperty
,
PartialType
}
from
'@nestjs/swagger'
import
{
IsEnum
,
IsNotEmpty
,
IsOptional
}
from
'class-validator'
import
{
IsEnum
,
IsNotEmpty
,
IsOptional
}
from
'class-validator'
import
{
LogFieldTypes
}
from
'../common/enums'
import
{
LogFieldTypes
}
from
'../common/enums'
...
@@ -18,18 +18,4 @@ export class CreateFieldDto {
...
@@ -18,18 +18,4 @@ export class CreateFieldDto {
type
:
LogFieldTypes
type
:
LogFieldTypes
}
}
export
class
QueryFieldDto
{
export
class
QueryFieldDto
extends
PartialType
(
CreateFieldDto
)
{}
@
ApiProperty
({
description
:
'字段名称'
,
required
:
false
})
name
?:
string
@
ApiProperty
({
description
:
'字段类型,枚举,目前只有"user"'
,
required
:
false
})
@
IsOptional
()
@
IsEnum
(
LogFieldTypes
,
{
message
:
'字段类型错误,目前只有user'
})
type
?:
LogFieldTypes
}
src/modules/config/config.service.ts
View file @
6806a53
...
@@ -27,7 +27,7 @@ export class ConfigService {
...
@@ -27,7 +27,7 @@ export class ConfigService {
return
resBody
return
resBody
}
}
async
upsertConfig
(
config
:
ConfigTypes
.
ILogConfig
):
Promise
<
CommonTypes
.
IResData
>
{
async
upsertConfig
(
config
:
Partial
<
ConfigTypes
.
ILogConfig
>
):
Promise
<
CommonTypes
.
IResData
>
{
const
now
=
new
Date
()
const
now
=
new
Date
()
await
this
.
prisma
.
log_config
.
upsert
({
where
:
{
device_id
:
config
.
device_id
},
update
:
{
...
config
,
updated_at
:
now
},
create
:
{
...
config
,
created_at
:
now
,
updated_at
:
now
}
})
await
this
.
prisma
.
log_config
.
upsert
({
where
:
{
device_id
:
config
.
device_id
},
update
:
{
...
config
,
updated_at
:
now
},
create
:
{
...
config
,
created_at
:
now
,
updated_at
:
now
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
...
...
src/modules/device/device.controller.ts
View file @
6806a53
import
{
Controller
,
Get
,
Post
,
Body
,
UseInterceptors
,
Query
,
UsePipes
,
Inject
}
from
'@nestjs/common'
import
{
Controller
,
Get
,
Post
,
Body
,
UseInterceptors
,
Query
,
UsePipes
,
Inject
}
from
'@nestjs/common'
import
{
DeviceService
}
from
'./device.service'
import
{
DeviceService
}
from
'./device.service'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
AddDeviceDto
,
AddDeviceInfoDto
,
DeviceFindOneDto
,
DeviceQueryDto
,
DeviceStatusDto
,
UpdateDeviceInfoDto
}
from
'../../dto'
import
{
AddDeviceInfoDto
,
DeviceFindOneDto
,
DeviceQueryDto
,
DeviceStatusDto
,
UpdateDeviceInfoDto
}
from
'../../dto'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
OnEvent
,
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
OnEvent
,
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
WsAccess
,
Events
,
WsMessageType
,
Command
,
RESPONSE_CODE
}
from
'../../common/enums'
import
{
WsAccess
,
Events
,
WsMessageType
,
Command
,
RESPONSE_CODE
}
from
'../../common/enums'
...
@@ -85,7 +85,6 @@ export class DeviceController {
...
@@ -85,7 +85,6 @@ export class DeviceController {
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
*/
@
OnEvent
(
Events
.
OnConnected
)
@
OnEvent
(
Events
.
OnConnected
)
@
OnEvent
(
Events
.
OnLogFileRemoved
)
@
Post
(
'/upsertStatus'
)
@
Post
(
'/upsertStatus'
)
@
ApiOperation
({
summary
:
'更新设备状态'
})
@
ApiOperation
({
summary
:
'更新设备状态'
})
async
upsertDeviceStatus
(@
Body
()
param
:
DeviceStatusDto
):
Promise
<
void
|
CommonTypes
.
IResData
>
{
async
upsertDeviceStatus
(@
Body
()
param
:
DeviceStatusDto
):
Promise
<
void
|
CommonTypes
.
IResData
>
{
...
@@ -94,6 +93,12 @@ export class DeviceController {
...
@@ -94,6 +93,12 @@ export class DeviceController {
this
.
eventEmitter
.
emit
(
Events
.
OnConnectError
,
out
)
this
.
eventEmitter
.
emit
(
Events
.
OnConnectError
,
out
)
})
})
}
}
@
OnEvent
(
Events
.
OnLogFileRemoved
)
@
Post
(
'/upsertFileRemovedTime'
)
@
ApiOperation
({
summary
:
'更新设备状态'
})
async
upsertFileRemovedTime
(@
Body
()
param
:
DeviceStatusDto
):
Promise
<
void
|
CommonTypes
.
IResData
>
{
return
await
this
.
deviceService
.
upsertFileRemovedTime
(
param
)
}
/**
/**
* 重置所有设备状态的方法
* 重置所有设备状态的方法
...
...
src/modules/device/device.service.ts
View file @
6806a53
...
@@ -120,6 +120,7 @@ export class DeviceService implements OnModuleInit {
...
@@ -120,6 +120,7 @@ export class DeviceService implements OnModuleInit {
})
})
}
}
await
prisma
.
device_status
.
create
({
data
:
{
device_id
:
id
,
status
:
WsStatus
.
Offline
}
})
await
prisma
.
device_status
.
create
({
data
:
{
device_id
:
id
,
status
:
WsStatus
.
Offline
}
})
// await this.addDeviceInfo(Object.assign({ device_id: id },deviceInfo))
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备成功'
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备成功'
}
}
}
...
@@ -127,6 +128,8 @@ export class DeviceService implements OnModuleInit {
...
@@ -127,6 +128,8 @@ export class DeviceService implements OnModuleInit {
async
addDeviceInfo
(
deviceInfo
:
DeviceTypes
.
IDeviceSaves
):
Promise
<
CommonTypes
.
IResData
>
{
async
addDeviceInfo
(
deviceInfo
:
DeviceTypes
.
IDeviceSaves
):
Promise
<
CommonTypes
.
IResData
>
{
const
now
=
new
Date
()
const
now
=
new
Date
()
const
{
device_id
,
...
data
}
=
deviceInfo
const
{
device_id
,
...
data
}
=
deviceInfo
console
.
log
(
'device_id'
,
device_id
)
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
data
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
data
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
if
(
sn
)
{
if
(
sn
)
{
...
@@ -144,7 +147,7 @@ export class DeviceService implements OnModuleInit {
...
@@ -144,7 +147,7 @@ export class DeviceService implements OnModuleInit {
data
:
user_fields
.
map
((
item
:
number
)
=>
({
device_id
,
field_id
:
item
}))
data
:
user_fields
.
map
((
item
:
number
)
=>
({
device_id
,
field_id
:
item
}))
})
})
}
}
await
prisma
.
device_status
.
create
({
data
:
{
device_id
:
deviceInfo
.
device_id
,
status
:
WsStatus
.
Offline
}
})
await
prisma
.
device_status
.
create
({
data
:
{
device_id
,
status
:
WsStatus
.
Offline
}
})
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备信息成功'
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备信息成功'
}
}
}
...
@@ -188,6 +191,14 @@ export class DeviceService implements OnModuleInit {
...
@@ -188,6 +191,14 @@ export class DeviceService implements OnModuleInit {
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
}
}
async
upsertFileRemovedTime
(
data
:
DeviceStatus
.
IDeviceStatus
):
Promise
<
CommonTypes
.
IResData
>
{
await
this
.
prisma
.
device_status
.
update
({
where
:
{
device_id
:
data
.
device_id
},
data
:
{
last_log_remove_time
:
data
.
last_log_remove_time
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
}
async
resetAllDeviceStatus
():
Promise
<
CommonTypes
.
IResData
>
{
async
resetAllDeviceStatus
():
Promise
<
CommonTypes
.
IResData
>
{
Logger
.
warn
(
'========重置所有设备状态========'
)
Logger
.
warn
(
'========重置所有设备状态========'
)
await
this
.
prisma
.
device_status
.
updateMany
({
await
this
.
prisma
.
device_status
.
updateMany
({
...
...
src/modules/file/file.controller.ts
View file @
6806a53
...
@@ -8,6 +8,7 @@ import { DocTypes } from '../../common/enums'
...
@@ -8,6 +8,7 @@ import { DocTypes } from '../../common/enums'
import
{
FileDeleteDto
,
FileQueryDto
}
from
'../../dto'
import
{
FileDeleteDto
,
FileQueryDto
}
from
'../../dto'
import
{
ApiOperation
,
ApiTags
}
from
'@nestjs/swagger'
import
{
ApiOperation
,
ApiTags
}
from
'@nestjs/swagger'
import
{
FilePathPipe
,
PagesPipe
,
ParseDeviceIdPipe
,
ParseIdPipe
}
from
'../../common/pipes'
import
{
FilePathPipe
,
PagesPipe
,
ParseDeviceIdPipe
,
ParseIdPipe
}
from
'../../common/pipes'
import
{
join
}
from
'path'
@
ApiTags
(
'文件管理'
)
@
ApiTags
(
'文件管理'
)
@
UsePipes
(
new
ValidationPipe
())
@
UsePipes
(
new
ValidationPipe
())
@
Controller
(
'file'
)
@
Controller
(
'file'
)
...
@@ -30,12 +31,11 @@ export class FileController {
...
@@ -30,12 +31,11 @@ export class FileController {
@
UsePipes
(
new
FilePathPipe
())
@
UsePipes
(
new
FilePathPipe
())
@
ApiOperation
({
summary
:
'文件上传'
})
@
ApiOperation
({
summary
:
'文件上传'
})
@
UploadFile
(
'file'
,
{
fileFilter
:
FileMimeTypeFilter
(
'text/plain'
),
limits
:
{
fileSize
:
1024
*
1024
*
100
}
})
@
UploadFile
(
'file'
,
{
fileFilter
:
FileMimeTypeFilter
(
'text/plain'
),
limits
:
{
fileSize
:
1024
*
1024
*
100
}
})
upload
(@
UploadedFile
()
file
:
Express
.
Multer
.
File
,
@
Req
()
req
:
Request
):
FileTypes
.
FileUpload
<
DocTypes
>
{
async
upload
(@
UploadedFile
()
file
:
Express
.
Multer
.
File
,
@
Req
()
req
:
Request
):
Promise
<
CommonTypes
.
IResData
<
FileTypes
.
FileUpload
<
DocTypes
>>
>
{
const
{
fieldname
,
encoding
,
destination
,
stream
,
buffer
,
path
,
...
o
}
=
file
const
{
fieldname
,
encoding
,
stream
,
buffer
,
path
:
origin_
path
,
...
o
}
=
file
const
bodyData
:
Pick
<
FileTypes
.
FileUpload
<
DocTypes
>
,
'device_id'
|
'type'
>
=
{
device_id
:
Number
(
req
.
body
.
deviceId
),
type
:
req
.
body
.
type
}
const
bodyData
:
Pick
<
FileTypes
.
FileUpload
<
DocTypes
>
,
'device_id'
|
'type'
>
=
{
device_id
:
Number
(
req
.
body
.
deviceId
),
type
:
req
.
body
.
type
}
const
f
:
FileTypes
.
FileUpload
<
DocTypes
>
=
Object
.
assign
({
created_at
:
new
Date
(),
path
:
`
${
req
.
protocol
}
://
${
req
.
get
(
'Host'
)}${
path
}
`
},
bodyData
,
o
)
const
f
:
FileTypes
.
FileUpload
<
DocTypes
>
=
Object
.
assign
({
created_at
:
new
Date
(),
path
:
`
${
req
.
protocol
}
://
${
req
.
get
(
'Host'
)}${
origin_path
}
`
},
bodyData
,
o
)
this
.
fileService
.
save
(
f
)
return
await
this
.
fileService
.
save
(
f
)
return
f
}
}
/**
/**
...
...
src/modules/file/file.service.ts
View file @
6806a53
...
@@ -6,6 +6,7 @@ import * as path from 'path'
...
@@ -6,6 +6,7 @@ import * as path from 'path'
import
{
DocTypes
,
RESPONSE_CODE
}
from
'../../common/enums'
import
{
DocTypes
,
RESPONSE_CODE
}
from
'../../common/enums'
import
{
PrismaService
}
from
'nestjs-prisma'
import
{
PrismaService
}
from
'nestjs-prisma'
import
{
Doc
}
from
'prettier'
import
{
Doc
}
from
'prettier'
import
{
join
}
from
'path'
@
Injectable
()
@
Injectable
()
export
class
FileService
{
export
class
FileService
{
...
@@ -52,22 +53,25 @@ export class FileService {
...
@@ -52,22 +53,25 @@ export class FileService {
* @returns 一个 Promise,成功时解析为包含操作结果的 CommonTypes.IResData 对象。
* @returns 一个 Promise,成功时解析为包含操作结果的 CommonTypes.IResData 对象。
*/
*/
async
save
(
file
:
FileTypes
.
FileUpload
<
DocTypes
>
):
Promise
<
CommonTypes
.
IResData
>
{
async
save
(
file
:
FileTypes
.
FileUpload
<
DocTypes
>
):
Promise
<
CommonTypes
.
IResData
>
{
const
{
filename
,
path
,
created_at
,
...
where
}
=
file
const
{
filename
,
path
,
created_at
,
destination
,
size
,
...
where
}
=
file
this
.
prisma
.
$transaction
(
async
prisma
=>
{
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
const
{
id
,
path
:
origin_path
}
=
(
await
prisma
.
file
.
findFirst
({
where
,
select
:
{
id
:
true
,
path
:
true
}
}))
||
{}
const
files
:
Pick
<
FileTypes
.
IFile
<
DocTypes
>
,
'id'
|
'destination'
|
'filename'
>
[]
=
await
prisma
.
file
.
findMany
({
where
,
select
:
{
id
:
true
,
destination
:
true
,
filename
:
true
}
})
if
(
id
)
{
if
(
files
?.
length
)
{
files
.
forEach
(
async
({
id
,
destination
:
dest
,
filename
:
fname
})
=>
{
const
origin_path
=
join
(
dest
,
fname
)
try
{
try
{
fs
.
existsSync
(
origin_path
)
&&
fs
.
unlinkSync
(
origin_path
)
fs
.
existsSync
(
origin_path
)
&&
fs
.
unlinkSync
(
origin_path
)
}
catch
(
err
)
{
}
catch
(
err
)
{
Logger
.
error
(
'删除文件失败'
,
err
)
Logger
.
error
(
'删除文件失败'
,
err
)
}
}
await
prisma
.
file
.
update
({
where
:
{
id
},
data
:
{
filename
,
path
,
created_at
}
})
await
prisma
.
file
.
update
({
where
:
{
id
},
data
:
{
filename
,
path
,
created_at
,
destination
}
})
})
}
else
{
}
else
{
await
prisma
.
file
.
create
({
data
:
file
})
await
prisma
.
file
.
create
({
data
:
file
})
}
}
await
prisma
.
device_status
.
update
({
where
:
{
device_id
:
file
.
device_id
},
data
:
{
last_log_upload_time
:
created_at
}
})
await
prisma
.
device_status
.
update
({
where
:
{
device_id
:
file
.
device_id
},
data
:
{
last_log_upload_time
:
created_at
}
})
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'文件保存成功'
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'文件保存成功'
,
data
:
{
path
:
file
.
path
}
}
}
}
/**
/**
...
...
src/typings/broadcast.d.ts
View file @
6806a53
declare
module
Broadcastypes
{
import
{
Command
}
from
'../common/enums'
declare
namespace
Broadcastypes
{
type
MessageData
<
T
extends
Command
>
=
T
extends
Command
.
RemoveLog
?
Required
<
Pick
<
WsTypes
.
MessageData
<
Command
.
RemoveLog
>
,
'days'
>>
&
Omit
<
MessageData
<
T
>
,
'days'
>
:
T
extends
Command
.
RemoveRecord
?
Required
<
Pick
<
WsTypes
.
MessageData
<
Command
.
RemoveRecord
>
,
'id'
>>
&
Omit
<
MessageData
<
T
>
,
'id'
>
:
T
extends
Command
.
GetStorage
?
Required
<
Pick
<
WsTypes
.
MessageData
<
Command
.
GetStorage
>
,
'key'
>>
&
Omit
<
MessageData
<
T
>
,
'key'
>
:
T
extends
Command
.
SetPollingInterval
?
Required
<
Pick
<
WsTypes
.
MessageData
<
Command
.
SetPollingInterval
>
,
'interval'
>>
&
Omit
<
MessageData
<
T
>
,
'interval'
>
:
WsTypes
.
MessageBody
<
T
>
}
}
src/typings/file.d.ts
View file @
6806a53
...
@@ -5,6 +5,7 @@ declare namespace FileTypes {
...
@@ -5,6 +5,7 @@ declare namespace FileTypes {
filename
:
string
filename
:
string
path
:
string
path
:
string
size
:
number
size
:
number
destination
:
string
type
:
T
type
:
T
}
}
type
FileUpload
<
T
>
=
Omit
<
IFile
<
T
>
,
'id'
>
type
FileUpload
<
T
>
=
Omit
<
IFile
<
T
>
,
'id'
>
...
...
src/typings/ws.d.ts
View file @
6806a53
...
@@ -24,7 +24,8 @@ declare namespace WsTypes {
...
@@ -24,7 +24,8 @@ declare namespace WsTypes {
command
:
T
command
:
T
days
?:
number
days
?:
number
id
?:
string
id
?:
string
interval
?:
number
interval
?:
number
,
key
?:
string
}
}
type
WsConnError
=
CommonTypes
.
IDeviceId
&
{
message
:
string
}
type
WsConnError
=
CommonTypes
.
IDeviceId
&
{
message
:
string
}
}
}
test.http
View file @
6806a53
# http://localhost:3000/device?
status=1
# http://localhost:3000/device?
user=尹
# http://localhost:3000/device/addAll
# http://localhost:3000/device/addAll
# http://localhost:3000/device/task
# http://localhost:3000/device/task
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
# content-type: application/json
# content-type: application/json
# {
# {
# "sn":"
26DYD24513203857
"
# "sn":"
test2002
"
# }
# }
# POST http://localhost:3000/device/addInfo HTTP/1.1
# POST http://localhost:3000/device/addInfo HTTP/1.1
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment