-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (93 loc) · 1.98 KB
/
main.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
package main
import (
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/skip2/go-qrcode"
)
const size = 300
func getIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
os.Exit(1)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && ipnet.IP.To4()[0] == 192 {
return ipnet.IP.String()
}
}
}
return ""
}
func getIcon() *walk.Icon {
q, _ := qrcode.New("QR Serve", qrcode.Medium)
ic, _ := walk.NewIconFromImage(q.Image(64))
return ic
}
func showError(text string) {
MainWindow{
Title: "QR Serve",
Icon: getIcon(),
Size: Size{size + 100, 200},
Layout: VBox{},
Children: []Widget{
TextLabel{
Text: text,
TextAlignment: AlignHCenterVCenter,
},
},
}.Run()
}
func main() {
wg := sync.WaitGroup{}
defer wg.Wait()
if len(os.Args) < 2 {
showError("File not specified. Please open your file with QR Serve.")
return
}
filePath := os.Args[1]
fileName := filepath.Base(filePath)
ip := getIP()
if ip == "" {
showError("Could not find LAN address")
return
}
ln, err := net.Listen("tcp", ip+":0")
if err != nil {
showError(err.Error())
return
}
defer ln.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
wg.Add(1)
defer wg.Done()
http.ServeFile(w, r, filePath)
})
go http.Serve(ln, nil)
urlFileName := url.QueryEscape(fileName)
if len(urlFileName) > 40 {
urlFileName = urlFileName[0:40]
}
q, _ := qrcode.New("http://"+ln.Addr().String()+"/"+urlFileName, qrcode.RecoveryLevel(0))
bm, _ := walk.NewBitmapFromImage(q.Image(size))
MainWindow{
Title: fileName,
Icon: getIcon(),
Size: Size{size + 100, size + 100},
Layout: VBox{},
Children: []Widget{
ImageView{
MinSize: Size{size, size},
MaxSize: Size{size, size},
Image: bm,
},
},
}.Run()
}