/
target.go
64 lines (56 loc) · 1.04 KB
/
target.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
package sdobjectstore
import (
"fmt"
"path/filepath"
)
type Target struct {
Typ TargetType
Prefix string
InternalPrefix string
Path string
}
type TargetType int
const (
DiscardTarget = TargetType(1)
FileTarget = TargetType(2)
HttpTarget = TargetType(3)
)
func (t *Target) Url() string {
return t.url(false, false)
}
func (t *Target) HttpsUrl() string {
return t.url(true, false)
}
func (t *Target) InternalUrl() string {
return t.url(false, true)
}
func (t *Target) url(https bool, internal bool) string {
switch t.Typ {
case DiscardTarget:
return ""
case FileTarget:
if https {
return ""
}
absFn, err := filepath.Abs(filepath.Join(t.Prefix, t.Path))
if err != nil {
return ""
}
return "file://" + absFn
case HttpTarget:
var protocol, host string
if https {
protocol = "https"
} else {
protocol = "http"
}
if internal {
host = t.InternalPrefix
} else {
host = t.Prefix
}
return fmt.Sprintf("%s://%s/%s", protocol, host, t.Path)
default:
return ""
}
}