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
|
// import fetchJsonp from 'fetch-jsonp'
import * as types from '../types'
// import { hashPath } from '../util'
import { store, history } from '../store'
import { post, pad, verify, preloadImage } from '../util'
import querystring from 'query-string'
// urls
const url = {
upload: () => process.env.API_HOST + '/search/api/upload',
search: () => process.env.API_HOST + '/search/api/fetch',
searchByVerifiedFrame: (verified, hash, frame) => process.env.API_HOST + '/search/api/search/' + verified + '/' + hash + '/' + pad(frame, 6),
searchByFrame: (hash, frame) => process.env.API_HOST + '/search/api/search/' + hash + '/' + pad(frame, 6),
browse: hash => process.env.API_HOST + '/search/api/list/' + hash,
random: () => process.env.API_HOST + '/search/api/random',
check: () => process.env.API_HOST + '/api/images/import/search',
}
export const publicUrl = {
browse: hash => '/search/browse/' + hash,
searchByVerifiedFrame: (verified, hash, frame) => '/search/keyframe/' + verify(verified) + '/' + hash + '/' + pad(frame, 6),
searchByFrame: (hash, frame) => '/search/keyframe/' + hash + '/' + pad(frame, 6),
review: () => '/search/review/'
}
// standard loading events
const loading = (tag, offset) => ({
type: types.search.loading,
tag,
offset
})
const loaded = (tag, data, offset = 0) => ({
type: types.search.loaded,
tag,
data,
offset
})
const error = (tag, err) => ({
type: types.search.error,
tag,
err
})
// search UI functions
export const panic = () => dispatch => {
history.push('/search/')
dispatch({ type: types.search.panic })
}
export const updateOptions = opt => dispatch => {
dispatch({ type: types.search.update_options, opt })
}
// API functions
export const upload = (file, query) => dispatch => {
const { options } = store.getState().search
const tag = 'query'
const fd = new FormData()
fd.append('query_img', file)
fd.append('limit', options.perPage)
if (!query) {
dispatch(loading(tag))
}
post(url.upload(), fd)
.then(data => {
if (query) {
const { timing } = data.query
data.query = {
...query,
timing,
}
let qs = {}
if (data.query.crop) {
let { x, y, w, h } = data.query.crop
qs.crop = [x, y, w, h].map(n => parseInt(n, 10)).join(',')
}
if (query.url && !query.hash) {
qs.url = query.url
}
// history.push(window.location.pathname + '#' + querystring.stringify(qs))
// window.location.hash = querystring.stringify(qs)
} else if (data.query.url && !window.location.search.match(data.query.url)) {
history.push('/search/?url=' + data.query.url)
}
dispatch(loaded(tag, data))
})
.catch(err => dispatch(error(tag, err)))
}
export const searchByVerifiedFrame = (verified, hash, frame, offset = 0) => dispatch => {
const { options } = store.getState().search
const tag = 'query'
dispatch(loading(tag, offset))
const qs = querystring.stringify({ limit: options.perPage, offset })
preloadImage({ verified, hash, frame })
fetch(url.searchByVerifiedFrame(verified, hash, frame) + '?' + qs, {
method: 'GET',
mode: 'cors',
})
.then(data => data.json())
.then(data => dispatch(loaded(tag, data, offset)))
.catch(err => dispatch(error(tag, err)))
}
export const searchByFrame = (hash, frame, offset = 0) => dispatch => {
const { options } = store.getState().search
const tag = 'query'
dispatch(loading(tag, offset))
const qs = querystring.stringify({ limit: options.perPage, offset })
preloadImage({ verified: false, hash, frame })
fetch(url.searchByFrame(hash, frame) + '?' + qs, {
method: 'GET',
mode: 'cors',
})
.then(data => data.json())
.then(data => dispatch(loaded(tag, data, offset)))
.catch(err => dispatch(error(tag, err)))
}
export const search = (uri, offset = 0) => dispatch => {
const { options } = store.getState().search
const tag = 'query'
dispatch(loading(tag, offset))
const qs = querystring.stringify({ url: uri, limit: options.perPage, offset })
if (uri.indexOf('static') === 0) {
preloadImage({ uri })
}
fetch(url.search(uri) + '?' + qs, {
method: 'GET',
mode: 'cors',
})
.then(data => data.json())
.then(data => dispatch(loaded(tag, data, offset)))
.catch(err => dispatch(error(tag, err)))
}
export const browse = hash => dispatch => {
const tag = 'browse'
dispatch(loading(tag))
fetch(url[tag](hash), {
method: 'GET',
mode: 'cors',
})
.then(data => data.json())
.then(data => dispatch(loaded(tag, data)))
.catch(err => dispatch(error(tag, err)))
}
export const random = () => dispatch => {
const { options } = store.getState().search
const qs = querystring.stringify({ limit: options.perPage })
const tag = 'query'
dispatch(loading(tag))
fetch(url.random() + '?' + qs, {
method: 'GET',
mode: 'cors',
})
.then(data => data.json())
.then(data => {
dispatch(loaded(tag, data))
history.push(publicUrl.searchByVerifiedFrame(data.query.verified, data.query.hash, data.query.frame))
// window.history.pushState(null, 'VSearch: Results', publicUrl.searchByVerifiedFrame(data.query.verified, data.query.hash, data.query.frame))
})
.catch(err => dispatch(error(tag, err)))
}
|