-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
61 lines (49 loc) · 1.23 KB
/
file.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
package file
import (
"encoding/base64"
"errors"
"fmt"
"strings"
)
var ErrStorageNotDefined = errors.New("storage not defined")
var ErrNotFound = errors.New("file not found")
type ID string
type StorageType string
const (
StorageTypeInMemory StorageType = "in_memory"
StorageTypePostgres StorageType = "postgres"
StorageTypeTelegramBot StorageType = "telegram_bot"
StorageTypeRemoteLink StorageType = "remote_link"
)
const linkBase64Delimiter = ":"
type Link struct {
StorageType StorageType
ID ID
}
func NewLinkFromBase64(base64Str string) (Link, error) {
decoded, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
return Link{}, err
}
var storageType StorageType
var id ID
decodedParts := strings.Split(string(decoded), linkBase64Delimiter)
if len(decodedParts) != 2 {
return Link{}, errors.New("incorrect base64 string")
}
storageType = StorageType(decodedParts[0])
id = ID(decodedParts[1])
return Link{
StorageType: storageType,
ID: id,
}, nil
}
func (l *Link) Base64() string {
str := fmt.Sprintf("%s"+linkBase64Delimiter+"%s", l.StorageType, l.ID)
return base64.StdEncoding.EncodeToString([]byte(str))
}
type ImageSize struct {
Width uint
Height uint
Link Link
}