Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
8c774316
Unverified
Commit
8c774316
authored
Mar 09, 2023
by
Cologler
Committed by
GitHub
Mar 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: build storage key (#1326)
* refactor build storage key * sort imports * use gofmt to format code
parent
25da3c07
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
12 deletions
+32
-12
resource.go
server/resource.go
+32
-12
No files found.
server/resource.go
View file @
8c774316
...
...
@@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"
"time"
...
...
@@ -24,6 +25,8 @@ const (
maxFileSize
=
32
<<
20
)
var
fileKeyPattern
=
regexp
.
MustCompile
(
`\{[a-z]{1,9}\}`
)
func
(
s
*
Server
)
registerResourceRoutes
(
g
*
echo
.
Group
)
{
g
.
POST
(
"/resource"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
...
...
@@ -136,22 +139,39 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if
storage
.
Type
==
api
.
StorageS3
{
s3Config
:=
storage
.
Config
.
S3Config
t
:=
time
.
Now
()
s3FileKey
:=
s3Config
.
Path
var
s3FileKey
string
if
s3Config
.
Path
==
""
{
s3FileKey
=
filename
}
else
if
!
strings
.
Contains
(
s3Config
.
Path
,
"{filename}"
)
{
s3FileKey
=
path
.
Join
(
s3Config
.
Path
,
filename
)
}
else
{
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{filename}"
,
filename
)
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{filetype}"
,
filetype
)
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{timestamp}"
,
fmt
.
Sprintf
(
"%d"
,
t
.
Unix
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{year}"
,
fmt
.
Sprintf
(
"%d"
,
t
.
Year
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{month}"
,
fmt
.
Sprintf
(
"%02d"
,
t
.
Month
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{day}"
,
fmt
.
Sprintf
(
"%02d"
,
t
.
Day
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{hour}"
,
fmt
.
Sprintf
(
"%02d"
,
t
.
Hour
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{minute}"
,
fmt
.
Sprintf
(
"%02d"
,
t
.
Minute
()))
s3FileKey
=
strings
.
ReplaceAll
(
s3FileKey
,
"{second}"
,
fmt
.
Sprintf
(
"%02d"
,
t
.
Second
()))
s3FileKey
=
fileKeyPattern
.
ReplaceAllStringFunc
(
s3Config
.
Path
,
func
(
s
string
)
string
{
switch
s
{
case
"{filename}"
:
return
filename
case
"{filetype}"
:
return
filetype
case
"{timestamp}"
:
return
fmt
.
Sprintf
(
"%d"
,
t
.
Unix
())
case
"{year}"
:
return
fmt
.
Sprintf
(
"%d"
,
t
.
Year
())
case
"{month}"
:
return
fmt
.
Sprintf
(
"%02d"
,
t
.
Month
())
case
"{day}"
:
return
fmt
.
Sprintf
(
"%02d"
,
t
.
Day
())
case
"{hour}"
:
return
fmt
.
Sprintf
(
"%02d"
,
t
.
Hour
())
case
"{minute}"
:
return
fmt
.
Sprintf
(
"%02d"
,
t
.
Minute
())
case
"{second}"
:
return
fmt
.
Sprintf
(
"%02d"
,
t
.
Second
())
}
return
s
})
if
!
strings
.
Contains
(
s3Config
.
Path
,
"{filename}"
)
{
s3FileKey
=
path
.
Join
(
s3FileKey
,
filename
)
}
}
s3client
,
err
:=
s3
.
NewClient
(
ctx
,
&
s3
.
Config
{
AccessKey
:
s3Config
.
AccessKey
,
SecretKey
:
s3Config
.
SecretKey
,
...
...
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