-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp_photo.go
170 lines (154 loc) · 4.43 KB
/
http_photo.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
package main
import (
"encoding/json"
"errors"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/schema"
"github.com/ncruces/jason"
"github.com/ncruces/rethinkraw/internal/util"
"github.com/ncruces/zenity"
)
func photoHandler(w http.ResponseWriter, r *http.Request) httpResult {
if err := r.ParseForm(); err != nil {
return httpResult{Status: http.StatusBadRequest, Error: err}
}
prefix := getPathPrefix(r)
path := fromURLPath(r.URL.Path, prefix)
_, meta := r.Form["meta"]
_, save := r.Form["save"]
_, export := r.Form["export"]
_, preview := r.Form["preview"]
_, settings := r.Form["settings"]
_, whiteBalance := r.Form["wb"]
switch {
case meta:
w.Header().Set("Cache-Control", "max-age=10")
if r := sendCached(w, r, path); r.Done() {
return r
}
if out, err := getMetaHTML(path); err != nil {
return httpResult{Error: err}
} else {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(out)
return httpResult{}
}
case save:
var xmp xmpSettings
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
if err := dec.Decode(&xmp, r.Form); err != nil {
return httpResult{Error: err}
}
xmp.Filename = filepath.Base(path)
if err := saveEdit(r.Context(), path, xmp); err != nil {
return httpResult{Error: err}
} else {
return httpResult{Status: http.StatusNoContent}
}
case export:
var xmp xmpSettings
var exp exportSettings
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
if err := dec.Decode(&xmp, r.Form); err != nil {
return httpResult{Error: err}
}
if err := dec.Decode(&exp, r.Form); err != nil {
return httpResult{Error: err}
}
xmp.Filename = filepath.Base(path)
exppath := exportPath(path, exp)
if isLocalhost(r) {
if res, err := zenity.SelectFileSave(zenity.Context(r.Context()), zenity.Filename(exppath), zenity.ConfirmOverwrite()); res != "" {
exppath = res
} else if errors.Is(err, zenity.ErrCanceled) {
return httpResult{Status: http.StatusNoContent}
} else if err == nil {
return httpResult{Status: http.StatusInternalServerError}
} else {
return httpResult{Error: err}
}
}
if out, err := exportEdit(r.Context(), path, xmp, exp); err != nil {
return httpResult{Error: err}
} else if isLocalhost(r) {
if err := os.WriteFile(exppath, out, 0666); err != nil {
return httpResult{Error: err}
} else {
return httpResult{Status: http.StatusNoContent}
}
} else {
name := filepath.Base(exppath)
w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+util.PercentEncode(name))
if exp.DNG {
w.Header().Set("Content-Type", "image/x-adobe-dng")
} else {
w.Header().Set("Content-Type", "image/jpeg")
}
w.Write(out)
return httpResult{}
}
case preview:
var xmp xmpSettings
var size struct{ Preview int }
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
if err := dec.Decode(&xmp, r.Form); err != nil {
return httpResult{Error: err}
}
if err := dec.Decode(&size, r.Form); err != nil {
return httpResult{Error: err}
}
if out, err := previewEdit(r.Context(), path, size.Preview, xmp); err != nil {
return httpResult{Error: err}
} else {
w.Header().Set("Content-Type", "image/jpeg")
w.Write(out)
return httpResult{}
}
case settings:
if xmp, err := loadEdit(path); err != nil {
return httpResult{Error: err}
} else {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(xmp); err != nil {
return httpResult{Error: err}
}
}
return httpResult{}
case whiteBalance:
var coords struct{ WB []float64 }
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
if err := dec.Decode(&coords, r.Form); err != nil {
return httpResult{Error: err}
}
if wb, err := loadWhiteBalance(r.Context(), path, coords.WB); err != nil {
return httpResult{Error: err}
} else {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(wb); err != nil {
return httpResult{Error: err}
}
}
return httpResult{}
default:
if _, err := os.Stat(path); err != nil {
return httpResult{Error: err}
}
w.Header().Set("Cache-Control", "max-age=300")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
return httpResult{
Error: templates.ExecuteTemplate(w, "photo.gohtml", jason.Object{
"Title": toUsrPath(path, prefix),
"Path": toURLPath(path, prefix),
"Name": filepath.Base(path),
}),
}
}
}