This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
import.go
256 lines (230 loc) · 4.54 KB
/
import.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/bakape/hydron/common"
"github.com/bakape/hydron/db"
"github.com/bakape/hydron/files"
imp "github.com/bakape/hydron/import"
)
/*
Import file/directory paths
paths: list for file and/or directory paths
del: delete files after import
fetchTags: also fetch tags from danbooru
storeName: store the original filename as a tag
tagStr: add string of tags to all imported files
*/
func importPaths(
paths []string, del, fetchTags, storeName bool, tagStr string,
) error {
paths, err := files.Traverse(paths)
if err != nil {
return err
}
// Buffer all paths into a channel
passPaths := make(chan string, len(paths))
for _, p := range paths {
passPaths <- p
}
// Process files in parallel
ch := make(chan Result)
n := runtime.NumCPU() + 1
for i := 0; i < n; i++ {
go func() {
for p := range passPaths {
res := new(Result)
_, res.err = importPath(p, del, fetchTags, storeName, tagStr)
ch <- *res
}
}()
}
// Aggregate and log results
p := progressLogger{
header: "importing, thumbnailing",
total: len(paths),
}
if fetchTags {
p.header += ", fetching tags"
}
for i := 0; i < len(paths); i++ {
store := new(Result)
*store = <-ch
err = store.err
if err != nil {
p.Err(err)
} else {
p.Done()
}
}
close(passPaths) // Stop worker goroutines
p.Close()
return nil
}
func importPath(
p string, del, fetchTags, storeName bool, tagStr string,
) (
img common.Image, err error,
) {
f, err := os.Open(p)
if err != nil {
return
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return
}
var name string
if storeName {
name = info.Name()
name = strings.TrimSuffix(name, filepath.Ext(name))
}
img, err = imp.ImportFile(
f,
int(info.Size()),
name,
tagStr,
fetchTags,
)
switch err {
case nil:
case imp.ErrImported:
err = nil
default:
err = fmt.Errorf("%s: %s", p, err)
return
}
if del {
// Close file before removing
f.Close()
f = nil
err = os.Remove(p)
}
return
}
// Download and import a file from the client
func importUpload(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(0)
if err != nil {
sendError(w, 400, err)
return
}
f, info, err := r.FormFile("file")
if err != nil {
sendError(w, 400, err)
return
}
defer f.Close()
size, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64)
if err != nil {
sendError(w, 400, err)
return
}
var name string
storeName, err := strconv.ParseBool(r.Form.Get("store_name"))
if err != nil {
sendError(w, 400, err)
return
}
if storeName {
name = strings.TrimSuffix(info.Filename, filepath.Ext(info.Filename))
}
img, err := imp.ImportFile(
f,
int(size),
name,
r.Form.Get("tags"),
r.Form.Get("fetch_tags") == "true",
)
switch err {
case nil:
case imp.ErrImported:
// Still return the JSON, if already imported
img, err = db.GetImage(img.SHA1)
if err != nil {
send500(w, r, err)
return
}
case imp.ErrUnsupportedFile:
sendError(w, 415, err)
return
default:
send500(w, r, err)
return
}
serveJSON(w, r, img)
}
// Copy of importPaths that sends progress to client
// Temporary name, think of a better one
func clientImportPaths(
w http.ResponseWriter,
r *http.Request,
paths []string,
del bool,
fetchTags bool,
storeName bool,
tagStr string,
) error {
paths, err := files.Traverse(paths)
if err != nil {
return err
}
// Buffer all paths into a channel
passPaths := make(chan string, len(paths))
for _, p := range paths {
passPaths <- p
}
// Process files in parallel
ch := make(chan Result)
n := runtime.NumCPU() + 1
for i := 0; i < n; i++ {
go func() {
for p := range passPaths {
res := new(Result)
res.img, res.err = importPath(
p, del, fetchTags, storeName, tagStr,
)
ch <- *res
}
}()
}
flusher, ok := w.(http.Flusher)
if !ok {
log.Fatal("Failed to make HTTP Flusher.")
}
total := len(paths)
for i := 0; i < total; i++ {
store := new(Result)
*store = <-ch
err = store.err
switch err {
case nil:
case imp.ErrImported:
case imp.ErrUnsupportedFile:
sendError(w, 415, err)
return err
default:
send500(w, r, err)
return err
}
jsonChunk, err := json.Marshal(Chunk{store.img.SHA1, i + 1, total})
if err != nil {
send500(w, r, err)
return err
}
// Need to separate chunks, hyphen never appears in chunk content
// so is safe to use
fmt.Fprintf(w, "%s-", jsonChunk)
flusher.Flush()
}
close(passPaths) // Stop worker goroutines
return nil
}