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
5361f76b
Unverified
Commit
5361f76b
authored
Apr 03, 2023
by
boojack
Committed by
GitHub
Apr 03, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update filename when creating resource (#1460)
parent
bdc00d67
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
16 deletions
+13
-16
resource.go
server/resource.go
+13
-16
No files found.
server/resource.go
View file @
5361f76b
...
@@ -79,7 +79,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -79,7 +79,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Upload file not found"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Upload file not found"
)
.
SetInternal
(
err
)
}
}
filename
:=
file
.
Filename
filetype
:=
file
.
Header
.
Get
(
"Content-Type"
)
filetype
:=
file
.
Header
.
Get
(
"Content-Type"
)
size
:=
file
.
Size
size
:=
file
.
Size
sourceFile
,
err
:=
file
.
Open
()
sourceFile
,
err
:=
file
.
Open
()
...
@@ -88,6 +87,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -88,6 +87,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
}
defer
sourceFile
.
Close
()
defer
sourceFile
.
Close
()
var
resourceCreate
*
api
.
ResourceCreate
systemSettingStorageServiceID
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingStorageServiceIDName
})
systemSettingStorageServiceID
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingStorageServiceIDName
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find storage"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find storage"
)
.
SetInternal
(
err
)
...
@@ -99,23 +99,19 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -99,23 +99,19 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal storage service id"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal storage service id"
)
.
SetInternal
(
err
)
}
}
}
}
var
resourceCreate
*
api
.
ResourceCreate
if
storageServiceID
==
api
.
DatabaseStorage
{
if
storageServiceID
==
api
.
DatabaseStorage
{
// Database storage.
fileBytes
,
err
:=
io
.
ReadAll
(
sourceFile
)
fileBytes
,
err
:=
io
.
ReadAll
(
sourceFile
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to read file"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to read file"
)
.
SetInternal
(
err
)
}
}
resourceCreate
=
&
api
.
ResourceCreate
{
resourceCreate
=
&
api
.
ResourceCreate
{
CreatorID
:
userID
,
CreatorID
:
userID
,
Filename
:
filename
,
Filename
:
file
.
File
name
,
Type
:
filetype
,
Type
:
filetype
,
Size
:
size
,
Size
:
size
,
Blob
:
fileBytes
,
Blob
:
fileBytes
,
}
}
}
else
if
storageServiceID
==
api
.
LocalStorage
{
}
else
if
storageServiceID
==
api
.
LocalStorage
{
// Local storage.
systemSettingLocalStoragePath
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingLocalStoragePathName
})
systemSettingLocalStoragePath
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingLocalStoragePathName
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find local storage path setting"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find local storage path setting"
)
.
SetInternal
(
err
)
...
@@ -131,9 +127,9 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -131,9 +127,9 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if
!
strings
.
Contains
(
filePath
,
"{filename}"
)
{
if
!
strings
.
Contains
(
filePath
,
"{filename}"
)
{
filePath
=
path
.
Join
(
filePath
,
"{filename}"
)
filePath
=
path
.
Join
(
filePath
,
"{filename}"
)
}
}
filePath
=
path
.
Join
(
s
.
Profile
.
Data
,
replacePathTemplate
(
filePath
,
filename
))
filePath
=
path
.
Join
(
s
.
Profile
.
Data
,
replacePathTemplate
(
filePath
,
file
.
File
name
))
dir
Path
:=
filepath
.
Dir
(
filePath
)
dir
,
filename
:=
filepath
.
Split
(
filePath
)
if
err
=
os
.
MkdirAll
(
dir
Path
,
os
.
ModePerm
);
err
!=
nil
{
if
err
=
os
.
MkdirAll
(
dir
,
os
.
ModePerm
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create directory"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create directory"
)
.
SetInternal
(
err
)
}
}
dst
,
err
:=
os
.
Create
(
filePath
)
dst
,
err
:=
os
.
Create
(
filePath
)
...
@@ -161,12 +157,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -161,12 +157,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if
storage
.
Type
==
api
.
StorageS3
{
if
storage
.
Type
==
api
.
StorageS3
{
s3Config
:=
storage
.
Config
.
S3Config
s3Config
:=
storage
.
Config
.
S3Config
var
s3FileKey
string
s3Client
,
err
:=
s3
.
NewClient
(
ctx
,
&
s3
.
Config
{
if
!
strings
.
Contains
(
s3Config
.
Path
,
"{filename}"
)
{
s3FileKey
=
path
.
Join
(
s3Config
.
Path
,
"{filename}"
)
}
s3FileKey
=
replacePathTemplate
(
s3FileKey
,
filename
)
s3client
,
err
:=
s3
.
NewClient
(
ctx
,
&
s3
.
Config
{
AccessKey
:
s3Config
.
AccessKey
,
AccessKey
:
s3Config
.
AccessKey
,
SecretKey
:
s3Config
.
SecretKey
,
SecretKey
:
s3Config
.
SecretKey
,
EndPoint
:
s3Config
.
EndPoint
,
EndPoint
:
s3Config
.
EndPoint
,
...
@@ -179,7 +170,13 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -179,7 +170,13 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to new s3 client"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to new s3 client"
)
.
SetInternal
(
err
)
}
}
link
,
err
:=
s3client
.
UploadFile
(
ctx
,
s3FileKey
,
filetype
,
sourceFile
)
filePath
:=
s3Config
.
Path
if
!
strings
.
Contains
(
filePath
,
"{filename}"
)
{
filePath
=
path
.
Join
(
filePath
,
"{filename}"
)
}
filePath
=
replacePathTemplate
(
filePath
,
file
.
Filename
)
_
,
filename
:=
filepath
.
Split
(
filePath
)
link
,
err
:=
s3Client
.
UploadFile
(
ctx
,
filePath
,
filetype
,
sourceFile
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to upload via s3 client"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to upload via s3 client"
)
.
SetInternal
(
err
)
}
}
...
...
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