| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package id |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"encoding/json" |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"strings" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
InvalidContentURI = errors.New("invalid Matrix content URI") |
| 19 |
InputNotJSONString = errors.New("input doesn't look like a JSON string") |
| 20 |
) |
| 21 |
|
| 22 |
// ContentURIString is a string that's expected to be a Matrix content URI. |
| 23 |
// It's useful for delaying the parsing of the content URI to move errors from the event content |
| 24 |
// JSON parsing step to a later step where more appropriate errors can be produced. |
| 25 |
type ContentURIString string |
| 26 |
|
| 27 |
func (uriString ContentURIString) Parse() (ContentURI, error) { |
| 28 |
return ParseContentURI(string(uriString)) |
| 29 |
} |
| 30 |
|
| 31 |
func (uriString ContentURIString) ParseOrIgnore() ContentURI { |
| 32 |
parsed, _ := ParseContentURI(string(uriString)) |
| 33 |
return parsed |
| 34 |
} |
| 35 |
|
| 36 |
// ContentURI represents a Matrix content URI. |
| 37 |
// https://spec.matrix.org/v1.2/client-server-api/#matrix-content-mxc-uris |
| 38 |
type ContentURI struct { |
| 39 |
Homeserver string |
| 40 |
FileID string |
| 41 |
} |
| 42 |
|
| 43 |
func MustParseContentURI(uri string) ContentURI { |
| 44 |
parsed, err := ParseContentURI(uri) |
| 45 |
if err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
return parsed |
| 49 |
} |
| 50 |
|
| 51 |
// ParseContentURI parses a Matrix content URI. |
| 52 |
func ParseContentURI(uri string) (parsed ContentURI, err error) { |
| 53 |
if len(uri) == 0 { |
| 54 |
return |
| 55 |
} else if !strings.HasPrefix(uri, "mxc://") { |
| 56 |
err = InvalidContentURI |
| 57 |
} else if index := strings.IndexRune(uri[6:], '/'); index == -1 || index == len(uri)-7 { |
| 58 |
err = InvalidContentURI |
| 59 |
} else { |
| 60 |
parsed.Homeserver = uri[6 : 6+index] |
| 61 |
parsed.FileID = uri[6+index+1:] |
| 62 |
} |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
var mxcBytes = []byte("mxc://") |
| 67 |
|
| 68 |
func ParseContentURIBytes(uri []byte) (parsed ContentURI, err error) { |
| 69 |
if len(uri) == 0 { |
| 70 |
return |
| 71 |
} else if !bytes.HasPrefix(uri, mxcBytes) { |
| 72 |
err = InvalidContentURI |
| 73 |
} else if index := bytes.IndexRune(uri[6:], '/'); index == -1 || index == len(uri)-7 { |
| 74 |
err = InvalidContentURI |
| 75 |
} else { |
| 76 |
parsed.Homeserver = string(uri[6 : 6+index]) |
| 77 |
parsed.FileID = string(uri[6+index+1:]) |
| 78 |
} |
| 79 |
return |
| 80 |
} |
| 81 |
|
| 82 |
func (uri *ContentURI) UnmarshalJSON(raw []byte) (err error) { |
| 83 |
if string(raw) == "null" { |
| 84 |
*uri = ContentURI{} |
| 85 |
return nil |
| 86 |
} else if len(raw) < 2 || raw[0] != '"' || raw[len(raw)-1] != '"' { |
| 87 |
return InputNotJSONString |
| 88 |
} |
| 89 |
parsed, err := ParseContentURIBytes(raw[1 : len(raw)-1]) |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
*uri = parsed |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
func (uri *ContentURI) MarshalJSON() ([]byte, error) { |
| 98 |
if uri.IsEmpty() { |
| 99 |
return []byte("null"), nil |
| 100 |
} |
| 101 |
return json.Marshal(uri.String()) |
| 102 |
} |
| 103 |
|
| 104 |
func (uri *ContentURI) UnmarshalText(raw []byte) (err error) { |
| 105 |
parsed, err := ParseContentURIBytes(raw) |
| 106 |
if err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
*uri = parsed |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
func (uri ContentURI) MarshalText() ([]byte, error) { |
| 114 |
if uri.IsEmpty() { |
| 115 |
return []byte(""), nil |
| 116 |
} |
| 117 |
return []byte(uri.String()), nil |
| 118 |
} |
| 119 |
|
| 120 |
func (uri *ContentURI) String() string { |
| 121 |
if uri.IsEmpty() { |
| 122 |
return "" |
| 123 |
} |
| 124 |
return fmt.Sprintf("mxc://%s/%s", uri.Homeserver, uri.FileID) |
| 125 |
} |
| 126 |
|
| 127 |
func (uri *ContentURI) CUString() ContentURIString { |
| 128 |
return ContentURIString(uri.String()) |
| 129 |
} |
| 130 |
|
| 131 |
func (uri *ContentURI) IsEmpty() bool { |
| 132 |
return len(uri.Homeserver) == 0 || len(uri.FileID) == 0 |
| 133 |
} |