Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
ce019738
ligase
项目概览
FinClip
/
ligase
通知
36
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
ligase
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ce019738
编写于
9月 21, 2017
作者:
M
Mark Haines
提交者:
GitHub
9月 21, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Pass a context when downloading remote media (#251)
上级
fef290c4
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
65 addition
and
20 deletion
+65
-20
src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go
...matrix-org/dendrite/cmd/dendrite-media-api-server/main.go
+4
-1
src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go
.../matrix-org/dendrite/cmd/dendrite-monolith-server/main.go
+1
-1
src/github.com/matrix-org/dendrite/mediaapi/routing/routing.go
...ithub.com/matrix-org/dendrite/mediaapi/routing/routing.go
+28
-5
src/github.com/matrix-org/dendrite/mediaapi/writers/download.go
...thub.com/matrix-org/dendrite/mediaapi/writers/download.go
+23
-10
vendor/manifest
vendor/manifest
+1
-1
vendor/src/github.com/matrix-org/gomatrixserverlib/client.go
vendor/src/github.com/matrix-org/gomatrixserverlib/client.go
+8
-2
未找到文件。
src/github.com/matrix-org/dendrite/cmd/dendrite-media-api-server/main.go
浏览文件 @
ce019738
...
...
@@ -24,6 +24,7 @@ import (
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dendrite/mediaapi/routing"
"github.com/matrix-org/dendrite/mediaapi/storage"
"github.com/matrix-org/gomatrixserverlib"
log
"github.com/Sirupsen/logrus"
)
...
...
@@ -51,10 +52,12 @@ func main() {
log
.
WithError
(
err
)
.
Panic
(
"Failed to open database"
)
}
client
:=
gomatrixserverlib
.
NewClient
()
log
.
Info
(
"Starting media API server on "
,
cfg
.
Listen
.
MediaAPI
)
api
:=
mux
.
NewRouter
()
routing
.
Setup
(
api
,
cfg
,
db
)
routing
.
Setup
(
api
,
cfg
,
db
,
client
)
common
.
SetupHTTPAPI
(
http
.
DefaultServeMux
,
api
)
log
.
Fatal
(
http
.
ListenAndServe
(
string
(
cfg
.
Listen
.
MediaAPI
),
nil
))
...
...
src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go
浏览文件 @
ce019738
...
...
@@ -325,7 +325,7 @@ func (m *monolith) setupAPIs() {
)
mediaapi_routing
.
Setup
(
m
.
api
,
m
.
cfg
,
m
.
mediaAPIDB
,
m
.
api
,
m
.
cfg
,
m
.
mediaAPIDB
,
&
m
.
federation
.
Client
,
)
syncapi_routing
.
Setup
(
m
.
api
,
syncapi_sync
.
NewRequestPool
(
...
...
src/github.com/matrix-org/dendrite/mediaapi/routing/routing.go
浏览文件 @
ce019738
...
...
@@ -31,7 +31,12 @@ import (
const
pathPrefixR0
=
"/_matrix/media/v1"
// Setup registers the media API HTTP handlers
func
Setup
(
apiMux
*
mux
.
Router
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
)
{
func
Setup
(
apiMux
*
mux
.
Router
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
client
*
gomatrixserverlib
.
Client
,
)
{
r0mux
:=
apiMux
.
PathPrefix
(
pathPrefixR0
)
.
Subrouter
()
activeThumbnailGeneration
:=
&
types
.
ActiveThumbnailGeneration
{
...
...
@@ -47,14 +52,21 @@ func Setup(apiMux *mux.Router, cfg *config.Dendrite, db *storage.Database) {
MXCToResult
:
map
[
string
]
*
types
.
RemoteRequestResult
{},
}
r0mux
.
Handle
(
"/download/{serverName}/{mediaId}"
,
makeDownloadAPI
(
"download"
,
cfg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
),
makeDownloadAPI
(
"download"
,
cfg
,
db
,
client
,
activeRemoteRequests
,
activeThumbnailGeneration
),
)
.
Methods
(
"GET"
)
r0mux
.
Handle
(
"/thumbnail/{serverName}/{mediaId}"
,
makeDownloadAPI
(
"thumbnail"
,
cfg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
),
makeDownloadAPI
(
"thumbnail"
,
cfg
,
db
,
client
,
activeRemoteRequests
,
activeThumbnailGeneration
),
)
.
Methods
(
"GET"
)
}
func
makeDownloadAPI
(
name
string
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
,
activeThumbnailGeneration
*
types
.
ActiveThumbnailGeneration
)
http
.
HandlerFunc
{
func
makeDownloadAPI
(
name
string
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
client
*
gomatrixserverlib
.
Client
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
,
activeThumbnailGeneration
*
types
.
ActiveThumbnailGeneration
,
)
http
.
HandlerFunc
{
return
prometheus
.
InstrumentHandler
(
name
,
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
req
=
util
.
RequestWithLogging
(
req
)
...
...
@@ -64,6 +76,17 @@ func makeDownloadAPI(name string, cfg *config.Dendrite, db *storage.Database, ac
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
vars
:=
mux
.
Vars
(
req
)
writers
.
Download
(
w
,
req
,
gomatrixserverlib
.
ServerName
(
vars
[
"serverName"
]),
types
.
MediaID
(
vars
[
"mediaId"
]),
cfg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
,
name
==
"thumbnail"
)
writers
.
Download
(
w
,
req
,
gomatrixserverlib
.
ServerName
(
vars
[
"serverName"
]),
types
.
MediaID
(
vars
[
"mediaId"
]),
cfg
,
db
,
client
,
activeRemoteRequests
,
activeThumbnailGeneration
,
name
==
"thumbnail"
,
)
}))
}
src/github.com/matrix-org/dendrite/mediaapi/writers/download.go
浏览文件 @
ce019738
...
...
@@ -68,6 +68,7 @@ func Download(
mediaID
types
.
MediaID
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
client
*
gomatrixserverlib
.
Client
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
,
activeThumbnailGeneration
*
types
.
ActiveThumbnailGeneration
,
isThumbnailRequest
bool
,
...
...
@@ -120,7 +121,8 @@ func Download(
}
metadata
,
err
:=
dReq
.
doDownload
(
req
.
Context
(),
w
,
cfg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
,
req
.
Context
(),
w
,
cfg
,
db
,
client
,
activeRemoteRequests
,
activeThumbnailGeneration
,
)
if
err
!=
nil
{
// TODO: Handle the fact we might have started writing the response
...
...
@@ -199,6 +201,7 @@ func (r *downloadRequest) doDownload(
w
http
.
ResponseWriter
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
client
*
gomatrixserverlib
.
Client
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
,
activeThumbnailGeneration
*
types
.
ActiveThumbnailGeneration
,
)
(
*
types
.
MediaMetadata
,
error
)
{
...
...
@@ -216,7 +219,7 @@ func (r *downloadRequest) doDownload(
}
// If we do not have a record and the origin is remote, we need to fetch it and respond with that file
resErr
:=
r
.
getRemoteFile
(
ctx
,
cfg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
,
ctx
,
c
lient
,
c
fg
,
db
,
activeRemoteRequests
,
activeThumbnailGeneration
,
)
if
resErr
!=
nil
{
return
nil
,
resErr
...
...
@@ -442,6 +445,7 @@ func (r *downloadRequest) generateThumbnail(
// Note: The named errorResponse return variable is used in a deferred broadcast of the metadata and error response to waiting goroutines.
func
(
r
*
downloadRequest
)
getRemoteFile
(
ctx
context
.
Context
,
client
*
gomatrixserverlib
.
Client
,
cfg
*
config
.
Dendrite
,
db
*
storage
.
Database
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
,
...
...
@@ -477,7 +481,8 @@ func (r *downloadRequest) getRemoteFile(
if
mediaMetadata
==
nil
{
// If we do not have a record, we need to fetch the remote file first and then respond from the local file
err
:=
r
.
fetchRemoteFileAndStoreMetadata
(
ctx
,
cfg
.
Media
.
AbsBasePath
,
*
cfg
.
Media
.
MaxFileSizeBytes
,
db
,
ctx
,
client
,
cfg
.
Media
.
AbsBasePath
,
*
cfg
.
Media
.
MaxFileSizeBytes
,
db
,
cfg
.
Media
.
ThumbnailSizes
,
activeThumbnailGeneration
,
cfg
.
Media
.
MaxThumbnailGenerators
,
)
...
...
@@ -541,6 +546,7 @@ func (r *downloadRequest) broadcastMediaMetadata(activeRemoteRequests *types.Act
// fetchRemoteFileAndStoreMetadata fetches the file from the remote server and stores its metadata in the database
func
(
r
*
downloadRequest
)
fetchRemoteFileAndStoreMetadata
(
ctx
context
.
Context
,
client
*
gomatrixserverlib
.
Client
,
absBasePath
config
.
Path
,
maxFileSizeBytes
config
.
FileSizeBytes
,
db
*
storage
.
Database
,
...
...
@@ -548,7 +554,9 @@ func (r *downloadRequest) fetchRemoteFileAndStoreMetadata(
activeThumbnailGeneration
*
types
.
ActiveThumbnailGeneration
,
maxThumbnailGenerators
int
,
)
error
{
finalPath
,
duplicate
,
err
:=
r
.
fetchRemoteFile
(
absBasePath
,
maxFileSizeBytes
)
finalPath
,
duplicate
,
err
:=
r
.
fetchRemoteFile
(
ctx
,
client
,
absBasePath
,
maxFileSizeBytes
,
)
if
err
!=
nil
{
return
err
}
...
...
@@ -597,11 +605,16 @@ func (r *downloadRequest) fetchRemoteFileAndStoreMetadata(
return
nil
}
func
(
r
*
downloadRequest
)
fetchRemoteFile
(
absBasePath
config
.
Path
,
maxFileSizeBytes
config
.
FileSizeBytes
)
(
types
.
Path
,
bool
,
error
)
{
func
(
r
*
downloadRequest
)
fetchRemoteFile
(
ctx
context
.
Context
,
client
*
gomatrixserverlib
.
Client
,
absBasePath
config
.
Path
,
maxFileSizeBytes
config
.
FileSizeBytes
,
)
(
types
.
Path
,
bool
,
error
)
{
r
.
Logger
.
Info
(
"Fetching remote file"
)
// create request for remote file
resp
,
err
:=
r
.
createRemoteRequest
()
resp
,
err
:=
r
.
createRemoteRequest
(
ctx
,
client
)
if
err
!=
nil
{
return
""
,
false
,
err
}
...
...
@@ -664,10 +677,10 @@ func (r *downloadRequest) fetchRemoteFile(absBasePath config.Path, maxFileSizeBy
return
types
.
Path
(
finalPath
),
duplicate
,
nil
}
func
(
r
*
downloadRequest
)
createRemoteRequest
(
)
(
*
http
.
Response
,
error
)
{
matrixClient
:=
gomatrixserverlib
.
NewClient
()
resp
,
err
:=
matrixClient
.
CreateMediaDownloadRequest
(
r
.
MediaMetadata
.
Origin
,
string
(
r
.
MediaMetadata
.
MediaID
))
func
(
r
*
downloadRequest
)
createRemoteRequest
(
ctx
context
.
Context
,
matrixClient
*
gomatrixserverlib
.
Client
,
)
(
*
http
.
Response
,
error
)
{
resp
,
err
:=
matrixClient
.
CreateMediaDownloadRequest
(
ctx
,
r
.
MediaMetadata
.
Origin
,
string
(
r
.
MediaMetadata
.
MediaID
))
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"file with media ID %q could not be downloaded from %q"
,
r
.
MediaMetadata
.
MediaID
,
r
.
MediaMetadata
.
Origin
)
}
...
...
vendor/manifest
浏览文件 @
ce019738
...
...
@@ -116,7 +116,7 @@
{
"importpath": "github.com/matrix-org/gomatrixserverlib",
"repository": "https://github.com/matrix-org/gomatrixserverlib",
"revision": "
ec5a0d21b03ed4d3bd955ecc9f7a69936f64391
e",
"revision": "
40b35e1c997fc7e35342aeb39187ff6bf3e10b2
e",
"branch": "master"
},
{
...
...
vendor/src/github.com/matrix-org/gomatrixserverlib/client.go
浏览文件 @
ce019738
...
...
@@ -236,9 +236,15 @@ func (fc *Client) LookupServerKeys( // nolint: gocyclo
}
// CreateMediaDownloadRequest creates a request for media on a homeserver and returns the http.Response or an error
func
(
fc
*
Client
)
CreateMediaDownloadRequest
(
matrixServer
ServerName
,
mediaID
string
)
(
*
http
.
Response
,
error
)
{
func
(
fc
*
Client
)
CreateMediaDownloadRequest
(
ctx
context
.
Context
,
matrixServer
ServerName
,
mediaID
string
,
)
(
*
http
.
Response
,
error
)
{
requestURL
:=
"matrix://"
+
string
(
matrixServer
)
+
"/_matrix/media/v1/download/"
+
string
(
matrixServer
)
+
"/"
+
mediaID
resp
,
err
:=
fc
.
client
.
Get
(
requestURL
)
req
,
err
:=
http
.
NewRequest
(
"GET"
,
requestURL
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
fc
.
client
.
Do
(
req
.
WithContext
(
ctx
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录