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
5ea561af
Unverified
Commit
5ea561af
authored
Jul 05, 2023
by
Athurg Gooth
Committed by
GitHub
Jul 05, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add support for purged resource link (#1897)
Add support for purged resource link
parent
2033b0c8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
78 deletions
+8
-78
resource.go
server/resource.go
+8
-78
No files found.
server/resource.go
View file @
5ea561af
...
...
@@ -392,8 +392,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
{
f
:=
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
if
err
!=
nil
{
...
...
@@ -411,80 +410,8 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Resource visibility not match"
)
.
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
)
}
resourceFind
:=
&
api
.
ResourceFind
{
ID
:
&
resourceID
,
PublicID
:
&
publicID
,
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
)
}
// Private resource require logined user is the creator
if
resourceVisibility
==
store
.
Private
&&
(
!
ok
||
userID
!=
resource
.
CreatorID
)
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Resource visibility not match"
)
.
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
))
})
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
)
}
resourceVisibility
,
err
:=
CheckResourceVisibility
(
ctx
,
s
.
Store
,
resourceID
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Failed to get resource visibility"
)
.
SetInternal
(
err
)
}
// Protected resource require a logined user
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
resourceVisibility
==
store
.
Protected
&&
(
!
ok
||
userID
<=
0
)
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Resource visibility not match"
)
.
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
)
...
...
@@ -512,7 +439,7 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
}
if
c
.
QueryParam
(
"thumbnail"
)
==
"1"
&&
common
.
HasPrefixes
(
resource
.
Type
,
"image/png"
,
"image/jpeg"
)
{
ext
:=
filepath
.
Ext
(
f
ilename
)
ext
:=
filepath
.
Ext
(
resource
.
F
ilename
)
thumbnailPath
:=
path
.
Join
(
s
.
Profile
.
Data
,
thumbnailImagePath
,
fmt
.
Sprintf
(
"%d-%s%s"
,
resource
.
ID
,
resource
.
PublicID
,
ext
))
thumbnailBlob
,
err
:=
getOrGenerateThumbnailImage
(
blob
,
thumbnailPath
)
if
err
!=
nil
{
...
...
@@ -532,7 +459,10 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
return
nil
}
return
c
.
Stream
(
http
.
StatusOK
,
resourceType
,
bytes
.
NewReader
(
blob
))
})
}
g
.
GET
(
"/r/:resourceId"
,
f
)
g
.
GET
(
"/r/:resourceId/"
,
f
)
g
.
GET
(
"/r/:resourceId/*"
,
f
)
}
func
(
s
*
Server
)
createResourceCreateActivity
(
ctx
context
.
Context
,
resource
*
api
.
Resource
)
error
{
...
...
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