/
calculated_previewer.go
65 lines (54 loc) · 2.21 KB
/
calculated_previewer.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
package previewers
import (
bytes2 "bytes"
"github.com/prometheus/client_golang/prometheus"
"github.com/ryanuber/go-glob"
"github.com/turt2live/matrix-media-repo/common"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/controllers/preview_controller/preview_types"
"github.com/turt2live/matrix-media-repo/metrics"
"github.com/turt2live/matrix-media-repo/util"
)
func GenerateCalculatedPreview(urlPayload *preview_types.UrlPayload, languageHeader string, ctx rcontext.RequestContext) (preview_types.PreviewResult, error) {
bytes, filename, contentType, contentLength, err := downloadRawContent(urlPayload, ctx.Config.UrlPreviews.FilePreviewTypes, languageHeader, ctx)
if err != nil {
ctx.Log.Error("Error downloading content: " + err.Error())
// Make sure the unsupported error gets passed through
if err == preview_types.ErrPreviewUnsupported {
return preview_types.PreviewResult{}, preview_types.ErrPreviewUnsupported
}
// We'll consider it not found for the sake of processing
return preview_types.PreviewResult{}, common.ErrMediaNotFound
}
stream := util.BufferToStream(bytes2.NewBuffer(bytes))
img := &preview_types.PreviewImage{
Data: stream,
ContentType: contentType,
Filename: filename,
ContentLength: int64(len(bytes)),
ContentLengthHeader: contentLength,
}
description := ""
filename = urlPayload.ParsedUrl.String()
if img != nil && img.Filename != "" {
filename = img.Filename
} else {
description = urlPayload.ParsedUrl.String()
}
// Clear the description so we don't duplicate the URL
if description == filename {
description = ""
}
result := &preview_types.PreviewResult{
Type: "", // intentionally empty
Url: urlPayload.ParsedUrl.String(),
Title: summarize(filename, ctx.Config.UrlPreviews.NumTitleWords, ctx.Config.UrlPreviews.MaxTitleLength),
Description: summarize(description, ctx.Config.UrlPreviews.NumWords, ctx.Config.UrlPreviews.MaxLength),
SiteName: "", // intentionally empty
}
if glob.Glob("image/*", img.ContentType) {
result.Image = img
}
metrics.UrlPreviewsGenerated.With(prometheus.Labels{"type": "calculated"}).Inc()
return *result, nil
}