Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
a45f008c
ligase
项目概览
FinClip
/
ligase
通知
38
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,发现更多精彩内容 >>
提交
a45f008c
编写于
5月 31, 2017
作者:
R
Robert Swain
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
mediaapi/storage: Don't leak sql.ErrNoRows out of storage package
上级
a936ad50
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
18 addition
and
46 deletion
+18
-46
src/github.com/matrix-org/dendrite/mediaapi/storage/storage.go
...ithub.com/matrix-org/dendrite/mediaapi/storage/storage.go
+6
-2
src/github.com/matrix-org/dendrite/mediaapi/writers/download.go
...thub.com/matrix-org/dendrite/mediaapi/writers/download.go
+12
-44
未找到文件。
src/github.com/matrix-org/dendrite/mediaapi/storage/storage.go
浏览文件 @
a45f008c
...
@@ -50,7 +50,11 @@ func (d *Database) StoreMediaMetadata(mediaMetadata *types.MediaMetadata) error
...
@@ -50,7 +50,11 @@ func (d *Database) StoreMediaMetadata(mediaMetadata *types.MediaMetadata) error
// GetMediaMetadata returns metadata about media stored on this server.
// GetMediaMetadata returns metadata about media stored on this server.
// The media could have been uploaded to this server or fetched from another server and cached here.
// The media could have been uploaded to this server or fetched from another server and cached here.
// Returns
sql.ErrNoRows
if there is no metadata associated with this media.
// Returns
nil metadata
if there is no metadata associated with this media.
func
(
d
*
Database
)
GetMediaMetadata
(
mediaID
types
.
MediaID
,
mediaOrigin
gomatrixserverlib
.
ServerName
)
(
*
types
.
MediaMetadata
,
error
)
{
func
(
d
*
Database
)
GetMediaMetadata
(
mediaID
types
.
MediaID
,
mediaOrigin
gomatrixserverlib
.
ServerName
)
(
*
types
.
MediaMetadata
,
error
)
{
return
d
.
statements
.
selectMedia
(
mediaID
,
mediaOrigin
)
mediaMetadata
,
err
:=
d
.
statements
.
selectMedia
(
mediaID
,
mediaOrigin
)
if
err
!=
nil
&&
err
==
sql
.
ErrNoRows
{
return
nil
,
nil
}
return
mediaMetadata
,
err
}
}
src/github.com/matrix-org/dendrite/mediaapi/writers/download.go
浏览文件 @
a45f008c
...
@@ -15,7 +15,6 @@
...
@@ -15,7 +15,6 @@
package
writers
package
writers
import
(
import
(
"database/sql"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"io"
...
@@ -39,13 +38,6 @@ const mediaIDCharacters = "A-Za-z0-9_=-"
...
@@ -39,13 +38,6 @@ const mediaIDCharacters = "A-Za-z0-9_=-"
// Note: unfortunately regex.MustCompile() cannot be assigned to a const
// Note: unfortunately regex.MustCompile() cannot be assigned to a const
var
mediaIDRegex
=
regexp
.
MustCompile
(
"["
+
mediaIDCharacters
+
"]+"
)
var
mediaIDRegex
=
regexp
.
MustCompile
(
"["
+
mediaIDCharacters
+
"]+"
)
// Error types used by downloadRequest.getMediaMetadata
// FIXME: make into types
var
(
errDBQuery
=
fmt
.
Errorf
(
"error querying database for media"
)
errDBNotFound
=
fmt
.
Errorf
(
"media not found"
)
)
// downloadRequest metadata included in or derivable from an download request
// downloadRequest metadata included in or derivable from an download request
// https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-media-r0-download-servername-mediaid
// https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-media-r0-download-servername-mediaid
type
downloadRequest
struct
{
type
downloadRequest
struct
{
...
@@ -121,15 +113,17 @@ func (r *downloadRequest) Validate() *util.JSONResponse {
...
@@ -121,15 +113,17 @@ func (r *downloadRequest) Validate() *util.JSONResponse {
func
(
r
*
downloadRequest
)
doDownload
(
w
http
.
ResponseWriter
,
cfg
*
config
.
MediaAPI
,
db
*
storage
.
Database
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
)
*
util
.
JSONResponse
{
func
(
r
*
downloadRequest
)
doDownload
(
w
http
.
ResponseWriter
,
cfg
*
config
.
MediaAPI
,
db
*
storage
.
Database
,
activeRemoteRequests
*
types
.
ActiveRemoteRequests
)
*
util
.
JSONResponse
{
// check if we have a record of the media in our database
// check if we have a record of the media in our database
mediaMetadata
,
err
:=
r
.
getMediaMetadata
(
db
)
mediaMetadata
,
err
:=
db
.
GetMediaMetadata
(
r
.
MediaMetadata
.
MediaID
,
r
.
MediaMetadata
.
Origin
)
if
err
==
nil
{
if
err
!=
nil
{
// If we have a record, we can respond from the local file
r
.
Logger
.
WithError
(
err
)
.
Error
(
"Error querying the database."
)
r
.
MediaMetadata
=
mediaMetadata
return
&
util
.
JSONResponse
{
return
r
.
respondFromLocalFile
(
w
,
cfg
.
AbsBasePath
)
Code
:
500
,
}
else
if
err
==
errDBNotFound
{
JSON
:
jsonerror
.
InternalServerError
(),
}
}
if
mediaMetadata
==
nil
{
if
r
.
MediaMetadata
.
Origin
==
cfg
.
ServerName
{
if
r
.
MediaMetadata
.
Origin
==
cfg
.
ServerName
{
// If we do not have a record and the origin is local, the file is not found
// If we do not have a record and the origin is local, the file is not found
r
.
Logger
.
WithError
(
err
)
.
Warn
(
"Failed to look up file in database"
)
return
&
util
.
JSONResponse
{
return
&
util
.
JSONResponse
{
Code
:
404
,
Code
:
404
,
JSON
:
jsonerror
.
NotFound
(
fmt
.
Sprintf
(
"File with media ID %q does not exist"
,
r
.
MediaMetadata
.
MediaID
)),
JSON
:
jsonerror
.
NotFound
(
fmt
.
Sprintf
(
"File with media ID %q does not exist"
,
r
.
MediaMetadata
.
MediaID
)),
...
@@ -141,35 +135,9 @@ func (r *downloadRequest) doDownload(w http.ResponseWriter, cfg *config.MediaAPI
...
@@ -141,35 +135,9 @@ func (r *downloadRequest) doDownload(w http.ResponseWriter, cfg *config.MediaAPI
JSON
:
jsonerror
.
NotFound
(
fmt
.
Sprintf
(
"File with media ID %q does not exist"
,
r
.
MediaMetadata
.
MediaID
)),
JSON
:
jsonerror
.
NotFound
(
fmt
.
Sprintf
(
"File with media ID %q does not exist"
,
r
.
MediaMetadata
.
MediaID
)),
}
}
}
}
// Another error from the database
// If we have a record, we can respond from the local file
r
.
Logger
.
WithError
(
err
)
.
WithFields
(
log
.
Fields
{
r
.
MediaMetadata
=
mediaMetadata
"MediaID"
:
r
.
MediaMetadata
.
MediaID
,
return
r
.
respondFromLocalFile
(
w
,
cfg
.
AbsBasePath
)
"Origin"
:
r
.
MediaMetadata
.
Origin
,
})
.
Error
(
"Error querying the database."
)
return
&
util
.
JSONResponse
{
Code
:
500
,
JSON
:
jsonerror
.
InternalServerError
(),
}
}
// getMediaMetadata queries the database for media metadata
func
(
r
*
downloadRequest
)
getMediaMetadata
(
db
*
storage
.
Database
)
(
*
types
.
MediaMetadata
,
error
)
{
mediaMetadata
,
err
:=
db
.
GetMediaMetadata
(
r
.
MediaMetadata
.
MediaID
,
r
.
MediaMetadata
.
Origin
)
if
err
!=
nil
{
if
err
==
sql
.
ErrNoRows
{
r
.
Logger
.
WithFields
(
log
.
Fields
{
"Origin"
:
r
.
MediaMetadata
.
Origin
,
"MediaID"
:
r
.
MediaMetadata
.
MediaID
,
})
.
Info
(
"Media not found in database."
)
return
nil
,
errDBNotFound
}
r
.
Logger
.
WithError
(
err
)
.
WithFields
(
log
.
Fields
{
"Origin"
:
r
.
MediaMetadata
.
Origin
,
"MediaID"
:
r
.
MediaMetadata
.
MediaID
,
})
.
Error
(
"Error querying database for media."
)
return
nil
,
errDBQuery
}
return
mediaMetadata
,
nil
}
}
// respondFromLocalFile reads a file from local storage and writes it to the http.ResponseWriter
// respondFromLocalFile reads a file from local storage and writes it to the http.ResponseWriter
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录