-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
179 lines (161 loc) · 3.97 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"syscall"
"github.com/ncruces/rethinkraw/internal/config"
"github.com/ncruces/rethinkraw/pkg/chrome"
"github.com/ncruces/rethinkraw/pkg/dngconv"
"github.com/ncruces/rethinkraw/pkg/optls"
"github.com/ncruces/rethinkraw/pkg/osutil"
"github.com/ncruces/rethinkraw/pkg/wine"
"github.com/ncruces/zenity"
)
var shutdown = make(chan os.Signal, 1)
var (
serverHost string
serverPort string
serverAuth string
serverPrefix string
serverConfig tls.Config
)
func init() {
signal.Notify(shutdown, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
osutil.CreateConsole()
osutil.CleanupArgs()
log.SetOutput(os.Stderr)
}
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
if err := config.SetupPaths(); err != nil {
return err
}
port := flag.Int("port", 39639, "the port on which the server listens for connections")
pass := flag.String("password", "$PASSWORD", "the password used to authenticate to the server (required)")
cert := flag.String("certfile", "", "the PEM encoded certificate `file`")
key := flag.String("keyfile", "", "the PEM encoded private key `file`")
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "usage: %s [OPTION]... DIRECTORY\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
const unspecified = "\x00"
*pass = unspecified
flag.Parse()
if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
wine.Startup()
}
serverPort = ":" + strconv.Itoa(*port)
var url url.URL
if config.ServerMode {
if flag.NArg() != 1 {
flag.Usage()
os.Exit(2)
}
if fi, err := os.Stat(flag.Arg(0)); err != nil {
return err
} else if abs, err := filepath.Abs(flag.Arg(0)); err != nil {
return err
} else if fi.IsDir() {
serverPrefix = abs
} else {
flag.Usage()
os.Exit(2)
}
if serverAuth = *pass; serverAuth == unspecified {
if env := os.Getenv("PASSWORD"); env != "" {
serverAuth = env
} else {
flag.Usage()
os.Exit(2)
}
}
if *cert != "" {
var err error
serverConfig.Certificates = make([]tls.Certificate, 1)
serverConfig.Certificates[0], err = tls.LoadX509KeyPair(*cert, *key)
if err != nil {
return err
}
}
serverConfig.NextProtos = []string{"h2"}
} else {
serverHost = "localhost"
url.Scheme = "http"
url.Host = serverHost + serverPort
if flag.NArg() > 0 {
if fi, err := os.Stat(flag.Arg(0)); err != nil {
return err
} else if abs, err := filepath.Abs(flag.Arg(0)); err != nil {
return err
} else if flag.NArg() > 1 {
url.Path = "/batch/" + toBatchPath(flag.Args()...)
} else if fi.IsDir() {
url.Path = "/gallery/" + toURLPath(abs, "")
} else {
url.Path = "/photo/" + toURLPath(abs, "")
}
}
}
if !dngconv.IsInstalled() {
if config.ServerMode {
log.Fatal("Please download and install Adobe DNG Converter.")
}
url.Path = "/dngconv.html"
}
if ln, err := optls.Listen("tcp", serverHost+serverPort, &serverConfig); err == nil {
http := setupHTTP()
exif, err := setupExifTool()
if err != nil {
return err
}
defer func() {
http.Shutdown(context.Background())
exif.Shutdown()
wine.Shutdown()
os.RemoveAll(config.TempDir)
}()
go http.Serve(ln)
} else if config.ServerMode {
return err
}
if config.ServerMode {
log.Print("listening on http://local.app.rethinkraw.com" + serverPort)
<-shutdown
return nil
}
if !zenity.IsAvailable() {
log.Fatal("Please install zenity.")
}
if !chrome.IsInstalled() {
return zenity.Error(
"Please download and install either Google Chrome or Microsoft Edge.",
zenity.Title("Google Chrome not found"))
}
data := filepath.Join(config.DataDir, "chrome")
cache := filepath.Join(config.TempDir, "chrome")
cmd := chrome.Command(url.String(), data, cache)
if err := cmd.Start(); err != nil {
return err
}
go func() {
for s := range shutdown {
cmd.Signal(s)
}
}()
return cmd.Wait()
}