This repository has been archived by the owner on Nov 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (137 loc) · 3.62 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"path/filepath"
"github.com/asticode/go-astilectron"
"github.com/asticode/go-astilectron-bootstrap"
"github.com/asticode/go-astilog"
"github.com/hidez8891/imgview/module"
"github.com/pkg/errors"
_ "github.com/hidez8891/imgview/module/archive"
_ "github.com/hidez8891/imgview/module/image"
)
// Vars
var (
AppName string
BuiltAt string
debug = flag.Bool("d", false, "enables the debug mode")
port = flag.Int("p", 4340, "server port")
wnd *astilectron.Window
srv *http.Server
modCache = make(map[string]module.ArchiveModule, 0)
)
func main() {
// Init
flag.Parse()
astilog.FlagInit()
// bootstrap options
options := bootstrap.Options{
Asset: Asset,
AstilectronOptions: astilectron.Options{
AppName: AppName,
AppIconDarwinPath: "resources/app/dist/icons/icon.icns",
AppIconDefaultPath: "resources/app/dist/icons/icon.png",
},
Debug: *debug,
Homepage: "dist/index.html",
MenuOptions: []*astilectron.MenuItemOptions{},
OnWait: func(_ *astilectron.Astilectron, w *astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
wnd = w
// start http server
runServer()
return nil
},
MessageHandler: apiEventHandler,
RestoreAssets: RestoreAssets,
WindowOptions: &astilectron.WindowOptions{
Height: astilectron.PtrInt(700),
Width: astilectron.PtrInt(700),
},
}
// Run bootstrap
astilog.Debugf("Running app built at %s", BuiltAt)
if err := bootstrap.Run(options); err != nil {
fmt.Println(err)
astilog.Fatal(errors.Wrap(err, "running bootstrap failed"))
}
// shutdown http server
if srv != nil {
srv.Shutdown(context.Background())
}
}
func runServer() {
http.HandleFunc("/image/", imageHandler)
http.Handle("/", http.NotFoundHandler())
addr := fmt.Sprintf("localhost:%d", *port)
srv = &http.Server{Addr: addr}
go func() {
astilog.Debugf("start web server [port:%d]", addr)
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
astilog.Fatal(errors.Wrap(err, "crash web server"))
}
}
}()
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
param := r.URL.Query()
encPath := param.Get("path")
if len(encPath) == 0 {
http.NotFound(w, r)
return
}
paths, err := decodeStringArray(encPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
file := paths[len(paths)-1]
modimg, err := module.GetImageModule(filepath.Ext(file))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(paths) == 1 {
// image file
if err := modimg.WriteResponse(w, file); err != nil {
astilog.Error(err.Error())
}
} else {
// image file into archive
arch := paths[0]
if _, ok := modCache[arch]; !ok {
for _, m := range modCache {
m.Close()
}
modCache = make(map[string]module.ArchiveModule, 0)
modarch, err := module.GetArchiveModule(filepath.Ext(arch))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := modarch.Open(arch); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
modCache[arch] = modarch
}
modarch := modCache[arch]
r, err := modarch.ReadFile(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Close()
info, err := modarch.ReadFileInfo(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := modimg.WriteResponseFromReader(w, r, info.Size()); err != nil {
astilog.Warn(err.Error())
}
}
}