This repository was archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmxc.go
97 lines (80 loc) · 2.64 KB
/
mxc.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
// Copyright 2017 Michael Telatynski <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mxclient
import (
"net/url"
"path"
"regexp"
"strconv"
)
// mxcRegex allows splitting an mxc into a serverName and mediaId
// FindStringSubmatch of which results in [_, serverName, mediaId] if valid
// and [] if invalid mxc is provided.
// Examples:
// "mxc://foo/bar" => ["mxc://foo/bar", "foo", "bar"]
// "mxc://bar/foo#auto" => ["mxc://bar/foo#auto", "bar", "foo"]
// "invalidMxc://whatever" => [] (Invalid MXC Caught)
var mxcRegex = regexp.MustCompile(`mxc://(.+?)/(.+?)(?:#.+)?$`)
type MXCURL struct {
string
homeserverURL string
}
// NewMXCURL constructs an MXCURL based on the mxc and the baseUrl to any Homeserver which can access the MXCURL.
func NewMXCURL(url string, baseUrl string) *MXCURL {
return &MXCURL{url, baseUrl}
}
// IsValid returns a boolean of whether or not this MXCURL appears valid.
func (m *MXCURL) IsValid() bool {
ok, _, _ := m.split()
return ok
}
func (m *MXCURL) split() (ok bool, serverName string, mediaId string) {
mxc := m.string
matches := mxcRegex.FindStringSubmatch(mxc)
ok = true
if len(matches) != 3 {
return false, "", ""
}
serverName = matches[1]
mediaId = matches[2]
return
}
func (m *MXCURL) mapMxcUrl(kind string) *url.URL {
ok, serverName, mediaId := m.split()
if !ok {
return nil
}
hsURL, _ := url.Parse(m.homeserverURL)
parts := []string{hsURL.Path}
parts = append(parts, "_matrix", "media", "r0", kind, serverName, mediaId)
hsURL.Path = path.Join(parts...)
return hsURL
}
// ToThumbUrl returns a http/s URL string representation of the MXCURL at the width&height specified.
func (m *MXCURL) ToThumbURL(width, height int, method string) string {
mediaUrl := m.mapMxcUrl("thumbnail")
if mediaUrl == nil {
return ""
}
q := mediaUrl.Query()
q.Set("width", strconv.Itoa(width))
q.Set("height", strconv.Itoa(height))
q.Set("method", method)
mediaUrl.RawQuery = q.Encode()
return mediaUrl.String()
}
// ToThumbUrl returns a http/s URL string representation of the original file uploaded at the MXCURL.s
func (m *MXCURL) ToURL() string {
return m.mapMxcUrl("download").String()
}