Skip to content

Commit 85da347

Browse files
author
flashine
committed
✔️修复一个关闭tab后再次查询相同内容无tab显示的bug,完善iconhash的计算方式
1 parent 0133d34 commit 85da347

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>com.google.guava</groupId>
3434
<artifactId>guava</artifactId>
35-
<version>29.0-jre</version>
35+
<version>30.1-jre</version>
3636
</dependency>
3737
</dependencies>
3838
<build>

src/main/java/org/fofaviewer/controls/CloseableTabPane.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
public class CloseableTabPane extends BorderPane {
1414
private final TabPane tabPane;
15-
private HashMap<String, Tab> tabMap;
1615
private HashMap<Tab, ArrayList<String>> dataMap;
1716

1817
public CloseableTabPane() {
1918
this.tabPane = new TabPane();
20-
this.tabMap = new HashMap<>();
2119
this.dataMap = new HashMap<>();
2220
StackPane sp = new StackPane();
2321
sp.getChildren().add(tabPane);
@@ -42,7 +40,6 @@ public void onChanged(Change<? extends Tab> c) {
4240
for (Tab tab:tabPane.getTabs()){
4341
if(tab.selectedProperty().getValue()){
4442
tabPane.getTabs().remove(tab);
45-
tabMap.remove(tab.getText());
4643
dataMap.remove(tab);
4744
break;
4845
}
@@ -57,8 +54,6 @@ public void onChanged(Change<? extends Tab> c) {
5754
if(tab.selectedProperty().getValue()){
5855
tabPane.getTabs().clear();
5956
tabPane.getTabs().add(tab);
60-
tabMap.clear();
61-
tabMap.put(tab.getText(), tab);
6257
ArrayList<String> tmp = dataMap.get(tab);
6358
dataMap.clear();
6459
dataMap.put(tab, tmp);
@@ -70,7 +65,6 @@ public void onChanged(Change<? extends Tab> c) {
7065
MenuItem closeAll = new MenuItem("关闭所有");
7166
closeAll.setOnAction(e->{
7267
tabPane.getTabs().clear();
73-
tabMap.clear();
7468
dataMap.clear();
7569
});
7670
menuButton.getItems().add(closeAll);
@@ -84,7 +78,6 @@ public void onChanged(Change<? extends Tab> c) {
8478
public void addTab(Tab tab, ArrayList<String> list){
8579
tab.setClosable(true);
8680
tabPane.getTabs().add(tab);
87-
tabMap.put(tab.getText(),tab);
8881
if(list != null){
8982
dataMap.put(tab, list);
9083
}else{
@@ -100,11 +93,21 @@ public void addAll(ObservableList<Tab> tabs){
10093
}
10194

10295
public boolean isExistTab(String name){
103-
return this.tabMap.get(name) != null;
96+
for(Tab tab : this.tabPane.getTabs()){
97+
if(tab.getText().equals(name)){
98+
return true;
99+
}
100+
}
101+
return false;
104102
}
105103

106104
public Tab getTab(String name){
107-
return this.tabMap.get(name);
105+
for(Tab tab : this.tabPane.getTabs()){
106+
if(tab.getText().equals(name)){
107+
return tab;
108+
}
109+
}
110+
return null;
108111
}
109112

110113
public ArrayList<String> getList(Tab tab){

src/main/java/org/fofaviewer/main/MainController.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,10 @@ public void handle(ActionEvent event) {
344344
res = helper.getImageFavicon(url + "/favicon.ico");
345345
}
346346
if(res != null){
347-
query(RequestHelper.encode(res.get("msg")));
347+
if(res.get("code").equals("error")){
348+
showAlert(Alert.AlertType.ERROR, null, res.get("msg"));return;
349+
}
350+
query(res.get("msg"));
348351
return;
349352
}
350353
showAlert(Alert.AlertType.ERROR, null, "该网站未提供favicon");

src/main/java/org/fofaviewer/utils/RequestHelper.java

+29-42
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public class RequestHelper {
3939
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0",
4040
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.41 Safari/537.36 Edg/88.0.705.22"
4141
};
42-
private final String path = System.getProperty("user.dir") +
43-
System.getProperty("file.separator") + "iconhash" + System.getProperty("file.separator");
4442

4543
private RequestHelper() {
4644
this.logger = Logger.getLogger("RequestHelper");
@@ -124,17 +122,14 @@ public HashMap<String, String> getHTML(String url) {
124122
/**
125123
* 提取网站favicon 需要两步:
126124
* 1. 直接访问url根目录的favicon,若404则跳转至第2步
127-
* 2. 访问网站,获取html页面,获取head头中的 link标签的ico 路径
125+
* 2. 访问网站,获取html页面,获取head中的 link标签的ico 路径
128126
*
129127
* @param url
130128
* @return
131129
*/
132130
public HashMap<String, String> getImageFavicon(String url) {
133-
String filename = this.path + "favicon.ico";
134-
File file = new File(filename);
135131
CloseableHttpResponse response = getResponse(url);
136132
HashMap<String, String> result = new HashMap<>();
137-
int cache = 10240;
138133
if (response != null) {
139134
int code = response.getStatusLine().getStatusCode();
140135
result.put("code", String.valueOf(code));
@@ -144,16 +139,18 @@ public HashMap<String, String> getImageFavicon(String url) {
144139
logger.log(Level.FINE, url + "无响应内容");
145140
return null;
146141
}
147-
FileOutputStream fileout = new FileOutputStream(file);
148142
InputStream is = response.getEntity().getContent();
149-
byte[] buffer = new byte[cache];
150-
int ch = 0;
151-
while ((ch = is.read(buffer)) != -1) {
152-
fileout.write(buffer, 0, ch);
143+
byte[] buffer = new byte[1024];
144+
ByteArrayOutputStream bos=new ByteArrayOutputStream();
145+
int len = 0;
146+
while((len = is.read(buffer))!=-1){
147+
bos.write(buffer,0, len);
153148
}
154-
is.close();
155-
fileout.flush();
156-
fileout.close();
149+
bos.flush();
150+
String encoded = new BASE64Encoder().encode(Objects.requireNonNull(bos.toByteArray()));
151+
String hash = getIconHash(encoded);
152+
result.put("msg", "icon_hash=\"" + hash + "\"");
153+
return result;
157154
} catch (Exception e) {
158155
result.put("code", "error");
159156
result.put("msg", e.getMessage());
@@ -166,11 +163,6 @@ public HashMap<String, String> getImageFavicon(String url) {
166163
logger.log(Level.WARNING, ex.getMessage(), ex);
167164
}
168165
}
169-
String hash = getIconHash(filename);
170-
if (hash != null) {
171-
result.put("msg", hash.replaceAll("\n", ""));
172-
return result;
173-
}
174166
}
175167
}
176168
return null;
@@ -202,18 +194,13 @@ public String getLinkIcon(String url) {
202194
}
203195

204196
/**
205-
* 使用iconhash计算favicon hash
197+
* 计算favicon hash
206198
*
207199
* @param f favicon的文件对象
208200
* @return favicon hash值
209201
*/
210202
private String getIconHash(String f) {
211-
String os = System.getProperty("os.name").toLowerCase(Locale.US);
212-
String arch = System.getProperty("os.arch").toLowerCase(Locale.US);
213-
StringBuilder filepath = new StringBuilder();
214-
System.out.println(f);
215-
String base64Str = ImageToBase64(f);
216-
int murmu = Hashing.murmur3_32().hashString(base64Str.replaceAll("\r", "") + "\n", StandardCharsets.UTF_8).asInt();
203+
int murmu = Hashing.murmur3_32().hashString(f.replaceAll("\r", "") + "\n", StandardCharsets.UTF_8).asInt();
217204
return String.valueOf(murmu);
218205
}
219206

@@ -232,22 +219,22 @@ public String getCertSerialNum(String host) {
232219
return null;
233220
}
234221

235-
private static String ImageToBase64(String imgPath) {
236-
byte[] data = null;
237-
// 读取图片字节数组
238-
try {
239-
InputStream in = new FileInputStream(imgPath);
240-
data = new byte[in.available()];
241-
in.read(data);
242-
in.close();
243-
} catch (IOException e) {
244-
e.printStackTrace();
245-
}
246-
// 对字节数组Base64编码
247-
BASE64Encoder encoder = new BASE64Encoder();
248-
// 返回Base64编码过的字节数组字符串
249-
return encoder.encode(Objects.requireNonNull(data));
250-
}
222+
// private static String ImageToBase64(InputStream imgPath) {
223+
// byte[] data = null;
224+
// // 读取图片字节数组
225+
// try {
226+
// InputStream in = new FileInputStream(imgPath);
227+
// data = new byte[in.available()];
228+
// in.read(data);
229+
// in.close();
230+
// } catch (IOException e) {
231+
// e.printStackTrace();
232+
// }
233+
// // 对字节数组Base64编码
234+
// BASE64Encoder encoder = new BASE64Encoder();
235+
// // 返回Base64编码过的字节数组字符串
236+
// return encoder.encode(Objects.requireNonNull(data));
237+
// }
251238

252239
/**
253240
* base64编码字符串

0 commit comments

Comments
 (0)