Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
A
Avatar Handler Re
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Avatar Handler Re
Commits
dd882e3f
Commit
dd882e3f
authored
Jul 31, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add any url get
parent
ebbbadba
Pipeline
#15069
passed with stages
in 5 minutes and 48 seconds
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
5 deletions
+46
-5
src/app.controller.ts
src/app.controller.ts
+34
-2
src/app.module.ts
src/app.module.ts
+1
-0
src/app.service.ts
src/app.service.ts
+11
-3
No files found.
src/app.controller.ts
View file @
dd882e3f
...
...
@@ -9,14 +9,19 @@ import {
import
{
AppService
}
from
'
./app.service
'
;
import
{
ApiOperation
,
ApiParam
,
ApiProduces
}
from
'
@nestjs/swagger
'
;
import
{
BlankReturnMessageDto
}
from
'
./dto/ReturnMessage.dto
'
;
import
{
ConfigService
}
from
'
@nestjs/config
'
;
@
Controller
()
export
class
AppController
{
constructor
(
private
readonly
appService
:
AppService
)
{}
private
readonly
urlGetPrefix
=
this
.
config
.
get
<
string
>
(
'
URL_GET_PREFIX
'
)
||
''
;
constructor
(
private
readonly
appService
:
AppService
,
private
config
:
ConfigService
,
)
{}
@
Get
(
'
avatar/:username/:size/:filename.png
'
)
@
Header
(
'
Cache-Control
'
,
'
public, max-age=600
'
)
// @Header('Content-Type', 'image/png')
@
ApiOperation
({
summary
:
'
获取裁剪后的用户头像
'
})
@
ApiParam
({
name
:
'
username
'
,
description
:
'
用户名
'
})
@
ApiParam
({
name
:
'
size
'
,
description
:
'
头像尺寸
'
,
type
:
'
number
'
})
...
...
@@ -35,4 +40,31 @@ export class AppController {
disposition
:
'
inline
'
,
});
}
@
Get
(
'
url/:url/:size/:filename.png
'
)
@
Header
(
'
Cache-Control
'
,
'
public, max-age=31536000
'
)
@
ApiOperation
({
summary
:
'
获取裁剪后的用户头像
'
})
@
ApiParam
({
name
:
'
url
'
,
description
:
'
地址,没有前缀
'
})
@
ApiParam
({
name
:
'
size
'
,
description
:
'
头像尺寸
'
,
type
:
'
number
'
})
@
ApiParam
({
name
:
'
filename
'
,
description
:
'
图片文件名
'
})
@
ApiProduces
(
'
image/png
'
)
async
getAnyImage
(
@
Param
(
'
url
'
)
url
:
string
,
@
Param
(
'
size
'
,
ParseIntPipe
)
size
:
number
,
)
{
if
(
size
<
8
||
size
>
1000
)
{
throw
new
BlankReturnMessageDto
(
400
,
'
Invalid size
'
).
toException
();
}
if
(
url
.
length
>
247
)
{
throw
new
BlankReturnMessageDto
(
400
,
'
Invalid url
'
).
toException
();
}
const
buffer
=
await
this
.
appService
.
getResizedImage
(
`
${
this
.
urlGetPrefix
}${
url
}
`
,
size
,
);
return
new
StreamableFile
(
buffer
,
{
type
:
'
image/png
'
,
disposition
:
'
inline
'
,
});
}
}
src/app.module.ts
View file @
dd882e3f
...
...
@@ -23,6 +23,7 @@ import { RedisModule } from '@nestjs-modules/ioredis';
type
:
'
postgres
'
,
entities
:
[
Avatar
],
// entities here
synchronize
:
!
config
.
get
(
'
DB_NO_INIT
'
),
dropSchema
:
!!
config
.
get
(
'
DB_DROP
'
),
host
:
config
.
get
(
'
DB_HOST
'
),
port
:
parseInt
(
config
.
get
(
'
DB_PORT
'
))
||
5432
,
username
:
config
.
get
(
'
DB_USER
'
),
...
...
src/app.service.ts
View file @
dd882e3f
...
...
@@ -18,7 +18,11 @@ export class AppService extends ConsoleLogger {
super
(
'
app
'
);
}
private
async
saveAvatar
(
url
:
string
,
size
:
number
,
buffer
:
Buffer
)
{
private
async
saveAvatar
(
url
:
string
,
size
:
number
,
buffer
:
Buffer
,
)
{
const
avatar
=
new
Avatar
();
avatar
.
url
=
url
;
avatar
.
size
=
size
;
...
...
@@ -26,8 +30,7 @@ export class AppService extends ConsoleLogger {
return
this
.
db
.
getRepository
(
Avatar
).
save
(
avatar
);
}
async
getImage
(
username
:
string
,
size
:
number
)
{
const
url
=
await
this
.
avatarApi
.
getURLFromUsername
(
username
);
async
getResizedImage
(
url
:
string
,
size
:
number
)
{
const
existingAvatar
=
await
this
.
db
.
getRepository
(
Avatar
).
findOne
({
where
:
{
url
,
...
...
@@ -74,4 +77,9 @@ export class AppService extends ConsoleLogger {
},
);
}
async
getImage
(
username
:
string
,
size
:
number
)
{
const
url
=
await
this
.
avatarApi
.
getURLFromUsername
(
username
);
return
this
.
getResizedImage
(
url
,
size
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment