-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_dialog.go
86 lines (75 loc) · 2.16 KB
/
http_dialog.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
package main
import (
"errors"
"io/fs"
"net/http"
"net/url"
"os"
"github.com/ncruces/zenity"
)
var extensions = map[string]struct{}{}
var filters zenity.FileFilter
func init() {
pattern := []string{
"public.camera-raw-image",
"*.CRW", "*.NEF", "*.RAF", "*.ORF", "*.MRW", "*.DCR", "*.MOS", "*.RAW", "*.PEF", "*.SRF",
"*.DNG", "*.X3F", "*.CR2", "*.ERF", "*.SR2", "*.KDC", "*.MFW", "*.MEF", "*.ARW", "*.NRW",
"*.RW2", "*.RWL", "*.IIQ", "*.3FR", "*.FFF", "*.SRW", "*.GPR", "*.DXO", "*.ARQ", "*.CR3",
}
filters = zenity.FileFilter{Name: "RAW photos", Patterns: pattern, CaseFold: true}
for _, ext := range pattern[1:] {
extensions[ext[1:]] = struct{}{}
}
}
func dialogHandler(_ http.ResponseWriter, r *http.Request) httpResult {
if !isLocalhost(r) {
return httpResult{Status: http.StatusForbidden}
}
if err := r.ParseForm(); err != nil {
return httpResult{Status: http.StatusBadRequest, Error: err}
}
var err error
var path string
var paths []string
_, photo := r.Form["photo"]
_, batch := r.Form["batch"]
_, gallery := r.Form["gallery"]
switch {
case batch:
paths, err = zenity.SelectFileMultiple(zenity.Context(r.Context()), filters)
case photo:
path, err = zenity.SelectFile(zenity.Context(r.Context()), filters)
case gallery:
path, err = zenity.SelectFile(zenity.Context(r.Context()), zenity.Directory())
default:
return httpResult{Status: http.StatusNotFound}
}
if path != "" {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return httpResult{Status: http.StatusUnprocessableEntity, Message: err.Error()}
} else if err != nil {
return httpResult{Error: err}
}
} else if len(paths) != 0 {
path = toBatchPath(paths...)
} else if errors.Is(err, zenity.ErrCanceled) {
return httpResult{Status: http.StatusResetContent}
} else if err == nil {
return httpResult{Status: http.StatusInternalServerError}
} else {
return httpResult{Error: err}
}
var url url.URL
switch {
case batch:
url.Path = "/batch/" + path
case photo:
url.Path = "/photo/" + toURLPath(path, "")
case gallery:
url.Path = "/gallery/" + toURLPath(path, "")
}
return httpResult{
Status: http.StatusSeeOther,
Location: url.String(),
}
}