forked from openvn/imgidx
/
galleyes.go
164 lines (157 loc) · 3.93 KB
/
galleyes.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
// Copyright 2011 The Open-VN.org Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package galleyes
import (
"crypto/md5"
"encoding/base64"
"github.com/openvn/nstuff"
"io"
"strconv"
"tipimage"
_ "tipimage/gif"
_ "tipimage/jpeg"
_ "tipimage/png"
)
func init() {
hashRouter, _ := nstuff.NewRouter("/hash$", Hash)
crawlRouter, _ := nstuff.NewRouter("/crawl$", Crawl)
indexRouter, _ := nstuff.NewRouter("$", Index)
var s nstuff.Host
s.AddRouters(crawlRouter, hashRouter, indexRouter)
s.Run()
}
func Index(s *nstuff.Host) {
html := `<!doctype html>
<html lang="vn">
<head>
<meta charset="utf-8">
<title>Image Index</title>
<link href="/statics/css/cupertino/jquery-ui-1.9.1.custom.css" rel="stylesheet">
<script src="/statics/js/jquery-1.8.2.js"></script>
<script src="/statics/js/jquery-ui-1.9.1.custom.js"></script>
<script>
$(function() {
$("#upload_link").click(function(){
$('#upload_frame').contents().find("#uploader").click();
});
});
function ShowResult(query) {
var arr = jQuery.parseJSON(query);
appendImage(arr, arr.length, 0);
}
function appendImage(arr, len, step) {
if (step >= len) {
return;
}
$("<img/>", {
"class": "img_result",
src: arr[step].SavedLocation,
title: arr[step].PageLocation
}).tooltip().hide().appendTo("#results").show(
'slide',
'easeInOutCubic',
1000,
function() {
appendImage(arr, len, step+1)
});
}
</script>
<style>
#layout {
width: 70%;
max-width: 700px;
margin-left: auto;
margin-right: auto;
}
#file_browser {
height: 100px;
border: 5px solid #d7ebf9;
border-radius: 25px;
text-align: center;
}
.img_result {
width: 100px;
margin: 10px;
border-radius: 10px;
}
</style>
</head>
<body>
<div id="layout">
<div class="ui-widget-content" id="file_browser">
<h1 id="upload_link">Select an Image!</h1>
<iframe id="upload_frame" class="ui-helper-hidden" src="hash"></iframe>
</div>
<div id="results"></div>
<form method="post" action="crawl">
<input type="text" name="url" />
<select name="deep">
<option value="-1">All</option>
<option value="0" selected="selected">Only this</option>
<option value="1">1 more levels</option>
<option value="3">3 more levels</option>
<option value="9">9 more levels</option>
<option value="27">27 more levels</option>
</select>
<input type="submit" value="upload" />
</form>
</div>
</body>
</html>`
s.Print(html)
}
func Hash(s *nstuff.Host) {
s.Print(`<!doctype html>
<html lang="vn">
<head>
<meta charset="utf-8">
<title>Image Index Uploader</title>
<script>
function submit() {
document.getElementById("form").submit();
}
`)
file, header, err := s.File("file")
if err == nil {
if err == nil {
filetype := header.Header.Get("Content-Type")
if filetype == "image/jpeg" || filetype == "image/png" || filetype == "image/gif" {
m, _, err := tipimage.Decode(file)
if err == nil {
h := md5.New()
io.Copy(h, file)
checksum := base64.URLEncoding.EncodeToString(h.Sum(nil))
phash, p := PHash(m)
FindImage(s, checksum, phash, p)
} else {
s.Print("error=invalid_image&err_detail=", err.Error())
}
}
} else {
s.Print("error=invalid_input")
}
}
s.Print(`
</script>
</head>
<body>
<form id="form" method="post" enctype="multipart/form-data" action="hash">
<input id="uploader" type="file" name="file" onchange="submit();" />
</form>
</body>
</html>
`)
}
func Crawl(s *nstuff.Host) {
page := s.Post("url")
if !ValidURL(page) {
s.Print("error: invalid image url.")
return
}
deep, err := strconv.Atoi(s.Post("deep"))
if err != nil {
deep = 0
}
NewImageIndex(s, page, deep).Index()
}