-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhoi.go
147 lines (125 loc) · 2.81 KB
/
hoi.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package hoi
import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
)
type Hoi struct {
publicDir string
config Config
}
func NewHoi() *Hoi {
// create public dir
publicDir := publicDir()
os.MkdirAll(publicDir, 0755)
return &Hoi{
publicDir: publicDir,
config: Load(configPath()),
}
}
func (h Hoi) TestFile(file string) (string, error) {
var (
path string
err error
)
// absolutize
path, err = filepath.Abs(file)
if err != nil {
return path, err
}
// check existence
_, err = os.Stat(path)
if err != nil {
return path, err
}
return path, nil
}
func (h Hoi) MakePublic(file string) string {
linked := h.makePublic(file)
return linked
}
func (h Hoi) makePublic(src string) string {
// create random directory
random := h.createRandomDir()
// make public by symblic link
file := filepath.Base(src)
os.Symlink(src, filepath.Join(h.publicDir, random, file))
return filepath.Join(random, file)
}
func (h Hoi) MakeMessage(msgs []string) string {
message := h.makeMessage(msgs)
return message
}
func (h Hoi) makeMessage(msgs []string) string {
// create random directory
random := h.createRandomDir()
// make public by message file
file, err := os.Create(filepath.Join(h.publicDir, random, "message.txt"))
if err != nil {
return ""
}
defer file.Close()
file.WriteString(strings.Join(msgs, " "))
return filepath.Join(random, "message.txt")
}
func (h Hoi) createRandomDir() string {
random := randomString(32)
os.Mkdir(filepath.Join(h.publicDir, random), 0755)
return random
}
func (h Hoi) Server() *HoiServer {
return &HoiServer{
DocumentRoot: h.publicDir,
Port: h.config.Port,
}
}
func (h Hoi) Clear() {
contents, _ := ioutil.ReadDir(h.publicDir)
for _, c := range contents {
os.RemoveAll(filepath.Join(h.publicDir, c.Name()))
}
}
func (h Hoi) ToUrl(path string) string {
server := h.Server()
return fmt.Sprintf("%s/%s", server.Url(), path)
}
func (h Hoi) Notify(to, message string) string {
n := NewNotifier(h.config.Notification)
if n == nil {
return ""
}
if err := n.Notify(to, message); err != nil {
return err.Error()
}
return fmt.Sprintf("Message sent successfully to %s\n", to)
}
func publicDir() string {
return filepath.Join(homeDir(), ".hoi", "temp_public")
}
func configPath() string {
return filepath.Join(homeDir(), ".hoi", "conf.json")
}
func homeDir() string {
usr, err := user.Current()
var homeDir string
if err == nil {
homeDir = usr.HomeDir
} else {
// Maybe it's cross compilation without cgo support. (darwin, unix)
homeDir = os.Getenv("HOME")
}
return homeDir
}
func randomString(length int) string {
alphanum := "0123456789abcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, length)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}