-
Notifications
You must be signed in to change notification settings - Fork 74
/
thumbnail.go
168 lines (150 loc) · 5.22 KB
/
thumbnail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package r0
import (
"errors"
"net/http"
"strconv"
"github.com/getsentry/sentry-go"
"github.com/turt2live/matrix-media-repo/api/_apimeta"
"github.com/turt2live/matrix-media-repo/api/_responses"
"github.com/turt2live/matrix-media-repo/api/_routers"
"github.com/turt2live/matrix-media-repo/database"
"github.com/turt2live/matrix-media-repo/pipelines/pipeline_download"
"github.com/turt2live/matrix-media-repo/pipelines/pipeline_thumbnail"
"github.com/turt2live/matrix-media-repo/util"
"github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/common"
"github.com/turt2live/matrix-media-repo/common/rcontext"
)
func ThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, user _apimeta.UserInfo) interface{} {
server := _routers.GetParam("server", r)
mediaId := _routers.GetParam("mediaId", r)
allowRemote := r.URL.Query().Get("allow_remote")
timeoutMs := r.URL.Query().Get("timeout_ms")
if !_routers.ServerNameRegex.MatchString(server) {
return _responses.BadRequest("invalid server ID")
}
downloadRemote := true
if allowRemote != "" {
parsedFlag, err := strconv.ParseBool(allowRemote)
if err != nil {
return _responses.BadRequest("allow_remote flag does not appear to be a boolean")
}
downloadRemote = parsedFlag
}
blockFor, err := util.CalcBlockForDuration(timeoutMs)
if err != nil {
return _responses.BadRequest("timeout_ms does not appear to be an integer")
}
rctx = rctx.LogWithFields(logrus.Fields{
"mediaId": mediaId,
"server": server,
"allowRemote": downloadRemote,
})
if !util.IsGlobalAdmin(user.UserId) && util.IsHostIgnored(server) {
rctx.Log.Warn("Request blocked due to domain being ignored.")
return _responses.MediaBlocked()
}
widthStr := r.URL.Query().Get("width")
heightStr := r.URL.Query().Get("height")
method := r.URL.Query().Get("method")
animatedStr := r.URL.Query().Get("animated")
if animatedStr == "" {
animatedStr = r.URL.Query().Get("org.matrix.msc2705.animated")
}
if widthStr == "" || heightStr == "" {
return _responses.BadRequest("Width and height are required")
}
width := 0
height := 0
animated := rctx.Config.Thumbnails.AllowAnimated && rctx.Config.Thumbnails.DefaultAnimated
if widthStr != "" {
parsedWidth, err := strconv.Atoi(widthStr)
if err != nil {
return _responses.BadRequest("Width does not appear to be an integer")
}
width = parsedWidth
}
if heightStr != "" {
parsedHeight, err := strconv.Atoi(heightStr)
if err != nil {
return _responses.BadRequest("Height does not appear to be an integer")
}
height = parsedHeight
}
if animatedStr != "" {
parsedFlag, err := strconv.ParseBool(animatedStr)
if err != nil {
return _responses.BadRequest("Animated flag does not appear to be a boolean")
}
animated = parsedFlag
}
if method == "" {
method = "scale"
}
rctx = rctx.LogWithFields(logrus.Fields{
"requestedWidth": width,
"requestedHeight": height,
"requestedMethod": method,
"requestedAnimated": animated,
})
if width <= 0 || height <= 0 {
return _responses.BadRequest("Width and height must be greater than zero")
}
thumbnail, stream, err := pipeline_thumbnail.Execute(rctx, server, mediaId, pipeline_thumbnail.ThumbnailOpts{
DownloadOpts: pipeline_download.DownloadOpts{
FetchRemoteIfNeeded: downloadRemote,
BlockForReadUntil: blockFor,
RecordOnly: false, // overridden
},
Width: width,
Height: height,
Method: method,
Animated: animated,
})
if err != nil {
if errors.Is(err, common.ErrMediaNotFound) {
return _responses.NotFoundError()
} else if errors.Is(err, common.ErrMediaTooLarge) {
return _responses.RequestTooLarge()
} else if errors.Is(err, common.ErrMediaQuarantined) {
rctx.Log.Debug("Quarantined media accessed. Has stream? ", stream != nil)
if stream != nil {
return _responses.MakeQuarantinedImageResponse(stream)
} else {
return _responses.NotFoundError() // We lie for security
}
} else if errors.Is(err, common.ErrMediaNotYetUploaded) {
return _responses.NotYetUploaded()
} else if errors.Is(err, common.ErrMediaDimensionsTooSmall) {
if stream == nil {
return _responses.NotFoundError() // something went wrong so just 404 the thumbnail
}
// We have a stream, and an error about image size, so we know there should be a media record
mediaDb := database.GetInstance().Media.Prepare(rctx)
record, err := mediaDb.GetById(server, mediaId)
if err != nil {
rctx.Log.Error("Unexpected error locating media record: ", err)
sentry.CaptureException(err)
return _responses.InternalServerError("Unexpected Error")
} else {
return &_responses.DownloadResponse{
ContentType: record.ContentType,
Filename: record.UploadName,
SizeBytes: record.SizeBytes,
Data: stream,
TargetDisposition: "infer",
}
}
}
rctx.Log.Error("Unexpected error locating media: ", err)
sentry.CaptureException(err)
return _responses.InternalServerError("Unexpected Error")
}
return &_responses.DownloadResponse{
ContentType: thumbnail.ContentType,
Filename: "thumbnail" + util.ExtensionForContentType(thumbnail.ContentType),
SizeBytes: thumbnail.SizeBytes,
Data: stream,
TargetDisposition: "infer",
}
}