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
3060dafb
Unverified
Commit
3060dafb
authored
Apr 16, 2023
by
boojack
Committed by
GitHub
Apr 16, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update resource link template (#1537)
parent
5cb43617
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
1 deletion
+52
-1
resource.go
server/resource.go
+51
-0
resource.ts
web/src/utils/resource.ts
+1
-1
No files found.
server/resource.go
View file @
3060dafb
...
...
@@ -314,6 +314,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
func
(
s
*
Server
)
registerResourcePublicRoutes
(
g
*
echo
.
Group
)
{
// (DEPRECATED) use /r/:resourceId/:publicId/:filename instead.
g
.
GET
(
"/r/:resourceId/:publicId"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
...
...
@@ -358,6 +359,56 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
}
return
c
.
Stream
(
http
.
StatusOK
,
resourceType
,
bytes
.
NewReader
(
blob
))
})
g
.
GET
(
"/r/:resourceId/:publicId/:filename"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"resourceId"
)))
.
SetInternal
(
err
)
}
publicID
,
err
:=
url
.
QueryUnescape
(
c
.
Param
(
"publicId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"publicID is invalid: %s"
,
c
.
Param
(
"publicId"
)))
.
SetInternal
(
err
)
}
filename
,
err
:=
url
.
QueryUnescape
(
c
.
Param
(
"filename"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"filename is invalid: %s"
,
c
.
Param
(
"filename"
)))
.
SetInternal
(
err
)
}
resourceFind
:=
&
api
.
ResourceFind
{
ID
:
&
resourceID
,
PublicID
:
&
publicID
,
Filename
:
&
filename
,
GetBlob
:
true
,
}
resource
,
err
:=
s
.
Store
.
FindResource
(
ctx
,
resourceFind
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to find resource by ID: %v"
,
resourceID
))
.
SetInternal
(
err
)
}
blob
:=
resource
.
Blob
if
resource
.
InternalPath
!=
""
{
src
,
err
:=
os
.
Open
(
resource
.
InternalPath
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to open the local resource: %s"
,
resource
.
InternalPath
))
.
SetInternal
(
err
)
}
defer
src
.
Close
()
blob
,
err
=
io
.
ReadAll
(
src
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to read the local resource: %s"
,
resource
.
InternalPath
))
.
SetInternal
(
err
)
}
}
c
.
Response
()
.
Writer
.
Header
()
.
Set
(
echo
.
HeaderCacheControl
,
"max-age=31536000, immutable"
)
c
.
Response
()
.
Writer
.
Header
()
.
Set
(
echo
.
HeaderContentSecurityPolicy
,
"default-src 'self'"
)
resourceType
:=
strings
.
ToLower
(
resource
.
Type
)
if
strings
.
HasPrefix
(
resourceType
,
"text"
)
{
resourceType
=
echo
.
MIMETextPlainCharsetUTF8
}
else
if
strings
.
HasPrefix
(
resourceType
,
"video"
)
||
strings
.
HasPrefix
(
resourceType
,
"audio"
)
{
http
.
ServeContent
(
c
.
Response
(),
c
.
Request
(),
resource
.
Filename
,
time
.
Unix
(
resource
.
UpdatedTs
,
0
),
bytes
.
NewReader
(
blob
))
return
nil
}
return
c
.
Stream
(
http
.
StatusOK
,
resourceType
,
bytes
.
NewReader
(
blob
))
})
}
func
(
s
*
Server
)
createResourceCreateActivity
(
c
echo
.
Context
,
resource
*
api
.
Resource
)
error
{
...
...
web/src/utils/resource.ts
View file @
3060dafb
...
...
@@ -3,5 +3,5 @@ export const getResourceUrl = (resource: Resource, withOrigin = true) => {
return
resource
.
externalLink
;
}
return
`
${
withOrigin
?
window
.
location
.
origin
:
""
}
/o/r/
${
resource
.
id
}
/
${
resource
.
publicId
}
`
;
return
`
${
withOrigin
?
window
.
location
.
origin
:
""
}
/o/r/
${
resource
.
id
}
/
${
resource
.
publicId
}
/
${
resource
.
filename
}
`
;
};
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