-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp_batch.go
188 lines (166 loc) · 4.67 KB
/
http_batch.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
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/schema"
"github.com/ncruces/rethinkraw/pkg/osutil"
"github.com/ncruces/zenity"
)
type multiStatus struct {
Code int `json:"code"`
Text string `json:"text"`
Body any `json:"response,omitempty"`
Done int `json:"done,omitempty"`
Total int `json:"total,omitempty"`
}
func batchHandler(w http.ResponseWriter, r *http.Request) httpResult {
if err := r.ParseForm(); err != nil {
return httpResult{Status: http.StatusBadRequest, Error: err}
}
prefix := getPathPrefix(r)
batch := fromBatchPath(r.URL.Path)
if len(batch) == 0 {
path := fromURLPath(r.URL.Path, prefix)
if fi, _ := os.Stat(path); fi != nil && fi.IsDir() {
return httpResult{Location: "/batch/" + toBatchPath(path)}
}
return httpResult{Status: http.StatusGone}
}
photos, err := findPhotos(batch)
if err != nil {
return httpResult{Error: err}
}
_, save := r.Form["save"]
_, export := r.Form["export"]
_, settings := r.Form["settings"]
switch {
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.Orientation = 0
results := batchProcess(r.Context(), photos, func(ctx context.Context, photo batchPhoto) error {
xmp := xmp
xmp.Filename = filepath.Base(photo.Path)
return saveEdit(ctx, photo.Path, xmp)
})
w.Header().Set("Content-Type", "application/x-ndjson")
w.WriteHeader(http.StatusMultiStatus)
batchResultWriter(w, results, len(photos))
return httpResult{}
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.Orientation = 0
var exppath string
if len(photos) > 0 {
exppath = filepath.Dir(photos[0].Path)
if res, err := zenity.SelectFile(zenity.Context(r.Context()), zenity.Directory(), zenity.Filename(exppath)); 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}
}
}
results := batchProcess(r.Context(), photos, func(ctx context.Context, photo batchPhoto) error {
err := batchProcessPhoto(ctx, photo, exppath, xmp, exp)
if err == nil && exp.Both {
err = batchProcessPhoto(ctx, photo, exppath, xmp, exportSettings{})
}
return err
})
w.Header().Set("Content-Type", "application/x-ndjson")
w.WriteHeader(http.StatusMultiStatus)
batchResultWriter(w, results, len(photos))
return httpResult{}
case settings:
if len(photos) == 0 {
return httpResult{Status: http.StatusNoContent}
}
if xmp, err := loadEdit(photos[0].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{}
default:
w.Header().Set("Cache-Control", "max-age=10")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct {
Export bool
Photos []struct{ Name, Path string }
}{
isLocalhost(r), nil,
}
for _, photo := range photos {
item := struct{ Name, Path string }{photo.Name, toURLPath(photo.Path, prefix)}
data.Photos = append(data.Photos, item)
}
return httpResult{
Error: templates.ExecuteTemplate(w, "batch.gohtml", data),
}
}
}
func batchProcessPhoto(ctx context.Context, photo batchPhoto, exppath string, xmp xmpSettings, exp exportSettings) error {
xmp.Filename = filepath.Base(photo.Path)
out, err := exportEdit(ctx, photo.Path, xmp, exp)
if err != nil {
return err
}
exppath = filepath.Join(exppath, exportPath(photo.Name, exp))
if err := os.MkdirAll(filepath.Dir(exppath), 0777); err != nil {
return err
}
f, err := osutil.NewFile(exppath)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(out)
if err != nil {
return err
}
return f.Close()
}
func batchResultWriter(w http.ResponseWriter, results <-chan error, total int) {
i := 0
enc := json.NewEncoder(w)
flush, _ := w.(http.Flusher)
for err := range results {
i += 1
var status multiStatus
if err != nil {
status.Code, status.Body = errorStatus(err)
} else {
status.Code = http.StatusOK
}
status.Done, status.Total = i, total
status.Text = http.StatusText(status.Code)
enc.Encode(status)
if flush != nil {
flush.Flush()
}
}
}