Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6ec45751
authored
Mar 13, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: remove consumeQuota
parent
9b284951
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 additions
and
8 deletions
+1
-8
relay/relay-image.go
+1
-8
No files found.
relay/relay-image.go
View file @
6ec45751
...
@@ -24,17 +24,14 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
...
@@ -24,17 +24,14 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
channelType
:=
c
.
GetInt
(
"channel"
)
channelType
:=
c
.
GetInt
(
"channel"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
userId
:=
c
.
GetInt
(
"id"
)
userId
:=
c
.
GetInt
(
"id"
)
consumeQuota
:=
c
.
GetBool
(
"consume_quota"
)
group
:=
c
.
GetString
(
"group"
)
group
:=
c
.
GetString
(
"group"
)
startTime
:=
time
.
Now
()
startTime
:=
time
.
Now
()
var
imageRequest
dto
.
ImageRequest
var
imageRequest
dto
.
ImageRequest
if
consumeQuota
{
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
imageRequest
)
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
imageRequest
)
if
err
!=
nil
{
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapper
(
err
,
"bind_request_body_failed"
,
http
.
StatusBadRequest
)
return
service
.
OpenAIErrorWrapper
(
err
,
"bind_request_body_failed"
,
http
.
StatusBadRequest
)
}
}
}
if
imageRequest
.
Model
==
""
{
if
imageRequest
.
Model
==
""
{
imageRequest
.
Model
=
"dall-e-2"
imageRequest
.
Model
=
"dall-e-2"
...
@@ -136,7 +133,7 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
...
@@ -136,7 +133,7 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
quota
:=
int
(
ratio
*
sizeRatio
*
qualityRatio
*
1000
)
*
imageRequest
.
N
quota
:=
int
(
ratio
*
sizeRatio
*
qualityRatio
*
1000
)
*
imageRequest
.
N
if
consumeQuota
&&
userQuota
-
quota
<
0
{
if
userQuota
-
quota
<
0
{
return
service
.
OpenAIErrorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
return
service
.
OpenAIErrorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
}
}
...
@@ -176,7 +173,6 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
...
@@ -176,7 +173,6 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
var
textResponse
dto
.
ImageResponse
var
textResponse
dto
.
ImageResponse
defer
func
(
ctx
context
.
Context
)
{
defer
func
(
ctx
context
.
Context
)
{
useTimeSeconds
:=
time
.
Now
()
.
Unix
()
-
startTime
.
Unix
()
useTimeSeconds
:=
time
.
Now
()
.
Unix
()
-
startTime
.
Unix
()
if
consumeQuota
{
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
return
}
}
...
@@ -196,10 +192,8 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
...
@@ -196,10 +192,8 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
}
}
}
}(
c
.
Request
.
Context
())
}(
c
.
Request
.
Context
())
if
consumeQuota
{
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -215,7 +209,6 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
...
@@ -215,7 +209,6 @@ func RelayImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusC
}
}
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
responseBody
))
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
responseBody
))
}
for
k
,
v
:=
range
resp
.
Header
{
for
k
,
v
:=
range
resp
.
Header
{
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
...
...
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