-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
546 lines (490 loc) · 12.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"log"
snet "net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
rice "github.com/GeertJohan/go.rice"
"github.com/chai2010/gettext-go"
"github.com/gorilla/mux"
"github.com/urfave/cli"
"golang.org/x/text/language"
)
// LomoWebVersion version auto generated
const LomoWebVersion = "2023-11-23.22-39-53.0.7097bb2"
const i18nMessage = `{
"zh_CN": {
"LC_MESSAGES": {
"message.json": [
{
"msgctxt" : "",
"msgid" : "Login",
"msgid_plural": "",
"msgstr" : ["登陆"]
},
{
"msgctxt" : "",
"msgid" : "Login form",
"msgid_plural": "",
"msgstr" : ["登陆表单"]
},
{
"msgctxt" : "",
"msgid" : "Username",
"msgid_plural": "",
"msgstr" : ["用户名"]
},
{
"msgctxt" : "",
"msgid" : "Password",
"msgid_plural": "",
"msgstr" : ["密码"]
},
{
"msgctxt" : "",
"msgid" : "Submit",
"msgid_plural": "",
"msgstr" : ["提交"]
},
{
"msgctxt" : "",
"msgid" : "Gallery",
"msgid_plural": "",
"msgstr" : ["图库"]
},
{
"msgctxt" : "",
"msgid" : "Inbox",
"msgid_plural": "",
"msgstr" : ["收件箱"]
},
{
"msgctxt" : "",
"msgid" : "Import",
"msgid_plural": "",
"msgstr" : ["导入"]
},
{
"msgctxt" : "",
"msgid" : "Logout",
"msgid_plural": "",
"msgstr" : ["退出"]
},
{
"msgctxt" : "",
"msgid" : "Lomorage Gallery",
"msgid_plural": "",
"msgstr" : ["Lomorage图库"]
},
{
"msgctxt" : "",
"msgid" : "Assets Import",
"msgid_plural": "",
"msgstr" : ["资源导入"]
},
{
"msgctxt" : "",
"msgid" : "Drag and drop image and video files or directory to import them into Lomorage",
"msgid_plural": "",
"msgstr" : ["要导入图片或视频到Lomorage,请将其拖拽到下面区域"]
},
{
"msgctxt" : "",
"msgid" : "Add files...",
"msgid_plural": "",
"msgstr" : ["添加文件..."]
},
{
"msgctxt" : "",
"msgid" : "Cancel upload",
"msgid_plural": "",
"msgstr" : ["取消上传"]
},
{
"msgctxt" : "",
"msgid" : "Delete selected",
"msgid_plural": "",
"msgstr" : ["删除选择"]
},
{
"msgctxt" : "",
"msgid" : "Processing...",
"msgid_plural": "",
"msgstr" : ["处理中..."]
},
{
"msgctxt" : "",
"msgid" : "Edit",
"msgid_plural": "",
"msgstr" : ["编辑"]
},
{
"msgctxt" : "",
"msgid" : "Start",
"msgid_plural": "",
"msgstr" : ["开始"]
},
{
"msgctxt" : "",
"msgid" : "Cancel",
"msgid_plural": "",
"msgstr" : ["取消"]
},
{
"msgctxt" : "",
"msgid" : "Error",
"msgid_plural": "",
"msgstr" : ["错误"]
},
{
"msgctxt" : "",
"msgid" : "Delete",
"msgid_plural": "",
"msgstr" : ["删除"]
}
]
}
},
"en_US": {
"LC_MESSAGES": {
"message.json": [
{
"msgctxt" : "",
"msgid" : "Login",
"msgid_plural": "",
"msgstr" : ["Login"]
},
{
"msgctxt" : "",
"msgid" : "Login form",
"msgid_plural": "",
"msgstr" : ["Login form"]
},
{
"msgctxt" : "",
"msgid" : "Username",
"msgid_plural": "",
"msgstr" : ["Username"]
},
{
"msgctxt" : "",
"msgid" : "Password",
"msgid_plural": "",
"msgstr" : ["Password"]
},
{
"msgctxt" : "",
"msgid" : "Submit",
"msgid_plural": "",
"msgstr" : ["Submit"]
},
{
"msgctxt" : "",
"msgid" : "Gallery",
"msgid_plural": "",
"msgstr" : ["Gallery"]
},
{
"msgctxt" : "",
"msgid" : "Inbox",
"msgid_plural": "",
"msgstr" : ["Inbox"]
},
{
"msgctxt" : "",
"msgid" : "Import",
"msgid_plural": "",
"msgstr" : ["Import"]
},
{
"msgctxt" : "",
"msgid" : "Logout",
"msgid_plural": "",
"msgstr" : ["Logout"]
},
{
"msgctxt" : "",
"msgid" : "Lomorage Gallery",
"msgid_plural": "",
"msgstr" : ["Lomorage Gallery"]
},
{
"msgctxt" : "",
"msgid" : "Assets Import",
"msgid_plural": "",
"msgstr" : ["Assets Import"]
},
{
"msgctxt" : "",
"msgid" : "Drag and drop image and video files or directory to import them into Lomorage",
"msgid_plural": "",
"msgstr" : ["Drag and drop image and video files or directory to import them into Lomorage"]
},
{
"msgctxt" : "",
"msgid" : "Add files...",
"msgid_plural": "",
"msgstr" : ["Add files..."]
},
{
"msgctxt" : "",
"msgid" : "Cancel upload",
"msgid_plural": "",
"msgstr" : ["Cancel upload"]
},
{
"msgctxt" : "",
"msgid" : "Delete selected",
"msgid_plural": "",
"msgstr" : ["Delete selected"]
},
{
"msgctxt" : "",
"msgid" : "Processing...",
"msgid_plural": "",
"msgstr" : ["Processing..."]
},
{
"msgctxt" : "",
"msgid" : "Edit",
"msgid_plural": "",
"msgstr" : ["Edit"]
},
{
"msgctxt" : "",
"msgid" : "Start",
"msgid_plural": "",
"msgstr" : ["Start"]
},
{
"msgctxt" : "",
"msgid" : "Cancel",
"msgid_plural": "",
"msgstr" : ["Cancel"]
},
{
"msgctxt" : "",
"msgid" : "Error",
"msgid_plural": "",
"msgstr" : ["Error"]
},
{
"msgctxt" : "",
"msgid" : "Delete",
"msgid_plural": "",
"msgstr" : ["Delete"]
}
]
}
}
}`
var gText gettext.Gettexter
// ListIPs list available ipv4 addresses
func ListIPs() ([]snet.IP, error) {
addrs, err := snet.InterfaceAddrs()
if err != nil {
return nil, err
}
ret := []snet.IP{}
for _, a := range addrs {
if ipnet, ok := a.(*snet.IPNet); ok && ipnet.IP.IsGlobalUnicast() {
if ipnet.IP.To4() != nil {
ret = append(ret, ipnet.IP)
}
}
}
return ret, nil
}
func changeLocale(locale string) {
gText.SetLanguage(locale)
}
func translate(input string) string {
return gText.PGettext("", input)
}
// LoadFile load html file
func LoadFile(fileName string) (string, error) {
// find a rice.Box
templateBox, err := rice.FindBox("templates")
if err != nil {
log.Printf("error finding templates: %v\n", err)
return "", err
}
// get file contents as string
templateString, err := templateBox.String(fileName)
if err != nil {
log.Printf("error reading templates: %v\n", err)
return "", err
}
funcMap := template.FuncMap{
"gettext": translate,
}
t, _ := template.New("foo").Funcs(funcMap).Parse(templateString)
var tpl bytes.Buffer
if err := t.Execute(&tpl, nil); err != nil {
return "", err
}
return tpl.String(), nil
}
// ChangePreferedLanguage change lang according to http header
func ChangePreferedLanguage(r *http.Request) {
var matcher = language.NewMatcher([]language.Tag{
language.English, // The first language is used as fallback.
language.Chinese,
})
accept := r.Header.Get("Accept-Language")
tag, _ := language.MatchStrings(matcher, accept)
if strings.HasPrefix(tag.String(), "zh") {
changeLocale("zh_CN")
} else {
changeLocale("en_US")
}
}
// Handlers
// LoginPageHandler for GET
func LoginPageHandler(response http.ResponseWriter, request *http.Request) {
ChangePreferedLanguage(request)
var body, _ = LoadFile("login.html")
io.WriteString(response, body)
}
// ImportPageHandler for GET
func ImportPageHandler(response http.ResponseWriter, request *http.Request) {
ChangePreferedLanguage(request)
var body, _ = LoadFile("import.html")
io.WriteString(response, body)
}
// GalleryPageHandler for GET
func GalleryPageHandler(response http.ResponseWriter, request *http.Request) {
ChangePreferedLanguage(request)
var body, _ = LoadFile("gallery.html")
io.WriteString(response, body)
}
// InboxPageHandler for GET
func InboxPageHandler(response http.ResponseWriter, request *http.Request) {
ChangePreferedLanguage(request)
var body, _ = LoadFile("inbox.html")
io.WriteString(response, body)
}
// ConfJsTemplate conf.js template
var ConfJsTemplate = `
var CONFIG = {
SERVICE_URL: '%v',
LOGIN_URI: 'login',
ASSERT_URI: 'asset',
PREVIEW_URI: 'preview',
CATEGORY_URI: 'category',
RECEIVE_URI: 'receive',
RECEIVE_USER_URI: 'receive/user',
RECEIVE_GROUP_URI: 'receive/group',
RECEIVE_ASSERT_URI: 'receive/asset',
RECEIVE_PREVIEW_URI: 'receive/preview',
getServiceUrl: function() {
if (CONFIG.SERVICE_URL === "") {
return window.location.protocol + "//" + window.location.hostname + ":8000";
} else {
return CONFIG.SERVICE_URL;
}
},
getLoginUrl: function() {
return CONFIG.getServiceUrl() + '/' + CONFIG.LOGIN_URI;
},
getUploadUrl: function() {
return CONFIG.getServiceUrl() + '/' + CONFIG.ASSERT_URI;
},
getAssetUrl: function(name) {
return CONFIG.getServiceUrl() + '/' + CONFIG.ASSERT_URI + '/' + name + "?token=" + sessionStorage.getItem("token") + "&orig=1";
},
getPreviewUrl: function(name) {
return CONFIG.getServiceUrl() + '/' + CONFIG.PREVIEW_URI + '/' + name + "?width=320&height=-1&token=" + sessionStorage.getItem("token");
},
getMonthLevelMerkleTreeUrl: function() {
return CONFIG.getServiceUrl() + '/' + CONFIG.CATEGORY_URI;
},
getAssetLevelMerkleTreeUrl: function(year, month) {
return CONFIG.getServiceUrl() + '/' + CONFIG.CATEGORY_URI + '/' + year + '/' + month;
},
getInboxUrl: function() {
return CONFIG.getServiceUrl() + '/' + CONFIG.RECEIVE_URI + "?token=" + sessionStorage.getItem("token");
},
getUserInboxUrl: function(uid) {
return CONFIG.getServiceUrl() + '/' + CONFIG.RECEIVE_USER_URI + '/' + uid + "?token=" + sessionStorage.getItem("token");
},
getGroupInboxUrl: function(gid) {
return CONFIG.getServiceUrl() + '/' + CONFIG.RECEIVE_GROUP_URI + '/' + gid + "?token=" + sessionStorage.getItem("token");
},
getInboxAssetUrl: function(shareid) {
return CONFIG.getServiceUrl() + '/' + CONFIG.RECEIVE_ASSERT_URI + '/' + shareid + "?token=" + sessionStorage.getItem("token") + "&orig=1";
},
getInboxPreviewUrl: function(shareid) {
return CONFIG.getServiceUrl() + '/' + CONFIG.RECEIVE_PREVIEW_URI + '/' + shareid + "?width=200&height=-1&token=" + sessionStorage.getItem("token");
}
}
`
// BaseURL base url from command line or local ip address
var BaseURL = ""
// ConfJsHandler server conf.js
func ConfJsHandler(response http.ResponseWriter, request *http.Request) {
io.WriteString(response, fmt.Sprintf(ConfJsTemplate, BaseURL))
}
func main() {
app := cli.NewApp()
app.Version = LomoWebVersion
app.Usage = "Lomorage web app"
app.Authors = []cli.Author{
cli.Author{
Name: "Jeromy Fu",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "baseurl",
Usage: "URL of Lomorage Service (lomod), default will be http://[lomoweb-host-ip]:8000",
},
&cli.UintFlag{
Name: "port",
Usage: "lomo-web listen port",
Value: 80,
},
}
app.Action = bootService
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func bootService(ctx *cli.Context) error {
BaseURL = ctx.String("baseurl")
log.Printf("Lomorage Service lomod url: %s", BaseURL)
gText = gettext.New("message", "", i18nMessage).SetLanguage("en_US")
var router = mux.NewRouter()
// This will serve files under http://localhost:8000/static/<filename>
router.HandleFunc("/static/lomo/js/conf.js", ConfJsHandler)
box := rice.MustFindBox("static")
staticFileServer := http.StripPrefix("/static/", http.FileServer(box.HTTPBox()))
router.PathPrefix("/static/").Handler(staticFileServer)
router.HandleFunc("/", LoginPageHandler) // GET
router.HandleFunc("/import", ImportPageHandler) // GET
router.HandleFunc("/gallery", GalleryPageHandler) // GET
router.HandleFunc("/inbox", InboxPageHandler) // GET
log.Println("Server started. Press Ctrl-C to stop server")
// Catch the Ctrl-C and SIGTERM from kill command
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
signalType := <-ch
signal.Stop(ch)
log.Println("Exit command received. Exiting...")
// this is a good place to flush everything to disk
// before terminating.
log.Println("Signal type : ", signalType)
os.Exit(0)
}()
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", ctx.Int("port")), router))
return nil
}